OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 11 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| 12 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
| 13 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
11 #include "mojo/public/cpp/bindings/message.h" | 14 #include "mojo/public/cpp/bindings/message.h" |
12 | 15 |
13 namespace mojo { | 16 namespace mojo { |
14 namespace internal { | 17 namespace internal { |
15 | 18 |
16 // Checks whether decoding the pointer will overflow and produce a pointer | 19 // Checks whether decoding the pointer will overflow and produce a pointer |
17 // smaller than |offset|. | 20 // smaller than |offset|. |
18 bool ValidateEncodedPointer(const uint64_t* offset); | 21 bool ValidateEncodedPointer(const uint64_t* offset); |
19 | 22 |
20 // Validates that |data| contains a valid struct header, in terms of alignment | 23 // Validates that |data| contains a valid struct header, in terms of alignment |
(...skipping 19 matching lines...) Expand all Loading... |
40 BoundsChecker bounds_checker(message->payload(), message->payload_num_bytes(), | 43 BoundsChecker bounds_checker(message->payload(), message->payload_num_bytes(), |
41 message->handles()->size()); | 44 message->handles()->size()); |
42 return ParamsType::Validate(message->payload(), &bounds_checker); | 45 return ParamsType::Validate(message->payload(), &bounds_checker); |
43 } | 46 } |
44 | 47 |
45 // The following methods validate control messages defined in | 48 // The following methods validate control messages defined in |
46 // interface_control_messages.mojom. | 49 // interface_control_messages.mojom. |
47 bool ValidateControlRequest(const Message* message); | 50 bool ValidateControlRequest(const Message* message); |
48 bool ValidateControlResponse(const Message* message); | 51 bool ValidateControlResponse(const Message* message); |
49 | 52 |
| 53 // The following Validate.*NonNullable() functions validate that the given |
| 54 // |input| is not null/invalid. |
| 55 template <typename T> |
| 56 bool ValidatePointerNonNullable(const T& input, const char* error_message) { |
| 57 if (input.offset) |
| 58 return true; |
| 59 |
| 60 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 61 error_message); |
| 62 return false; |
| 63 } |
| 64 |
| 65 template <typename T> |
| 66 bool ValidateInlinedUnionNonNullable(const T& input, |
| 67 const char* error_message) { |
| 68 if (!input.is_null()) |
| 69 return true; |
| 70 |
| 71 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 72 error_message); |
| 73 return false; |
| 74 } |
| 75 |
| 76 bool ValidateHandleNonNullable(const Handle& input, const char* error_message); |
| 77 |
| 78 bool ValidateInterfaceIdNonNullable(InterfaceId input, |
| 79 const char* error_message); |
| 80 |
| 81 template <typename T> |
| 82 bool ValidateArray(const ArrayPointer<T>& input, |
| 83 BoundsChecker* bounds_checker, |
| 84 const ArrayValidateParams* validate_params) { |
| 85 if (!ValidateEncodedPointer(&input.offset)) { |
| 86 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); |
| 87 return false; |
| 88 } |
| 89 |
| 90 return Array_Data<T>::Validate(DecodePointerRaw(&input.offset), |
| 91 bounds_checker, validate_params); |
| 92 } |
| 93 |
| 94 template <typename T> |
| 95 bool ValidateMap(const StructPointer<T>& input, |
| 96 BoundsChecker* bounds_checker, |
| 97 const ArrayValidateParams* value_validate_params) { |
| 98 if (!ValidateEncodedPointer(&input.offset)) { |
| 99 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); |
| 100 return false; |
| 101 } |
| 102 |
| 103 return T::Validate(DecodePointerRaw(&input.offset), bounds_checker, |
| 104 value_validate_params); |
| 105 } |
| 106 |
| 107 template <typename T> |
| 108 bool ValidateStruct(const StructPointer<T>& input, |
| 109 BoundsChecker* bounds_checker) { |
| 110 if (!ValidateEncodedPointer(&input.offset)) { |
| 111 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); |
| 112 return false; |
| 113 } |
| 114 |
| 115 return T::Validate(DecodePointerRaw(&input.offset), bounds_checker); |
| 116 } |
| 117 |
| 118 template <typename T> |
| 119 bool ValidateInlinedUnion(const T& input, BoundsChecker* bounds_checker) { |
| 120 return T::Validate(&input, bounds_checker, true); |
| 121 } |
| 122 |
| 123 bool ValidateHandle(const Handle& input, BoundsChecker* bounds_checker); |
| 124 |
| 125 bool ValidateAssociatedInterfaceId(InterfaceId input); |
| 126 |
50 } // namespace internal | 127 } // namespace internal |
51 } // namespace mojo | 128 } // namespace mojo |
52 | 129 |
53 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 130 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
OLD | NEW |