| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/public/cpp/bindings/lib/validation_util.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 | |
| 9 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | |
| 10 #include "mojo/public/cpp/bindings/lib/message_internal.h" | |
| 11 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
| 12 #include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace internal { | |
| 16 | |
| 17 bool ValidateEncodedPointer(const uint64_t* offset) { | |
| 18 // - Make sure |*offset| is no more than 32-bits. | |
| 19 // - Cast |offset| to uintptr_t so overflow behavior is well defined across | |
| 20 // 32-bit and 64-bit systems. | |
| 21 return *offset <= std::numeric_limits<uint32_t>::max() && | |
| 22 (reinterpret_cast<uintptr_t>(offset) + | |
| 23 static_cast<uint32_t>(*offset) >= | |
| 24 reinterpret_cast<uintptr_t>(offset)); | |
| 25 } | |
| 26 | |
| 27 bool ValidateStructHeaderAndClaimMemory(const void* data, | |
| 28 BoundsChecker* bounds_checker) { | |
| 29 if (!IsAligned(data)) { | |
| 30 ReportValidationError(VALIDATION_ERROR_MISALIGNED_OBJECT); | |
| 31 return false; | |
| 32 } | |
| 33 if (!bounds_checker->IsValidRange(data, sizeof(StructHeader))) { | |
| 34 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); | |
| 35 return false; | |
| 36 } | |
| 37 | |
| 38 const StructHeader* header = static_cast<const StructHeader*>(data); | |
| 39 | |
| 40 if (header->num_bytes < sizeof(StructHeader)) { | |
| 41 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 if (!bounds_checker->ClaimMemory(data, header->num_bytes)) { | |
| 46 ReportValidationError(VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 bool ValidateMessageIsRequestWithoutResponse(const Message* message) { | |
| 54 if (message->has_flag(kMessageIsResponse) || | |
| 55 message->has_flag(kMessageExpectsResponse)) { | |
| 56 ReportValidationError(VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS); | |
| 57 return false; | |
| 58 } | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 bool ValidateMessageIsRequestExpectingResponse(const Message* message) { | |
| 63 if (message->has_flag(kMessageIsResponse) || | |
| 64 !message->has_flag(kMessageExpectsResponse)) { | |
| 65 ReportValidationError(VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS); | |
| 66 return false; | |
| 67 } | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 bool ValidateMessageIsResponse(const Message* message) { | |
| 72 if (message->has_flag(kMessageExpectsResponse) || | |
| 73 !message->has_flag(kMessageIsResponse)) { | |
| 74 ReportValidationError(VALIDATION_ERROR_MESSAGE_HEADER_INVALID_FLAGS); | |
| 75 return false; | |
| 76 } | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 bool ValidateControlRequest(const Message* message) { | |
| 81 switch (message->header()->name) { | |
| 82 case kRunMessageId: | |
| 83 return ValidateMessageIsRequestExpectingResponse(message) && | |
| 84 ValidateMessagePayload<RunMessageParams_Data>(message); | |
| 85 case kRunOrClosePipeMessageId: | |
| 86 return ValidateMessageIsRequestWithoutResponse(message) && | |
| 87 ValidateMessagePayload<RunOrClosePipeMessageParams_Data>(message); | |
| 88 } | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 bool ValidateControlResponse(const Message* message) { | |
| 93 if (!ValidateMessageIsResponse(message)) | |
| 94 return false; | |
| 95 switch (message->header()->name) { | |
| 96 case kRunMessageId: | |
| 97 return ValidateMessagePayload<RunResponseMessageParams_Data>(message); | |
| 98 } | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 } // namespace internal | |
| 103 } // namespace mojo | |
| OLD | NEW |