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