1 // Copyright (c) 2012 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.
5 #include "sandbox/linux/seccomp-bpf/die.h"
6 #include "sandbox/linux/seccomp-bpf/errorcode.h"
9 namespace playground2 {
11 ErrorCode::ErrorCode(int err) {
14 err_ = SECCOMP_RET_ALLOW;
15 error_type_ = ET_SIMPLE;
17 case ERR_MIN_ERRNO ... ERR_MAX_ERRNO:
18 err_ = SECCOMP_RET_ERRNO + err;
19 error_type_ = ET_SIMPLE;
22 SANDBOX_DIE("Invalid use of ErrorCode object");
26 ErrorCode::ErrorCode(Trap::TrapFnc fnc, const void *aux, bool safe,
28 : error_type_(ET_TRAP),
30 aux_(const_cast<void *>(aux)),
32 err_(SECCOMP_RET_TRAP + id) {
35 ErrorCode::ErrorCode(int argno, ArgType width, Operation op, uint64_t value,
36 const ErrorCode *passed, const ErrorCode *failed)
37 : error_type_(ET_COND),
44 err_(SECCOMP_RET_INVALID) {
45 if (op < 0 || op >= OP_NUM_OPS) {
46 SANDBOX_DIE("Invalid opcode in BPF sandbox rules");
50 bool ErrorCode::Equals(const ErrorCode& err) const {
51 if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) {
52 SANDBOX_DIE("Dereferencing invalid ErrorCode");
54 if (error_type_ != err.error_type_) {
57 if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) {
58 return err_ == err.err_;
59 } else if (error_type_ == ET_COND) {
60 return value_ == err.value_ &&
61 argno_ == err.argno_ &&
62 width_ == err.width_ &&
64 passed_->Equals(*err.passed_) &&
65 failed_->Equals(*err.failed_);
67 SANDBOX_DIE("Corrupted ErrorCode");
71 bool ErrorCode::LessThan(const ErrorCode& err) const {
72 // Implementing a "LessThan()" operator allows us to use ErrorCode objects
73 // as keys in STL containers; most notably, it also allows us to put them
74 // into std::set<>. Actual ordering is not important as long as it is
76 if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) {
77 SANDBOX_DIE("Dereferencing invalid ErrorCode");
79 if (error_type_ != err.error_type_) {
80 return error_type_ < err.error_type_;
82 if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) {
83 return err_ < err.err_;
84 } else if (error_type_ == ET_COND) {
85 if (value_ != err.value_) {
86 return value_ < err.value_;
87 } else if (argno_ != err.argno_) {
88 return argno_ < err.argno_;
89 } else if (width_ != err.width_) {
90 return width_ < err.width_;
91 } else if (op_ != err.op_) {
93 } else if (!passed_->Equals(*err.passed_)) {
94 return passed_->LessThan(*err.passed_);
95 } else if (!failed_->Equals(*err.failed_)) {
96 return failed_->LessThan(*err.failed_);
101 SANDBOX_DIE("Corrupted ErrorCode");