Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ | 5 #ifndef SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ |
| 6 #define SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ | 6 #define SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ |
| 7 | 7 |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/process/launch.h" | 15 #include "base/process/launch.h" |
| 16 #include "base/process/process.h" | 16 #include "base/process/process.h" |
| 17 #include "sandbox/sandbox_export.h" | 17 #include "sandbox/sandbox_export.h" |
| 18 | 18 |
| 19 namespace sandbox { | 19 namespace sandbox { |
| 20 | 20 |
| 21 inline int SignalExitCode(int sig) { | |
|
jln (very slow on Chromium)
2015/05/28 09:02:33
Let's qualify this in NamespaceSandbox:: as a stat
rickyz (no longer on Chrome)
2015/05/29 23:16:39
Done.
| |
| 22 return -sig & 0xff; | |
| 23 } | |
| 24 | |
| 21 // Helper class for starting a process inside a new user, PID, and network | 25 // Helper class for starting a process inside a new user, PID, and network |
| 22 // namespace. Before using a namespace sandbox, check for namespaces support | 26 // namespace. Before using a namespace sandbox, check for namespaces support |
| 23 // using Credentials::CanCreateProcessInNewUserNS. | 27 // using Credentials::CanCreateProcessInNewUserNS. |
| 24 // | 28 // |
| 25 // A typical use for "A" launching a sandboxed process "B" would be: | 29 // A typical use for "A" launching a sandboxed process "B" would be: |
| 26 // 1. A sets up a command line and launch options for process B. | 30 // 1. A sets up a command line and launch options for process B. |
| 27 // 2. A launches B with LaunchProcess. | 31 // 2. A launches B with LaunchProcess. |
| 28 // 3. B should be prepared to assume the role of init(1). In particular, apart | 32 // 3. B should be prepared to assume the role of init(1). In particular, apart |
| 29 // from SIGKILL and SIGSTOP, B cannot receive any signal for which it does | 33 // from SIGKILL and SIGSTOP, B cannot receive any signal for which it does |
| 30 // not have an explicit signal handler registered. | 34 // not have an explicit signal handler registered. |
| 31 // If B dies, all the processes in the namespace will die. | 35 // If B dies, all the processes in the namespace will die. |
| 32 // B can fork() and the parent can assume the role of init(1), by using | 36 // B can fork() and the parent can assume the role of init(1), by using |
| 33 // CreateInitProcessReaper(). | 37 // CreateInitProcessReaper(). |
| 34 // 4. B chroots using Credentials::MoveToNewUserNS() and | 38 // 4. B chroots using Credentials::MoveToNewUserNS() and |
| 35 // Credentials::DropFileSystemAccess() | 39 // Credentials::DropFileSystemAccess() |
| 36 // 5. B drops capabilities gained by entering the new user namespace with | 40 // 5. B drops capabilities gained by entering the new user namespace with |
| 37 // Credentials::DropAllCapabilities(). | 41 // Credentials::DropAllCapabilities(). |
| 38 class SANDBOX_EXPORT NamespaceSandbox { | 42 class SANDBOX_EXPORT NamespaceSandbox { |
| 39 public: | 43 public: |
| 40 #if !defined(OS_NACL_NONSFI) | 44 #if !defined(OS_NACL_NONSFI) |
| 41 static const int kDefaultExitCode = 1; | |
| 42 | |
| 43 // Launch a new process inside its own user/PID/network namespaces (depending | 45 // Launch a new process inside its own user/PID/network namespaces (depending |
| 44 // on kernel support). Requires at a minimum that user namespaces are | 46 // on kernel support). Requires at a minimum that user namespaces are |
| 45 // supported (use Credentials::CanCreateProcessInNewUserNS to check this). | 47 // supported (use Credentials::CanCreateProcessInNewUserNS to check this). |
| 46 // | 48 // |
| 47 // pre_exec_delegate and clone_flags fields of LaunchOptions should be nullptr | 49 // pre_exec_delegate and clone_flags fields of LaunchOptions should be nullptr |
| 48 // and 0, respectively, since this function makes a copy of options and | 50 // and 0, respectively, since this function makes a copy of options and |
| 49 // overrides them. | 51 // overrides them. |
| 50 static base::Process LaunchProcess(const base::CommandLine& cmdline, | 52 static base::Process LaunchProcess(const base::CommandLine& cmdline, |
| 51 const base::LaunchOptions& options); | 53 const base::LaunchOptions& options); |
| 52 static base::Process LaunchProcess(const std::vector<std::string>& argv, | 54 static base::Process LaunchProcess(const std::vector<std::string>& argv, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 63 // processes ignore such signals unless they have an explicit handler set. | 65 // processes ignore such signals unless they have an explicit handler set. |
| 64 // | 66 // |
| 65 // This function requries CAP_SYS_ADMIN. If |drop_capabilities_in_child| is | 67 // This function requries CAP_SYS_ADMIN. If |drop_capabilities_in_child| is |
| 66 // true, then capabilities are dropped in the child. | 68 // true, then capabilities are dropped in the child. |
| 67 static pid_t ForkInNewPidNamespace(bool drop_capabilities_in_child); | 69 static pid_t ForkInNewPidNamespace(bool drop_capabilities_in_child); |
| 68 | 70 |
| 69 // Installs a signal handler for: | 71 // Installs a signal handler for: |
| 70 // | 72 // |
| 71 // SIGHUP, SIGINT, SIGABRT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 | 73 // SIGHUP, SIGINT, SIGABRT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
| 72 // | 74 // |
| 73 // that exits with kDefaultExitCode. These are signals whose default action is | 75 // that exits with SignalExitCode(sig). These are signals whose default action |
| 74 // to terminate the program (apart from SIGILL, SIGFPE, and SIGSEGV, which | 76 // is to terminate the program (apart from SIGILL, SIGFPE, and SIGSEGV, which |
| 75 // will still terminate the process if e.g. an illegal instruction is | 77 // will still terminate the process if e.g. an illegal instruction is |
| 76 // encountered, etc.). | 78 // encountered, etc.). |
| 77 // | 79 // |
| 78 // If any of these already had a signal handler installed, this function will | 80 // If any of these already had a signal handler installed, this function will |
| 79 // not override them. | 81 // not override them. |
| 80 static void InstallDefaultTerminationSignalHandlers(); | 82 static void InstallDefaultTerminationSignalHandlers(); |
| 81 | 83 |
| 82 // Installs a signal handler for |sig| which exits with |exit_code|. If a | 84 // Installs a signal handler for |sig| which exits with |exit_code|. If a |
| 83 // signal handler was already present for |sig|, does nothing and returns | 85 // signal handler was already present for |sig|, does nothing and returns |
| 84 // false. | 86 // false. |
| 85 static bool InstallTerminationSignalHandler(int sig, int exit_code); | 87 static bool InstallTerminationSignalHandler(int sig, int exit_code); |
| 86 #endif // !defined(OS_NACL_NONSFI) | 88 #endif // !defined(OS_NACL_NONSFI) |
| 87 | 89 |
| 88 // Returns whether the namespace sandbox created a new user, PID, and network | 90 // Returns whether the namespace sandbox created a new user, PID, and network |
| 89 // namespace. In particular, InNewUserNamespace should return true iff the | 91 // namespace. In particular, InNewUserNamespace should return true iff the |
| 90 // process was started via this class. | 92 // process was started via this class. |
| 91 static bool InNewUserNamespace(); | 93 static bool InNewUserNamespace(); |
| 92 static bool InNewPidNamespace(); | 94 static bool InNewPidNamespace(); |
| 93 static bool InNewNetNamespace(); | 95 static bool InNewNetNamespace(); |
| 94 | 96 |
| 95 private: | 97 private: |
| 96 DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceSandbox); | 98 DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceSandbox); |
| 97 }; | 99 }; |
| 98 | 100 |
| 99 } // namespace sandbox | 101 } // namespace sandbox |
| 100 | 102 |
| 101 #endif // SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ | 103 #endif // SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_ |
| OLD | NEW |