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.
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
#include "sandboxed_api/sandbox2/network_proxy/client.h"
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/seccomp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <iostream>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2020-02-12 21:21:12 +08:00
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2020-08-28 19:49:15 +08:00
|
|
|
#include "absl/status/status.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
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
int NetworkProxyClient::ConnectHandler(int sockfd, const struct sockaddr* addr,
|
|
|
|
socklen_t addrlen) {
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status status = Connect(sockfd, addr, addrlen);
|
2020-02-12 21:21:12 +08:00
|
|
|
if (status.ok()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "ConnectHandler() failed: " << status.message();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status NetworkProxyClient::Connect(int sockfd,
|
2020-02-12 21:21:12 +08:00
|
|
|
const struct sockaddr* addr,
|
|
|
|
socklen_t addrlen) {
|
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
|
|
|
|
// Check if socket is SOCK_STREAM
|
|
|
|
int type;
|
|
|
|
socklen_t type_size = sizeof(int);
|
|
|
|
int result = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, &type, &type_size);
|
|
|
|
if (result == -1) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("Invalid socket FD");
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
if (type_size != sizeof(int) || type != SOCK_STREAM) {
|
|
|
|
errno = EINVAL;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(
|
2020-02-12 21:21:12 +08:00
|
|
|
"Invalid socket, only SOCK_STREAM is allowed");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send sockaddr struct
|
|
|
|
if (!comms_.SendBytes(reinterpret_cast<const uint8_t*>(addr), addrlen)) {
|
|
|
|
errno = EIO;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError("Sending data to network proxy failed");
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_RETURN_IF_ERROR(ReceiveRemoteResult());
|
|
|
|
|
|
|
|
// Receive new socket
|
|
|
|
int s;
|
|
|
|
if (!comms_.RecvFD(&s)) {
|
|
|
|
errno = EIO;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError("Receiving data from network proxy failed");
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
if (dup2(s, sockfd) == -1) {
|
|
|
|
close(s);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError("Processing data from network proxy failed");
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status NetworkProxyClient::ReceiveRemoteResult() {
|
2020-02-12 21:21:12 +08:00
|
|
|
int result;
|
|
|
|
if (!comms_.RecvInt32(&result)) {
|
|
|
|
errno = EIO;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InternalError("Receiving data from the network proxy failed");
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
if (result != 0) {
|
|
|
|
errno = result;
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(errno, "Error in network proxy server");
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
|
2022-11-30 21:47:09 +08:00
|
|
|
NetworkProxyClient* NetworkProxyHandler::network_proxy_client_ = nullptr;
|
2020-02-12 21:21:12 +08:00
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status NetworkProxyHandler::InstallNetworkProxyHandler(
|
2020-02-12 21:21:12 +08:00
|
|
|
NetworkProxyClient* npc) {
|
2022-11-30 21:47:09 +08:00
|
|
|
if (network_proxy_client_ != nullptr) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::AlreadyExistsError(
|
2020-02-12 21:21:12 +08:00
|
|
|
"Network proxy handler is already installed");
|
|
|
|
}
|
2022-11-30 21:47:09 +08:00
|
|
|
network_proxy_client_ = npc;
|
|
|
|
if (!SyscallTrap::Install([](int nr, SyscallTrap::Args args, uintptr_t* rv) {
|
|
|
|
return ProcessSeccompTrap(nr, args, rv);
|
|
|
|
})) {
|
|
|
|
return absl::InternalError("Could not install syscall trap");
|
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
|
2022-11-30 21:47:09 +08:00
|
|
|
bool NetworkProxyHandler::ProcessSeccompTrap(int nr, SyscallTrap::Args args,
|
|
|
|
uintptr_t* rv) {
|
2020-02-12 21:21:12 +08:00
|
|
|
int sockfd;
|
|
|
|
const struct sockaddr* addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
|
2022-11-30 21:47:09 +08:00
|
|
|
if (nr == __NR_connect) {
|
|
|
|
sockfd = static_cast<int>(args[0]);
|
|
|
|
addr = reinterpret_cast<const struct sockaddr*>(args[1]);
|
|
|
|
addrlen = static_cast<socklen_t>(args[2]);
|
2020-09-10 20:47:32 +08:00
|
|
|
#if defined(SAPI_PPC64_LE)
|
2022-11-30 21:47:09 +08:00
|
|
|
} else if (nr == __NR_socketcall &&
|
|
|
|
static_cast<int>(args[0]) == SYS_CONNECT) {
|
|
|
|
auto* connect_args = reinterpret_cast<uint64_t*>(args[1]);
|
2020-09-11 21:33:57 +08:00
|
|
|
sockfd = static_cast<int>(connect_args[0]);
|
|
|
|
addr = reinterpret_cast<const struct sockaddr*>(connect_args[1]);
|
|
|
|
addrlen = static_cast<socklen_t>(connect_args[2]);
|
2020-02-12 21:21:12 +08:00
|
|
|
#endif
|
|
|
|
} else {
|
2022-11-30 21:47:09 +08:00
|
|
|
return false;
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status result = network_proxy_client_->Connect(sockfd, addr, addrlen);
|
2020-02-12 21:21:12 +08:00
|
|
|
if (result.ok()) {
|
2022-11-30 21:47:09 +08:00
|
|
|
*rv = 0;
|
2020-02-12 21:21:12 +08:00
|
|
|
} else {
|
2022-11-30 21:47:09 +08:00
|
|
|
*rv = -errno;
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
2022-11-30 21:47:09 +08:00
|
|
|
return true;
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|