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::Executor class
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <climits>
|
|
|
|
#include <cstddef>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2022-03-08 00:43:03 +08:00
|
|
|
#include <string_view>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-04-21 21:15:07 +08:00
|
|
|
#include "absl/status/status.h"
|
2022-03-08 00:43:03 +08:00
|
|
|
#include "absl/strings/match.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2022-03-08 00:43:03 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
|
|
|
#include "sandboxed_api/config.h"
|
2020-07-17 19:54:20 +08:00
|
|
|
#include "sandboxed_api/sandbox2/fork_client.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/forkserver.pb.h"
|
|
|
|
#include "sandboxed_api/sandbox2/global_forkclient.h"
|
|
|
|
#include "sandboxed_api/sandbox2/ipc.h"
|
|
|
|
#include "sandboxed_api/sandbox2/util.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
|
2021-01-28 18:20:13 +08:00
|
|
|
namespace file_util = ::sapi::file_util;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-03-08 00:43:03 +08:00
|
|
|
namespace {
|
|
|
|
void DisableCompressStackDepot(ForkRequest& request) {
|
|
|
|
auto disable_compress_stack_depot = [&request](absl::string_view sanitizer) {
|
|
|
|
auto prefix = absl::StrCat(sanitizer, "_OPTIONS=");
|
|
|
|
auto it = std::find_if(request.mutable_envs()->begin(),
|
|
|
|
request.mutable_envs()->end(),
|
|
|
|
[&prefix](const std::string& env) {
|
|
|
|
return absl::StartsWith(env, prefix);
|
|
|
|
});
|
|
|
|
constexpr absl::string_view option = "compress_stack_depot=0";
|
|
|
|
if (it != request.mutable_envs()->end()) {
|
|
|
|
// If it's already there, the last value will be used.
|
|
|
|
absl::StrAppend(&*it, ":", option);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request.add_envs(absl::StrCat(prefix, option));
|
|
|
|
};
|
|
|
|
if constexpr (sapi::sanitizers::IsASan()) {
|
|
|
|
disable_compress_stack_depot("ASAN");
|
|
|
|
}
|
|
|
|
if constexpr (sapi::sanitizers::IsMSan()) {
|
|
|
|
disable_compress_stack_depot("MSAN");
|
|
|
|
}
|
|
|
|
if constexpr (sapi::sanitizers::IsLSan()) {
|
|
|
|
disable_compress_stack_depot("LSAN");
|
|
|
|
}
|
|
|
|
if constexpr (sapi::sanitizers::IsHwASan()) {
|
|
|
|
disable_compress_stack_depot("HWSAN");
|
|
|
|
}
|
|
|
|
if constexpr (sapi::sanitizers::IsTSan()) {
|
|
|
|
disable_compress_stack_depot("TSAN");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> Executor::CopyEnviron() {
|
2021-12-20 21:07:40 +08:00
|
|
|
return util::CharPtrArray(environ).ToStringVector();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2022-03-09 21:16:27 +08:00
|
|
|
absl::StatusOr<Executor::Process> Executor::StartSubProcess(
|
|
|
|
int32_t clone_flags, const Namespace* ns, const std::vector<int>& caps) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (started_) {
|
2022-03-09 21:16:27 +08:00
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"This executor has already been started");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!path_.empty()) {
|
2021-08-05 19:15:42 +08:00
|
|
|
exec_fd_ = file_util::fileops::FDCloser(open(path_.c_str(), O_PATH));
|
|
|
|
if (exec_fd_.get() < 0) {
|
2022-03-09 21:16:27 +08:00
|
|
|
if (errno == ENOENT) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(errno, path_);
|
2022-03-09 21:16:27 +08:00
|
|
|
}
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(errno,
|
|
|
|
absl::StrCat("Could not open file ", path_));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (libunwind_sbox_for_pid_ != 0) {
|
|
|
|
VLOG(1) << "StartSubProcces, starting libunwind";
|
2021-08-05 19:15:42 +08:00
|
|
|
} else if (exec_fd_.get() < 0) {
|
2019-03-19 00:21:48 +08:00
|
|
|
VLOG(1) << "StartSubProcess, with [Fork-Server]";
|
|
|
|
} else if (!path_.empty()) {
|
|
|
|
VLOG(1) << "StartSubProcess, with file " << path_;
|
|
|
|
} else {
|
2021-08-05 19:15:42 +08:00
|
|
|
VLOG(1) << "StartSubProcess, with fd " << exec_fd_.get();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ForkRequest request;
|
2021-07-12 17:37:17 +08:00
|
|
|
*request.mutable_args() = {argv_.begin(), argv_.end()};
|
|
|
|
*request.mutable_envs() = {envp_.begin(), envp_.end()};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Add LD_ORIGIN_PATH to envs, as it'll make the amount of syscalls invoked by
|
2021-05-17 19:06:39 +08:00
|
|
|
// ld.so smaller.
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!path_.empty()) {
|
|
|
|
request.add_envs(absl::StrCat("LD_ORIGIN_PATH=",
|
|
|
|
file_util::fileops::StripBasename(path_)));
|
|
|
|
}
|
|
|
|
|
2022-03-08 00:43:03 +08:00
|
|
|
// Disable optimization to avoid related syscalls.
|
|
|
|
if constexpr (sapi::sanitizers::IsAny()) {
|
|
|
|
DisableCompressStackDepot(request);
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// If neither the path, nor exec_fd is specified, just assume that we need to
|
|
|
|
// send a fork request.
|
|
|
|
//
|
|
|
|
// Otherwise, it's either sandboxing pre- or post-execve with the global
|
|
|
|
// Fork-Server.
|
|
|
|
if (libunwind_sbox_for_pid_ != 0) {
|
|
|
|
request.set_mode(FORKSERVER_FORK_JOIN_SANDBOX_UNWIND);
|
2021-08-05 19:15:42 +08:00
|
|
|
} else if (exec_fd_.get() == -1) {
|
2019-03-19 00:21:48 +08:00
|
|
|
request.set_mode(FORKSERVER_FORK);
|
|
|
|
} else if (enable_sandboxing_pre_execve_) {
|
|
|
|
request.set_mode(FORKSERVER_FORK_EXECVE_SANDBOX);
|
|
|
|
} else {
|
|
|
|
request.set_mode(FORKSERVER_FORK_EXECVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ns) {
|
|
|
|
clone_flags |= ns->GetCloneFlags();
|
|
|
|
*request.mutable_mount_tree() = ns->mounts().GetMountTree();
|
2019-06-14 17:08:43 +08:00
|
|
|
request.set_hostname(ns->hostname());
|
2022-03-09 00:00:46 +08:00
|
|
|
request.set_allow_mount_propagation(ns->allow_mount_propagation());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
request.set_clone_flags(clone_flags);
|
|
|
|
|
2021-07-12 17:37:17 +08:00
|
|
|
for (auto cap : caps) {
|
|
|
|
request.add_capabilities(cap);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-08-05 19:15:42 +08:00
|
|
|
file_util::fileops::FDCloser ns_fd;
|
2019-03-19 00:21:48 +08:00
|
|
|
if (libunwind_sbox_for_pid_ != 0) {
|
2021-07-12 17:37:17 +08:00
|
|
|
const std::string ns_path =
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("/proc/", libunwind_sbox_for_pid_, "/ns/user");
|
2021-08-05 19:15:42 +08:00
|
|
|
ns_fd = file_util::fileops::FDCloser(open(ns_path.c_str(), O_RDONLY));
|
2022-03-09 21:16:27 +08:00
|
|
|
if (ns_fd.get() == -1) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(
|
|
|
|
errno, absl::StrCat("Could not open user ns fd (", ns_path, ")"));
|
2022-03-09 21:16:27 +08:00
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2022-03-09 21:16:27 +08:00
|
|
|
Process process;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
if (fork_client_) {
|
2022-03-09 21:16:27 +08:00
|
|
|
process.main_pid = fork_client_->SendRequest(
|
|
|
|
request, exec_fd_.get(), client_comms_fd_.get(), ns_fd.get(),
|
|
|
|
&process.init_pid);
|
2020-11-06 00:47:34 +08:00
|
|
|
} else {
|
2022-03-09 21:16:27 +08:00
|
|
|
process.main_pid = GlobalForkClient::SendRequest(
|
|
|
|
request, exec_fd_.get(), client_comms_fd_.get(), ns_fd.get(),
|
|
|
|
&process.init_pid);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
started_ = true;
|
|
|
|
|
2021-08-05 19:15:42 +08:00
|
|
|
client_comms_fd_.Close();
|
|
|
|
exec_fd_.Close();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-03-09 21:16:27 +08:00
|
|
|
VLOG(1) << "StartSubProcess returned with: " << process.main_pid;
|
|
|
|
return process;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ForkClient> Executor::StartForkServer() {
|
|
|
|
// This flag is set explicitly to 'true' during object instantiation, and
|
|
|
|
// custom fork-servers should never be sandboxed.
|
|
|
|
set_enable_sandbox_before_exec(false);
|
2022-03-09 21:16:27 +08:00
|
|
|
absl::StatusOr<Process> process = StartSubProcess(0);
|
|
|
|
if (!process.ok()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-10-12 20:22:51 +08:00
|
|
|
return std::make_unique<ForkClient>(process->main_pid, ipc_.comms());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Executor::SetUpServerSideCommsFd() {
|
|
|
|
int sv[2];
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
|
|
|
|
PLOG(FATAL) << "socketpair(AF_UNIX, SOCK_STREAM) failed";
|
|
|
|
}
|
|
|
|
|
2021-08-05 19:15:42 +08:00
|
|
|
client_comms_fd_ = file_util::fileops::FDCloser(sv[0]);
|
|
|
|
ipc_.SetUpServerSideComms(sv[1]);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|