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 <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "sandbox/linux/bpf_dsl/bpf_dsl_impl.h" | 11 #include "sandbox/linux/bpf_dsl/bpf_dsl_impl.h" |
| 12 #include "sandbox/linux/bpf_dsl/errorcode.h" |
12 #include "sandbox/linux/bpf_dsl/policy_compiler.h" | 13 #include "sandbox/linux/bpf_dsl/policy_compiler.h" |
13 #include "sandbox/linux/seccomp-bpf/die.h" | |
14 #include "sandbox/linux/seccomp-bpf/errorcode.h" | |
15 | 14 |
16 namespace sandbox { | 15 namespace sandbox { |
17 namespace bpf_dsl { | 16 namespace bpf_dsl { |
18 namespace { | 17 namespace { |
19 | 18 |
20 intptr_t BPFFailure(const struct arch_seccomp_data&, void* aux) { | |
21 SANDBOX_DIE(static_cast<char*>(aux)); | |
22 } | |
23 | |
24 class AllowResultExprImpl : public internal::ResultExprImpl { | 19 class AllowResultExprImpl : public internal::ResultExprImpl { |
25 public: | 20 public: |
26 AllowResultExprImpl() {} | 21 AllowResultExprImpl() {} |
27 | 22 |
28 ErrorCode Compile(PolicyCompiler* pc) const override { | 23 ErrorCode Compile(PolicyCompiler* pc) const override { |
29 return ErrorCode(ErrorCode::ERR_ALLOWED); | 24 return ErrorCode(ErrorCode::ERR_ALLOWED); |
30 } | 25 } |
31 | 26 |
32 bool IsAllow() const override { return true; } | 27 bool IsAllow() const override { return true; } |
33 | 28 |
(...skipping 16 matching lines...) Expand all Loading... |
50 bool IsDeny() const override { return true; } | 45 bool IsDeny() const override { return true; } |
51 | 46 |
52 private: | 47 private: |
53 ~ErrorResultExprImpl() override {} | 48 ~ErrorResultExprImpl() override {} |
54 | 49 |
55 int err_; | 50 int err_; |
56 | 51 |
57 DISALLOW_COPY_AND_ASSIGN(ErrorResultExprImpl); | 52 DISALLOW_COPY_AND_ASSIGN(ErrorResultExprImpl); |
58 }; | 53 }; |
59 | 54 |
| 55 class KillResultExprImpl : public internal::ResultExprImpl { |
| 56 public: |
| 57 KillResultExprImpl() {} |
| 58 |
| 59 ErrorCode Compile(PolicyCompiler* pc) const override { |
| 60 return ErrorCode(ErrorCode::ERR_KILL); |
| 61 } |
| 62 |
| 63 bool IsDeny() const override { return true; } |
| 64 |
| 65 private: |
| 66 ~KillResultExprImpl() override {} |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(KillResultExprImpl); |
| 69 }; |
| 70 |
60 class TraceResultExprImpl : public internal::ResultExprImpl { | 71 class TraceResultExprImpl : public internal::ResultExprImpl { |
61 public: | 72 public: |
62 TraceResultExprImpl(uint16_t aux) : aux_(aux) {} | 73 TraceResultExprImpl(uint16_t aux) : aux_(aux) {} |
63 | 74 |
64 ErrorCode Compile(PolicyCompiler* pc) const override { | 75 ErrorCode Compile(PolicyCompiler* pc) const override { |
65 return ErrorCode(ErrorCode::ERR_TRACE + aux_); | 76 return ErrorCode(ErrorCode::ERR_TRACE + aux_); |
66 } | 77 } |
67 | 78 |
68 private: | 79 private: |
69 ~TraceResultExprImpl() override {} | 80 ~TraceResultExprImpl() override {} |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 return std::numeric_limits<uint32_t>::max(); | 259 return std::numeric_limits<uint32_t>::max(); |
249 case 8: | 260 case 8: |
250 return std::numeric_limits<uint64_t>::max(); | 261 return std::numeric_limits<uint64_t>::max(); |
251 default: | 262 default: |
252 CHECK(false) << "Unimplemented DefaultMask case"; | 263 CHECK(false) << "Unimplemented DefaultMask case"; |
253 return 0; | 264 return 0; |
254 } | 265 } |
255 } | 266 } |
256 | 267 |
257 BoolExpr ArgEq(int num, size_t size, uint64_t mask, uint64_t val) { | 268 BoolExpr ArgEq(int num, size_t size, uint64_t mask, uint64_t val) { |
| 269 // If this is changed, update Arg<T>::EqualTo's static_cast rules |
| 270 // accordingly. |
258 CHECK(size == 4 || size == 8); | 271 CHECK(size == 4 || size == 8); |
259 | 272 |
260 // TODO(mdempsky): Should we just always use TP_64BIT? | 273 // TODO(mdempsky): Should we just always use TP_64BIT? |
261 const ErrorCode::ArgType arg_type = | 274 const ErrorCode::ArgType arg_type = |
262 (size == 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT; | 275 (size == 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT; |
263 | 276 |
264 return BoolExpr(new const PrimitiveBoolExprImpl(num, arg_type, mask, val)); | 277 return BoolExpr(new const PrimitiveBoolExprImpl(num, arg_type, mask, val)); |
265 } | 278 } |
266 | 279 |
267 } // namespace internal | 280 } // namespace internal |
268 | 281 |
269 ResultExpr Allow() { | 282 ResultExpr Allow() { |
270 return ResultExpr(new const AllowResultExprImpl()); | 283 return ResultExpr(new const AllowResultExprImpl()); |
271 } | 284 } |
272 | 285 |
273 ResultExpr Error(int err) { | 286 ResultExpr Error(int err) { |
274 return ResultExpr(new const ErrorResultExprImpl(err)); | 287 return ResultExpr(new const ErrorResultExprImpl(err)); |
275 } | 288 } |
276 | 289 |
277 ResultExpr Kill(const char* msg) { | 290 ResultExpr Kill() { |
278 return Trap(BPFFailure, msg); | 291 return ResultExpr(new const KillResultExprImpl()); |
279 } | 292 } |
280 | 293 |
281 ResultExpr Trace(uint16_t aux) { | 294 ResultExpr Trace(uint16_t aux) { |
282 return ResultExpr(new const TraceResultExprImpl(aux)); | 295 return ResultExpr(new const TraceResultExprImpl(aux)); |
283 } | 296 } |
284 | 297 |
285 ResultExpr Trap(TrapRegistry::TrapFnc trap_func, const void* aux) { | 298 ResultExpr Trap(TrapRegistry::TrapFnc trap_func, const void* aux) { |
286 return ResultExpr( | 299 return ResultExpr( |
287 new const TrapResultExprImpl(trap_func, aux, true /* safe */)); | 300 new const TrapResultExprImpl(trap_func, aux, true /* safe */)); |
288 } | 301 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 new const IfThenResultExprImpl(clause.first, clause.second, expr)); | 367 new const IfThenResultExprImpl(clause.first, clause.second, expr)); |
355 } | 368 } |
356 return expr; | 369 return expr; |
357 } | 370 } |
358 | 371 |
359 } // namespace bpf_dsl | 372 } // namespace bpf_dsl |
360 } // namespace sandbox | 373 } // namespace sandbox |
361 | 374 |
362 template class scoped_refptr<const sandbox::bpf_dsl::internal::BoolExprImpl>; | 375 template class scoped_refptr<const sandbox::bpf_dsl::internal::BoolExprImpl>; |
363 template class scoped_refptr<const sandbox::bpf_dsl::internal::ResultExprImpl>; | 376 template class scoped_refptr<const sandbox::bpf_dsl::internal::ResultExprImpl>; |
OLD | NEW |