Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: sandbox/linux/seccomp-bpf/errorcode.h

Issue 10833044: Refactored ErrorCode into it's own class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Made changes requested by reviewer (jln) Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #ifndef ERRORCODE_H__
6 #define ERRORCODE_H__
7
8 namespace playground2 {
9
10 struct arch_seccomp_data;
11
jln (very slow on Chromium) 2012/07/30 18:50:39 The class documentation should be right above the
12 class ErrorCode {
13 // This class holds all the possible values that can returned by a sandbox
14 // policy.
15 // We can either wrap a symbolic ErrorCode (i.e. ERR_XXX enum values), an
16 // errno value (in the range 1..4095), a pointer to a TrapFnc callback
17 // handling a SECCOMP_RET_TRAP trap, or a complex constraint.
18 // All of the commonly used values are stored in the "err_" field. So, code
19 // that is using the ErrorCode class typically operates on a single 32bit
20 // field.
21
22 public:
23 enum {
24 // Allow this system call.
25 ERR_ALLOWED = 0x0000,
26
27 // Deny the system call with a particular "errno" value.
28 ERR_MIN_ERRNO = 1,
29 ERR_MAX_ERRNO = 4095,
30
31 // This code should never be used directly, it is used internally only.
32 ERR_INVALID = -1,
33 };
34
35 // TrapFnc is a pointer to a function that handles Seccomp traps in
36 // user-space. The seccomp policy can request that a trap handler gets
37 // installed; it does so by returning a suitable ErrorCode() from the
38 // syscallEvaluator. See the ErrorCode() constructor for how to pass in
39 // the function pointer.
40 // Please note that TrapFnc is executed from signal context and must be
41 // async-signal safe:
42 // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html
43 typedef intptr_t (*TrapFnc)(const struct arch_seccomp_data& args, void *aux);
44
45 enum ArgType {
46 TP_32BIT, TP_64BIT,
47 };
48
49 enum Operation {
50 OP_EQUAL, OP_GREATER, OP_GREATER_EQUAL, OP_HAS_BITS,
51 OP_NUM_OPS,
52 };
53
54 ErrorCode() :
55 error_type_(ET_INVALID),
56 err_(ERR_INVALID) {
57 }
58 explicit ErrorCode(int err);
59
60 // For all practical purposes, ErrorCodes are treated as if they were
61 // structs. The copy constructor and assignment operator are trivial and
62 // we do not need to explicitly specify them.
63 // Most notably, it is in fact perfectly OK to directly copy the passed_ and
64 // failed_ field. They only ever get set by our private constructor, and the
65 // callers handle life-cycle management for these objects.
66
67 // Destructor
68 ~ErrorCode() { }
69
70 bool Equals(const ErrorCode& err) const;
71 bool LessThan(const ErrorCode& err) const;
72
73 uint32_t err() const { return err_; }
74
75 struct LessThan {
76 bool operator()(const ErrorCode& a, const ErrorCode& b) const {
77 return a.LessThan(b);
78 }
79 };
80
81 private:
82 friend class Sandbox;
83 friend class Verifier;
84
85 enum ErrorType {
86 ET_INVALID, ET_SIMPLE, ET_TRAP, ET_COND,
87 };
88
89 static void die(const char *msg) __attribute__((noreturn));
90
91 // If we are wrapping a callback, we must assign a unique id. This id is
92 // how the kernel tells us which one of our different SECCOMP_RET_TRAP
93 // cases has been triggered.
94 ErrorCode(TrapFnc fnc, const void *aux, uint16_t id);
95
96 // Some system calls require inspection of arguments. This constructor
97 // allows us to specify additional constraints.
98 ErrorCode(int argno, ArgType width, Operation op, uint64_t value,
99 const ErrorCode *passed, const ErrorCode *failed);
100
101 ErrorType error_type_;
102
103 union {
104 // Fields needed for SECCOMP_RET_TRAP callbacks
105 struct {
106 TrapFnc fnc_; // Callback function and arg, if trap was
107 void *aux_; // triggered by the kernel's BPF filter.
jln (very slow on Chromium) 2012/07/30 18:50:39 Nit: C++ style "void* aux;"
108 };
109
110 // Fields needed when inspecting additional arguments.
111 struct {
112 uint64_t value_; // Value that we are comparing with
113 int argno_; // Syscall arg number that we are inspecting
114 ArgType width_; // Whether we are looking at a 32/64bit value
115 Operation op_; // Comparision operation
116 const ErrorCode *passed_; // Value to be returned if comparison passed,
117 const ErrorCode *failed_; // or if it failed.
jln (very slow on Chromium) 2012/07/30 18:50:39 Nit: C++ style "X* ptr"
118 };
119 };
120
121 // 32bit field used for all possible types of ErrorCode values. This is
122 // the value that uniquely identifies any ErrorCode and it (typically) can
123 // be emitted directly into a BPF filter program.
124 uint32_t err_;
125
126 };
127
128 } // namespace
129
130 #endif // ERRORCODE_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698