| 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/bindings_internal.h" |
| 11 #include "mojo/public/cpp/bindings/lib/serialization_util.h" | 11 #include "mojo/public/cpp/bindings/lib/serialization_util.h" |
| 12 #include "mojo/public/cpp/bindings/lib/validate_params.h" | 12 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
| 13 #include "mojo/public/cpp/bindings/lib/validation_context.h" | 13 #include "mojo/public/cpp/bindings/lib/validation_context.h" |
| 14 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 14 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 15 #include "mojo/public/cpp/bindings/message.h" | 15 #include "mojo/public/cpp/bindings/message.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 namespace internal { | 18 namespace internal { |
| 19 | 19 |
| 20 // Checks whether decoding the pointer will overflow and produce a pointer | 20 // Checks whether decoding the pointer will overflow and produce a pointer |
| 21 // smaller than |offset|. | 21 // smaller than |offset|. |
| 22 bool ValidateEncodedPointer(const uint64_t* offset); | 22 bool ValidateEncodedPointer(const uint64_t* offset); |
| 23 | 23 |
| 24 template <typename T> |
| 25 bool ValidatePointer(const Pointer<T>& input, |
| 26 ValidationContext* validation_context) { |
| 27 bool result = ValidateEncodedPointer(&input.offset); |
| 28 if (!result) |
| 29 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_POINTER); |
| 30 |
| 31 return result; |
| 32 } |
| 33 |
| 24 // Validates that |data| contains a valid struct header, in terms of alignment | 34 // Validates that |data| contains a valid struct header, in terms of alignment |
| 25 // and size (i.e., the |num_bytes| field of the header is sufficient for storing | 35 // and size (i.e., the |num_bytes| field of the header is sufficient for storing |
| 26 // the header itself). Besides, it checks that the memory range | 36 // the header itself). Besides, it checks that the memory range |
| 27 // [data, data + num_bytes) is not marked as occupied by other objects in | 37 // [data, data + num_bytes) is not marked as occupied by other objects in |
| 28 // |validation_context|. On success, the memory range is marked as occupied. | 38 // |validation_context|. On success, the memory range is marked as occupied. |
| 29 // Note: Does not verify |version| or that |num_bytes| is correct for the | 39 // Note: Does not verify |version| or that |num_bytes| is correct for the |
| 30 // claimed version. | 40 // claimed version. |
| 31 bool ValidateStructHeaderAndClaimMemory(const void* data, | 41 bool ValidateStructHeaderAndClaimMemory(const void* data, |
| 32 ValidationContext* validation_context); | 42 ValidationContext* validation_context); |
| 33 | 43 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 bool ValidateHandleOrInterfaceNonNullable( | 121 bool ValidateHandleOrInterfaceNonNullable( |
| 112 const Interface_Data& input, | 122 const Interface_Data& input, |
| 113 const char* error_message, | 123 const char* error_message, |
| 114 ValidationContext* validation_context); | 124 ValidationContext* validation_context); |
| 115 bool ValidateHandleOrInterfaceNonNullable( | 125 bool ValidateHandleOrInterfaceNonNullable( |
| 116 const Handle_Data& input, | 126 const Handle_Data& input, |
| 117 const char* error_message, | 127 const char* error_message, |
| 118 ValidationContext* validation_context); | 128 ValidationContext* validation_context); |
| 119 | 129 |
| 120 template <typename T> | 130 template <typename T> |
| 121 bool ValidateArray(const Pointer<Array_Data<T>>& input, | 131 bool ValidateContainer(const Pointer<T>& input, |
| 122 ValidationContext* validation_context, | 132 ValidationContext* validation_context, |
| 123 const ContainerValidateParams* validate_params) { | 133 const ContainerValidateParams* validate_params) { |
| 124 if (!ValidateEncodedPointer(&input.offset)) { | 134 return ValidatePointer(input, validation_context) && |
| 125 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_POINTER); | 135 T::Validate(input.Get(), validation_context, validate_params); |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 return Array_Data<T>::Validate(DecodePointerRaw(&input.offset), | |
| 130 validation_context, validate_params); | |
| 131 } | |
| 132 | |
| 133 template <typename T> | |
| 134 bool ValidateMap(const Pointer<T>& input, | |
| 135 ValidationContext* validation_context, | |
| 136 const ContainerValidateParams* validate_params) { | |
| 137 if (!ValidateEncodedPointer(&input.offset)) { | |
| 138 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_POINTER); | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 142 return T::Validate(DecodePointerRaw(&input.offset), validation_context, | |
| 143 validate_params); | |
| 144 } | 136 } |
| 145 | 137 |
| 146 template <typename T> | 138 template <typename T> |
| 147 bool ValidateStruct(const Pointer<T>& input, | 139 bool ValidateStruct(const Pointer<T>& input, |
| 148 ValidationContext* validation_context) { | 140 ValidationContext* validation_context) { |
| 149 if (!ValidateEncodedPointer(&input.offset)) { | 141 return ValidatePointer(input, validation_context) && |
| 150 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_POINTER); | 142 T::Validate(input.Get(), validation_context); |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 return T::Validate(DecodePointerRaw(&input.offset), validation_context); | |
| 155 } | 143 } |
| 156 | 144 |
| 157 template <typename T> | 145 template <typename T> |
| 158 bool ValidateInlinedUnion(const T& input, | 146 bool ValidateInlinedUnion(const T& input, |
| 159 ValidationContext* validation_context) { | 147 ValidationContext* validation_context) { |
| 160 return T::Validate(&input, validation_context, true); | 148 return T::Validate(&input, validation_context, true); |
| 161 } | 149 } |
| 162 | 150 |
| 163 template <typename T> | 151 template <typename T> |
| 164 bool ValidateNonInlinedUnion(const Pointer<T>& input, | 152 bool ValidateNonInlinedUnion(const Pointer<T>& input, |
| 165 ValidationContext* validation_context) { | 153 ValidationContext* validation_context) { |
| 166 if (!ValidateEncodedPointer(&input.offset)) { | 154 return ValidatePointer(input, validation_context) && |
| 167 ReportValidationError(validation_context, VALIDATION_ERROR_ILLEGAL_POINTER); | 155 T::Validate(input.Get(), validation_context, false); |
| 168 return false; | |
| 169 } | |
| 170 | |
| 171 return T::Validate(DecodePointerRaw(&input.offset), validation_context, | |
| 172 false); | |
| 173 } | 156 } |
| 174 | 157 |
| 175 bool ValidateHandleOrInterface(const AssociatedInterface_Data& input, | 158 bool ValidateHandleOrInterface(const AssociatedInterface_Data& input, |
| 176 ValidationContext* validation_context); | 159 ValidationContext* validation_context); |
| 177 bool ValidateHandleOrInterface(const AssociatedInterfaceRequest_Data& input, | 160 bool ValidateHandleOrInterface(const AssociatedInterfaceRequest_Data& input, |
| 178 ValidationContext* validation_context); | 161 ValidationContext* validation_context); |
| 179 bool ValidateHandleOrInterface(const Interface_Data& input, | 162 bool ValidateHandleOrInterface(const Interface_Data& input, |
| 180 ValidationContext* validation_context); | 163 ValidationContext* validation_context); |
| 181 bool ValidateHandleOrInterface(const Handle_Data& input, | 164 bool ValidateHandleOrInterface(const Handle_Data& input, |
| 182 ValidationContext* validation_context); | 165 ValidationContext* validation_context); |
| 183 | 166 |
| 184 } // namespace internal | 167 } // namespace internal |
| 185 } // namespace mojo | 168 } // namespace mojo |
| 186 | 169 |
| 187 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 170 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
| OLD | NEW |