| 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/bounds_checker.h" | 10 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| 11 #include "mojo/public/cpp/bindings/message.h" | |
| 12 | 11 |
| 13 namespace mojo { | 12 namespace mojo { |
| 14 namespace internal { | 13 namespace internal { |
| 15 | 14 |
| 16 // Checks whether decoding the pointer will overflow and produce a pointer | 15 // Checks whether decoding the pointer will overflow and produce a pointer |
| 17 // smaller than |offset|. | 16 // smaller than |offset|. |
| 18 bool ValidateEncodedPointer(const uint64_t* offset); | 17 bool ValidateEncodedPointer(const uint64_t* offset); |
| 19 | 18 |
| 20 // Validates that |data| contains a valid struct header, in terms of alignment | 19 // Validates that |data| contains a valid struct header, in terms of alignment |
| 21 // and size (i.e., the |num_bytes| field of the header is sufficient for storing | 20 // and size (i.e., the |num_bytes| field of the header is sufficient for storing |
| 22 // the header itself). Besides, it checks that the memory range | 21 // the header itself). Besides, it checks that the memory range |
| 23 // [data, data + num_bytes) is not marked as occupied by other objects in | 22 // [data, data + num_bytes) is not marked as occupied by other objects in |
| 24 // |bounds_checker|. On success, the memory range is marked as occupied. | 23 // |bounds_checker|. On success, the memory range is marked as occupied. |
| 25 // Note: Does not verify |version| or that |num_bytes| is correct for the | 24 // Note: Does not verify |version| or that |num_bytes| is correct for the |
| 26 // claimed version. | 25 // claimed version. |
| 27 bool ValidateStructHeaderAndClaimMemory(const void* data, | 26 bool ValidateStructHeaderAndClaimMemory(const void* data, |
| 28 BoundsChecker* bounds_checker); | 27 BoundsChecker* bounds_checker); |
| 29 | 28 |
| 30 // Validates that the message is a request which doesn't expect a response. | |
| 31 bool ValidateMessageIsRequestWithoutResponse(const Message* message); | |
| 32 // Validates that the message is a request expecting a response. | |
| 33 bool ValidateMessageIsRequestExpectingResponse(const Message* message); | |
| 34 // Validates that the message is a response. | |
| 35 bool ValidateMessageIsResponse(const Message* message); | |
| 36 | |
| 37 // Validates that the message payload is a valid struct of type ParamsType. | |
| 38 template <typename ParamsType> | |
| 39 bool ValidateMessagePayload(const Message* message) { | |
| 40 BoundsChecker bounds_checker(message->payload(), message->payload_num_bytes(), | |
| 41 message->handles()->size()); | |
| 42 return ParamsType::Validate(message->payload(), &bounds_checker); | |
| 43 } | |
| 44 | |
| 45 } // namespace internal | 29 } // namespace internal |
| 46 } // namespace mojo | 30 } // namespace mojo |
| 47 | 31 |
| 48 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ | 32 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_UTIL_H_ |
| OLD | NEW |