2020-02-12 21:21:12 +08:00
|
|
|
// Copyright 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-02-12 21:21:12 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_NETWORK_PROXY_CLIENT_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_NETWORK_PROXY_CLIENT_H_
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
2020-02-12 21:21:12 +08:00
|
|
|
|
2020-08-28 19:49:15 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-02-12 21:21:12 +08:00
|
|
|
#include "absl/synchronization/mutex.h"
|
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
2022-11-30 21:47:09 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util/syscall_trap.h"
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
class NetworkProxyClient {
|
|
|
|
public:
|
|
|
|
static constexpr char kFDName[] = "sb2_networkproxy";
|
|
|
|
|
|
|
|
explicit NetworkProxyClient(int fd) : comms_(fd) {}
|
|
|
|
|
|
|
|
NetworkProxyClient(const NetworkProxyClient&) = delete;
|
|
|
|
NetworkProxyClient& operator=(const NetworkProxyClient&) = delete;
|
|
|
|
|
2021-04-26 19:59:52 +08:00
|
|
|
// Establishes a new network connection with semantics similar to a regular
|
|
|
|
// connect() call. Arguments are sent to network proxy server, which sends
|
|
|
|
// back a connected socket.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Connect(int sockfd, const struct sockaddr* addr,
|
2020-02-12 21:21:12 +08:00
|
|
|
socklen_t addrlen);
|
|
|
|
private:
|
|
|
|
Comms comms_;
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status ReceiveRemoteResult();
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
// Needed to make the Proxy thread safe.
|
|
|
|
absl::Mutex mutex_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkProxyHandler {
|
|
|
|
public:
|
|
|
|
// Installs the handler that redirects connect() syscalls to the trap
|
2021-04-26 19:59:52 +08:00
|
|
|
// function. This function exchanges data with NetworkProxyServer that checks
|
2020-02-12 21:21:12 +08:00
|
|
|
// if this connection is allowed and sends the connected socket to us.
|
2020-02-28 01:23:44 +08:00
|
|
|
static absl::Status InstallNetworkProxyHandler(NetworkProxyClient* npc);
|
2021-04-26 19:59:52 +08:00
|
|
|
|
2022-11-30 21:47:09 +08:00
|
|
|
static bool ProcessSeccompTrap(int nr, SyscallTrap::Args args, uintptr_t* rv);
|
2020-02-12 21:21:12 +08:00
|
|
|
|
2022-11-30 21:47:09 +08:00
|
|
|
static NetworkProxyClient* network_proxy_client_;
|
2020-02-12 21:21:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_NETWORK_PROXY_CLIENT_H_
|