OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_WIN_SRC_POLICY_ENGINE_OPCODES_H_ | 5 #ifndef SANDBOX_WIN_SRC_POLICY_ENGINE_OPCODES_H_ |
6 #define SANDBOX_WIN_SRC_POLICY_ENGINE_OPCODES_H_ | 6 #define SANDBOX_WIN_SRC_POLICY_ENGINE_OPCODES_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include "base/macros.h" |
9 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
10 #include "sandbox/win/src/policy_engine_params.h" | 13 #include "sandbox/win/src/policy_engine_params.h" |
11 | 14 |
12 // The low-level policy is implemented using the concept of policy 'opcodes'. | 15 // The low-level policy is implemented using the concept of policy 'opcodes'. |
13 // An opcode is a structure that contains enough information to perform one | 16 // An opcode is a structure that contains enough information to perform one |
14 // comparison against one single input parameter. For example, an opcode can | 17 // comparison against one single input parameter. For example, an opcode can |
15 // encode just one of the following comparison: | 18 // encode just one of the following comparison: |
16 // | 19 // |
17 // - Is input parameter 3 not equal to NULL? | 20 // - Is input parameter 3 not equal to NULL? |
18 // - Does input parameter 2 start with L"c:\\"? | 21 // - Does input parameter 2 start with L"c:\\"? |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 OP_NUMBER_MATCH, // Match a 32-bit integer as n == a. | 80 OP_NUMBER_MATCH, // Match a 32-bit integer as n == a. |
78 OP_NUMBER_MATCH_RANGE, // Match a 32-bit integer as a <= n <= b. | 81 OP_NUMBER_MATCH_RANGE, // Match a 32-bit integer as a <= n <= b. |
79 OP_NUMBER_AND_MATCH, // Match using bitwise AND; as in: n & a != 0. | 82 OP_NUMBER_AND_MATCH, // Match using bitwise AND; as in: n & a != 0. |
80 OP_WSTRING_MATCH, // Match a string for equality. | 83 OP_WSTRING_MATCH, // Match a string for equality. |
81 OP_ACTION // Evaluates to an action opcode. | 84 OP_ACTION // Evaluates to an action opcode. |
82 }; | 85 }; |
83 | 86 |
84 // Options that apply to every opcode. They are specified when creating | 87 // Options that apply to every opcode. They are specified when creating |
85 // each opcode using OpcodeFactory::MakeOpXXXXX() family of functions | 88 // each opcode using OpcodeFactory::MakeOpXXXXX() family of functions |
86 // Do nothing special. | 89 // Do nothing special. |
87 const uint32 kPolNone = 0; | 90 const uint32_t kPolNone = 0; |
88 | 91 |
89 // Convert EVAL_TRUE into EVAL_FALSE and vice-versa. This allows to express | 92 // Convert EVAL_TRUE into EVAL_FALSE and vice-versa. This allows to express |
90 // negated conditions such as if ( a && !b). | 93 // negated conditions such as if ( a && !b). |
91 const uint32 kPolNegateEval = 1; | 94 const uint32_t kPolNegateEval = 1; |
92 | 95 |
93 // Zero the MatchContext context structure. This happens after the opcode | 96 // Zero the MatchContext context structure. This happens after the opcode |
94 // is evaluated. | 97 // is evaluated. |
95 const uint32 kPolClearContext = 2; | 98 const uint32_t kPolClearContext = 2; |
96 | 99 |
97 // Use OR when evaluating this set of opcodes. The policy evaluator by default | 100 // Use OR when evaluating this set of opcodes. The policy evaluator by default |
98 // uses AND when evaluating. Very helpful when | 101 // uses AND when evaluating. Very helpful when |
99 // used with kPolNegateEval. For example if you have a condition best expressed | 102 // used with kPolNegateEval. For example if you have a condition best expressed |
100 // as if(! (a && b && c)), the use of this flags allows it to be expressed as | 103 // as if(! (a && b && c)), the use of this flags allows it to be expressed as |
101 // if ((!a) || (!b) || (!c)). | 104 // if ((!a) || (!b) || (!c)). |
102 const uint32 kPolUseOREval = 4; | 105 const uint32_t kPolUseOREval = 4; |
103 | 106 |
104 // Keeps the evaluation state between opcode evaluations. This is used | 107 // Keeps the evaluation state between opcode evaluations. This is used |
105 // for string matching where the next opcode needs to continue matching | 108 // for string matching where the next opcode needs to continue matching |
106 // from the last character position from the current opcode. The match | 109 // from the last character position from the current opcode. The match |
107 // context is preserved across opcode evaluation unless an opcode specifies | 110 // context is preserved across opcode evaluation unless an opcode specifies |
108 // as an option kPolClearContext. | 111 // as an option kPolClearContext. |
109 struct MatchContext { | 112 struct MatchContext { |
110 size_t position; | 113 size_t position; |
111 uint32 options; | 114 uint32_t options; |
112 | 115 |
113 MatchContext() { | 116 MatchContext() { |
114 Clear(); | 117 Clear(); |
115 } | 118 } |
116 | 119 |
117 void Clear() { | 120 void Clear() { |
118 position = 0; | 121 position = 0; |
119 options = 0; | 122 options = 0; |
120 } | 123 } |
121 }; | 124 }; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 bool IsAction() const { | 186 bool IsAction() const { |
184 return (OP_ACTION == opcode_id_); | 187 return (OP_ACTION == opcode_id_); |
185 }; | 188 }; |
186 | 189 |
187 // Returns the opcode type. | 190 // Returns the opcode type. |
188 OpcodeID GetID() const { | 191 OpcodeID GetID() const { |
189 return opcode_id_; | 192 return opcode_id_; |
190 } | 193 } |
191 | 194 |
192 // Returns the stored options such as kPolNegateEval and others. | 195 // Returns the stored options such as kPolNegateEval and others. |
193 uint32 GetOptions() const { | 196 uint32_t GetOptions() const { return options_; } |
194 return options_; | |
195 } | |
196 | 197 |
197 // Sets the stored options such as kPolNegateEval. | 198 // Sets the stored options such as kPolNegateEval. |
198 void SetOptions(uint32 options) { | 199 void SetOptions(uint32_t options) { |
199 options_ = base::checked_cast<uint16>(options); | 200 options_ = base::checked_cast<uint16_t>(options); |
200 } | 201 } |
201 | 202 |
202 private: | 203 private: |
203 | 204 |
204 static const size_t kArgumentCount = 4; // The number of supported argument. | 205 static const size_t kArgumentCount = 4; // The number of supported argument. |
205 | 206 |
206 struct OpcodeArgument { | 207 struct OpcodeArgument { |
207 UINT_PTR mem; | 208 UINT_PTR mem; |
208 }; | 209 }; |
209 | 210 |
210 // Better define placement new in the class instead of relying on the | 211 // Better define placement new in the class instead of relying on the |
211 // global definition which seems to be fubared. | 212 // global definition which seems to be fubared. |
212 void* operator new(size_t, void* location) { | 213 void* operator new(size_t, void* location) { |
213 return location; | 214 return location; |
214 } | 215 } |
215 | 216 |
216 // Helper function to evaluate the opcode. The parameters have the same | 217 // Helper function to evaluate the opcode. The parameters have the same |
217 // meaning that in Evaluate(). | 218 // meaning that in Evaluate(). |
218 EvalResult EvaluateHelper(const ParameterSet* parameters, | 219 EvalResult EvaluateHelper(const ParameterSet* parameters, |
219 MatchContext* match); | 220 MatchContext* match); |
220 OpcodeID opcode_id_; | 221 OpcodeID opcode_id_; |
221 int16 parameter_; | 222 int16_t parameter_; |
222 // TODO(cpu): Making |options_| a uint32 would avoid casting, but causes test | 223 // TODO(cpu): Making |options_| a uint32_t would avoid casting, but causes |
223 // failures. Somewhere code is relying on the size of this struct. | 224 // test failures. Somewhere code is relying on the size of this struct. |
224 // http://crbug.com/420296 | 225 // http://crbug.com/420296 |
225 uint16 options_; | 226 uint16_t options_; |
226 OpcodeArgument arguments_[PolicyOpcode::kArgumentCount]; | 227 OpcodeArgument arguments_[PolicyOpcode::kArgumentCount]; |
227 }; | 228 }; |
228 | 229 |
229 enum StringMatchOptions { | 230 enum StringMatchOptions { |
230 CASE_SENSITIVE = 0, // Pay or Not attention to the case as defined by | 231 CASE_SENSITIVE = 0, // Pay or Not attention to the case as defined by |
231 CASE_INSENSITIVE = 1, // RtlCompareUnicodeString windows API. | 232 CASE_INSENSITIVE = 1, // RtlCompareUnicodeString windows API. |
232 EXACT_LENGHT = 2 // Don't do substring match. Do full string match. | 233 EXACT_LENGHT = 2 // Don't do substring match. Do full string match. |
233 }; | 234 }; |
234 | 235 |
235 // Opcodes that do string comparisons take a parameter that is the starting | 236 // Opcodes that do string comparisons take a parameter that is the starting |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 memory_top_ = reinterpret_cast<char*>(&policy->opcodes[0]); | 291 memory_top_ = reinterpret_cast<char*>(&policy->opcodes[0]); |
291 memory_bottom_ = &memory_top_[memory_size]; | 292 memory_bottom_ = &memory_top_[memory_size]; |
292 } | 293 } |
293 | 294 |
294 // Returns the available memory to make opcodes. | 295 // Returns the available memory to make opcodes. |
295 size_t memory_size() const { | 296 size_t memory_size() const { |
296 return memory_bottom_ - memory_top_; | 297 return memory_bottom_ - memory_top_; |
297 } | 298 } |
298 | 299 |
299 // Creates an OpAlwaysFalse opcode. | 300 // Creates an OpAlwaysFalse opcode. |
300 PolicyOpcode* MakeOpAlwaysFalse(uint32 options); | 301 PolicyOpcode* MakeOpAlwaysFalse(uint32_t options); |
301 | 302 |
302 // Creates an OpAlwaysFalse opcode. | 303 // Creates an OpAlwaysFalse opcode. |
303 PolicyOpcode* MakeOpAlwaysTrue(uint32 options); | 304 PolicyOpcode* MakeOpAlwaysTrue(uint32_t options); |
304 | 305 |
305 // Creates an OpAction opcode. | 306 // Creates an OpAction opcode. |
306 // action: The action to return when Evaluate() is called. | 307 // action: The action to return when Evaluate() is called. |
307 PolicyOpcode* MakeOpAction(EvalResult action, uint32 options); | 308 PolicyOpcode* MakeOpAction(EvalResult action, uint32_t options); |
308 | 309 |
309 // Creates an OpNumberMatch opcode. | 310 // Creates an OpNumberMatch opcode. |
310 // selected_param: index of the input argument. It must be a uint32 or the | 311 // selected_param: index of the input argument. It must be a uint32_t or the |
311 // evaluation result will generate a EVAL_ERROR. | 312 // evaluation result will generate a EVAL_ERROR. |
312 // match: the number to compare against the selected_param. | 313 // match: the number to compare against the selected_param. |
313 PolicyOpcode* MakeOpNumberMatch(int16 selected_param, | 314 PolicyOpcode* MakeOpNumberMatch(int16_t selected_param, |
314 uint32 match, | 315 uint32_t match, |
315 uint32 options); | 316 uint32_t options); |
316 | 317 |
317 // Creates an OpNumberMatch opcode (void pointers are cast to numbers). | 318 // Creates an OpNumberMatch opcode (void pointers are cast to numbers). |
318 // selected_param: index of the input argument. It must be an void* or the | 319 // selected_param: index of the input argument. It must be an void* or the |
319 // evaluation result will generate a EVAL_ERROR. | 320 // evaluation result will generate a EVAL_ERROR. |
320 // match: the pointer numeric value to compare against selected_param. | 321 // match: the pointer numeric value to compare against selected_param. |
321 PolicyOpcode* MakeOpVoidPtrMatch(int16 selected_param, | 322 PolicyOpcode* MakeOpVoidPtrMatch(int16_t selected_param, |
322 const void* match, | 323 const void* match, |
323 uint32 options); | 324 uint32_t options); |
324 | 325 |
325 // Creates an OpNumberMatchRange opcode using the memory passed in the ctor. | 326 // Creates an OpNumberMatchRange opcode using the memory passed in the ctor. |
326 // selected_param: index of the input argument. It must be a uint32 or the | 327 // selected_param: index of the input argument. It must be a uint32_t or the |
327 // evaluation result will generate a EVAL_ERROR. | 328 // evaluation result will generate a EVAL_ERROR. |
328 // lower_bound, upper_bound: the range to compare against selected_param. | 329 // lower_bound, upper_bound: the range to compare against selected_param. |
329 PolicyOpcode* MakeOpNumberMatchRange(int16 selected_param, | 330 PolicyOpcode* MakeOpNumberMatchRange(int16_t selected_param, |
330 uint32 lower_bound, | 331 uint32_t lower_bound, |
331 uint32 upper_bound, | 332 uint32_t upper_bound, |
332 uint32 options); | 333 uint32_t options); |
333 | 334 |
334 // Creates an OpWStringMatch opcode using the raw memory passed in the ctor. | 335 // Creates an OpWStringMatch opcode using the raw memory passed in the ctor. |
335 // selected_param: index of the input argument. It must be a wide string | 336 // selected_param: index of the input argument. It must be a wide string |
336 // pointer or the evaluation result will generate a EVAL_ERROR. | 337 // pointer or the evaluation result will generate a EVAL_ERROR. |
337 // match_str: string to compare against selected_param. | 338 // match_str: string to compare against selected_param. |
338 // start_position: when its value is from 0 to < 0x7fff it indicates an | 339 // start_position: when its value is from 0 to < 0x7fff it indicates an |
339 // offset from the selected_param string where to perform the comparison. If | 340 // offset from the selected_param string where to perform the comparison. If |
340 // the value is SeekForward then a substring search is performed. If the | 341 // the value is SeekForward then a substring search is performed. If the |
341 // value is SeekToEnd the comparison is performed against the last part of | 342 // value is SeekToEnd the comparison is performed against the last part of |
342 // the selected_param string. | 343 // the selected_param string. |
343 // Note that the range in the position (0 to 0x7fff) is dictated by the | 344 // Note that the range in the position (0 to 0x7fff) is dictated by the |
344 // current implementation. | 345 // current implementation. |
345 // match_opts: Indicates additional matching flags. Currently CaseInsensitive | 346 // match_opts: Indicates additional matching flags. Currently CaseInsensitive |
346 // is supported. | 347 // is supported. |
347 PolicyOpcode* MakeOpWStringMatch(int16 selected_param, | 348 PolicyOpcode* MakeOpWStringMatch(int16_t selected_param, |
348 const wchar_t* match_str, | 349 const wchar_t* match_str, |
349 int start_position, | 350 int start_position, |
350 StringMatchOptions match_opts, | 351 StringMatchOptions match_opts, |
351 uint32 options); | 352 uint32_t options); |
352 | 353 |
353 // Creates an OpNumberAndMatch opcode using the raw memory passed in the ctor. | 354 // Creates an OpNumberAndMatch opcode using the raw memory passed in the ctor. |
354 // selected_param: index of the input argument. It must be uint32 or the | 355 // selected_param: index of the input argument. It must be uint32_t or the |
355 // evaluation result will generate a EVAL_ERROR. | 356 // evaluation result will generate a EVAL_ERROR. |
356 // match: the value to bitwise AND against selected_param. | 357 // match: the value to bitwise AND against selected_param. |
357 PolicyOpcode* MakeOpNumberAndMatch(int16 selected_param, | 358 PolicyOpcode* MakeOpNumberAndMatch(int16_t selected_param, |
358 uint32 match, | 359 uint32_t match, |
359 uint32 options); | 360 uint32_t options); |
360 | 361 |
361 private: | 362 private: |
362 // Constructs the common part of every opcode. selected_param is the index | 363 // Constructs the common part of every opcode. selected_param is the index |
363 // of the input param to use when evaluating the opcode. Pass -1 in | 364 // of the input param to use when evaluating the opcode. Pass -1 in |
364 // selected_param to indicate that no input parameter is required. | 365 // selected_param to indicate that no input parameter is required. |
365 PolicyOpcode* MakeBase(OpcodeID opcode_id, uint32 options, | 366 PolicyOpcode* MakeBase(OpcodeID opcode_id, |
366 int16 selected_param); | 367 uint32_t options, |
| 368 int16_t selected_param); |
367 | 369 |
368 // Allocates (and copies) a string (of size length) inside the buffer and | 370 // Allocates (and copies) a string (of size length) inside the buffer and |
369 // returns the displacement with respect to start. | 371 // returns the displacement with respect to start. |
370 ptrdiff_t AllocRelative(void* start, const wchar_t* str, size_t lenght); | 372 ptrdiff_t AllocRelative(void* start, const wchar_t* str, size_t lenght); |
371 | 373 |
372 // Points to the lowest currently available address of the memory | 374 // Points to the lowest currently available address of the memory |
373 // used to make the opcodes. This pointer increments as opcodes are made. | 375 // used to make the opcodes. This pointer increments as opcodes are made. |
374 char* memory_top_; | 376 char* memory_top_; |
375 | 377 |
376 // Points to the highest currently available address of the memory | 378 // Points to the highest currently available address of the memory |
377 // used to make the opcodes. This pointer decrements as opcode strings are | 379 // used to make the opcodes. This pointer decrements as opcode strings are |
378 // allocated. | 380 // allocated. |
379 char* memory_bottom_; | 381 char* memory_bottom_; |
380 | 382 |
381 DISALLOW_COPY_AND_ASSIGN(OpcodeFactory); | 383 DISALLOW_COPY_AND_ASSIGN(OpcodeFactory); |
382 }; | 384 }; |
383 | 385 |
384 } // namespace sandbox | 386 } // namespace sandbox |
385 | 387 |
386 #endif // SANDBOX_WIN_SRC_POLICY_ENGINE_OPCODES_H_ | 388 #endif // SANDBOX_WIN_SRC_POLICY_ENGINE_OPCODES_H_ |
OLD | NEW |