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