2020-07-17 19:54:20 +08:00
|
|
|
// Copyright 2020 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-07-17 19:54:20 +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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/fork_client.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/check.h"
|
|
|
|
#include "absl/log/log.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/synchronization/mutex.h"
|
2020-07-17 19:54:20 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/forkserver.pb.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2020-07-17 19:54:20 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
using ::sapi::file_util::fileops::FDCloser;
|
|
|
|
|
2023-12-13 19:33:36 +08:00
|
|
|
ForkClient::ForkClient(pid_t pid, Comms* comms, bool is_global)
|
|
|
|
: pid_(pid), comms_(comms), is_global_(is_global) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ForkClient::~ForkClient() {
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:22:02 +08:00
|
|
|
SandboxeeProcess ForkClient::SendRequest(const ForkRequest& request,
|
|
|
|
int exec_fd, int comms_fd) {
|
2023-03-09 00:08:35 +08:00
|
|
|
SandboxeeProcess process;
|
2020-07-17 19:54:20 +08:00
|
|
|
// Acquire the channel ownership for this request (transaction).
|
|
|
|
absl::MutexLock l(&comms_mutex_);
|
|
|
|
|
|
|
|
if (!comms_->SendProtoBuf(request)) {
|
2021-09-29 15:45:35 +08:00
|
|
|
LOG(ERROR) << "Sending PB to the ForkServer failed";
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-07-17 19:54:20 +08:00
|
|
|
}
|
2021-09-29 15:45:35 +08:00
|
|
|
CHECK(comms_fd != -1) << "comms_fd was not properly set up";
|
2020-07-17 19:54:20 +08:00
|
|
|
if (!comms_->SendFD(comms_fd)) {
|
2021-09-29 15:45:35 +08:00
|
|
|
LOG(ERROR) << "Sending Comms FD (" << comms_fd
|
|
|
|
<< ") to the ForkServer failed";
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-07-17 19:54:20 +08:00
|
|
|
}
|
|
|
|
if (request.mode() == FORKSERVER_FORK_EXECVE ||
|
|
|
|
request.mode() == FORKSERVER_FORK_EXECVE_SANDBOX) {
|
2021-09-29 15:45:35 +08:00
|
|
|
CHECK(exec_fd != -1) << "exec_fd cannot be -1 in execve mode";
|
2020-07-17 19:54:20 +08:00
|
|
|
if (!comms_->SendFD(exec_fd)) {
|
2021-09-29 15:45:35 +08:00
|
|
|
LOG(ERROR) << "Sending Exec FD (" << exec_fd
|
|
|
|
<< ") to the ForkServer failed";
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-07-17 19:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t pid;
|
|
|
|
// Receive init process ID.
|
|
|
|
if (!comms_->RecvInt32(&pid)) {
|
2021-09-29 15:45:35 +08:00
|
|
|
LOG(ERROR) << "Receiving init PID from the ForkServer failed";
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-07-17 19:54:20 +08:00
|
|
|
}
|
2023-02-07 18:22:02 +08:00
|
|
|
process.init_pid = static_cast<pid_t>(pid);
|
2020-07-17 19:54:20 +08:00
|
|
|
|
|
|
|
// Receive sandboxee process ID.
|
|
|
|
if (!comms_->RecvInt32(&pid)) {
|
2021-09-29 15:45:35 +08:00
|
|
|
LOG(ERROR) << "Receiving sandboxee PID from the ForkServer failed";
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-07-17 19:54:20 +08:00
|
|
|
}
|
2023-02-07 18:22:02 +08:00
|
|
|
process.main_pid = static_cast<pid_t>(pid);
|
2023-03-09 00:08:35 +08:00
|
|
|
if (request.monitor_type() == FORKSERVER_MONITOR_UNOTIFY) {
|
|
|
|
int fd = -1;
|
|
|
|
if (!comms_->RecvFD(&fd)) {
|
|
|
|
LOG(ERROR) << "Receiving status fd from the ForkServer failed";
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
process.status_fd = FDCloser(fd);
|
|
|
|
}
|
2023-02-07 18:22:02 +08:00
|
|
|
return process;
|
2020-07-17 19:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|