OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sandbox/linux/syscall_broker/broker_policy.h" | |
6 | |
7 #include <fcntl.h> | |
8 #include <stdint.h> | |
9 #include <string.h> | |
10 | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/logging.h" | |
15 #include "sandbox/linux/syscall_broker/broker_common.h" | |
16 | |
17 namespace sandbox { | |
18 namespace syscall_broker { | |
19 | |
20 namespace { | |
21 | |
22 // We maintain a list of flags that have been reviewed for "sanity" and that | |
23 // we're ok to allow in the broker. | |
24 // I.e. here is where we wouldn't add O_RESET_FILE_SYSTEM. | |
25 bool IsAllowedOpenFlags(int flags) { | |
26 // First, check the access mode. | |
27 const int access_mode = flags & O_ACCMODE; | |
28 if (access_mode != O_RDONLY && access_mode != O_WRONLY && | |
29 access_mode != O_RDWR) { | |
30 return false; | |
31 } | |
32 | |
33 // We only support a 2-parameters open, so we forbid O_CREAT. | |
34 if (flags & O_CREAT) { | |
35 return false; | |
36 } | |
37 | |
38 // Some flags affect the behavior of the current process. We don't support | |
39 // them and don't allow them for now. | |
40 if (flags & kCurrentProcessOpenFlagsMask) | |
41 return false; | |
42 | |
43 // Now check that all the flags are known to us. | |
44 const int creation_and_status_flags = flags & ~O_ACCMODE; | |
45 | |
46 const int known_flags = O_APPEND | O_ASYNC | O_CLOEXEC | O_CREAT | O_DIRECT | | |
47 O_DIRECTORY | O_EXCL | O_LARGEFILE | O_NOATIME | | |
48 O_NOCTTY | O_NOFOLLOW | O_NONBLOCK | O_NDELAY | | |
49 O_SYNC | O_TRUNC; | |
50 | |
51 const int unknown_flags = ~known_flags; | |
52 const bool has_unknown_flags = creation_and_status_flags & unknown_flags; | |
53 return !has_unknown_flags; | |
54 } | |
55 | |
56 // Needs to be async signal safe if |file_to_open| is NULL. | |
57 // TODO(jln): assert signal safety. | |
58 bool GetFileNameInWhitelist(const std::vector<std::string>& allowed_file_names, | |
59 const char* requested_filename, | |
60 const char** file_to_open) { | |
61 if (file_to_open && *file_to_open) { | |
62 // Make sure that callers never pass a non-empty string. In case callers | |
63 // wrongly forget to check the return value and look at the string | |
64 // instead, this could catch bugs. | |
65 RAW_LOG(FATAL, "*file_to_open should be NULL"); | |
66 return false; | |
67 } | |
68 | |
69 // Look for |requested_filename| in |allowed_file_names|. | |
70 // We don't use ::find() because it takes a std::string and | |
71 // the conversion allocates memory. | |
72 std::vector<std::string>::const_iterator it; | |
73 for (it = allowed_file_names.begin(); it != allowed_file_names.end(); it++) { | |
74 if (strcmp(requested_filename, it->c_str()) == 0) { | |
75 if (file_to_open) | |
76 *file_to_open = it->c_str(); | |
77 return true; | |
78 } | |
79 } | |
80 return false; | |
81 } | |
82 | |
83 } // namespace | |
84 | |
85 BrokerPolicy::BrokerPolicy(int denied_errno, | |
86 const std::vector<std::string>& allowed_r_files, | |
87 const std::vector<std::string>& allowed_w_files) | |
88 : denied_errno_(denied_errno), | |
89 allowed_r_files_(allowed_r_files), | |
90 allowed_w_files_(allowed_w_files) { | |
91 } | |
92 | |
93 BrokerPolicy::~BrokerPolicy() { | |
94 } | |
95 | |
96 // Check if calling access() should be allowed on |requested_filename| with | |
97 // mode |requested_mode|. | |
98 // Note: access() being a system call to check permissions, this can get a bit | |
99 // confusing. We're checking if calling access() should even be allowed with | |
100 // the same policy we would use for open(). | |
101 // If |file_to_access| is not NULL, we will return the matching pointer from | |
102 // the whitelist. For paranoia a caller should then use |file_to_access|. See | |
103 // GetFileNameIfAllowedToOpen() fore more explanation. | |
leecam
2014/10/31 21:06:25
for
jln (very slow on Chromium)
2014/10/31 21:43:19
Done.
| |
104 // return true if calling access() on this file should be allowed, false | |
105 // otherwise. | |
106 // Async signal safe if and only if |file_to_access| is NULL. | |
107 bool BrokerPolicy::GetFileNameIfAllowedToAccess( | |
108 const char* requested_filename, | |
109 int requested_mode, | |
110 const char** file_to_access) const { | |
111 // First, check if |requested_mode| is existence, ability to read or ability | |
112 // to write. We do not support X_OK. | |
113 if (requested_mode != F_OK && requested_mode & ~(R_OK | W_OK)) { | |
114 return false; | |
115 } | |
116 switch (requested_mode) { | |
117 case F_OK: | |
118 // We allow to check for file existence if we can either read or write. | |
119 return GetFileNameInWhitelist( | |
120 allowed_r_files_, requested_filename, file_to_access) || | |
121 GetFileNameInWhitelist( | |
122 allowed_w_files_, requested_filename, file_to_access); | |
123 case R_OK: | |
124 return GetFileNameInWhitelist( | |
125 allowed_r_files_, requested_filename, file_to_access); | |
126 case W_OK: | |
127 return GetFileNameInWhitelist( | |
128 allowed_w_files_, requested_filename, file_to_access); | |
129 case R_OK | W_OK: { | |
130 bool allowed_for_read_and_write = | |
131 GetFileNameInWhitelist(allowed_r_files_, requested_filename, NULL) && | |
132 GetFileNameInWhitelist( | |
133 allowed_w_files_, requested_filename, file_to_access); | |
134 return allowed_for_read_and_write; | |
135 } | |
136 default: | |
137 return false; | |
138 } | |
139 } | |
140 | |
141 // Check if |requested_filename| can be opened with flags |requested_flags|. | |
142 // If |file_to_open| is not NULL, we will return the matching pointer from the | |
143 // whitelist. For paranoia, a caller should then use |file_to_open| rather | |
144 // than |requested_filename|, so that it never attempts to open an | |
145 // attacker-controlled file name, even if an attacker managed to fool the | |
146 // string comparison mechanism. | |
147 // Return true if opening should be allowed, false otherwise. | |
148 // Async signal safe if and only if |file_to_open| is NULL. | |
149 bool BrokerPolicy::GetFileNameIfAllowedToOpen(const char* requested_filename, | |
150 int requested_flags, | |
151 const char** file_to_open) const { | |
152 if (!IsAllowedOpenFlags(requested_flags)) { | |
153 return false; | |
154 } | |
155 switch (requested_flags & O_ACCMODE) { | |
156 case O_RDONLY: | |
157 return GetFileNameInWhitelist( | |
158 allowed_r_files_, requested_filename, file_to_open); | |
159 case O_WRONLY: | |
160 return GetFileNameInWhitelist( | |
161 allowed_w_files_, requested_filename, file_to_open); | |
162 case O_RDWR: { | |
163 bool allowed_for_read_and_write = | |
164 GetFileNameInWhitelist(allowed_r_files_, requested_filename, NULL) && | |
165 GetFileNameInWhitelist( | |
166 allowed_w_files_, requested_filename, file_to_open); | |
167 return allowed_for_read_and_write; | |
168 } | |
169 default: | |
170 return false; | |
171 } | |
172 } | |
173 | |
174 } // namespace syscall_broker | |
175 | |
176 } // namespace sandbox | |
OLD | NEW |