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

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

Issue 572753002: Decouple Trap from ErrorCode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix indenting; revert volatile changes Created 6 years, 3 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #ifndef SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__
6 #define SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ 6 #define SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__
7 7
8 #include <signal.h> 8 #include <signal.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <map> 11 #include <map>
12 #include <vector>
13 12
14 #include "base/basictypes.h" 13 #include "base/macros.h"
15 #include "sandbox/sandbox_export.h" 14 #include "sandbox/sandbox_export.h"
16 15
17 namespace sandbox { 16 namespace sandbox {
18 17
19 class ErrorCode; 18 // This must match the kernel's seccomp_data structure.
19 struct arch_seccomp_data {
20 int nr;
21 uint32_t arch;
22 uint64_t instruction_pointer;
23 uint64_t args[6];
24 };
20 25
21 // The Trap class allows a BPF filter program to branch out to user space by 26 // The Trap class allows a BPF filter program to branch out to user space by
22 // raising a SIGSYS signal. 27 // raising a SIGSYS signal.
23 // N.B.: This class does not perform any synchronization operations. If 28 // N.B.: This class does not perform any synchronization operations. If
24 // modifications are made to any of the traps, it is the caller's 29 // modifications are made to any of the traps, it is the caller's
25 // responsibility to ensure that this happens in a thread-safe fashion. 30 // responsibility to ensure that this happens in a thread-safe fashion.
26 // Preferably, that means that no other threads should be running at that 31 // Preferably, that means that no other threads should be running at that
27 // time. For the purposes of our sandbox, this assertion should always be 32 // time. For the purposes of our sandbox, this assertion should always be
28 // true. Threads are incompatible with the seccomp sandbox anyway. 33 // true. Threads are incompatible with the seccomp sandbox anyway.
29 class SANDBOX_EXPORT Trap { 34 class SANDBOX_EXPORT Trap {
(...skipping 10 matching lines...) Expand all
40 // In other words, it reports an error by returning an exit code in the 45 // In other words, it reports an error by returning an exit code in the
41 // range -1..-4096. It should not set errno when reporting errors; on the 46 // range -1..-4096. It should not set errno when reporting errors; on the
42 // other hand, accidentally modifying errno is harmless and the changes will 47 // other hand, accidentally modifying errno is harmless and the changes will
43 // be undone afterwards. 48 // be undone afterwards.
44 typedef intptr_t (*TrapFnc)(const struct arch_seccomp_data& args, void* aux); 49 typedef intptr_t (*TrapFnc)(const struct arch_seccomp_data& args, void* aux);
45 50
46 // Registers a new trap handler and sets up the appropriate SIGSYS handler 51 // Registers a new trap handler and sets up the appropriate SIGSYS handler
47 // as needed. 52 // as needed.
48 // N.B.: This makes a permanent state change. Traps cannot be unregistered, 53 // N.B.: This makes a permanent state change. Traps cannot be unregistered,
49 // as that would break existing BPF filters that are still active. 54 // as that would break existing BPF filters that are still active.
50 static ErrorCode MakeTrap(TrapFnc fnc, const void* aux, bool safe); 55 static uint16_t MakeTrap(TrapFnc fnc, const void* aux, bool safe);
51 56
52 // Enables support for unsafe traps in the SIGSYS signal handler. This is a 57 // Enables support for unsafe traps in the SIGSYS signal handler. This is a
53 // one-way fuse. It works in conjunction with the BPF compiler emitting code 58 // one-way fuse. It works in conjunction with the BPF compiler emitting code
54 // that unconditionally allows system calls, if they have a magic return 59 // that unconditionally allows system calls, if they have a magic return
55 // address (i.e. SandboxSyscall(-1)). 60 // address (i.e. SandboxSyscall(-1)).
56 // Once unsafe traps are enabled, the sandbox is essentially compromised. 61 // Once unsafe traps are enabled, the sandbox is essentially compromised.
57 // But this is still a very useful feature for debugging purposes. Use with 62 // But this is still a very useful feature for debugging purposes. Use with
58 // care. This feature is availably only if enabled by the user (see above). 63 // care. This feature is availably only if enabled by the user (see above).
59 // Returns "true", if unsafe traps were turned on. 64 // Returns "true", if unsafe traps were turned on.
60 static bool EnableUnsafeTrapsInSigSysHandler(); 65 static bool EnableUnsafeTrapsInSigSysHandler();
61 66
62 // Returns the ErrorCode associate with a particular trap id. 67 // Returns true if a safe trap handler is associated with a
63 static ErrorCode ErrorCodeFromTrapId(uint16_t id); 68 // particular trap ID.
69 static bool IsSafeTrapId(uint16_t id);
64 70
65 private: 71 private:
66 struct TrapKey { 72 struct TrapKey {
73 TrapKey() : fnc(NULL), aux(NULL), safe(false) {}
67 TrapKey(TrapFnc f, const void* a, bool s) : fnc(f), aux(a), safe(s) {} 74 TrapKey(TrapFnc f, const void* a, bool s) : fnc(f), aux(a), safe(s) {}
68 TrapFnc fnc; 75 TrapFnc fnc;
69 const void* aux; 76 const void* aux;
70 bool safe; 77 bool safe;
71 bool operator<(const TrapKey&) const; 78 bool operator<(const TrapKey&) const;
72 }; 79 };
73 typedef std::map<TrapKey, uint16_t> TrapIds; 80 typedef std::map<TrapKey, uint16_t> TrapIds;
74 81
75 // Our constructor is private. A shared global instance is created 82 // Our constructor is private. A shared global instance is created
76 // automatically as needed. 83 // automatically as needed.
(...skipping 10 matching lines...) Expand all
87 // It also gracefully deals with methods that should check for the singleton, 94 // It also gracefully deals with methods that should check for the singleton,
88 // but avoid instantiating it, if it doesn't exist yet 95 // but avoid instantiating it, if it doesn't exist yet
89 // (e.g. ErrorCodeFromTrapId()). 96 // (e.g. ErrorCodeFromTrapId()).
90 static Trap* GetInstance(); 97 static Trap* GetInstance();
91 static void SigSysAction(int nr, siginfo_t* info, void* void_context); 98 static void SigSysAction(int nr, siginfo_t* info, void* void_context);
92 99
93 // Make sure that SigSys is not inlined in order to get slightly better crash 100 // Make sure that SigSys is not inlined in order to get slightly better crash
94 // dumps. 101 // dumps.
95 void SigSys(int nr, siginfo_t* info, void* void_context) 102 void SigSys(int nr, siginfo_t* info, void* void_context)
96 __attribute__((noinline)); 103 __attribute__((noinline));
97 ErrorCode MakeTrapImpl(TrapFnc fnc, const void* aux, bool safe); 104 uint16_t MakeTrapImpl(TrapFnc fnc, const void* aux, bool safe);
98 bool SandboxDebuggingAllowedByUser() const; 105 bool SandboxDebuggingAllowedByUser() const;
99 106
100 // We have a global singleton that handles all of our SIGSYS traps. This 107 // We have a global singleton that handles all of our SIGSYS traps. This
101 // variable must never be deallocated after it has been set up initially, as 108 // variable must never be deallocated after it has been set up initially, as
102 // there is no way to reset in-kernel BPF filters that generate SIGSYS 109 // there is no way to reset in-kernel BPF filters that generate SIGSYS
103 // events. 110 // events.
104 static Trap* global_trap_; 111 static Trap* global_trap_;
105 112
106 TrapIds trap_ids_; // Maps from TrapKeys to numeric ids 113 TrapIds trap_ids_; // Maps from TrapKeys to numeric ids
107 ErrorCode* trap_array_; // Array of ErrorCodes indexed by ids 114 TrapKey* trap_array_; // Array of TrapKeys indexed by ids
108 size_t trap_array_size_; // Currently used size of array 115 size_t trap_array_size_; // Currently used size of array
109 size_t trap_array_capacity_; // Currently allocated capacity of array 116 size_t trap_array_capacity_; // Currently allocated capacity of array
110 bool has_unsafe_traps_; // Whether unsafe traps have been enabled 117 bool has_unsafe_traps_; // Whether unsafe traps have been enabled
111 118
112 // Copying and assigning is unimplemented. It doesn't make sense for a 119 // Copying and assigning is unimplemented. It doesn't make sense for a
113 // singleton. 120 // singleton.
114 DISALLOW_COPY_AND_ASSIGN(Trap); 121 DISALLOW_COPY_AND_ASSIGN(Trap);
115 }; 122 };
116 123
117 } // namespace sandbox 124 } // namespace sandbox
118 125
119 #endif // SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ 126 #endif // SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698