2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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
|
2019-03-19 00:21:48 +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.
|
|
|
|
|
|
|
|
// Implementation of the sandbox2::ForkServer class.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/global_forkclient.h"
|
|
|
|
|
2019-04-09 18:44:34 +08:00
|
|
|
#include <fcntl.h>
|
2023-01-03 21:36:02 +08:00
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/mman.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/socket.h>
|
2021-08-05 22:07:08 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2019-04-09 18:44:34 +08:00
|
|
|
#include <syscall.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-01-03 21:36:02 +08:00
|
|
|
#include <climits>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <csignal>
|
|
|
|
#include <cstdlib>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2020-12-02 19:05:06 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-01-03 21:36:02 +08:00
|
|
|
#include "absl/cleanup/cleanup.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/flag.h"
|
|
|
|
#include "absl/log/log.h"
|
2022-03-03 00:11:37 +08:00
|
|
|
#include "absl/status/status.h"
|
|
|
|
#include "absl/status/statusor.h"
|
2020-12-02 19:05:06 +08:00
|
|
|
#include "absl/strings/ascii.h"
|
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/strings/str_join.h"
|
|
|
|
#include "absl/strings/str_split.h"
|
|
|
|
#include "absl/strings/string_view.h"
|
2021-01-12 01:33:32 +08:00
|
|
|
#include "absl/synchronization/mutex.h"
|
2022-03-03 00:11:37 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-04-09 18:44:34 +08:00
|
|
|
#include "sandboxed_api/embed_file.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
2020-12-14 19:33:29 +08:00
|
|
|
#include "sandboxed_api/sandbox2/fork_client.h"
|
2019-04-09 18:44:34 +08:00
|
|
|
#include "sandboxed_api/sandbox2/forkserver_bin_embed.h"
|
2023-03-28 20:50:06 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util.h"
|
2022-03-03 00:11:37 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
|
2022-03-03 00:11:37 +08:00
|
|
|
namespace file_util = ::sapi::file_util;
|
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string ToString(GlobalForkserverStartMode mode) {
|
|
|
|
switch (mode) {
|
|
|
|
case GlobalForkserverStartMode::kOnDemand:
|
|
|
|
return "ondemand";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
bool AbslParseFlag(absl::string_view text, GlobalForkserverStartModeSet* out,
|
|
|
|
std::string* error) {
|
|
|
|
*out = {};
|
|
|
|
if (text == "never") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (absl::string_view mode : absl::StrSplit(text, ',')) {
|
|
|
|
mode = absl::StripAsciiWhitespace(mode);
|
|
|
|
if (mode == "ondemand") {
|
|
|
|
*out |= GlobalForkserverStartMode::kOnDemand;
|
|
|
|
} else {
|
|
|
|
*error = absl::StrCat("Invalid forkserver start mode: ", mode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string AbslUnparseFlag(GlobalForkserverStartModeSet in) {
|
|
|
|
std::vector<std::string> str_modes;
|
|
|
|
for (size_t i = 0; i < GlobalForkserverStartModeSet::kSize; ++i) {
|
|
|
|
auto mode = static_cast<GlobalForkserverStartMode>(i);
|
|
|
|
if (in.contains(mode)) {
|
|
|
|
str_modes.push_back(ToString(mode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str_modes.empty()) {
|
|
|
|
return "never";
|
|
|
|
}
|
|
|
|
return absl::StrJoin(str_modes, ",");
|
|
|
|
}
|
2020-12-10 02:53:06 +08:00
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
} // namespace sandbox2
|
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
ABSL_FLAG(std::string, sandbox2_forkserver_binary_path, "",
|
2022-03-03 00:11:37 +08:00
|
|
|
"Path to forkserver_bin binary");
|
2022-10-20 21:48:06 +08:00
|
|
|
ABSL_FLAG(sandbox2::GlobalForkserverStartModeSet,
|
|
|
|
sandbox2_forkserver_start_mode,
|
|
|
|
sandbox2::GlobalForkserverStartModeSet(
|
|
|
|
sandbox2::GlobalForkserverStartMode::kOnDemand)
|
|
|
|
,
|
2020-12-02 19:05:06 +08:00
|
|
|
"When Sandbox2 Forkserver process should be started");
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
namespace sandbox2 {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
namespace {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
GlobalForkserverStartModeSet GetForkserverStartMode() {
|
2022-10-20 21:48:06 +08:00
|
|
|
return absl::GetFlag(FLAGS_sandbox2_forkserver_start_mode);
|
2020-12-02 19:05:06 +08:00
|
|
|
}
|
|
|
|
|
2023-01-03 21:36:02 +08:00
|
|
|
struct ForkserverArgs {
|
|
|
|
int exec_fd;
|
|
|
|
int comms_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
int LaunchForkserver(void* vargs) {
|
|
|
|
auto* args = static_cast<ForkserverArgs*>(vargs);
|
|
|
|
// Move the comms FD to the proper, expected FD number.
|
|
|
|
// The new FD will not be CLOEXEC, which is what we want.
|
|
|
|
// If exec_fd == Comms::kSandbox2ClientCommsFD then it would be replaced by
|
|
|
|
// the comms fd and result in EACCESS at execveat.
|
|
|
|
// So first move exec_fd to another fd number.
|
|
|
|
if (args->exec_fd == Comms::kSandbox2ClientCommsFD) {
|
|
|
|
args->exec_fd = dup(args->exec_fd);
|
|
|
|
SAPI_RAW_PCHECK(args->exec_fd != -1, "duping exec fd failed");
|
|
|
|
fcntl(args->exec_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
SAPI_RAW_PCHECK(dup2(args->comms_fd, Comms::kSandbox2ClientCommsFD) != -1,
|
|
|
|
"duping comms fd failed");
|
|
|
|
|
|
|
|
char proc_name[] = "S2-FORK-SERV";
|
|
|
|
char* const argv[] = {proc_name, nullptr};
|
2023-03-28 20:50:06 +08:00
|
|
|
util::Execveat(args->exec_fd, "", argv, environ, AT_EMPTY_PATH);
|
2023-01-03 21:36:02 +08:00
|
|
|
SAPI_RAW_PLOG(FATAL, "Could not launch forkserver binary");
|
|
|
|
}
|
|
|
|
|
2021-08-10 15:33:04 +08:00
|
|
|
absl::StatusOr<std::unique_ptr<GlobalForkClient>> StartGlobalForkServer() {
|
2021-09-22 22:16:12 +08:00
|
|
|
SAPI_RAW_LOG(INFO, "Starting global forkserver");
|
|
|
|
|
2022-03-03 00:11:37 +08:00
|
|
|
// Allow passing of a spearate forkserver_bin via flag
|
|
|
|
int exec_fd = -1;
|
|
|
|
if (!absl::GetFlag(FLAGS_sandbox2_forkserver_binary_path).empty()) {
|
|
|
|
exec_fd = open(absl::GetFlag(FLAGS_sandbox2_forkserver_binary_path).c_str(),
|
|
|
|
O_RDONLY);
|
|
|
|
}
|
|
|
|
if (exec_fd < 0) {
|
|
|
|
// For Android we expect the forkserver_bin in the flag
|
|
|
|
if constexpr (sapi::host_os::IsAndroid()) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(
|
2022-03-03 00:11:37 +08:00
|
|
|
errno,
|
2022-04-21 21:15:07 +08:00
|
|
|
"Open init binary passed via --sandbox2_forkserver_binary_path");
|
2022-03-03 00:11:37 +08:00
|
|
|
}
|
|
|
|
// Extract the fd when it's owned by EmbedFile
|
|
|
|
exec_fd = sapi::EmbedFile::instance()->GetDupFdForFileToc(
|
|
|
|
forkserver_bin_embed_create());
|
|
|
|
}
|
2021-08-10 15:33:04 +08:00
|
|
|
if (exec_fd < 0) {
|
|
|
|
return absl::InternalError("Getting FD for init binary failed");
|
|
|
|
}
|
2022-03-03 00:11:37 +08:00
|
|
|
file_util::fileops::FDCloser exec_fd_closer(exec_fd);
|
2020-11-06 00:47:34 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
int sv[2];
|
2021-08-10 15:33:04 +08:00
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(errno, "Creating socket pair failed");
|
2021-08-10 15:33:04 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Fork the fork-server, and clean-up the resources (close remote sockets).
|
2023-03-09 16:13:49 +08:00
|
|
|
const size_t stack_size = PTHREAD_STACK_MIN;
|
2023-01-03 21:36:02 +08:00
|
|
|
int clone_flags = CLONE_VM | CLONE_VFORK | SIGCHLD;
|
|
|
|
// CLONE_VM does not play well with TSan.
|
|
|
|
if constexpr (sapi::sanitizers::IsTSan()) {
|
|
|
|
clone_flags &= ~CLONE_VM & ~CLONE_VFORK;
|
|
|
|
}
|
|
|
|
char* stack =
|
2023-03-09 16:13:49 +08:00
|
|
|
static_cast<char*>(mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
|
2023-01-03 21:36:02 +08:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0));
|
|
|
|
if (stack == MAP_FAILED) {
|
|
|
|
return absl::ErrnoToStatus(errno, "Allocating stack failed");
|
|
|
|
}
|
2023-03-09 16:13:49 +08:00
|
|
|
absl::Cleanup stack_dealloc = [stack, stack_size] {
|
|
|
|
munmap(stack, stack_size);
|
|
|
|
};
|
2023-01-03 21:36:02 +08:00
|
|
|
ForkserverArgs args = {
|
|
|
|
.exec_fd = exec_fd,
|
|
|
|
.comms_fd = sv[0],
|
|
|
|
};
|
2023-03-09 16:13:49 +08:00
|
|
|
pid_t pid = clone(LaunchForkserver, &stack[stack_size], clone_flags, &args,
|
2023-01-03 21:36:02 +08:00
|
|
|
nullptr, nullptr, nullptr);
|
2021-08-10 15:33:04 +08:00
|
|
|
if (pid == -1) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(errno, "Forking forkserver process failed");
|
2021-08-10 15:33:04 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
close(sv[0]);
|
2022-10-12 20:22:51 +08:00
|
|
|
return std::make_unique<GlobalForkClient>(sv[1], pid);
|
2020-11-06 00:47:34 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-08-06 21:53:27 +08:00
|
|
|
void WaitForForkserver(pid_t pid) {
|
2021-09-22 22:16:12 +08:00
|
|
|
int status;
|
|
|
|
pid_t wpid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
|
2021-08-06 21:53:27 +08:00
|
|
|
if (wpid != pid) {
|
|
|
|
SAPI_RAW_PLOG(ERROR, "Waiting for %d failed", pid);
|
|
|
|
}
|
2021-09-22 22:16:12 +08:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
int exit_code = WEXITSTATUS(status);
|
|
|
|
if (exit_code == 0) {
|
|
|
|
SAPI_RAW_LOG(INFO, "forkserver (pid=%d) terminated normally", pid);
|
|
|
|
} else {
|
|
|
|
SAPI_RAW_LOG(WARNING, "forkserver (pid=%d) terminated with exit code %d",
|
|
|
|
pid, exit_code);
|
|
|
|
}
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
SAPI_RAW_LOG(WARNING, "forkserver (pid=%d) terminated by signal %d", pid,
|
|
|
|
WTERMSIG(status));
|
|
|
|
}
|
2021-08-06 21:53:27 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
absl::Mutex GlobalForkClient::instance_mutex_(absl::kConstInit);
|
|
|
|
GlobalForkClient* GlobalForkClient::instance_ = nullptr;
|
|
|
|
|
2020-12-10 18:49:25 +08:00
|
|
|
void GlobalForkClient::EnsureStarted(GlobalForkserverStartMode mode) {
|
|
|
|
absl::MutexLock lock(&instance_mutex_);
|
|
|
|
EnsureStartedLocked(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalForkClient::EnsureStartedLocked(GlobalForkserverStartMode mode) {
|
2021-08-10 15:33:04 +08:00
|
|
|
if (instance_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (getenv(kForkServerDisableEnv)) {
|
|
|
|
SAPI_RAW_LOG(ERROR,
|
|
|
|
"Start of the Global Fork-Server prevented by the %s "
|
|
|
|
"environment variable present",
|
|
|
|
kForkServerDisableEnv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!GetForkserverStartMode().contains(mode)) {
|
|
|
|
SAPI_RAW_LOG(
|
|
|
|
ERROR, "Start of the Global Fork-Server prevented by commandline flag");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
absl::StatusOr<std::unique_ptr<GlobalForkClient>> forkserver =
|
|
|
|
StartGlobalForkServer();
|
|
|
|
if (!forkserver.ok()) {
|
|
|
|
SAPI_RAW_LOG(ERROR, "Starting forkserver failed: %s",
|
|
|
|
forkserver.status().message().data());
|
|
|
|
}
|
|
|
|
instance_ = forkserver->release();
|
2020-12-10 18:49:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalForkClient::ForceStart() {
|
2020-12-02 19:05:06 +08:00
|
|
|
absl::MutexLock lock(&GlobalForkClient::instance_mutex_);
|
2020-12-10 18:49:25 +08:00
|
|
|
SAPI_RAW_CHECK(instance_ == nullptr,
|
|
|
|
"A force start requested when the Global Fork-Server was "
|
|
|
|
"already running");
|
2021-08-10 15:33:04 +08:00
|
|
|
absl::StatusOr<std::unique_ptr<GlobalForkClient>> forkserver =
|
|
|
|
StartGlobalForkServer();
|
|
|
|
SAPI_RAW_CHECK(forkserver.ok(), forkserver.status().message().data());
|
|
|
|
instance_ = forkserver->release();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
void GlobalForkClient::Shutdown() {
|
2021-08-05 22:07:08 +08:00
|
|
|
pid_t pid = -1;
|
|
|
|
{
|
|
|
|
absl::MutexLock lock(&GlobalForkClient::instance_mutex_);
|
|
|
|
if (instance_) {
|
|
|
|
pid = instance_->fork_client_.pid();
|
|
|
|
}
|
|
|
|
delete instance_;
|
|
|
|
instance_ = nullptr;
|
|
|
|
}
|
|
|
|
if (pid != -1) {
|
2021-08-06 21:53:27 +08:00
|
|
|
WaitForForkserver(pid);
|
2021-08-05 22:07:08 +08:00
|
|
|
}
|
2020-12-02 19:05:06 +08:00
|
|
|
}
|
2020-11-06 00:47:34 +08:00
|
|
|
|
2023-02-07 18:22:02 +08:00
|
|
|
SandboxeeProcess GlobalForkClient::SendRequest(const ForkRequest& request,
|
|
|
|
int exec_fd, int comms_fd) {
|
2021-08-06 21:53:27 +08:00
|
|
|
absl::ReleasableMutexLock lock(&GlobalForkClient::instance_mutex_);
|
2020-12-10 18:49:25 +08:00
|
|
|
EnsureStartedLocked(GlobalForkserverStartMode::kOnDemand);
|
2021-08-10 15:33:04 +08:00
|
|
|
if (!instance_) {
|
2023-03-09 00:08:35 +08:00
|
|
|
return SandboxeeProcess();
|
2021-08-10 15:33:04 +08:00
|
|
|
}
|
2023-02-07 18:22:02 +08:00
|
|
|
SandboxeeProcess process =
|
|
|
|
instance_->fork_client_.SendRequest(request, exec_fd, comms_fd);
|
2020-12-02 19:05:06 +08:00
|
|
|
if (instance_->comms_.IsTerminated()) {
|
2020-11-06 00:47:34 +08:00
|
|
|
LOG(ERROR) << "Global forkserver connection terminated";
|
2021-08-06 21:53:27 +08:00
|
|
|
pid_t server_pid = instance_->fork_client_.pid();
|
|
|
|
delete instance_;
|
|
|
|
instance_ = nullptr;
|
|
|
|
// Don't wait for process exit while still holding the lock and potentially
|
|
|
|
// blocking other threads.
|
|
|
|
lock.Release();
|
|
|
|
WaitForForkserver(server_pid);
|
2020-11-06 00:47:34 +08:00
|
|
|
}
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-11-06 00:47:34 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
pid_t GlobalForkClient::GetPid() {
|
2020-12-02 19:05:06 +08:00
|
|
|
absl::MutexLock lock(&instance_mutex_);
|
2020-12-10 18:49:25 +08:00
|
|
|
EnsureStartedLocked(GlobalForkserverStartMode::kOnDemand);
|
2021-08-10 15:33:04 +08:00
|
|
|
if (!instance_) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-12-02 19:05:06 +08:00
|
|
|
return instance_->fork_client_.pid();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-01-12 01:33:32 +08:00
|
|
|
|
|
|
|
bool GlobalForkClient::IsStarted() {
|
|
|
|
absl::ReaderMutexLock lock(&instance_mutex_);
|
|
|
|
return instance_ != nullptr;
|
|
|
|
}
|
2020-11-06 00:47:34 +08:00
|
|
|
} // namespace sandbox2
|