Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / sandbox / linux / syscall_broker / broker_process.h
blob50e7eee034ad2f955b1d9d7a551d3d8df53f225c
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
3 // found in the LICENSE file.
5 #ifndef SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_
6 #define SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/pickle.h"
15 #include "base/process/process.h"
16 #include "sandbox/linux/syscall_broker/broker_policy.h"
17 #include "sandbox/sandbox_export.h"
19 namespace sandbox {
21 namespace syscall_broker {
23 class BrokerClient;
25 // Create a new "broker" process to which we can send requests via an IPC
26 // channel by forking the current process.
27 // This is a low level IPC mechanism that is suitable to be called from a
28 // signal handler.
29 // A process would typically create a broker process before entering
30 // sandboxing.
31 // 1. BrokerProcess open_broker(read_whitelist, write_whitelist);
32 // 2. CHECK(open_broker.Init(NULL));
33 // 3. Enable sandbox.
34 // 4. Use open_broker.Open() to open files.
35 class SANDBOX_EXPORT BrokerProcess {
36 public:
37 // |denied_errno| is the error code returned when methods such as Open()
38 // or Access() are invoked on a file which is not in the whitelist. EACCESS
39 // would be a typical value.
40 // |allowed_r_files| and |allowed_w_files| are white lists of files that can
41 // be opened later via the Open() API, respectively for reading and writing.
42 // A file available read-write should be listed in both.
43 // |fast_check_in_client| and |quiet_failures_for_tests| are reserved for
44 // unit tests, don't use it.
45 BrokerProcess(int denied_errno,
46 const std::vector<std::string>& allowed_r_files,
47 const std::vector<std::string>& allowed_w_files,
48 bool fast_check_in_client = true,
49 bool quiet_failures_for_tests = false);
50 ~BrokerProcess();
51 // Will initialize the broker process. There should be no threads at this
52 // point, since we need to fork().
53 // broker_process_init_callback will be called in the new broker process,
54 // after fork() returns.
55 bool Init(const base::Callback<bool(void)>& broker_process_init_callback);
57 // Can be used in place of access(). Will be async signal safe.
58 // X_OK will always return an error in practice since the broker process
59 // doesn't support execute permissions.
60 // It's similar to the access() system call and will return -errno on errors.
61 int Access(const char* pathname, int mode) const;
62 // Can be used in place of open(). Will be async signal safe.
63 // The implementation only supports certain white listed flags and will
64 // return -EPERM on other flags.
65 // It's similar to the open() system call and will return -errno on errors.
66 int Open(const char* pathname, int flags) const;
68 int broker_pid() const { return broker_pid_; }
70 private:
71 friend class BrokerProcessTestHelper;
73 // Close the IPC channel with the other party. This should only be used
74 // by tests an none of the class methods should be used afterwards.
75 void CloseChannel();
77 bool initialized_; // Whether we've been through Init() yet.
78 const bool fast_check_in_client_;
79 const bool quiet_failures_for_tests_;
80 pid_t broker_pid_; // The PID of the broker (child).
81 syscall_broker::BrokerPolicy policy_; // The sandboxing policy.
82 scoped_ptr<syscall_broker::BrokerClient> broker_client_;
84 DISALLOW_COPY_AND_ASSIGN(BrokerProcess);
87 } // namespace syscall_broker
89 } // namespace sandbox
91 #endif // SANDBOX_LINUX_SERVICES_BROKER_PROCESS_H_