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 SANDBOX_LINUX_BPF_DSL_BPF_DSL_H_ | 5 #ifndef SANDBOX_LINUX_BPF_DSL_BPF_DSL_H_ |
6 #define SANDBOX_LINUX_BPF_DSL_BPF_DSL_H_ | 6 #define SANDBOX_LINUX_BPF_DSL_BPF_DSL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | |
11 #include <utility> | 10 #include <utility> |
12 | 11 |
13 #include "base/macros.h" | 12 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
15 #include "sandbox/linux/bpf_dsl/cons.h" | 14 #include "sandbox/linux/bpf_dsl/cons.h" |
16 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" | 15 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" |
17 #include "sandbox/linux/seccomp-bpf/trap.h" | 16 #include "sandbox/linux/seccomp-bpf/trap.h" |
18 #include "sandbox/sandbox_export.h" | 17 #include "sandbox/sandbox_export.h" |
19 | 18 |
20 namespace sandbox { | 19 namespace sandbox { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // } | 54 // } |
56 // | 55 // |
57 // private: | 56 // private: |
58 // DISALLOW_COPY_AND_ASSIGN(SillyPolicy); | 57 // DISALLOW_COPY_AND_ASSIGN(SillyPolicy); |
59 // }; | 58 // }; |
60 // | 59 // |
61 // More generally, the DSL currently supports the following grammar: | 60 // More generally, the DSL currently supports the following grammar: |
62 // | 61 // |
63 // result = Allow() | Error(errno) | Trap(trap_func, arg) | 62 // result = Allow() | Error(errno) | Trap(trap_func, arg) |
64 // | If(bool, result)[.ElseIf(bool, result)].Else(result) | 63 // | If(bool, result)[.ElseIf(bool, result)].Else(result) |
65 // bool = arg == val | (arg & mask) == mask | (arg & mask) == 0 | 64 // bool = arg == val | (arg & mask) == val |
66 // | !bool | bool && bool | bool || bool | 65 // | !bool | bool && bool | bool || bool |
67 // | 66 // |
68 // The semantics of each function and operator are intended to be | 67 // The semantics of each function and operator are intended to be |
69 // intuitive, but are described in more detail below. | 68 // intuitive, but are described in more detail below. |
70 // | 69 // |
71 // (Credit to Sean Parent's "Inheritance is the Base Class of Evil" | 70 // (Credit to Sean Parent's "Inheritance is the Base Class of Evil" |
72 // talk at Going Native 2013 for promoting value semantics via shared | 71 // talk at Going Native 2013 for promoting value semantics via shared |
73 // pointers to immutable state.) | 72 // pointers to immutable state.) |
74 | 73 |
75 namespace sandbox { | 74 namespace sandbox { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // Trap specifies a result that the system call should be handled by | 127 // Trap specifies a result that the system call should be handled by |
129 // trapping back into userspace and invoking |trap_func|, passing | 128 // trapping back into userspace and invoking |trap_func|, passing |
130 // |aux| as the second parameter. | 129 // |aux| as the second parameter. |
131 SANDBOX_EXPORT ResultExpr Trap(Trap::TrapFnc trap_func, void* aux); | 130 SANDBOX_EXPORT ResultExpr Trap(Trap::TrapFnc trap_func, void* aux); |
132 | 131 |
133 template <typename T> | 132 template <typename T> |
134 class SANDBOX_EXPORT Arg { | 133 class SANDBOX_EXPORT Arg { |
135 public: | 134 public: |
136 // Initializes the Arg to represent the |num|th system call | 135 // Initializes the Arg to represent the |num|th system call |
137 // argument (indexed from 0), which is of type |T|. | 136 // argument (indexed from 0), which is of type |T|. |
138 explicit Arg(int num) | 137 explicit Arg(int num); |
139 : num_(num), mask_(std::numeric_limits<uint64_t>::max()) {} | |
140 | 138 |
141 Arg(const Arg& arg) : num_(arg.num_), mask_(arg.mask_) {} | 139 Arg(const Arg& arg) : num_(arg.num_), mask_(arg.mask_) {} |
142 | 140 |
143 // Returns an Arg representing the current argument, but after | 141 // Returns an Arg representing the current argument, but after |
144 // bitwise-and'ing it with |rhs|. | 142 // bitwise-and'ing it with |rhs|. |
145 friend Arg operator&(const Arg& lhs, uint64_t rhs) { | 143 friend Arg operator&(const Arg& lhs, uint64_t rhs) { |
146 return Arg(lhs.num_, lhs.mask_ & rhs); | 144 return Arg(lhs.num_, lhs.mask_ & rhs); |
147 } | 145 } |
148 | 146 |
149 // Returns a boolean expression comparing whether the system call | 147 // Returns a boolean expression comparing whether the system call |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // templates. | 202 // templates. |
205 namespace internal { | 203 namespace internal { |
206 | 204 |
207 // Returns a boolean expression that represents whether system call | 205 // Returns a boolean expression that represents whether system call |
208 // argument |num| of size |size| is equal to |val|, when masked | 206 // argument |num| of size |size| is equal to |val|, when masked |
209 // according to |mask|. Users should use the Arg template class below | 207 // according to |mask|. Users should use the Arg template class below |
210 // instead of using this API directly. | 208 // instead of using this API directly. |
211 SANDBOX_EXPORT BoolExpr | 209 SANDBOX_EXPORT BoolExpr |
212 ArgEq(int num, size_t size, uint64_t mask, uint64_t val); | 210 ArgEq(int num, size_t size, uint64_t mask, uint64_t val); |
213 | 211 |
| 212 // Returns the default mask for a system call argument of the specified size. |
| 213 SANDBOX_EXPORT uint64_t DefaultMask(size_t size); |
| 214 |
214 // Internal interface implemented by BoolExpr implementations. | 215 // Internal interface implemented by BoolExpr implementations. |
215 class SANDBOX_EXPORT BoolExprImpl : public base::RefCounted<BoolExprImpl> { | 216 class SANDBOX_EXPORT BoolExprImpl : public base::RefCounted<BoolExprImpl> { |
216 public: | 217 public: |
217 BoolExprImpl() {} | 218 BoolExprImpl() {} |
218 virtual ErrorCode Compile(SandboxBPF* sb, | 219 virtual ErrorCode Compile(SandboxBPF* sb, |
219 ErrorCode true_ec, | 220 ErrorCode true_ec, |
220 ErrorCode false_ec) const = 0; | 221 ErrorCode false_ec) const = 0; |
221 | 222 |
222 protected: | 223 protected: |
223 virtual ~BoolExprImpl() {} | 224 virtual ~BoolExprImpl() {} |
(...skipping 12 matching lines...) Expand all Loading... |
236 protected: | 237 protected: |
237 virtual ~ResultExprImpl() {} | 238 virtual ~ResultExprImpl() {} |
238 | 239 |
239 private: | 240 private: |
240 friend class base::RefCounted<ResultExprImpl>; | 241 friend class base::RefCounted<ResultExprImpl>; |
241 DISALLOW_COPY_AND_ASSIGN(ResultExprImpl); | 242 DISALLOW_COPY_AND_ASSIGN(ResultExprImpl); |
242 }; | 243 }; |
243 | 244 |
244 } // namespace internal | 245 } // namespace internal |
245 | 246 |
| 247 template <typename T> |
| 248 Arg<T>::Arg(int num) |
| 249 : num_(num), mask_(internal::DefaultMask(sizeof(T))) { |
| 250 } |
| 251 |
246 // Definition requires ArgEq to have been declared. Moved out-of-line | 252 // Definition requires ArgEq to have been declared. Moved out-of-line |
247 // to minimize how much internal clutter users have to ignore while | 253 // to minimize how much internal clutter users have to ignore while |
248 // reading the header documentation. | 254 // reading the header documentation. |
249 // | 255 // |
250 // Additionally, we use this helper member function to avoid linker errors | 256 // Additionally, we use this helper member function to avoid linker errors |
251 // caused by defining operator== out-of-line. For a more detailed explanation, | 257 // caused by defining operator== out-of-line. For a more detailed explanation, |
252 // see http://www.parashift.com/c++-faq-lite/template-friends.html. | 258 // see http://www.parashift.com/c++-faq-lite/template-friends.html. |
253 template <typename T> | 259 template <typename T> |
254 BoolExpr Arg<T>::EqualTo(T val) const { | 260 BoolExpr Arg<T>::EqualTo(T val) const { |
255 return internal::ArgEq(num_, sizeof(T), mask_, static_cast<uint64_t>(val)); | 261 return internal::ArgEq(num_, sizeof(T), mask_, static_cast<uint64_t>(val)); |
256 } | 262 } |
257 | 263 |
258 } // namespace bpf_dsl | 264 } // namespace bpf_dsl |
259 } // namespace sandbox | 265 } // namespace sandbox |
260 | 266 |
261 #endif // SANDBOX_LINUX_BPF_DSL_BPF_DSL_H_ | 267 #endif // SANDBOX_LINUX_BPF_DSL_BPF_DSL_H_ |
OLD | NEW |