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_
11 #include "base/command_line.h"
12 #include "base/macros.h"
13 #include "base/process/launch.h"
14 #include "base/process/process.h"
15 #include "sandbox/sandbox_export.h"
19 // Helper class for starting a process inside a new user, PID, and network
20 // namespace. Before using a namespace sandbox, check for namespaces support
21 // using Credentials::CanCreateProcessInNewUserNS.
23 // A typical use for "A" launching a sandboxed process "B" would be:
24 // 1. A sets up a command line and launch options for process B.
25 // 2. A launches B with LaunchProcess.
26 // 3. B should be prepared to assume the role of init(1). In particular, apart
27 // from SIGKILL and SIGSTOP, B cannot receive any signal for which it does
28 // not have an explicit signal handler registered.
29 // If B dies, all the processes in the namespace will die.
30 // B can fork() and the parent can assume the role of init(1), by using
31 // CreateInitProcessReaper().
32 // 4. B chroots using Credentials::MoveToNewUserNS() and
33 // Credentials::DropFileSystemAccess()
34 // 5. B drops capabilities gained by entering the new user namespace with
35 // Credentials::DropAllCapabilities().
36 class SANDBOX_EXPORT NamespaceSandbox
{
38 // Launch a new process inside its own user/PID/network namespaces (depending
39 // on kernel support). Requires at a minimum that user namespaces are
40 // supported (use Credentials::CanCreateProcessInNewUserNS to check this).
42 // pre_exec_delegate and clone_flags fields of LaunchOptions should be nullptr
43 // and 0, respectively, since this function makes a copy of options and
45 static base::Process
LaunchProcess(const base::CommandLine
& cmdline
,
46 const base::LaunchOptions
& options
);
47 static base::Process
LaunchProcess(const std::vector
<std::string
>& argv
,
48 const base::LaunchOptions
& options
);
50 // Returns whether the namespace sandbox created a new user, PID, and network
51 // namespace. In particular, InNewUserNamespace should return true iff the
52 // process was started via this class.
53 static bool InNewUserNamespace();
54 static bool InNewPidNamespace();
55 static bool InNewNetNamespace();
58 DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceSandbox
);
61 } // namespace sandbox
63 #endif // SANDBOX_LINUX_SERVICES_NAMESPACE_SANDBOX_H_