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