Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / sandbox / linux / services / namespace_sandbox.h
blob80097fb16afa625415070e8e581540e514e67abf
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.
5 #ifndef SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_
6 #define SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_
8 #include <sys/types.h>
10 #include <string>
11 #include <vector>
13 #include "base/command_line.h"
14 #include "base/macros.h"
15 #include "base/process/launch.h"
16 #include "base/process/process.h"
17 #include "sandbox/sandbox_export.h"
19 namespace sandbox {
21 // Helper class for starting a process inside a new user, PID, and network
22 // namespace. Before using a namespace sandbox, check for namespaces support
23 // using Credentials::CanCreateProcessInNewUserNS.
25 // 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.
27 // 2. A launches B with LaunchProcess.
28 // 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
30 // not have an explicit signal handler registered.
31 // 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
33 // CreateInitProcessReaper().
34 // 4. B chroots using Credentials::MoveToNewUserNS() and
35 // Credentials::DropFileSystemAccess()
36 // 5. B drops capabilities gained by entering the new user namespace with
37 // Credentials::DropAllCapabilities().
38 class SANDBOX_EXPORT NamespaceSandbox {
39 public:
40 #if !defined(OS_NACL_NONSFI)
41 static const int kDefaultExitCode = 1;
43 // Launch a new process inside its own user/PID/network namespaces (depending
44 // on kernel support). Requires at a minimum that user namespaces are
45 // supported (use Credentials::CanCreateProcessInNewUserNS to check this).
47 // 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
49 // overrides them.
50 static base::Process LaunchProcess(const base::CommandLine& cmdline,
51 const base::LaunchOptions& options);
52 static base::Process LaunchProcess(const std::vector<std::string>& argv,
53 const base::LaunchOptions& options);
55 // Forks a process in its own PID namespace. The child process is the init
56 // process inside of the PID namespace, so if the child needs to fork further,
57 // it should call CreateInitProcessReaper, which turns the init process into a
58 // reaper process.
60 // Otherwise, the child should setup handlers for signals which should
61 // terminate the process using InstallDefaultTerminationSignalHandlers or
62 // InstallTerminationSignalHandler. This works around the fact that init
63 // processes ignore such signals unless they have an explicit handler set.
65 // This function requries CAP_SYS_ADMIN. If |drop_capabilities_in_child| is
66 // true, then capabilities are dropped in the child.
67 static pid_t ForkInNewPidNamespace(bool drop_capabilities_in_child);
69 // Installs a signal handler for:
71 // SIGHUP, SIGINT, SIGABRT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
73 // that exits with kDefaultExitCode. These are signals whose default action is
74 // to terminate the program (apart from SIGILL, SIGFPE, and SIGSEGV, which
75 // will still terminate the process if e.g. an illegal instruction is
76 // encountered, etc.).
78 // If any of these already had a signal handler installed, this function will
79 // not override them.
80 static void InstallDefaultTerminationSignalHandlers();
82 // 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
84 // false.
85 static bool InstallTerminationSignalHandler(int sig, int exit_code);
86 #endif // !defined(OS_NACL_NONSFI)
88 // Returns whether the namespace sandbox created a new user, PID, and network
89 // namespace. In particular, InNewUserNamespace should return true iff the
90 // process was started via this class.
91 static bool InNewUserNamespace();
92 static bool InNewPidNamespace();
93 static bool InNewNetNamespace();
95 private:
96 DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceSandbox);
99 } // namespace sandbox
101 #endif // SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_