OLD | NEW |
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> | 12 #include <vector> |
13 | 13 |
14 #include "sandbox/linux/seccomp-bpf/port.h" | 14 #include "sandbox/linux/seccomp-bpf/port.h" |
15 | 15 |
16 | |
17 namespace playground2 { | 16 namespace playground2 { |
18 | 17 |
19 class ErrorCode; | 18 class ErrorCode; |
20 | 19 |
21 // The Trap class allows a BPF filter program to branch out to user space by | 20 // The Trap class allows a BPF filter program to branch out to user space by |
22 // raising a SIGSYS signal. | 21 // raising a SIGSYS signal. |
23 // N.B.: This class does not perform any synchronization operations. If | 22 // 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 | 23 // 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. | 24 // responsibility to ensure that this happens in a thread-safe fashion. |
26 // Preferably, that means that no other threads should be running at that | 25 // 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 | 26 // time. For the purposes of our sandbox, this assertion should always be |
28 // true. Threads are incompatible with the seccomp sandbox anyway. | 27 // true. Threads are incompatible with the seccomp sandbox anyway. |
29 class Trap { | 28 class Trap { |
30 public: | 29 public: |
31 // TrapFnc is a pointer to a function that handles Seccomp traps in | 30 // TrapFnc is a pointer to a function that handles Seccomp traps in |
32 // user-space. The seccomp policy can request that a trap handler gets | 31 // user-space. The seccomp policy can request that a trap handler gets |
33 // installed; it does so by returning a suitable ErrorCode() from the | 32 // installed; it does so by returning a suitable ErrorCode() from the |
34 // syscallEvaluator. See the ErrorCode() constructor for how to pass in | 33 // syscallEvaluator. See the ErrorCode() constructor for how to pass in |
35 // the function pointer. | 34 // the function pointer. |
36 // Please note that TrapFnc is executed from signal context and must be | 35 // Please note that TrapFnc is executed from signal context and must be |
37 // async-signal safe: | 36 // async-signal safe: |
38 // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html | 37 // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html |
39 // Also note that it follows the calling convention of native system calls. | 38 // Also note that it follows the calling convention of native system calls. |
40 // In other words, it reports an error by returning an exit code in the | 39 // 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 | 40 // 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 | 41 // other hand, accidentally modifying errno is harmless and the changes will |
43 // be undone afterwards. | 42 // be undone afterwards. |
44 typedef intptr_t (*TrapFnc)(const struct arch_seccomp_data& args, void *aux); | 43 typedef intptr_t (*TrapFnc)(const struct arch_seccomp_data& args, void* aux); |
45 | 44 |
46 // Registers a new trap handler and sets up the appropriate SIGSYS handler | 45 // Registers a new trap handler and sets up the appropriate SIGSYS handler |
47 // as needed. | 46 // as needed. |
48 // N.B.: This makes a permanent state change. Traps cannot be unregistered, | 47 // N.B.: This makes a permanent state change. Traps cannot be unregistered, |
49 // as that would break existing BPF filters that are still active. | 48 // as that would break existing BPF filters that are still active. |
50 static ErrorCode MakeTrap(TrapFnc fnc, const void *aux, bool safe); | 49 static ErrorCode MakeTrap(TrapFnc fnc, const void* aux, bool safe); |
51 | 50 |
52 // Enables support for unsafe traps in the SIGSYS signal handler. This is a | 51 // 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 | 52 // 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 | 53 // that unconditionally allows system calls, if they have a magic return |
55 // address (i.e. SandboxSyscall(-1)). | 54 // address (i.e. SandboxSyscall(-1)). |
56 // Once unsafe traps are enabled, the sandbox is essentially compromised. | 55 // Once unsafe traps are enabled, the sandbox is essentially compromised. |
57 // But this is still a very useful feature for debugging purposes. Use with | 56 // 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). | 57 // care. This feature is availably only if enabled by the user (see above). |
59 // Returns "true", if unsafe traps were turned on. | 58 // Returns "true", if unsafe traps were turned on. |
60 static bool EnableUnsafeTrapsInSigSysHandler(); | 59 static bool EnableUnsafeTrapsInSigSysHandler(); |
61 | 60 |
62 // Returns the ErrorCode associate with a particular trap id. | 61 // Returns the ErrorCode associate with a particular trap id. |
63 static ErrorCode ErrorCodeFromTrapId(uint16_t id); | 62 static ErrorCode ErrorCodeFromTrapId(uint16_t id); |
64 | 63 |
65 private: | 64 private: |
66 // The destructor is unimplemented. Don't ever attempt to destruct this | 65 // The destructor is unimplemented. Don't ever attempt to destruct this |
67 // object. It'll break subsequent system calls that trigger a SIGSYS. | 66 // object. It'll break subsequent system calls that trigger a SIGSYS. |
68 ~Trap(); | 67 ~Trap(); |
69 | 68 |
70 struct TrapKey { | 69 struct TrapKey { |
71 TrapKey(TrapFnc f, const void *a, bool s) | 70 TrapKey(TrapFnc f, const void* a, bool s) : fnc(f), aux(a), safe(s) {} |
72 : fnc(f), | 71 TrapFnc fnc; |
73 aux(a), | 72 const void* aux; |
74 safe(s) { | 73 bool safe; |
75 } | |
76 TrapFnc fnc; | |
77 const void *aux; | |
78 bool safe; | |
79 bool operator<(const TrapKey&) const; | 74 bool operator<(const TrapKey&) const; |
80 }; | 75 }; |
81 typedef std::map<TrapKey, uint16_t> TrapIds; | 76 typedef std::map<TrapKey, uint16_t> TrapIds; |
82 | 77 |
83 // We only have a very small number of methods. We opt to make them static | 78 // We only have a very small number of methods. We opt to make them static |
84 // and have them internally call GetInstance(). This is a little more | 79 // and have them internally call GetInstance(). This is a little more |
85 // convenient than having each caller obtain short-lived reference to the | 80 // convenient than having each caller obtain short-lived reference to the |
86 // singleton. | 81 // singleton. |
87 // It also gracefully deals with methods that should check for the singleton, | 82 // It also gracefully deals with methods that should check for the singleton, |
88 // but avoid instantiating it, if it doesn't exist yet | 83 // but avoid instantiating it, if it doesn't exist yet |
89 // (e.g. ErrorCodeFromTrapId()). | 84 // (e.g. ErrorCodeFromTrapId()). |
90 static Trap *GetInstance(); | 85 static Trap* GetInstance(); |
91 static void SigSysAction(int nr, siginfo_t *info, void *void_context); | 86 static void SigSysAction(int nr, siginfo_t* info, void* void_context); |
92 | 87 |
93 // Make sure that SigSys is not inlined in order to get slightly better crash | 88 // Make sure that SigSys is not inlined in order to get slightly better crash |
94 // dumps. | 89 // dumps. |
95 void SigSys(int nr, siginfo_t *info, void *void_context) | 90 void SigSys(int nr, siginfo_t* info, void* void_context) |
96 __attribute__ ((noinline)); | 91 __attribute__((noinline)); |
97 ErrorCode MakeTrapImpl(TrapFnc fnc, const void *aux, bool safe); | 92 ErrorCode MakeTrapImpl(TrapFnc fnc, const void* aux, bool safe); |
98 bool SandboxDebuggingAllowedByUser() const; | 93 bool SandboxDebuggingAllowedByUser() const; |
99 | 94 |
100 | |
101 | |
102 // We have a global singleton that handles all of our SIGSYS traps. This | 95 // We have a global singleton that handles all of our SIGSYS traps. This |
103 // variable must never be deallocated after it has been set up initially, as | 96 // variable must never be deallocated after it has been set up initially, as |
104 // there is no way to reset in-kernel BPF filters that generate SIGSYS | 97 // there is no way to reset in-kernel BPF filters that generate SIGSYS |
105 // events. | 98 // events. |
106 static Trap *global_trap_; | 99 static Trap* global_trap_; |
107 | 100 |
108 TrapIds trap_ids_; // Maps from TrapKeys to numeric ids | 101 TrapIds trap_ids_; // Maps from TrapKeys to numeric ids |
109 ErrorCode *trap_array_; // Array of ErrorCodes indexed by ids | 102 ErrorCode* trap_array_; // Array of ErrorCodes indexed by ids |
110 size_t trap_array_size_; // Currently used size of array | 103 size_t trap_array_size_; // Currently used size of array |
111 size_t trap_array_capacity_; // Currently allocated capacity of array | 104 size_t trap_array_capacity_; // Currently allocated capacity of array |
112 bool has_unsafe_traps_; // Whether unsafe traps have been enabled | 105 bool has_unsafe_traps_; // Whether unsafe traps have been enabled |
113 | 106 |
114 // Our constructor is private. A shared global instance is created | 107 // Our constructor is private. A shared global instance is created |
115 // automatically as needed. | 108 // automatically as needed. |
116 // Copying and assigning is unimplemented. It doesn't make sense for a | 109 // Copying and assigning is unimplemented. It doesn't make sense for a |
117 // singleton. | 110 // singleton. |
118 DISALLOW_IMPLICIT_CONSTRUCTORS(Trap); | 111 DISALLOW_IMPLICIT_CONSTRUCTORS(Trap); |
119 }; | 112 }; |
120 | 113 |
121 } // namespace playground2 | 114 } // namespace playground2 |
122 | 115 |
123 #endif // SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ | 116 #endif // SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ |
OLD | NEW |