Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_SUID_SETUID_SANDBOX_CLIENT_H_ | 5 #ifndef SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_ |
| 6 #define SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_ | 6 #define SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/environment.h" | 8 #include "base/environment.h" |
|
hidehiko
2015/02/04 15:40:05
nit: It's better to keep including base/basictypes
mdempsky
2015/02/05 03:02:07
Oops, yeah, added "base/macros.h".
| |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/files/scoped_file.h" | |
| 13 #include "base/process/launch.h" | |
| 14 #include "sandbox/sandbox_export.h" | 9 #include "sandbox/sandbox_export.h" |
| 15 | 10 |
| 16 namespace sandbox { | 11 namespace sandbox { |
| 17 | 12 |
| 18 // Helper class to use the setuid sandbox. This class is to be used both | 13 // Helper class to use the setuid sandbox. This class is to be used |
| 19 // before launching the setuid helper and after being executed through the | 14 // after being executed through the setuid helper. |
| 20 // setuid helper. | |
| 21 // This class is difficult to use. It has been created by refactoring very old | 15 // This class is difficult to use. It has been created by refactoring very old |
| 22 // code scathered through the Chromium code base. | 16 // code scathered through the Chromium code base. |
| 23 // | 17 // |
| 24 // A typical use for "A" launching a sandboxed process "B" would be: | 18 // A typical use for "A" launching a sandboxed process "B" would be: |
| 25 // 1. A calls SetupLaunchEnvironment() | 19 // (Steps 1 through 4 are described in setuid_sandbox_host.h.) |
| 26 // 2. A sets up a CommandLine and then amends it with | |
| 27 // PrependWrapper() (or manually, by relying on GetSandboxBinaryPath()). | |
| 28 // 3. A uses SetupLaunchOptions() to arrange for a dummy descriptor for the | |
| 29 // setuid sandbox ABI. | |
| 30 // 4. A launches B with base::LaunchProcess, using the amended CommandLine. | |
| 31 // 5. B uses CloseDummyFile() to close the dummy file descriptor. | 20 // 5. B uses CloseDummyFile() to close the dummy file descriptor. |
| 32 // 6. B performs various initializations that require access to the file | 21 // 6. B performs various initializations that require access to the file |
| 33 // system. | 22 // system. |
| 34 // 6.b (optional) B uses sandbox::Credentials::HasOpenDirectory() to verify | 23 // 6.b (optional) B uses sandbox::Credentials::HasOpenDirectory() to verify |
| 35 // that no directory is kept open (which would allow bypassing the setuid | 24 // that no directory is kept open (which would allow bypassing the setuid |
| 36 // sandbox). | 25 // sandbox). |
| 37 // 7. B should be prepared to assume the role of init(1). In particular, B | 26 // 7. B should be prepared to assume the role of init(1). In particular, B |
| 38 // cannot receive any signal from any other process, excluding SIGKILL. | 27 // cannot receive any signal from any other process, excluding SIGKILL. |
| 39 // If B dies, all the processes in the namespace will die. | 28 // If B dies, all the processes in the namespace will die. |
| 40 // B can fork() and the parent can assume the role of init(1), by using | 29 // B can fork() and the parent can assume the role of init(1), by using |
| 41 // CreateInitProcessReaper(). | 30 // CreateInitProcessReaper(). |
| 42 // 8. B requests being chroot-ed through ChrootMe() and | 31 // 8. B requests being chroot-ed through ChrootMe() and |
| 43 // requests other sandboxing status via the status functions. | 32 // requests other sandboxing status via the status functions. |
| 44 class SANDBOX_EXPORT SetuidSandboxClient { | 33 class SANDBOX_EXPORT SetuidSandboxClient { |
| 45 public: | 34 public: |
| 46 // All instantation should go through this factory method. | 35 // All instantation should go through this factory method. |
| 47 static class SetuidSandboxClient* Create(); | 36 static SetuidSandboxClient* Create(); |
| 48 ~SetuidSandboxClient(); | 37 ~SetuidSandboxClient(); |
| 49 | 38 |
| 50 // Close the dummy file descriptor leftover from the sandbox ABI. | 39 // Close the dummy file descriptor leftover from the sandbox ABI. |
| 51 void CloseDummyFile(); | 40 void CloseDummyFile(); |
| 52 // Ask the setuid helper over the setuid sandbox IPC channel to chroot() us | 41 // Ask the setuid helper over the setuid sandbox IPC channel to chroot() us |
| 53 // to an empty directory. | 42 // to an empty directory. |
| 54 // Will only work if we have been launched through the setuid helper. | 43 // Will only work if we have been launched through the setuid helper. |
| 55 bool ChrootMe(); | 44 bool ChrootMe(); |
| 56 | 45 |
| 57 // Did we get launched through an up to date setuid binary ? | 46 // Did we get launched through an up to date setuid binary ? |
| 58 bool IsSuidSandboxUpToDate() const; | 47 bool IsSuidSandboxUpToDate() const; |
| 59 // Did we get launched through the setuid helper ? | 48 // Did we get launched through the setuid helper ? |
| 60 bool IsSuidSandboxChild() const; | 49 bool IsSuidSandboxChild() const; |
| 61 // Did the setuid helper create a new PID namespace ? | 50 // Did the setuid helper create a new PID namespace ? |
| 62 bool IsInNewPIDNamespace() const; | 51 bool IsInNewPIDNamespace() const; |
| 63 // Did the setuid helper create a new network namespace ? | 52 // Did the setuid helper create a new network namespace ? |
| 64 bool IsInNewNETNamespace() const; | 53 bool IsInNewNETNamespace() const; |
| 65 // Are we done and fully sandboxed ? | 54 // Are we done and fully sandboxed ? |
| 66 bool IsSandboxed() const; | 55 bool IsSandboxed() const; |
| 67 | 56 |
| 68 // The setuid sandbox may still be disabled via the environment. | |
| 69 // This is tracked in crbug.com/245376. | |
| 70 bool IsDisabledViaEnvironment(); | |
| 71 // Get the sandbox binary path. This method knows about the | |
| 72 // CHROME_DEVEL_SANDBOX environment variable used for user-managed builds. If | |
| 73 // the sandbox binary cannot be found, it will return an empty FilePath. | |
| 74 base::FilePath GetSandboxBinaryPath(); | |
| 75 // Modify |cmd_line| to launch via the setuid sandbox. Crash if the setuid | |
| 76 // sandbox binary cannot be found. |cmd_line| must not be NULL. | |
| 77 void PrependWrapper(base::CommandLine* cmd_line); | |
| 78 // Set-up the launch options for launching via the setuid sandbox. Caller is | |
| 79 // responsible for keeping |dummy_fd| alive until LaunchProcess() completes. | |
| 80 // |options| and |fds_to_remap| must not be NULL. | |
| 81 // (Keeping |dummy_fd| alive is an unfortunate historical artifact of the | |
| 82 // chrome-sandbox ABI.) | |
| 83 void SetupLaunchOptions(base::LaunchOptions* options, | |
| 84 base::FileHandleMappingVector* fds_to_remap, | |
| 85 base::ScopedFD* dummy_fd); | |
| 86 // Set-up the environment. This should be done prior to launching the setuid | |
| 87 // helper. | |
| 88 void SetupLaunchEnvironment(); | |
| 89 | |
| 90 private: | 57 private: |
| 91 SetuidSandboxClient(); | 58 SetuidSandboxClient(); |
| 92 | 59 |
| 93 // Holds the environment. Will never be NULL. | 60 // Holds the environment. Will never be NULL. |
| 94 base::Environment* env_; | 61 base::Environment* env_; |
| 95 bool sandboxed_; | 62 bool sandboxed_; |
| 96 | 63 |
| 97 DISALLOW_COPY_AND_ASSIGN(SetuidSandboxClient); | 64 DISALLOW_COPY_AND_ASSIGN(SetuidSandboxClient); |
| 98 }; | 65 }; |
| 99 | 66 |
| 100 } // namespace sandbox | 67 } // namespace sandbox |
| 101 | 68 |
| 102 #endif // SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_ | 69 #endif // SANDBOX_LINUX_SUID_SETUID_SANDBOX_CLIENT_H_ |
| OLD | NEW |