OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ |
| 6 #define SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ |
| 7 |
| 8 #include "base/files/scoped_file.h" |
| 9 #include "base/process/launch.h" |
| 10 #include "base/process/process_handle.h" |
| 11 #include "sandbox/sandbox_export.h" |
| 12 |
| 13 namespace sandbox { |
| 14 |
| 15 // Helper class for starting a process inside a new user, PID, and network |
| 16 // namespace. The expected usage looks like: |
| 17 // |
| 18 // A typical use for "A" launching a sandboxed process "B" would be: |
| 19 // 1. A sets up a command line for process B. |
| 20 // 2. A calls SetupLaunchOptions which sets up the a pipe for communicating with |
| 21 // the child. |
| 22 // 3. A launches B with base::LaunchProcess. |
| 23 // 4. A calls PrepareSandboxedProcess, at which point B begins running. |
| 24 // 5. B should be prepared to assume the role of init(1). In particular, apart |
| 25 // from SIGKILL and SIGSTOP, B cannot receive any signal for which it does |
| 26 // not have an explicit signal handler registered. |
| 27 // If B dies, all the processes in the namespace will die. |
| 28 // B can fork() and the parent can assume the role of init(1), by using |
| 29 // CreateInitProcessReaper(). |
| 30 // 6. B chroots using Credentials::MoveToNewUserNS() and |
| 31 // Credentials::DropFileSystemAccess(). |
| 32 // |
| 33 // An instance of this class may only be used once. |
| 34 class SANDBOX_EXPORT NamespaceSandbox { |
| 35 public: |
| 36 NamespaceSandbox(); |
| 37 ~NamespaceSandbox(); |
| 38 |
| 39 // Setup the launch options for the sandboxed process. The caller is |
| 40 // responsible for setting options->fds_to_remap = fds_to_remap. |
| 41 void SetupLaunchOptions(base::LaunchOptions* options, |
| 42 base::FileHandleMappingVector* fds_to_remap); |
| 43 |
| 44 // Prepares the child process before it execs the new program. This adds an |
| 45 // identity mapping for the uid and gid inside the user namespace, then |
| 46 // signals the child to exec the new program. |
| 47 void PrepareSandboxedProcess(base::ProcessId pid); |
| 48 |
| 49 // Returns whether the namespace sandbox created a new user, PID, and network |
| 50 // namespace. In particular, InNewUserNamespace should return true iff the |
| 51 // process was started via this class. |
| 52 static bool InNewUserNamespace(); |
| 53 static bool InNewPidNamespace(); |
| 54 static bool InNewNetNamespace(); |
| 55 |
| 56 private: |
| 57 class ReadFromPipeDelegate : public base::LaunchOptions::PreExecDelegate { |
| 58 public: |
| 59 ReadFromPipeDelegate(); |
| 60 ~ReadFromPipeDelegate() override; |
| 61 void RunAsyncSafe() override; |
| 62 void set_fd(int fd); |
| 63 |
| 64 private: |
| 65 int fd_; |
| 66 DISALLOW_COPY_AND_ASSIGN(ReadFromPipeDelegate); |
| 67 }; |
| 68 // Pipe fds used to communicate with the child process. |
| 69 base::ScopedFD read_fd_; |
| 70 base::ScopedFD write_fd_; |
| 71 |
| 72 ReadFromPipeDelegate read_from_pipe_delegate_; |
| 73 }; |
| 74 |
| 75 } // namespace sandbox |
| 76 |
| 77 #endif // SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ |
OLD | NEW |