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 |
(...skipping 16 matching lines...) Expand all Loading... |
27 class SANDBOX_EXPORT Trap : public bpf_dsl::TrapRegistry { | 27 class SANDBOX_EXPORT Trap : public bpf_dsl::TrapRegistry { |
28 public: | 28 public: |
29 uint16_t Add(TrapFnc fnc, const void* aux, bool safe) override; | 29 uint16_t Add(TrapFnc fnc, const void* aux, bool safe) override; |
30 | 30 |
31 bool EnableUnsafeTraps() override; | 31 bool EnableUnsafeTraps() override; |
32 | 32 |
33 // Registry returns the trap registry used by Trap's SIGSYS handler, | 33 // Registry returns the trap registry used by Trap's SIGSYS handler, |
34 // creating it if necessary. | 34 // creating it if necessary. |
35 static bpf_dsl::TrapRegistry* Registry(); | 35 static bpf_dsl::TrapRegistry* Registry(); |
36 | 36 |
37 // Registers a new trap handler and sets up the appropriate SIGSYS handler | 37 // SandboxDebuggingAllowedByUser returns whether the |
38 // as needed. | 38 // "CHROME_SANDBOX_DEBUGGING" environment variable is set. |
39 // N.B.: This makes a permanent state change. Traps cannot be unregistered, | 39 static bool SandboxDebuggingAllowedByUser(); |
40 // as that would break existing BPF filters that are still active. | |
41 // TODO(mdempsky): Deprecated; remove. | |
42 static uint16_t MakeTrap(TrapFnc fnc, const void* aux, bool safe); | |
43 | |
44 // Enables support for unsafe traps in the SIGSYS signal handler. This is a | |
45 // one-way fuse. It works in conjunction with the BPF compiler emitting code | |
46 // that unconditionally allows system calls, if they have a magic return | |
47 // address (i.e. SandboxSyscall(-1)). | |
48 // Once unsafe traps are enabled, the sandbox is essentially compromised. | |
49 // But this is still a very useful feature for debugging purposes. Use with | |
50 // care. This feature is availably only if enabled by the user (see above). | |
51 // Returns "true", if unsafe traps were turned on. | |
52 // TODO(mdempsky): Deprecated; remove. | |
53 static bool EnableUnsafeTrapsInSigSysHandler(); | |
54 | 40 |
55 private: | 41 private: |
56 struct TrapKey { | 42 struct TrapKey { |
57 TrapKey() : fnc(NULL), aux(NULL), safe(false) {} | 43 TrapKey() : fnc(NULL), aux(NULL), safe(false) {} |
58 TrapKey(TrapFnc f, const void* a, bool s) : fnc(f), aux(a), safe(s) {} | 44 TrapKey(TrapFnc f, const void* a, bool s) : fnc(f), aux(a), safe(s) {} |
59 TrapFnc fnc; | 45 TrapFnc fnc; |
60 const void* aux; | 46 const void* aux; |
61 bool safe; | 47 bool safe; |
62 bool operator<(const TrapKey&) const; | 48 bool operator<(const TrapKey&) const; |
63 }; | 49 }; |
64 typedef std::map<TrapKey, uint16_t> TrapIds; | 50 typedef std::map<TrapKey, uint16_t> TrapIds; |
65 | 51 |
66 // Our constructor is private. A shared global instance is created | 52 // Our constructor is private. A shared global instance is created |
67 // automatically as needed. | 53 // automatically as needed. |
68 Trap(); | 54 Trap(); |
69 | 55 |
70 // The destructor is unimplemented. Don't ever attempt to destruct this | 56 // The destructor is unimplemented as destroying this object would |
71 // object. It'll break subsequent system calls that trigger a SIGSYS. | 57 // break subsequent system calls that trigger a SIGSYS. |
72 ~Trap(); | 58 ~Trap() = delete; |
73 | 59 |
74 static void SigSysAction(int nr, siginfo_t* info, void* void_context); | 60 static void SigSysAction(int nr, siginfo_t* info, void* void_context); |
75 | 61 |
76 // Make sure that SigSys is not inlined in order to get slightly better crash | 62 // Make sure that SigSys is not inlined in order to get slightly better crash |
77 // dumps. | 63 // dumps. |
78 void SigSys(int nr, siginfo_t* info, void* void_context) | 64 void SigSys(int nr, siginfo_t* info, void* void_context) |
79 __attribute__((noinline)); | 65 __attribute__((noinline)); |
80 bool SandboxDebuggingAllowedByUser() const; | |
81 | |
82 // We have a global singleton that handles all of our SIGSYS traps. This | 66 // We have a global singleton that handles all of our SIGSYS traps. This |
83 // variable must never be deallocated after it has been set up initially, as | 67 // variable must never be deallocated after it has been set up initially, as |
84 // there is no way to reset in-kernel BPF filters that generate SIGSYS | 68 // there is no way to reset in-kernel BPF filters that generate SIGSYS |
85 // events. | 69 // events. |
86 static Trap* global_trap_; | 70 static Trap* global_trap_; |
87 | 71 |
88 TrapIds trap_ids_; // Maps from TrapKeys to numeric ids | 72 TrapIds trap_ids_; // Maps from TrapKeys to numeric ids |
89 TrapKey* trap_array_; // Array of TrapKeys indexed by ids | 73 TrapKey* trap_array_; // Array of TrapKeys indexed by ids |
90 size_t trap_array_size_; // Currently used size of array | 74 size_t trap_array_size_; // Currently used size of array |
91 size_t trap_array_capacity_; // Currently allocated capacity of array | 75 size_t trap_array_capacity_; // Currently allocated capacity of array |
92 bool has_unsafe_traps_; // Whether unsafe traps have been enabled | 76 bool has_unsafe_traps_; // Whether unsafe traps have been enabled |
93 | 77 |
94 // Copying and assigning is unimplemented. It doesn't make sense for a | 78 // Copying and assigning is unimplemented. It doesn't make sense for a |
95 // singleton. | 79 // singleton. |
96 DISALLOW_COPY_AND_ASSIGN(Trap); | 80 DISALLOW_COPY_AND_ASSIGN(Trap); |
97 }; | 81 }; |
98 | 82 |
99 } // namespace sandbox | 83 } // namespace sandbox |
100 | 84 |
101 #endif // SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ | 85 #endif // SANDBOX_LINUX_SECCOMP_BPF_TRAP_H__ |
OLD | NEW |