Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SYSCALL_BROKER_BROKER_POLICY_H_ | 5 #ifndef SANDBOX_LINUX_SYSCALL_BROKER_BROKER_POLICY_H_ |
| 6 #define SANDBOX_LINUX_SYSCALL_BROKER_BROKER_POLICY_H_ | 6 #define SANDBOX_LINUX_SYSCALL_BROKER_BROKER_POLICY_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 12 | 13 |
| 13 namespace sandbox { | 14 namespace sandbox { |
| 14 namespace syscall_broker { | 15 namespace syscall_broker { |
| 15 | 16 |
| 17 struct BrokerPermission { | |
|
Jorge Lucangeli Obes
2014/11/12 17:29:42
I thought about this a little last night. I think
| |
| 18 std::string path; // Allowed Path | |
| 19 bool recursive; // Allow everything under this path. |path| must be a dir. | |
| 20 bool unlink; // unlink after openning. | |
| 21 bool allow_read; | |
| 22 bool allow_write; | |
| 23 bool allow_create; | |
| 24 | |
| 25 BrokerPermission(std::string path_, | |
| 26 bool recursive_, | |
| 27 bool unlink_, | |
| 28 bool allow_read_, | |
| 29 bool allow_write_, | |
| 30 bool allow_create_) | |
| 31 : path(path_), | |
| 32 recursive(recursive_), | |
| 33 unlink(unlink_), | |
| 34 allow_read(allow_read_), | |
| 35 allow_write(allow_write_), | |
| 36 allow_create(allow_create_) {} | |
| 37 }; | |
| 38 | |
| 39 #define BROKER_PERM_READ_ONLY(path) \ | |
| 40 BrokerPermission(path, false, false, true, false, false) | |
| 41 | |
| 42 #define BROKER_PERM_READ_ONLY_RECURSIVE(path) \ | |
| 43 BrokerPermission(path, true, false, true, false, false) | |
| 44 | |
| 45 #define BROKER_PERM_WRITE_ONLY(path) \ | |
| 46 BrokerPermission(path, false, false, false, true, false) | |
| 47 | |
| 48 #define BROKER_PERM_READ_WRITE(path) \ | |
| 49 BrokerPermission(path, false, false, true, true, false) | |
| 50 | |
| 51 #define BROKER_PERM_READ_WRITE_CREATE(path) \ | |
| 52 BrokerPermission(path, false, false, true, true, true) | |
| 53 | |
| 54 #define BROKER_PERM_READ_WRITE_CREATE_UNLINK(path) \ | |
| 55 BrokerPermission(path, false, true, true, true, true) | |
| 56 | |
| 57 #define BROKER_PERM_READ_WRITE_CREATE_UNLINK_RECURSIVE(path) \ | |
| 58 BrokerPermission(path, true, true, true, true, true) | |
| 59 | |
| 16 // BrokerPolicy allows to define the security policy enforced by a | 60 // BrokerPolicy allows to define the security policy enforced by a |
| 17 // BrokerHost. The BrokerHost will evaluate requests sent over its | 61 // BrokerHost. The BrokerHost will evaluate requests sent over its |
| 18 // IPC channel according to the BrokerPolicy. | 62 // IPC channel according to the BrokerPolicy. |
| 19 // Some of the methods of this class can be used in an async-signal safe | 63 // Some of the methods of this class can be used in an async-signal safe |
| 20 // way. | 64 // way. |
| 21 class BrokerPolicy { | 65 class BrokerPolicy { |
| 22 public: | 66 public: |
| 23 // |denied_errno| is the error code returned when IPC requests for system | 67 // |denied_errno| is the error code returned when IPC requests for system |
| 24 // calls such as open() or access() are denied because a file is not in the | 68 // calls such as open() or access() are denied because a file is not in the |
| 25 // whitelist. EACCESS would be a typical value. | 69 // whitelist. EACCESS would be a typical value. |
| 26 // |allowed_r_files| and |allowed_w_files| are white lists of files that | 70 // |permissions| is a list of BrokerPermission objects that define |
| 27 // should be allowed for opening, respectively for reading and writing. | 71 // what the broker will allow. |
| 28 // A file available read-write should be listed in both. | |
| 29 BrokerPolicy(int denied_errno, | 72 BrokerPolicy(int denied_errno, |
| 30 const std::vector<std::string>& allowed_r_files, | 73 const std::vector<BrokerPermission>& permissions); |
| 31 const std::vector<std::string>& allowed_w_files_); | 74 |
| 32 ~BrokerPolicy(); | 75 ~BrokerPolicy(); |
| 33 | 76 |
| 34 // Check if calling access() should be allowed on |requested_filename| with | 77 // Check if calling access() should be allowed on |requested_filename| with |
| 35 // mode |requested_mode|. | 78 // mode |requested_mode|. |
| 36 // Note: access() being a system call to check permissions, this can get a bit | 79 // Note: access() being a system call to check permissions, this can get a bit |
| 37 // confusing. We're checking if calling access() should even be allowed with | 80 // confusing. We're checking if calling access() should even be allowed with |
| 38 // the same policy we would use for open(). | 81 // the same policy we would use for open(). |
| 39 // If |file_to_access| is not NULL, we will return the matching pointer from | 82 // If |file_to_access| is not NULL, we will return the matching pointer from |
| 40 // the whitelist. For paranoia a caller should then use |file_to_access|. See | 83 // the whitelist. For paranoia a caller should then use |file_to_access|. See |
| 41 // GetFileNameIfAllowedToOpen() for more explanation. | 84 // GetFileNameIfAllowedToOpen() for more explanation. |
| 42 // return true if calling access() on this file should be allowed, false | 85 // return true if calling access() on this file should be allowed, false |
| 43 // otherwise. | 86 // otherwise. |
| 44 // Async signal safe if and only if |file_to_access| is NULL. | 87 // Async signal safe if and only if |file_to_access| is NULL. |
| 45 bool GetFileNameIfAllowedToAccess(const char* requested_filename, | 88 bool GetFileNameIfAllowedToAccess(const char* requested_filename, |
| 46 int requested_mode, | 89 int requested_mode, |
| 47 const char** file_to_access) const; | 90 const char** file_to_access) const; |
| 48 | 91 |
| 49 // Check if |requested_filename| can be opened with flags |requested_flags|. | 92 // Check if |requested_filename| can be opened with flags |requested_flags|. |
| 50 // If |file_to_open| is not NULL, we will return the matching pointer from the | 93 // If |file_to_open| is not NULL, we will return the matching pointer from the |
| 51 // whitelist. For paranoia, a caller should then use |file_to_open| rather | 94 // whitelist. For paranoia, a caller should then use |file_to_open| rather |
| 52 // than |requested_filename|, so that it never attempts to open an | 95 // than |requested_filename|, so that it never attempts to open an |
| 53 // attacker-controlled file name, even if an attacker managed to fool the | 96 // attacker-controlled file name, even if an attacker managed to fool the |
| 54 // string comparison mechanism. | 97 // string comparison mechanism. |
| 55 // Return true if opening should be allowed, false otherwise. | 98 // Return true if opening should be allowed, false otherwise. |
| 56 // Async signal safe if and only if |file_to_open| is NULL. | 99 // Async signal safe if and only if |file_to_open| is NULL. |
| 57 bool GetFileNameIfAllowedToOpen(const char* requested_filename, | 100 bool GetFileNameIfAllowedToOpen(const char* requested_filename, |
| 58 int requested_flags, | 101 int requested_flags, |
| 59 const char** file_to_open) const; | 102 const char** file_to_open, |
| 103 bool* unlink_after_open) const; | |
| 60 int denied_errno() const { return denied_errno_; } | 104 int denied_errno() const { return denied_errno_; } |
| 61 | 105 |
| 62 private: | 106 private: |
| 63 const int denied_errno_; | 107 const int denied_errno_; |
| 64 const std::vector<std::string> allowed_r_files_; | 108 const std::vector<BrokerPermission> permissions_; |
| 65 const std::vector<std::string> allowed_w_files_; | 109 const BrokerPermission* permissions_array_; |
| 110 const size_t num_of_permissions_; | |
| 111 | |
| 66 DISALLOW_COPY_AND_ASSIGN(BrokerPolicy); | 112 DISALLOW_COPY_AND_ASSIGN(BrokerPolicy); |
| 67 }; | 113 }; |
| 68 | 114 |
| 69 } // namespace syscall_broker | 115 } // namespace syscall_broker |
| 70 | 116 |
| 71 } // namespace sandbox | 117 } // namespace sandbox |
| 72 | 118 |
| 73 #endif // SANDBOX_LINUX_SYSCALL_BROKER_BROKER_POLICY_H_ | 119 #endif // SANDBOX_LINUX_SYSCALL_BROKER_BROKER_POLICY_H_ |
| OLD | NEW |