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.
|
|
|
|
|
|
|
|
// StartGlobalForkServer() is called early in a process using a constructor, so
|
|
|
|
// it can fork() safely (in a single-threaded context)
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_GLOBAL_FORKCLIENT_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_GLOBAL_FORKCLIENT_H_
|
|
|
|
|
2020-07-17 19:54:20 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-12-10 02:53:06 +08:00
|
|
|
#include <bitset>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cstddef>
|
2020-12-10 02:53:06 +08:00
|
|
|
#include <string>
|
|
|
|
|
2020-12-02 19:05:06 +08:00
|
|
|
#include "absl/base/thread_annotations.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/declare.h"
|
2020-12-10 02:53:06 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2020-12-02 19:05:06 +08:00
|
|
|
#include "absl/synchronization/mutex.h"
|
2020-11-06 00:47:34 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
2020-07-17 19:54:20 +08:00
|
|
|
#include "sandboxed_api/sandbox2/fork_client.h"
|
2020-11-06 00:47:34 +08:00
|
|
|
#include "sandboxed_api/sandbox2/forkserver.pb.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
2020-12-10 18:49:25 +08:00
|
|
|
enum class GlobalForkserverStartMode {
|
|
|
|
kOnDemand,
|
|
|
|
// MUST be the last element
|
|
|
|
kNumGlobalForkserverStartModes,
|
|
|
|
};
|
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
class GlobalForkClient {
|
|
|
|
public:
|
|
|
|
GlobalForkClient(int fd, pid_t pid)
|
2023-12-13 19:33:36 +08:00
|
|
|
: comms_(fd), fork_client_(pid, &comms_, /*is_global=*/true) {}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-02-07 18:22:02 +08:00
|
|
|
static SandboxeeProcess SendRequest(const ForkRequest& request, int exec_fd,
|
|
|
|
int comms_fd)
|
2020-12-02 19:05:06 +08:00
|
|
|
ABSL_LOCKS_EXCLUDED(instance_mutex_);
|
|
|
|
static pid_t GetPid() ABSL_LOCKS_EXCLUDED(instance_mutex_);
|
2020-11-06 00:47:34 +08:00
|
|
|
|
2020-12-10 18:49:25 +08:00
|
|
|
static void EnsureStarted() ABSL_LOCKS_EXCLUDED(instance_mutex_) {
|
|
|
|
EnsureStarted(GlobalForkserverStartMode::kOnDemand);
|
|
|
|
}
|
2020-12-02 19:05:06 +08:00
|
|
|
static void Shutdown() ABSL_LOCKS_EXCLUDED(instance_mutex_);
|
2021-01-12 01:33:32 +08:00
|
|
|
static bool IsStarted() ABSL_LOCKS_EXCLUDED(instance_mutex_);
|
2020-11-06 00:47:34 +08:00
|
|
|
|
|
|
|
private:
|
2020-12-10 18:49:25 +08:00
|
|
|
friend void StartGlobalForkserverFromLibCtor();
|
2020-12-02 19:05:06 +08:00
|
|
|
|
2020-12-10 18:49:25 +08:00
|
|
|
static void ForceStart() ABSL_LOCKS_EXCLUDED(instance_mutex_);
|
|
|
|
static void EnsureStarted(GlobalForkserverStartMode mode)
|
|
|
|
ABSL_LOCKS_EXCLUDED(instance_mutex_);
|
|
|
|
static void EnsureStartedLocked(GlobalForkserverStartMode mode)
|
2020-12-02 19:05:06 +08:00
|
|
|
ABSL_EXCLUSIVE_LOCKS_REQUIRED(instance_mutex_);
|
|
|
|
|
2020-12-10 18:49:25 +08:00
|
|
|
static absl::Mutex instance_mutex_;
|
|
|
|
static GlobalForkClient* instance_ ABSL_GUARDED_BY(instance_mutex_);
|
|
|
|
|
2020-11-06 00:47:34 +08:00
|
|
|
Comms comms_;
|
|
|
|
ForkClient fork_client_;
|
|
|
|
};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-12-10 02:53:06 +08:00
|
|
|
class GlobalForkserverStartModeSet {
|
|
|
|
public:
|
|
|
|
static constexpr size_t kSize = static_cast<size_t>(
|
|
|
|
GlobalForkserverStartMode::kNumGlobalForkserverStartModes);
|
|
|
|
|
|
|
|
GlobalForkserverStartModeSet() {}
|
|
|
|
explicit GlobalForkserverStartModeSet(GlobalForkserverStartMode value) {
|
|
|
|
value_[static_cast<size_t>(value)] = true;
|
|
|
|
}
|
|
|
|
GlobalForkserverStartModeSet& operator|=(GlobalForkserverStartMode value) {
|
|
|
|
value_[static_cast<size_t>(value)] = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
GlobalForkserverStartModeSet operator|(
|
|
|
|
GlobalForkserverStartMode value) const {
|
|
|
|
GlobalForkserverStartModeSet rv(*this);
|
|
|
|
rv |= value;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
bool contains(GlobalForkserverStartMode value) const {
|
|
|
|
return value_[static_cast<size_t>(value)];
|
|
|
|
}
|
|
|
|
bool empty() { return value_.none(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::bitset<kSize> value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool AbslParseFlag(absl::string_view text, GlobalForkserverStartModeSet* out,
|
|
|
|
std::string* error);
|
|
|
|
std::string AbslUnparseFlag(GlobalForkserverStartModeSet in);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sandbox2
|
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
ABSL_DECLARE_FLAG(sandbox2::GlobalForkserverStartModeSet,
|
|
|
|
sandbox2_forkserver_start_mode);
|
|
|
|
ABSL_DECLARE_FLAG(std::string, sandbox2_forkserver_binary_path);
|
2020-12-10 02:53:06 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#endif // SANDBOXED_API_SANDBOX2_GLOBAL_FORKCLIENT_H_
|