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.
|
|
|
|
|
|
|
|
// sandbox2::ForkServer is a class which serves fork()ing request for the
|
|
|
|
// clients.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_FORKSERVER_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_FORKSERVER_H_
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2019-03-26 22:54:02 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-03-22 19:06:25 +08:00
|
|
|
#include "absl/base/attributes.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
class Comms;
|
|
|
|
class ForkRequest;
|
|
|
|
|
|
|
|
class ForkServer {
|
|
|
|
public:
|
|
|
|
ForkServer(const ForkServer&) = delete;
|
|
|
|
ForkServer& operator=(const ForkServer&) = delete;
|
|
|
|
|
|
|
|
explicit ForkServer(Comms* comms) : comms_(comms) {
|
|
|
|
if (!Initialize()) {
|
|
|
|
LOG(FATAL) << "Could not initialize the ForkServer";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 17:21:31 +08:00
|
|
|
// Returns whether the connection with the forkserver was terminated.
|
|
|
|
bool IsTerminated() const;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Receives a fork request from the master process. The started process does
|
|
|
|
// not need to be waited for (with waitid/waitpid/wait3/wait4) as the current
|
|
|
|
// process will have the SIGCHLD set to sa_flags=SA_NOCLDWAIT.
|
|
|
|
// Returns values defined as with fork() (-1 means error).
|
2019-12-02 21:31:19 +08:00
|
|
|
pid_t ServeRequest();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
private:
|
2019-11-20 01:01:59 +08:00
|
|
|
// Creates and launched the child process.
|
2023-08-22 22:11:27 +08:00
|
|
|
void LaunchChild(const ForkRequest& request, int execve_fd, int client_fd,
|
|
|
|
uid_t uid, gid_t gid, int signaling_fd, int status_fd,
|
2019-11-20 01:01:59 +08:00
|
|
|
bool avoid_pivot_root) const;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Prepares the Fork-Server (worker side, not the requester side) for work by
|
|
|
|
// sanitizing the environment:
|
|
|
|
// - go down if the parent goes down,
|
|
|
|
// - become subreaper - PR_SET_CHILD_SUBREAPER (man prctl),
|
|
|
|
// - don't convert children processes into zombies if they terminate.
|
2019-11-20 01:01:59 +08:00
|
|
|
bool Initialize();
|
2019-12-02 21:31:19 +08:00
|
|
|
|
|
|
|
// Creates initial namespaces used as a template for namespaced sandboxees
|
|
|
|
void CreateInitialNamespaces();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Prepares arguments for the upcoming execve (if execve was requested).
|
|
|
|
static void PrepareExecveArgs(const ForkRequest& request,
|
|
|
|
std::vector<std::string>* args,
|
|
|
|
std::vector<std::string>* envp);
|
|
|
|
|
|
|
|
// Ensures that no unnecessary file descriptors are lingering after execve().
|
2023-08-22 22:11:27 +08:00
|
|
|
static void SanitizeEnvironment();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Executes the sandboxee, or exit with Executor::kFailedExecve.
|
2021-12-20 21:07:40 +08:00
|
|
|
static void ExecuteProcess(int execve_fd, const char* const* argv,
|
2023-03-22 19:06:25 +08:00
|
|
|
const char* const* envp) ABSL_ATTRIBUTE_NORETURN;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Runs namespace initializers for a sandboxee.
|
|
|
|
static void InitializeNamespaces(const ForkRequest& request, uid_t uid,
|
2019-11-20 01:01:59 +08:00
|
|
|
gid_t gid, bool avoid_pivot_root);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Comms channel which is used to send requests to this class. Not owned by
|
|
|
|
// the object.
|
|
|
|
Comms* comms_;
|
2019-11-20 01:01:59 +08:00
|
|
|
int initial_mntns_fd_ = -1;
|
|
|
|
int initial_userns_fd_ = -1;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_FORKSERVER_H_
|