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

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

Issue 2312813002: Limit Mojo messages recursion depth (Closed)
Patch Set: Address more review comments Created 4 years, 3 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
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_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/compiler_specific.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 14 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
14 15
16 static const int kMaxRecursionDepth = 100;
17
15 namespace mojo { 18 namespace mojo {
16 19
17 class Message; 20 class Message;
18 21
19 namespace internal { 22 namespace internal {
20 23
21 // ValidationContext is used when validating object sizes, pointers and handle 24 // ValidationContext is used when validating object sizes, pointers and handle
22 // indices in the payload of incoming messages. 25 // indices in the payload of incoming messages.
23 class ValidationContext { 26 class ValidationContext {
24 public: 27 public:
25 // [data, data + data_num_bytes) specifies the initial valid memory range. 28 // [data, data + data_num_bytes) specifies the initial valid memory range.
26 // [0, num_handles) specifies the initial valid range of handle indices. 29 // [0, num_handles) specifies the initial valid range of handle indices.
27 // 30 //
28 // If provided, |message| and |description| provide additional information 31 // If provided, |message| and |description| provide additional information
29 // to use when reporting validation errors. In addition if |message| is 32 // to use when reporting validation errors. In addition if |message| is
30 // provided, the MojoNotifyBadMessage API will be used to notify the system of 33 // provided, the MojoNotifyBadMessage API will be used to notify the system of
31 // such errors. 34 // such errors.
32 ValidationContext(const void* data, 35 ValidationContext(const void* data,
33 size_t data_num_bytes, 36 size_t data_num_bytes,
34 size_t num_handles, 37 size_t num_handles,
35 Message* message = nullptr, 38 Message* message = nullptr,
36 const base::StringPiece& description = ""); 39 const base::StringPiece& description = "",
40 int stack_depth = 0);
37 41
38 ~ValidationContext(); 42 ~ValidationContext();
39 43
40 // Claims the specified memory range. 44 // Claims the specified memory range.
41 // The method succeeds if the range is valid to claim. (Please see 45 // The method succeeds if the range is valid to claim. (Please see
42 // the comments for IsValidRange().) 46 // the comments for IsValidRange().)
43 // On success, the valid memory range is shrinked to begin right after the end 47 // On success, the valid memory range is shrinked to begin right after the end
44 // of the claimed range. 48 // of the claimed range.
45 bool ClaimMemory(const void* position, uint32_t num_bytes) { 49 bool ClaimMemory(const void* position, uint32_t num_bytes) {
46 uintptr_t begin = reinterpret_cast<uintptr_t>(position); 50 uintptr_t begin = reinterpret_cast<uintptr_t>(position);
(...skipping 27 matching lines...) Expand all
74 78
75 // Returns true if the specified range is not empty, and the range is 79 // Returns true if the specified range is not empty, and the range is
76 // contained inside the valid memory range. 80 // contained inside the valid memory range.
77 bool IsValidRange(const void* position, uint32_t num_bytes) const { 81 bool IsValidRange(const void* position, uint32_t num_bytes) const {
78 uintptr_t begin = reinterpret_cast<uintptr_t>(position); 82 uintptr_t begin = reinterpret_cast<uintptr_t>(position);
79 uintptr_t end = begin + num_bytes; 83 uintptr_t end = begin + num_bytes;
80 84
81 return InternalIsValidRange(begin, end); 85 return InternalIsValidRange(begin, end);
82 } 86 }
83 87
88 // This object should be created on the stack once every time we recurse down
89 // into a subfield during validation to make sure we don't recurse too deep
90 // and blow the stack.
91 class ScopedDepthTracker {
92 public:
93 // |ctx| must outlive this object.
94 explicit ScopedDepthTracker(ValidationContext* ctx) : ctx_(ctx) {
95 ++ctx_->stack_depth_;
96 }
97
98 ~ScopedDepthTracker() { --ctx_->stack_depth_; }
99
100 private:
101 ValidationContext* ctx_;
102
103 DISALLOW_COPY_AND_ASSIGN(ScopedDepthTracker);
104 };
105
106 // Returns true if the recursion depth limit has been reached.
107 bool ExceedsMaxDepth() WARN_UNUSED_RESULT {
108 return stack_depth_ > kMaxRecursionDepth;
109 }
110
84 Message* message() const { return message_; } 111 Message* message() const { return message_; }
85 const base::StringPiece& description() const { return description_; } 112 const base::StringPiece& description() const { return description_; }
86 113
87 private: 114 private:
88 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const { 115 bool InternalIsValidRange(uintptr_t begin, uintptr_t end) const {
89 return end > begin && begin >= data_begin_ && end <= data_end_; 116 return end > begin && begin >= data_begin_ && end <= data_end_;
90 } 117 }
91 118
92 Message* const message_; 119 Message* const message_;
93 const base::StringPiece description_; 120 const base::StringPiece description_;
94 121
95 // [data_begin_, data_end_) is the valid memory range. 122 // [data_begin_, data_end_) is the valid memory range.
96 uintptr_t data_begin_; 123 uintptr_t data_begin_;
97 uintptr_t data_end_; 124 uintptr_t data_end_;
98 125
99 // [handle_begin_, handle_end_) is the valid handle index range. 126 // [handle_begin_, handle_end_) is the valid handle index range.
100 uint32_t handle_begin_; 127 uint32_t handle_begin_;
101 uint32_t handle_end_; 128 uint32_t handle_end_;
102 129
130 int stack_depth_;
131
103 DISALLOW_COPY_AND_ASSIGN(ValidationContext); 132 DISALLOW_COPY_AND_ASSIGN(ValidationContext);
104 }; 133 };
105 134
106 } // namespace internal 135 } // namespace internal
107 } // namespace mojo 136 } // namespace mojo
108 137
109 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_ 138 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATION_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698