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_SUID_SETUID_SANDBOX_HOST_H_
6 #define SANDBOX_LINUX_SUID_SETUID_SANDBOX_HOST_H_
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_file.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/process/launch.h"
13 #include "sandbox/sandbox_export.h"
17 // Helper class to use the setuid sandbox. This class is to be used
18 // before launching the setuid helper.
19 // This class is difficult to use. It has been created by refactoring very old
20 // code scathered through the Chromium code base.
22 // A typical use for "A" launching a sandboxed process "B" would be:
23 // 1. A calls SetupLaunchEnvironment()
24 // 2. A sets up a base::CommandLine and then amends it with
25 // PrependWrapper() (or manually, by relying on GetSandboxBinaryPath()).
26 // 3. A uses SetupLaunchOptions() to arrange for a dummy descriptor for the
27 // setuid sandbox ABI.
28 // 4. A launches B with base::LaunchProcess, using the amended
30 // (The remaining steps are described within setuid_sandbox_client.h.)
31 class SANDBOX_EXPORT SetuidSandboxHost
{
33 // All instantation should go through this factory method.
34 static SetuidSandboxHost
* Create();
37 // The setuid sandbox may still be disabled via the environment.
38 // This is tracked in crbug.com/245376.
39 bool IsDisabledViaEnvironment();
40 // Get the sandbox binary path. This method knows about the
41 // CHROME_DEVEL_SANDBOX environment variable used for user-managed builds. If
42 // the sandbox binary cannot be found, it will return an empty FilePath.
43 base::FilePath
GetSandboxBinaryPath();
44 // Modify |cmd_line| to launch via the setuid sandbox. Crash if the setuid
45 // sandbox binary cannot be found. |cmd_line| must not be NULL.
46 void PrependWrapper(base::CommandLine
* cmd_line
);
47 // Set-up the launch options for launching via the setuid sandbox. Caller is
48 // responsible for keeping |dummy_fd| alive until LaunchProcess() completes.
49 // |options| and |fds_to_remap| must not be NULL.
50 // (Keeping |dummy_fd| alive is an unfortunate historical artifact of the
51 // chrome-sandbox ABI.)
52 void SetupLaunchOptions(base::LaunchOptions
* options
,
53 base::FileHandleMappingVector
* fds_to_remap
,
54 base::ScopedFD
* dummy_fd
);
55 // Set-up the environment. This should be done prior to launching the setuid
57 void SetupLaunchEnvironment();
60 explicit SetuidSandboxHost(base::Environment
* env
);
62 // Holds the environment. Will never be NULL.
63 scoped_ptr
<base::Environment
> env_
;
65 DISALLOW_COPY_AND_ASSIGN(SetuidSandboxHost
);
68 } // namespace sandbox
70 #endif // SANDBOX_LINUX_SUID_SETUID_SANDBOX_HOST_H_