Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / sandbox / linux / seccomp-bpf-helpers / sigsys_handlers.cc
1 // Copyright (c) 2013 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 // Note: any code in this file MUST be async-signal safe.
6
7 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h"
8
9 #include <sys/syscall.h>
10 #include <unistd.h>
11
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "base/posix/eintr_wrapper.h"
15 #include "build/build_config.h"
16 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
17 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
18 #include "sandbox/linux/seccomp-bpf/syscall.h"
19 #include "sandbox/linux/services/linux_syscalls.h"
20
21 #if defined(__mips__)
22 // __NR_Linux, is defined in <asm/unistd.h>.
23 #include <asm/unistd.h>
24 #endif
25
26 #define SECCOMP_MESSAGE_COMMON_CONTENT "seccomp-bpf failure"
27 #define SECCOMP_MESSAGE_CLONE_CONTENT "clone() failure"
28 #define SECCOMP_MESSAGE_PRCTL_CONTENT "prctl() failure"
29 #define SECCOMP_MESSAGE_IOCTL_CONTENT "ioctl() failure"
30 #define SECCOMP_MESSAGE_KILL_CONTENT "(tg)kill() failure"
31 #define SECCOMP_MESSAGE_FUTEX_CONTENT "futex() failure"
32
33 namespace {
34
35 inline bool IsArchitectureX86_64() {
36 #if defined(__x86_64__)
37   return true;
38 #else
39   return false;
40 #endif
41 }
42
43 // Write |error_message| to stderr. Similar to RawLog(), but a bit more careful
44 // about async-signal safety. |size| is the size to write and should typically
45 // not include a terminating \0.
46 void WriteToStdErr(const char* error_message, size_t size) {
47   while (size > 0) {
48     // TODO(jln): query the current policy to check if send() is available and
49     // use it to perform a non-blocking write.
50     const int ret = HANDLE_EINTR(write(STDERR_FILENO, error_message, size));
51     // We can't handle any type of error here.
52     if (ret <= 0 || static_cast<size_t>(ret) > size) break;
53     size -= ret;
54     error_message += ret;
55   }
56 }
57
58 // Invalid syscall values are truncated to zero.
59 // On architectures where base value is zero (Intel and Arm),
60 // syscall number is the same as offset from base.
61 // This function returns values between 0 and 1023 on all architectures.
62 // On architectures where base value is different than zero (currently only
63 // Mips), we are truncating valid syscall values to offset from base.
64 uint32_t SyscallNumberToOffsetFromBase(uint32_t sysno) {
65 #if defined(__mips__)
66   // On MIPS syscall numbers are in different range than on x86 and ARM.
67   // Valid MIPS O32 ABI syscall __NR_syscall will be truncated to zero for
68   // simplicity.
69   sysno = sysno - __NR_Linux;
70 #endif
71
72   if (sysno >= 1024)
73     sysno = 0;
74
75   return sysno;
76 }
77
78 // Print a seccomp-bpf failure to handle |sysno| to stderr in an
79 // async-signal safe way.
80 void PrintSyscallError(uint32_t sysno) {
81   if (sysno >= 1024)
82     sysno = 0;
83   // TODO(markus): replace with async-signal safe snprintf when available.
84   const size_t kNumDigits = 4;
85   char sysno_base10[kNumDigits];
86   uint32_t rem = sysno;
87   uint32_t mod = 0;
88   for (int i = kNumDigits - 1; i >= 0; i--) {
89     mod = rem % 10;
90     rem /= 10;
91     sysno_base10[i] = '0' + mod;
92   }
93 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32)
94   static const char kSeccompErrorPrefix[] = __FILE__
95       ":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT " in syscall 4000 + ";
96 #else
97   static const char kSeccompErrorPrefix[] =
98       __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT " in syscall ";
99 #endif
100   static const char kSeccompErrorPostfix[] = "\n";
101   WriteToStdErr(kSeccompErrorPrefix, sizeof(kSeccompErrorPrefix) - 1);
102   WriteToStdErr(sysno_base10, sizeof(sysno_base10));
103   WriteToStdErr(kSeccompErrorPostfix, sizeof(kSeccompErrorPostfix) - 1);
104 }
105
106 }  // namespace.
107
108 namespace sandbox {
109
110 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data& args, void* aux) {
111   uint32_t syscall = SyscallNumberToOffsetFromBase(args.nr);
112
113   PrintSyscallError(syscall);
114
115   // Encode 8-bits of the 1st two arguments too, so we can discern which socket
116   // type, which fcntl, ... etc., without being likely to hit a mapped
117   // address.
118   // Do not encode more bits here without thinking about increasing the
119   // likelihood of collision with mapped pages.
120   syscall |= ((args.args[0] & 0xffUL) << 12);
121   syscall |= ((args.args[1] & 0xffUL) << 20);
122   // Purposefully dereference the syscall as an address so it'll show up very
123   // clearly and easily in crash dumps.
124   volatile char* addr = reinterpret_cast<volatile char*>(syscall);
125   *addr = '\0';
126   // In case we hit a mapped address, hit the null page with just the syscall,
127   // for paranoia.
128   syscall &= 0xfffUL;
129   addr = reinterpret_cast<volatile char*>(syscall);
130   *addr = '\0';
131   for (;;)
132     _exit(1);
133 }
134
135 // TODO(jln): refactor the reporting functions.
136
137 intptr_t SIGSYSCloneFailure(const struct arch_seccomp_data& args, void* aux) {
138   static const char kSeccompCloneError[] =
139       __FILE__":**CRASHING**:" SECCOMP_MESSAGE_CLONE_CONTENT "\n";
140   WriteToStdErr(kSeccompCloneError, sizeof(kSeccompCloneError) - 1);
141   // "flags" is the first argument in the kernel's clone().
142   // Mark as volatile to be able to find the value on the stack in a minidump.
143   volatile uint64_t clone_flags = args.args[0];
144   volatile char* addr;
145   if (IsArchitectureX86_64()) {
146     addr = reinterpret_cast<volatile char*>(clone_flags & 0xFFFFFF);
147     *addr = '\0';
148   }
149   // Hit the NULL page if this fails to fault.
150   addr = reinterpret_cast<volatile char*>(clone_flags & 0xFFF);
151   *addr = '\0';
152   for (;;)
153     _exit(1);
154 }
155
156 intptr_t SIGSYSPrctlFailure(const struct arch_seccomp_data& args,
157                             void* /* aux */) {
158   static const char kSeccompPrctlError[] =
159       __FILE__":**CRASHING**:" SECCOMP_MESSAGE_PRCTL_CONTENT "\n";
160   WriteToStdErr(kSeccompPrctlError, sizeof(kSeccompPrctlError) - 1);
161   // Mark as volatile to be able to find the value on the stack in a minidump.
162   volatile uint64_t option = args.args[0];
163   volatile char* addr =
164       reinterpret_cast<volatile char*>(option & 0xFFF);
165   *addr = '\0';
166   for (;;)
167     _exit(1);
168 }
169
170 intptr_t SIGSYSIoctlFailure(const struct arch_seccomp_data& args,
171                             void* /* aux */) {
172   static const char kSeccompIoctlError[] =
173       __FILE__":**CRASHING**:" SECCOMP_MESSAGE_IOCTL_CONTENT "\n";
174   WriteToStdErr(kSeccompIoctlError, sizeof(kSeccompIoctlError) - 1);
175   // Make "request" volatile so that we can see it on the stack in a minidump.
176   volatile uint64_t request = args.args[1];
177   volatile char* addr = reinterpret_cast<volatile char*>(request & 0xFFFF);
178   *addr = '\0';
179   // Hit the NULL page if this fails.
180   addr = reinterpret_cast<volatile char*>(request & 0xFFF);
181   *addr = '\0';
182   for (;;)
183     _exit(1);
184 }
185
186 intptr_t SIGSYSKillFailure(const struct arch_seccomp_data& args,
187                            void* /* aux */) {
188    static const char kSeccompKillError[] =
189       __FILE__":**CRASHING**:" SECCOMP_MESSAGE_KILL_CONTENT "\n";
190   WriteToStdErr(kSeccompKillError, sizeof(kSeccompKillError) - 1);
191   // Make "request" volatile so that we can see it on the stack in a minidump.
192   volatile uint64_t pid = args.args[0];
193   volatile char* addr = reinterpret_cast<volatile char*>(pid & 0xFFF);
194   *addr = '\0';
195   // Hit the NULL page if this fails.
196   addr = reinterpret_cast<volatile char*>(pid & 0xFFF);
197   *addr = '\0';
198   for (;;)
199     _exit(1);
200 }
201
202 intptr_t SIGSYSFutexFailure(const struct arch_seccomp_data& args,
203                             void* /* aux */) {
204   static const char kSeccompFutexError[] =
205       __FILE__ ":**CRASHING**:" SECCOMP_MESSAGE_FUTEX_CONTENT "\n";
206   WriteToStdErr(kSeccompFutexError, sizeof(kSeccompFutexError) - 1);
207   volatile int futex_op = args.args[1];
208   volatile char* addr = reinterpret_cast<volatile char*>(futex_op & 0xFFF);
209   *addr = '\0';
210   for (;;)
211     _exit(1);
212 }
213
214 intptr_t SIGSYSSchedHandler(const struct arch_seccomp_data& args,
215                             void* aux) {
216   switch (args.nr) {
217     case __NR_sched_getaffinity:
218     case __NR_sched_getattr:
219     case __NR_sched_getparam:
220     case __NR_sched_getscheduler:
221     case __NR_sched_rr_get_interval:
222     case __NR_sched_setaffinity:
223     case __NR_sched_setattr:
224     case __NR_sched_setparam:
225     case __NR_sched_setscheduler:
226       const pid_t tid = syscall(__NR_gettid);
227       // The first argument is the pid.  If is our thread id, then replace it
228       // with 0, which is equivalent and allowed by the policy.
229       if (args.args[0] == static_cast<uint64_t>(tid)) {
230         return Syscall::Call(args.nr,
231                              0,
232                              static_cast<intptr_t>(args.args[1]),
233                              static_cast<intptr_t>(args.args[2]),
234                              static_cast<intptr_t>(args.args[3]),
235                              static_cast<intptr_t>(args.args[4]),
236                              static_cast<intptr_t>(args.args[5]));
237       }
238       break;
239   }
240
241   CrashSIGSYS_Handler(args, aux);
242
243   // Should never be reached.
244   RAW_CHECK(false);
245   return -ENOSYS;
246 }
247
248 bpf_dsl::ResultExpr CrashSIGSYS() {
249   return bpf_dsl::Trap(CrashSIGSYS_Handler, NULL);
250 }
251
252 bpf_dsl::ResultExpr CrashSIGSYSClone() {
253   return bpf_dsl::Trap(SIGSYSCloneFailure, NULL);
254 }
255
256 bpf_dsl::ResultExpr CrashSIGSYSPrctl() {
257   return bpf_dsl::Trap(SIGSYSPrctlFailure, NULL);
258 }
259
260 bpf_dsl::ResultExpr CrashSIGSYSIoctl() {
261   return bpf_dsl::Trap(SIGSYSIoctlFailure, NULL);
262 }
263
264 bpf_dsl::ResultExpr CrashSIGSYSKill() {
265   return bpf_dsl::Trap(SIGSYSKillFailure, NULL);
266 }
267
268 bpf_dsl::ResultExpr CrashSIGSYSFutex() {
269   return bpf_dsl::Trap(SIGSYSFutexFailure, NULL);
270 }
271
272 bpf_dsl::ResultExpr RewriteSchedSIGSYS() {
273   return bpf_dsl::Trap(SIGSYSSchedHandler, NULL);
274 }
275
276 const char* GetErrorMessageContentForTests() {
277   return SECCOMP_MESSAGE_COMMON_CONTENT;
278 }
279
280 const char* GetCloneErrorMessageContentForTests() {
281   return SECCOMP_MESSAGE_CLONE_CONTENT;
282 }
283
284 const char* GetPrctlErrorMessageContentForTests() {
285   return SECCOMP_MESSAGE_PRCTL_CONTENT;
286 }
287
288 const char* GetIoctlErrorMessageContentForTests() {
289   return SECCOMP_MESSAGE_IOCTL_CONTENT;
290 }
291
292 const char* GetKillErrorMessageContentForTests() {
293   return SECCOMP_MESSAGE_KILL_CONTENT;
294 }
295
296 const char* GetFutexErrorMessageContentForTests() {
297   return SECCOMP_MESSAGE_FUTEX_CONTENT;
298 }
299
300 }  // namespace sandbox.