Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: mojo/public/cpp/bindings/lib/map_data_internal.h

Issue 2036623002: Validate map keys in C++ Mojo bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/map_serialization.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_MAP_DATA_INTERNAL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
7 7
8 #include "mojo/public/cpp/bindings/lib/array_internal.h" 8 #include "mojo/public/cpp/bindings/lib/array_internal.h"
9 #include "mojo/public/cpp/bindings/lib/validate_params.h" 9 #include "mojo/public/cpp/bindings/lib/validate_params.h"
10 #include "mojo/public/cpp/bindings/lib/validation_errors.h" 10 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
11 #include "mojo/public/cpp/bindings/lib/validation_util.h" 11 #include "mojo/public/cpp/bindings/lib/validation_util.h"
12 12
13 namespace mojo { 13 namespace mojo {
14 namespace internal { 14 namespace internal {
15 15
16 inline const ArrayValidateParams* GetMapKeyValidateParamsDefault() {
17 // The memory allocated here never gets released to not cause an exit time
18 // destructor.
19 static const ArrayValidateParams* validate_params =
20 new ArrayValidateParams(0, false, nullptr);
21 return validate_params;
22 }
23
24 inline const ArrayValidateParams* GetMapKeyValidateParamsForStrings() {
25 // The memory allocated here never gets released to not cause an exit time
26 // destructor.
27 static const ArrayValidateParams* validate_params = new ArrayValidateParams(
28 0, false, new ArrayValidateParams(0, false, nullptr));
29 return validate_params;
30 }
31
32 template <typename MapKey>
33 struct MapKeyValidateParamsFactory {
34 static const ArrayValidateParams* Get() {
35 return GetMapKeyValidateParamsDefault();
36 }
37 };
38
39 // For non-nullable strings only. (Which is OK; map keys can't be null.)
40 template <>
41 struct MapKeyValidateParamsFactory<mojo::internal::Array_Data<char>*> {
42 static const ArrayValidateParams* Get() {
43 return GetMapKeyValidateParamsForStrings();
44 }
45 };
46
47 // Map serializes into a struct which has two arrays as struct fields, the keys 16 // Map serializes into a struct which has two arrays as struct fields, the keys
48 // and the values. 17 // and the values.
49 template <typename Key, typename Value> 18 template <typename Key, typename Value>
50 class Map_Data { 19 class Map_Data {
51 public: 20 public:
52 static Map_Data* New(Buffer* buf) { 21 static Map_Data* New(Buffer* buf) {
53 return new (buf->Allocate(sizeof(Map_Data))) Map_Data(); 22 return new (buf->Allocate(sizeof(Map_Data))) Map_Data();
54 } 23 }
55 24
25 // |validate_params| must have non-null |key_validate_params| and
26 // |element_validate_params| members.
56 static bool Validate(const void* data, 27 static bool Validate(const void* data,
57 BoundsChecker* bounds_checker, 28 BoundsChecker* bounds_checker,
58 const ArrayValidateParams* value_validate_params) { 29 const ArrayValidateParams* validate_params) {
59 if (!data) 30 if (!data)
60 return true; 31 return true;
61 32
62 if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker)) 33 if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker))
63 return false; 34 return false;
64 35
65 const Map_Data* object = static_cast<const Map_Data*>(data); 36 const Map_Data* object = static_cast<const Map_Data*>(data);
66 if (object->header_.num_bytes != sizeof(Map_Data) || 37 if (object->header_.num_bytes != sizeof(Map_Data) ||
67 object->header_.version != 0) { 38 object->header_.version != 0) {
68 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); 39 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
69 return false; 40 return false;
70 } 41 }
71 42
72 if (!ValidateEncodedPointer(&object->keys.offset)) { 43 if (!ValidateEncodedPointer(&object->keys.offset)) {
73 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); 44 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER);
74 return false; 45 return false;
75 } 46 }
76 if (!object->keys.offset) { 47 if (!object->keys.offset) {
77 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 48 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
78 "null key array in map struct"); 49 "null key array in map struct");
79 return false; 50 return false;
80 } 51 }
81 const ArrayValidateParams* key_validate_params =
82 MapKeyValidateParamsFactory<Key>::Get();
83 if (!Array_Data<Key>::Validate(DecodePointerRaw(&object->keys.offset), 52 if (!Array_Data<Key>::Validate(DecodePointerRaw(&object->keys.offset),
84 bounds_checker, key_validate_params)) { 53 bounds_checker,
54 validate_params->key_validate_params)) {
85 return false; 55 return false;
86 } 56 }
87 57
88 if (!ValidateEncodedPointer(&object->values.offset)) { 58 if (!ValidateEncodedPointer(&object->values.offset)) {
89 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); 59 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER);
90 return false; 60 return false;
91 } 61 }
92 if (!object->values.offset) { 62 if (!object->values.offset) {
93 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, 63 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
94 "null value array in map struct"); 64 "null value array in map struct");
95 return false; 65 return false;
96 } 66 }
97 if (!Array_Data<Value>::Validate(DecodePointerRaw(&object->values.offset), 67 if (!Array_Data<Value>::Validate(
98 bounds_checker, value_validate_params)) { 68 DecodePointerRaw(&object->values.offset),
69 bounds_checker, validate_params->element_validate_params)) {
99 return false; 70 return false;
100 } 71 }
101 72
102 const ArrayHeader* key_header = 73 const ArrayHeader* key_header =
103 static_cast<const ArrayHeader*>(DecodePointerRaw(&object->keys.offset)); 74 static_cast<const ArrayHeader*>(DecodePointerRaw(&object->keys.offset));
104 const ArrayHeader* value_header = static_cast<const ArrayHeader*>( 75 const ArrayHeader* value_header = static_cast<const ArrayHeader*>(
105 DecodePointerRaw(&object->values.offset)); 76 DecodePointerRaw(&object->values.offset));
106 if (key_header->num_elements != value_header->num_elements) { 77 if (key_header->num_elements != value_header->num_elements) {
107 ReportValidationError(VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP); 78 ReportValidationError(VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP);
108 return false; 79 return false;
(...skipping 23 matching lines...) Expand all
132 header_.version = 0; 103 header_.version = 0;
133 } 104 }
134 ~Map_Data() = delete; 105 ~Map_Data() = delete;
135 }; 106 };
136 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)"); 107 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)");
137 108
138 } // namespace internal 109 } // namespace internal
139 } // namespace mojo 110 } // namespace mojo
140 111
141 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ 112 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/map_serialization.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698