| 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 "sandbox/linux/bpf_dsl/bpf_dsl.h" | 5 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 ResultExpr then_result_; | 87 ResultExpr then_result_; |
| 88 ResultExpr else_result_; | 88 ResultExpr else_result_; |
| 89 | 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(IfThenResultExprImpl); | 90 DISALLOW_COPY_AND_ASSIGN(IfThenResultExprImpl); |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 class PrimitiveBoolExprImpl : public internal::BoolExprImpl { | 93 class PrimitiveBoolExprImpl : public internal::BoolExprImpl { |
| 94 public: | 94 public: |
| 95 PrimitiveBoolExprImpl(int argno, | 95 PrimitiveBoolExprImpl(int argno, |
| 96 ErrorCode::ArgType is_32bit, | 96 ErrorCode::ArgType is_32bit, |
| 97 ErrorCode::Operation op, | 97 uint64_t mask, |
| 98 uint64_t value) | 98 uint64_t value) |
| 99 : argno_(argno), is_32bit_(is_32bit), op_(op), value_(value) {} | 99 : argno_(argno), is_32bit_(is_32bit), mask_(mask), value_(value) {} |
| 100 | 100 |
| 101 virtual ErrorCode Compile(SandboxBPF* sb, | 101 virtual ErrorCode Compile(SandboxBPF* sb, |
| 102 ErrorCode true_ec, | 102 ErrorCode true_ec, |
| 103 ErrorCode false_ec) const OVERRIDE { | 103 ErrorCode false_ec) const OVERRIDE { |
| 104 return sb->Cond(argno_, is_32bit_, op_, value_, true_ec, false_ec); | 104 return sb->CondMaskedEqual( |
| 105 argno_, is_32bit_, mask_, value_, true_ec, false_ec); |
| 105 } | 106 } |
| 106 | 107 |
| 107 private: | 108 private: |
| 108 virtual ~PrimitiveBoolExprImpl() {} | 109 virtual ~PrimitiveBoolExprImpl() {} |
| 109 | 110 |
| 110 int argno_; | 111 int argno_; |
| 111 ErrorCode::ArgType is_32bit_; | 112 ErrorCode::ArgType is_32bit_; |
| 112 ErrorCode::Operation op_; | 113 uint64_t mask_; |
| 113 uint64_t value_; | 114 uint64_t value_; |
| 114 | 115 |
| 115 DISALLOW_COPY_AND_ASSIGN(PrimitiveBoolExprImpl); | 116 DISALLOW_COPY_AND_ASSIGN(PrimitiveBoolExprImpl); |
| 116 }; | 117 }; |
| 117 | 118 |
| 118 class NegateBoolExprImpl : public internal::BoolExprImpl { | 119 class NegateBoolExprImpl : public internal::BoolExprImpl { |
| 119 public: | 120 public: |
| 120 explicit NegateBoolExprImpl(const BoolExpr& cond) : cond_(cond) {} | 121 explicit NegateBoolExprImpl(const BoolExpr& cond) : cond_(cond) {} |
| 121 | 122 |
| 122 virtual ErrorCode Compile(SandboxBPF* sb, | 123 virtual ErrorCode Compile(SandboxBPF* sb, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 BoolExpr lhs_; | 171 BoolExpr lhs_; |
| 171 BoolExpr rhs_; | 172 BoolExpr rhs_; |
| 172 | 173 |
| 173 DISALLOW_COPY_AND_ASSIGN(OrBoolExprImpl); | 174 DISALLOW_COPY_AND_ASSIGN(OrBoolExprImpl); |
| 174 }; | 175 }; |
| 175 | 176 |
| 176 } // namespace | 177 } // namespace |
| 177 | 178 |
| 178 namespace internal { | 179 namespace internal { |
| 179 | 180 |
| 181 uint64_t DefaultMask(size_t size) { |
| 182 switch (size) { |
| 183 case 4: |
| 184 return std::numeric_limits<uint32_t>::max(); |
| 185 case 8: |
| 186 return std::numeric_limits<uint64_t>::max(); |
| 187 default: |
| 188 CHECK(false) << "Unimplemented DefaultMask case"; |
| 189 return 0; |
| 190 } |
| 191 } |
| 192 |
| 180 BoolExpr ArgEq(int num, size_t size, uint64_t mask, uint64_t val) { | 193 BoolExpr ArgEq(int num, size_t size, uint64_t mask, uint64_t val) { |
| 181 CHECK(num >= 0 && num < 6); | 194 CHECK(num >= 0 && num < 6); |
| 182 CHECK(size >= 1 && size <= 8); | 195 CHECK(size >= 1 && size <= 8); |
| 183 CHECK_NE(0U, mask) << "zero mask doesn't make sense"; | 196 CHECK_NE(0U, mask) << "zero mask doesn't make sense"; |
| 184 CHECK_EQ(val, val & mask) << "val contains masked out bits"; | 197 CHECK_EQ(val, val & mask) << "val contains masked out bits"; |
| 185 | 198 |
| 186 // TODO(mdempsky): Should we just always use TP_64BIT? | 199 // TODO(mdempsky): Should we just always use TP_64BIT? |
| 187 const ErrorCode::ArgType arg_type = | 200 const ErrorCode::ArgType arg_type = |
| 188 (size <= 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT; | 201 (size <= 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT; |
| 189 | 202 |
| 190 if (mask == std::numeric_limits<uint64_t>::max()) { | 203 return BoolExpr(new const PrimitiveBoolExprImpl(num, arg_type, mask, val)); |
| 191 // Arg == Val | |
| 192 return BoolExpr(new const PrimitiveBoolExprImpl( | |
| 193 num, arg_type, ErrorCode::OP_EQUAL, val)); | |
| 194 } | |
| 195 | |
| 196 if (mask == val) { | |
| 197 // (Arg & Mask) == Mask | |
| 198 return BoolExpr(new const PrimitiveBoolExprImpl( | |
| 199 num, arg_type, ErrorCode::OP_HAS_ALL_BITS, mask)); | |
| 200 } | |
| 201 | |
| 202 if (val == 0) { | |
| 203 // (Arg & Mask) == 0, which is semantically equivalent to !((arg & mask) != | |
| 204 // 0). | |
| 205 return !BoolExpr(new const PrimitiveBoolExprImpl( | |
| 206 num, arg_type, ErrorCode::OP_HAS_ANY_BITS, mask)); | |
| 207 } | |
| 208 | |
| 209 CHECK(false) << "Unimplemented ArgEq case"; | |
| 210 return BoolExpr(); | |
| 211 } | 204 } |
| 212 | 205 |
| 213 } // namespace internal | 206 } // namespace internal |
| 214 | 207 |
| 215 ResultExpr Allow() { | 208 ResultExpr Allow() { |
| 216 return ResultExpr(new const AllowResultExprImpl()); | 209 return ResultExpr(new const AllowResultExprImpl()); |
| 217 } | 210 } |
| 218 | 211 |
| 219 ResultExpr Error(int err) { | 212 ResultExpr Error(int err) { |
| 220 return ResultExpr(new const ErrorResultExprImpl(err)); | 213 return ResultExpr(new const ErrorResultExprImpl(err)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 ErrorCode SandboxBPFDSLPolicy::InvalidSyscall(SandboxBPF* sb) const { | 291 ErrorCode SandboxBPFDSLPolicy::InvalidSyscall(SandboxBPF* sb) const { |
| 299 return InvalidSyscall()->Compile(sb); | 292 return InvalidSyscall()->Compile(sb); |
| 300 } | 293 } |
| 301 | 294 |
| 302 ResultExpr SandboxBPFDSLPolicy::Trap(Trap::TrapFnc trap_func, void* aux) { | 295 ResultExpr SandboxBPFDSLPolicy::Trap(Trap::TrapFnc trap_func, void* aux) { |
| 303 return bpf_dsl::Trap(trap_func, aux); | 296 return bpf_dsl::Trap(trap_func, aux); |
| 304 } | 297 } |
| 305 | 298 |
| 306 } // namespace bpf_dsl | 299 } // namespace bpf_dsl |
| 307 } // namespace sandbox | 300 } // namespace sandbox |
| OLD | NEW |