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 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" | 5 #include "mojo/public/cpp/bindings/lib/validation_context.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.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/message.h" |
12 #include "mojo/public/cpp/system/handle.h" | 13 #include "mojo/public/cpp/system/handle.h" |
13 | 14 |
14 namespace mojo { | 15 namespace mojo { |
15 namespace internal { | 16 namespace internal { |
16 | 17 |
17 BoundsChecker::BoundsChecker(const void* data, | 18 ValidationContext::ValidationContext(const void* data, |
18 uint32_t data_num_bytes, | 19 uint32_t data_num_bytes, |
19 size_t num_handles) | 20 size_t num_handles, |
20 : data_begin_(reinterpret_cast<uintptr_t>(data)), | 21 Message* message, |
| 22 const base::StringPiece& description) |
| 23 : message_(message), |
| 24 description_(description), |
| 25 data_begin_(reinterpret_cast<uintptr_t>(data)), |
21 data_end_(data_begin_ + data_num_bytes), | 26 data_end_(data_begin_ + data_num_bytes), |
22 handle_begin_(0), | 27 handle_begin_(0), |
23 handle_end_(static_cast<uint32_t>(num_handles)) { | 28 handle_end_(static_cast<uint32_t>(num_handles)) { |
24 if (data_end_ < data_begin_) { | 29 if (data_end_ < data_begin_) { |
25 // The calculation of |data_end_| overflowed. | 30 // The calculation of |data_end_| overflowed. |
26 // It shouldn't happen but if it does, set the range to empty so | 31 // It shouldn't happen but if it does, set the range to empty so |
27 // IsValidRange() and ClaimMemory() always fail. | 32 // IsValidRange() and ClaimMemory() always fail. |
28 NOTREACHED(); | 33 NOTREACHED(); |
29 data_end_ = data_begin_; | 34 data_end_ = data_begin_; |
30 } | 35 } |
31 if (handle_end_ < num_handles) { | 36 if (handle_end_ < num_handles) { |
32 // Assigning |num_handles| to |handle_end_| overflowed. | 37 // Assigning |num_handles| to |handle_end_| overflowed. |
33 // It shouldn't happen but if it does, set the handle index range to empty. | 38 // It shouldn't happen but if it does, set the handle index range to empty. |
34 NOTREACHED(); | 39 NOTREACHED(); |
35 handle_end_ = 0; | 40 handle_end_ = 0; |
36 } | 41 } |
37 } | 42 } |
38 | 43 |
39 BoundsChecker::~BoundsChecker() { | 44 ValidationContext::~ValidationContext() { |
40 } | 45 } |
41 | 46 |
42 bool BoundsChecker::ClaimMemory(const void* position, uint32_t num_bytes) { | 47 bool ValidationContext::ClaimMemory(const void* position, uint32_t num_bytes) { |
43 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | 48 uintptr_t begin = reinterpret_cast<uintptr_t>(position); |
44 uintptr_t end = begin + num_bytes; | 49 uintptr_t end = begin + num_bytes; |
45 | 50 |
46 if (!InternalIsValidRange(begin, end)) | 51 if (!InternalIsValidRange(begin, end)) |
47 return false; | 52 return false; |
48 | 53 |
49 data_begin_ = end; | 54 data_begin_ = end; |
50 return true; | 55 return true; |
51 } | 56 } |
52 | 57 |
53 bool BoundsChecker::ClaimHandle(const Handle_Data& encoded_handle) { | 58 bool ValidationContext::ClaimHandle(const Handle_Data& encoded_handle) { |
54 uint32_t index = encoded_handle.value; | 59 uint32_t index = encoded_handle.value; |
55 if (index == kEncodedInvalidHandleValue) | 60 if (index == kEncodedInvalidHandleValue) |
56 return true; | 61 return true; |
57 | 62 |
58 if (index < handle_begin_ || index >= handle_end_) | 63 if (index < handle_begin_ || index >= handle_end_) |
59 return false; | 64 return false; |
60 | 65 |
61 // |index| + 1 shouldn't overflow, because |index| is not the max value of | 66 // |index| + 1 shouldn't overflow, because |index| is not the max value of |
62 // uint32_t (it is less than |handle_end_|). | 67 // uint32_t (it is less than |handle_end_|). |
63 handle_begin_ = index + 1; | 68 handle_begin_ = index + 1; |
64 return true; | 69 return true; |
65 } | 70 } |
66 | 71 |
67 bool BoundsChecker::IsValidRange(const void* position, | 72 bool ValidationContext::IsValidRange(const void* position, |
68 uint32_t num_bytes) const { | 73 uint32_t num_bytes) const { |
69 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | 74 uintptr_t begin = reinterpret_cast<uintptr_t>(position); |
70 uintptr_t end = begin + num_bytes; | 75 uintptr_t end = begin + num_bytes; |
71 | 76 |
72 return InternalIsValidRange(begin, end); | 77 return InternalIsValidRange(begin, end); |
73 } | 78 } |
74 | 79 |
75 bool BoundsChecker::InternalIsValidRange(uintptr_t begin, uintptr_t end) const { | 80 bool ValidationContext::InternalIsValidRange(uintptr_t begin, |
| 81 uintptr_t end) const { |
76 return end > begin && begin >= data_begin_ && end <= data_end_; | 82 return end > begin && begin >= data_begin_ && end <= data_end_; |
77 } | 83 } |
78 | 84 |
79 } // namespace internal | 85 } // namespace internal |
80 } // namespace mojo | 86 } // namespace mojo |
OLD | NEW |