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/server.h"
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <pthread.h>
|
2021-04-26 19:59:52 +08:00
|
|
|
#include <signal.h>
|
2020-02-12 21:21:12 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2020-02-20 23:45:22 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <atomic>
|
2020-02-12 21:21:12 +08:00
|
|
|
#include <cerrno>
|
2023-09-05 15:56:21 +08:00
|
|
|
#include <cstdint>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2020-02-12 21:21:12 +08:00
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2023-09-05 15:56:21 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/network_proxy/filtering.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file_util = ::sapi::file_util;
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
NetworkProxyServer::NetworkProxyServer(int fd, AllowedHosts* allowed_hosts,
|
|
|
|
pthread_t monitor_thread_id)
|
|
|
|
: violation_occurred_(false),
|
2022-10-20 21:48:06 +08:00
|
|
|
comms_(std::make_unique<Comms>(fd)),
|
2020-02-20 23:45:22 +08:00
|
|
|
fatal_error_(false),
|
|
|
|
monitor_thread_id_(monitor_thread_id),
|
|
|
|
allowed_hosts_(allowed_hosts) {}
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
void NetworkProxyServer::ProcessConnectRequest() {
|
|
|
|
std::vector<uint8_t> addr;
|
|
|
|
if (!comms_->RecvBytes(&addr)) {
|
|
|
|
fatal_error_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
const struct sockaddr* saddr = reinterpret_cast<const sockaddr*>(addr.data());
|
2020-02-12 21:21:12 +08:00
|
|
|
|
|
|
|
// Only IPv4 TCP and IPv6 TCP are supported.
|
2020-02-20 23:45:22 +08:00
|
|
|
if (!((addr.size() == sizeof(sockaddr_in) && saddr->sa_family == AF_INET) ||
|
2020-02-12 21:21:12 +08:00
|
|
|
(addr.size() == sizeof(sockaddr_in6) &&
|
2020-02-20 23:45:22 +08:00
|
|
|
saddr->sa_family == AF_INET6))) {
|
2020-02-12 21:21:12 +08:00
|
|
|
SendError(EINVAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
if (!allowed_hosts_->IsHostAllowed(saddr)) {
|
|
|
|
NotifyViolation(saddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int new_socket = socket(saddr->sa_family, SOCK_STREAM, 0);
|
2020-02-12 21:21:12 +08:00
|
|
|
if (new_socket < 0) {
|
|
|
|
SendError(errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_util::fileops::FDCloser new_socket_closer(new_socket);
|
|
|
|
|
|
|
|
int result = connect(
|
|
|
|
new_socket, reinterpret_cast<const sockaddr*>(addr.data()), addr.size());
|
|
|
|
|
2023-09-05 15:56:21 +08:00
|
|
|
if (result < 0) {
|
|
|
|
SendError(errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotifySuccess();
|
|
|
|
if (!fatal_error_ && !comms_->SendFD(new_socket)) {
|
|
|
|
fatal_error_ = true;
|
|
|
|
return;
|
2020-02-12 21:21:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkProxyServer::Run() {
|
2020-02-20 23:45:22 +08:00
|
|
|
while (!fatal_error_ &&
|
|
|
|
!violation_occurred_.load(std::memory_order_relaxed)) {
|
2020-02-12 21:21:12 +08:00
|
|
|
ProcessConnectRequest();
|
|
|
|
}
|
|
|
|
LOG(INFO)
|
|
|
|
<< "Clean shutdown or error occurred, shutting down NetworkProxyServer";
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkProxyServer::SendError(int saved_errno) {
|
|
|
|
if (!comms_->SendInt32(saved_errno)) {
|
|
|
|
fatal_error_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkProxyServer::NotifySuccess() {
|
|
|
|
if (!comms_->SendInt32(0)) {
|
|
|
|
fatal_error_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
void NetworkProxyServer::NotifyViolation(const struct sockaddr* saddr) {
|
2020-09-02 23:46:48 +08:00
|
|
|
if (absl::StatusOr<std::string> result = AddrToString(saddr); result.ok()) {
|
2020-04-02 22:42:17 +08:00
|
|
|
violation_msg_ = std::move(result).value();
|
2020-02-20 23:45:22 +08:00
|
|
|
} else {
|
|
|
|
violation_msg_ = std::string(result.status().message());
|
|
|
|
}
|
|
|
|
violation_occurred_.store(true, std::memory_order_release);
|
|
|
|
pthread_kill(monitor_thread_id_, SIGCHLD);
|
|
|
|
}
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2020-02-12 21:21:12 +08:00
|
|
|
} // namespace sandbox2
|