OLD | NEW |
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_VALIDATION_CONTEXT_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 | 16 |
17 class Handle; | |
18 class Message; | 17 class Message; |
19 | 18 |
20 namespace internal { | 19 namespace internal { |
21 | 20 |
22 // ValidationContext is used when validating object sizes, pointers and handle | 21 // ValidationContext is used when validating object sizes, pointers and handle |
23 // indices in the payload of incoming messages. | 22 // indices in the payload of incoming messages. |
24 class ValidationContext { | 23 class ValidationContext { |
25 public: | 24 public: |
26 // [data, data + data_num_bytes) specifies the initial valid memory range. | 25 // [data, data + data_num_bytes) specifies the initial valid memory range. |
27 // [0, num_handles) specifies the initial valid range of handle indices. | 26 // [0, num_handles) specifies the initial valid range of handle indices. |
28 // | 27 // |
29 // If provided, |message| and |description| provide additional information | 28 // If provided, |message| and |description| provide additional information |
30 // to use when reporting validation errors. In addition if |message| is | 29 // to use when reporting validation errors. In addition if |message| is |
31 // provided, the MojoNotifyBadMessage API will be used to notify the system of | 30 // provided, the MojoNotifyBadMessage API will be used to notify the system of |
32 // such errors. | 31 // such errors. |
33 ValidationContext(const void* data, | 32 ValidationContext(const void* data, |
34 size_t data_num_bytes, | 33 size_t data_num_bytes, |
35 size_t num_handles, | 34 size_t num_handles, |
36 Message* message = nullptr, | 35 Message* message = nullptr, |
37 const base::StringPiece& description = ""); | 36 const base::StringPiece& description = ""); |
38 | 37 |
39 ~ValidationContext(); | 38 ~ValidationContext(); |
40 | 39 |
41 // Claims the specified memory range. | 40 // Claims the specified memory range. |
42 // The method succeeds if the range is valid to claim. (Please see | 41 // The method succeeds if the range is valid to claim. (Please see |
43 // the comments for IsValidRange().) | 42 // the comments for IsValidRange().) |
44 // On success, the valid memory range is shrinked to begin right after the end | 43 // On success, the valid memory range is shrinked to begin right after the end |
45 // of the claimed range. | 44 // of the claimed range. |
46 bool ClaimMemory(const void* position, uint32_t num_bytes); | 45 bool ClaimMemory(const void* position, uint32_t num_bytes) { |
| 46 uintptr_t begin = reinterpret_cast<uintptr_t>(position); |
| 47 uintptr_t end = begin + num_bytes; |
| 48 |
| 49 if (!InternalIsValidRange(begin, end)) |
| 50 return false; |
| 51 |
| 52 data_begin_ = end; |
| 53 return true; |
| 54 } |
47 | 55 |
48 // Claims the specified encoded handle (which is basically a handle index). | 56 // Claims the specified encoded handle (which is basically a handle index). |
49 // The method succeeds if: | 57 // The method succeeds if: |
50 // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|. | 58 // - |encoded_handle|'s value is |kEncodedInvalidHandleValue|. |
51 // - the handle is contained inside the valid range of handle indices. In this | 59 // - the handle is contained inside the valid range of handle indices. In this |
52 // case, the valid range is shinked to begin right after the claimed handle. | 60 // case, the valid range is shinked to begin right after the claimed handle. |
53 bool ClaimHandle(const Handle_Data& encoded_handle); | 61 bool ClaimHandle(const Handle_Data& encoded_handle) { |
| 62 uint32_t index = encoded_handle.value; |
| 63 if (index == kEncodedInvalidHandleValue) |
| 64 return true; |
| 65 |
| 66 if (index < handle_begin_ || index >= handle_end_) |
| 67 return false; |
| 68 |
| 69 // |index| + 1 shouldn't overflow, because |index| is not the max value of |
| 70 // uint32_t (it is less than |handle_end_|). |
| 71 handle_begin_ = index + 1; |
| 72 return true; |
| 73 } |
54 | 74 |
55 // Returns true if the specified range is not empty, and the range is | 75 // Returns true if the specified range is not empty, and the range is |
56 // contained inside the valid memory range. | 76 // contained inside the valid memory range. |
57 bool IsValidRange(const void* position, uint32_t num_bytes) const; | 77 bool IsValidRange(const void* position, uint32_t num_bytes) const { |
| 78 uintptr_t begin = reinterpret_cast<uintptr_t>(position); |
| 79 uintptr_t end = begin + num_bytes; |
| 80 |
| 81 return InternalIsValidRange(begin, end); |
| 82 } |
58 | 83 |
59 Message* message() const { return message_; } | 84 Message* message() const { return message_; } |
60 const base::StringPiece& description() const { return description_; } | 85 const base::StringPiece& description() const { return description_; } |
61 | 86 |
62 private: | 87 private: |
63 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const; | 88 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const { |
| 89 return end > begin && begin >= data_begin_ && end <= data_end_; |
| 90 } |
64 | 91 |
65 Message* const message_; | 92 Message* const message_; |
66 const base::StringPiece description_; | 93 const base::StringPiece description_; |
67 | 94 |
68 // [data_begin_, data_end_) is the valid memory range. | 95 // [data_begin_, data_end_) is the valid memory range. |
69 uintptr_t data_begin_; | 96 uintptr_t data_begin_; |
70 uintptr_t data_end_; | 97 uintptr_t data_end_; |
71 | 98 |
72 // [handle_begin_, handle_end_) is the valid handle index range. | 99 // [handle_begin_, handle_end_) is the valid handle index range. |
73 uint32_t handle_begin_; | 100 uint32_t handle_begin_; |
74 uint32_t handle_end_; | 101 uint32_t handle_end_; |
75 | 102 |
76 DISALLOW_COPY_AND_ASSIGN(ValidationContext); | 103 DISALLOW_COPY_AND_ASSIGN(ValidationContext); |
77 }; | 104 }; |
78 | 105 |
79 } // namespace internal | 106 } // namespace internal |
80 } // namespace mojo | 107 } // namespace mojo |
81 | 108 |
82 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ | 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ |
OLD | NEW |