OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_HELPERS_BPF_DSL_H_ |
| 6 #define SANDBOX_LINUX_SECCOMP_BPF_HELPERS_BPF_DSL_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <utility> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "sandbox/linux/seccomp-bpf-helpers/cons.h" |
| 15 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" |
| 16 #include "sandbox/linux/seccomp-bpf/trap.h" |
| 17 #include "sandbox/sandbox_export.h" |
| 18 |
| 19 namespace sandbox { |
| 20 class ErrorCode; |
| 21 class SandboxBPF; |
| 22 } |
| 23 |
| 24 // The sandbox::bpf_dsl namespace provides a domain-specific language |
| 25 // to make writing BPF policies more expressive. In general, the |
| 26 // object types all have value semantics (i.e., they can be copied |
| 27 // around, returned from or passed to function calls, etc. without any |
| 28 // surprising side effects), though not all support assignment. |
| 29 // |
| 30 // An idiomatic and demonstrative (albeit silly) example of this API |
| 31 // would be: |
| 32 // |
| 33 // #include "sandbox/linux/seccomp-bpf-helpers/bpf_dsl.h" |
| 34 // |
| 35 // using namespace sandbox::bpf_dsl; |
| 36 // |
| 37 // class SillyPolicy : public SandboxBPFDSLPolicy { |
| 38 // public: |
| 39 // SillyPolicy() {} |
| 40 // virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE { |
| 41 // if (sysno == __NR_fcntl) { |
| 42 // Arg<int> fd(0), cmd(1); |
| 43 // Arg<unsigned long> flags(2); |
| 44 // const unsigned long kBadFlags = ~(O_ACCMODE | O_NONBLOCK); |
| 45 // return If(fd == 0 && cmd == F_SETFL && (flags & kBadFlags) == 0, |
| 46 // Allow()) |
| 47 // .ElseIf(cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC, |
| 48 // Error(EMFILE)) |
| 49 // .Else(Trap(SetFlagHandler, NULL)); |
| 50 // } else { |
| 51 // return Allow(); |
| 52 // } |
| 53 // } |
| 54 // |
| 55 // private: |
| 56 // DISALLOW_COPY_AND_ASSIGN(SillyPolicy); |
| 57 // }; |
| 58 // |
| 59 // More generally, the DSL currently supports the following grammar: |
| 60 // |
| 61 // result = Allow() | Error(errno) | Trap(trap_func, arg) |
| 62 // | If(bool, result)[.ElseIf(bool, result)].Else(result) |
| 63 // bool = arg == val | (arg & mask) == mask | (arg & mask) == 0 |
| 64 // | !bool | bool && bool | bool || bool |
| 65 // |
| 66 // The semantics of each function and operator are intended to be |
| 67 // intuitive, but are described in more detail below. |
| 68 // |
| 69 // (Credit to Sean Parent's "Inheritance is the Base Class of Evil" |
| 70 // talk at Going Native 2013 for promoting value semantics via shared |
| 71 // pointers to immutable state.) |
| 72 |
| 73 namespace sandbox { |
| 74 namespace bpf_dsl { |
| 75 |
| 76 // Forward declarations of classes; see below for proper documentation. |
| 77 class Elser; |
| 78 namespace internal { |
| 79 class ResultExprImpl; |
| 80 class BoolExprImpl; |
| 81 } |
| 82 |
| 83 // ResultExpr is an opaque reference to an immutable result expression tree. |
| 84 typedef scoped_refptr<const internal::ResultExprImpl> ResultExpr; |
| 85 |
| 86 // BoolExpr is an opaque reference to an immutable boolean expression tree. |
| 87 typedef scoped_refptr<const internal::BoolExprImpl> BoolExpr; |
| 88 |
| 89 // Helper class to make writing policies easier. |
| 90 class SANDBOX_EXPORT SandboxBPFDSLPolicy : public SandboxBPFPolicy { |
| 91 public: |
| 92 SandboxBPFDSLPolicy() : SandboxBPFPolicy() {} |
| 93 virtual ~SandboxBPFDSLPolicy() {} |
| 94 |
| 95 // User extension point for writing custom sandbox policies. |
| 96 virtual ResultExpr EvaluateSyscall(int sysno) const = 0; |
| 97 |
| 98 // Optional overload for specifying alternate behavior for invalid |
| 99 // system calls. The default is to return ENOSYS. |
| 100 virtual ResultExpr InvalidSyscall() const; |
| 101 |
| 102 // Override implementations from SandboxBPFPolicy. Marked as FINAL |
| 103 // to prevent mixups with child classes accidentally overloading |
| 104 // these instead of the above methods. |
| 105 virtual ErrorCode EvaluateSyscall(SandboxBPF* sb, |
| 106 int sysno) const OVERRIDE FINAL; |
| 107 virtual ErrorCode InvalidSyscall(SandboxBPF* sb) const OVERRIDE FINAL; |
| 108 |
| 109 // Helper method so policies can just write Trap(func, aux). |
| 110 static ResultExpr Trap(::sandbox::Trap::TrapFnc trap_func, void* aux); |
| 111 |
| 112 private: |
| 113 DISALLOW_COPY_AND_ASSIGN(SandboxBPFDSLPolicy); |
| 114 }; |
| 115 |
| 116 // Allow specifies a result that the system call should be allowed to |
| 117 // execute normally. |
| 118 SANDBOX_EXPORT ResultExpr Allow(); |
| 119 |
| 120 // Error specifies a result that the system call should fail with |
| 121 // error number |err|. As a special case, Error(0) will result in the |
| 122 // system call appearing to have succeeded, but without having any |
| 123 // side effects. |
| 124 SANDBOX_EXPORT ResultExpr Error(int err); |
| 125 |
| 126 // Trap specifies a result that the system call should be handled by |
| 127 // trapping back into userspace and invoking |trap_func|, passing |
| 128 // |aux| as the second parameter. |
| 129 SANDBOX_EXPORT ResultExpr Trap(::sandbox::Trap::TrapFnc trap_func, void* aux); |
| 130 |
| 131 template <typename T> |
| 132 class SANDBOX_EXPORT Arg { |
| 133 public: |
| 134 // Initializes the Arg to represent the |num|th system call |
| 135 // argument (indexed from 0), which is of type |T|. |
| 136 explicit Arg(int num) : num_(num), mask_(-1) {} |
| 137 |
| 138 Arg(const Arg& arg) : num_(arg.num_), mask_(arg.mask_) {} |
| 139 |
| 140 // Returns an Arg representing the current argument, but after |
| 141 // bitwise-and'ing it with |rhs|. |
| 142 Arg operator&(uint64_t rhs) const { return Arg(num_, mask_ & rhs); } |
| 143 |
| 144 // Returns a boolean expression comparing whether the system call |
| 145 // argument (after applying any bitmasks, if appropriate) equals |rhs|. |
| 146 BoolExpr operator==(T rhs) const; |
| 147 |
| 148 private: |
| 149 Arg(int num, uint64_t mask) : num_(num), mask_(mask) {} |
| 150 int num_; |
| 151 uint64_t mask_; |
| 152 DISALLOW_ASSIGN(Arg); |
| 153 }; |
| 154 |
| 155 // Various ways to combine boolean expressions into more complex expressions. |
| 156 // They follow standard boolean algebra laws. |
| 157 SANDBOX_EXPORT BoolExpr operator!(BoolExpr cond); |
| 158 SANDBOX_EXPORT BoolExpr operator&&(BoolExpr lhs, BoolExpr rhs); |
| 159 SANDBOX_EXPORT BoolExpr operator||(BoolExpr lhs, BoolExpr rhs); |
| 160 |
| 161 // If begins a conditional result expression predicated on the |
| 162 // specified boolean expression. |
| 163 SANDBOX_EXPORT Elser If(BoolExpr cond, ResultExpr then_result); |
| 164 |
| 165 class SANDBOX_EXPORT Elser { |
| 166 public: |
| 167 Elser(const Elser& elser); |
| 168 ~Elser(); |
| 169 |
| 170 // ElseIf extends the conditional result expression with another |
| 171 // "if then" clause, predicated on the specified boolean expression. |
| 172 Elser ElseIf(BoolExpr cond, ResultExpr then_result) const; |
| 173 |
| 174 // Else terminates a conditional result expression using |else_result| as |
| 175 // the default fallback result expression. |
| 176 ResultExpr Else(ResultExpr else_result) const; |
| 177 |
| 178 private: |
| 179 typedef std::pair<BoolExpr, ResultExpr> Clause; |
| 180 explicit Elser(Cons<Clause>::List clause_list); |
| 181 Cons<Clause>::List clause_list_; |
| 182 friend Elser If(BoolExpr, ResultExpr); |
| 183 DISALLOW_ASSIGN(Elser); |
| 184 }; |
| 185 |
| 186 // ===================================================================== |
| 187 // Official API ends here. |
| 188 // ===================================================================== |
| 189 |
| 190 // Definitions below are necessary here only for C++03 compatibility. |
| 191 // Once C++11 is available, they should be moved into bpf_dsl.cc via extern |
| 192 // templates. |
| 193 namespace internal { |
| 194 |
| 195 // Returns a boolean expression that represents whether system call |
| 196 // argument |num| of size |size| is equal to |val|, when masked |
| 197 // according to |mask|. Users should use the Arg template class below |
| 198 // instead of using this API directly. |
| 199 SANDBOX_EXPORT BoolExpr |
| 200 ArgEq(int num, size_t size, uint64_t mask, uint64_t val); |
| 201 |
| 202 // Internal interface implemented by BoolExpr implementations. |
| 203 class SANDBOX_EXPORT BoolExprImpl : public base::RefCounted<BoolExprImpl> { |
| 204 public: |
| 205 BoolExprImpl() {} |
| 206 virtual ErrorCode Compile(SandboxBPF* sb, |
| 207 ErrorCode true_ec, |
| 208 ErrorCode false_ec) const = 0; |
| 209 |
| 210 protected: |
| 211 virtual ~BoolExprImpl() {} |
| 212 |
| 213 private: |
| 214 friend class base::RefCounted<BoolExprImpl>; |
| 215 DISALLOW_COPY_AND_ASSIGN(BoolExprImpl); |
| 216 }; |
| 217 |
| 218 // Internal interface implemented by ResultExpr implementations. |
| 219 class SANDBOX_EXPORT ResultExprImpl : public base::RefCounted<ResultExprImpl> { |
| 220 public: |
| 221 ResultExprImpl() {} |
| 222 virtual ErrorCode Compile(SandboxBPF* sb) const = 0; |
| 223 |
| 224 protected: |
| 225 virtual ~ResultExprImpl() {} |
| 226 |
| 227 private: |
| 228 friend class base::RefCounted<ResultExprImpl>; |
| 229 DISALLOW_COPY_AND_ASSIGN(ResultExprImpl); |
| 230 }; |
| 231 |
| 232 } // namespace internal |
| 233 |
| 234 // Definition requires ArgEq to have been declared. Moved out-of-line |
| 235 // to minimize how much internal clutter users have to ignore while |
| 236 // reading the header documentation. |
| 237 template <typename T> |
| 238 BoolExpr Arg<T>::operator==(T rhs) const { |
| 239 return internal::ArgEq(num_, sizeof(T), mask_, static_cast<uint64_t>(rhs)); |
| 240 } |
| 241 |
| 242 } // namespace bpf_dsl |
| 243 } // namespace sandbox |
| 244 |
| 245 #endif // SANDBOX_LINUX_SECCOMP_BPF_HELPERS_BPF_DSL_H_ |
OLD | NEW |