| 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_SERVICES_BROKER_PROCESS_H_ | 5 #ifndef SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_ |
| 6 #define SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_ | 6 #define SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/pickle.h" | 14 #include "base/pickle.h" |
| 14 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 16 #include "sandbox/linux/syscall_broker/broker_policy.h" |
| 15 #include "sandbox/sandbox_export.h" | 17 #include "sandbox/sandbox_export.h" |
| 16 | 18 |
| 17 namespace sandbox { | 19 namespace sandbox { |
| 18 | 20 |
| 21 namespace syscall_broker { |
| 22 class BrokerClient; |
| 23 } |
| 24 |
| 19 // Create a new "broker" process to which we can send requests via an IPC | 25 // Create a new "broker" process to which we can send requests via an IPC |
| 20 // channel. | 26 // channel by forking the current process. |
| 21 // This is a low level IPC mechanism that is suitable to be called from a | 27 // This is a low level IPC mechanism that is suitable to be called from a |
| 22 // signal handler. | 28 // signal handler. |
| 23 // A process would typically create a broker process before entering | 29 // A process would typically create a broker process before entering |
| 24 // sandboxing. | 30 // sandboxing. |
| 25 // 1. BrokerProcess open_broker(read_whitelist, write_whitelist); | 31 // 1. BrokerProcess open_broker(read_whitelist, write_whitelist); |
| 26 // 2. CHECK(open_broker.Init(NULL)); | 32 // 2. CHECK(open_broker.Init(NULL)); |
| 27 // 3. Enable sandbox. | 33 // 3. Enable sandbox. |
| 28 // 4. Use open_broker.Open() to open files. | 34 // 4. Use open_broker.Open() to open files. |
| 29 class SANDBOX_EXPORT BrokerProcess { | 35 class SANDBOX_EXPORT BrokerProcess { |
| 30 public: | 36 public: |
| 31 // |denied_errno| is the error code returned when methods such as Open() | 37 // |denied_errno| is the error code returned when methods such as Open() |
| 32 // or Access() are invoked on a file which is not in the whitelist. EACCESS | 38 // or Access() are invoked on a file which is not in the whitelist. EACCESS |
| 33 // would be a typical value. | 39 // would be a typical value. |
| 34 // |allowed_r_files| and |allowed_w_files| are white lists of files that can | 40 // |allowed_r_files| and |allowed_w_files| are white lists of files that can |
| 35 // be opened later via the Open() API, respectively for reading and writing. | 41 // be opened later via the Open() API, respectively for reading and writing. |
| 36 // A file available read-write should be listed in both. | 42 // A file available read-write should be listed in both. |
| 37 // |fast_check_in_client| and |quiet_failures_for_tests| are reserved for | 43 // |fast_check_in_client| and |quiet_failures_for_tests| are reserved for |
| 38 // unit tests, don't use it. | 44 // unit tests, don't use it. |
| 39 explicit BrokerProcess(int denied_errno, | 45 BrokerProcess(int denied_errno, |
| 40 const std::vector<std::string>& allowed_r_files, | 46 const std::vector<std::string>& allowed_r_files, |
| 41 const std::vector<std::string>& allowed_w_files, | 47 const std::vector<std::string>& allowed_w_files, |
| 42 bool fast_check_in_client = true, | 48 bool fast_check_in_client = true, |
| 43 bool quiet_failures_for_tests = false); | 49 bool quiet_failures_for_tests = false); |
| 44 ~BrokerProcess(); | 50 ~BrokerProcess(); |
| 45 // Will initialize the broker process. There should be no threads at this | 51 // Will initialize the broker process. There should be no threads at this |
| 46 // point, since we need to fork(). | 52 // point, since we need to fork(). |
| 47 // broker_process_init_callback will be called in the new broker process, | 53 // broker_process_init_callback will be called in the new broker process, |
| 48 // after fork() returns. | 54 // after fork() returns. |
| 49 bool Init(const base::Callback<bool(void)>& broker_process_init_callback); | 55 bool Init(const base::Callback<bool(void)>& broker_process_init_callback); |
| 50 | 56 |
| 51 // Can be used in place of access(). Will be async signal safe. | 57 // Can be used in place of access(). Will be async signal safe. |
| 52 // X_OK will always return an error in practice since the broker process | 58 // X_OK will always return an error in practice since the broker process |
| 53 // doesn't support execute permissions. | 59 // doesn't support execute permissions. |
| 54 // It's similar to the access() system call and will return -errno on errors. | 60 // It's similar to the access() system call and will return -errno on errors. |
| 55 int Access(const char* pathname, int mode) const; | 61 int Access(const char* pathname, int mode) const; |
| 56 // Can be used in place of open(). Will be async signal safe. | 62 // Can be used in place of open(). Will be async signal safe. |
| 57 // The implementation only supports certain white listed flags and will | 63 // The implementation only supports certain white listed flags and will |
| 58 // return -EPERM on other flags. | 64 // return -EPERM on other flags. |
| 59 // It's similar to the open() system call and will return -errno on errors. | 65 // It's similar to the open() system call and will return -errno on errors. |
| 60 int Open(const char* pathname, int flags) const; | 66 int Open(const char* pathname, int flags) const; |
| 61 | 67 |
| 62 int broker_pid() const { return broker_pid_; } | 68 int broker_pid() const { return broker_pid_; } |
| 63 | 69 |
| 64 private: | 70 private: |
| 65 enum IPCCommands { | 71 bool initialized_; // Whether we've been through Init() yet. |
| 66 kCommandInvalid = 0, | 72 bool is_child_; // Whether we're the child (broker process). |
| 67 kCommandOpen, | 73 bool fast_check_in_client_; |
| 68 kCommandAccess, | 74 bool quiet_failures_for_tests_; |
| 69 }; | 75 pid_t broker_pid_; // The PID of the broker (child). |
| 70 int PathAndFlagsSyscall(enum IPCCommands command_type, | 76 syscall_broker::BrokerPolicy policy_; // The sandboxing policy. |
| 71 const char* pathname, | 77 scoped_ptr<syscall_broker::BrokerClient> |
| 72 int flags) const; | 78 broker_client_; // Can only exist if is_child_ is true. |
| 73 bool HandleRequest() const; | |
| 74 bool HandleRemoteCommand(IPCCommands command_type, | |
| 75 int reply_ipc, | |
| 76 const Pickle& read_pickle, | |
| 77 PickleIterator iter) const; | |
| 78 | 79 |
| 79 void AccessFileForIPC(const std::string& requested_filename, | |
| 80 int mode, | |
| 81 Pickle* write_pickle) const; | |
| 82 void OpenFileForIPC(const std::string& requested_filename, | |
| 83 int flags, | |
| 84 Pickle* write_pickle, | |
| 85 std::vector<int>* opened_files) const; | |
| 86 bool GetFileNameIfAllowedToAccess(const char*, int, const char**) const; | |
| 87 bool GetFileNameIfAllowedToOpen(const char*, int, const char**) const; | |
| 88 const int denied_errno_; | |
| 89 bool initialized_; // Whether we've been through Init() yet. | |
| 90 bool is_child_; // Whether we're the child (broker process). | |
| 91 bool fast_check_in_client_; // Whether to forward a request that we know | |
| 92 // will be denied to the broker. | |
| 93 bool quiet_failures_for_tests_; // Disable certain error message when | |
| 94 // testing for failures. | |
| 95 pid_t broker_pid_; // The PID of the broker (child). | |
| 96 const std::vector<std::string> allowed_r_files_; // Files allowed for read. | |
| 97 const std::vector<std::string> allowed_w_files_; // Files allowed for write. | |
| 98 int ipc_socketpair_; // Our communication channel to parent or child. | 80 int ipc_socketpair_; // Our communication channel to parent or child. |
| 99 DISALLOW_IMPLICIT_CONSTRUCTORS(BrokerProcess); | 81 DISALLOW_COPY_AND_ASSIGN(BrokerProcess); |
| 100 | 82 |
| 101 friend class BrokerProcessTestHelper; | 83 friend class BrokerProcessTestHelper; |
| 102 }; | 84 }; |
| 103 | 85 |
| 104 } // namespace sandbox | 86 } // namespace sandbox |
| 105 | 87 |
| 106 #endif // SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_ | 88 #endif // SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_ |
| OLD | NEW |