OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 // Note: any code in this file MUST be async-signal safe. | 5 // Note: any code in this file MUST be async-signal safe. |
6 | 6 |
7 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" | 7 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" |
8 | 8 |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 // TODO(jln): query the current policy to check if send() is available and | 37 // TODO(jln): query the current policy to check if send() is available and |
38 // use it to perform a non-blocking write. | 38 // use it to perform a non-blocking write. |
39 const int ret = HANDLE_EINTR(write(STDERR_FILENO, error_message, size)); | 39 const int ret = HANDLE_EINTR(write(STDERR_FILENO, error_message, size)); |
40 // We can't handle any type of error here. | 40 // We can't handle any type of error here. |
41 if (ret <= 0 || static_cast<size_t>(ret) > size) break; | 41 if (ret <= 0 || static_cast<size_t>(ret) > size) break; |
42 size -= ret; | 42 size -= ret; |
43 error_message += ret; | 43 error_message += ret; |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
| 47 // Invalid syscall values are truncated to zero. |
| 48 // On architectures where base value is zero (Intel and Arm), |
| 49 // syscall number is the same as offset from base. |
| 50 // This function returns values between 0 and 1023 on all architectures. |
| 51 // On architectures where base value is different than zero (currently only |
| 52 // Mips), we are truncating valid syscall values to offset from base. |
| 53 uint32_t SyscallNumberToOffsetFromBase(uint32_t sysno) { |
| 54 #if defined(__mips__) |
| 55 // On MIPS syscall numbers are in different range than on x86 and ARM. |
| 56 // Valid MIPS O32 ABI syscall __NR_syscall will be truncated to zero for |
| 57 // simplicity. |
| 58 sysno = sysno - __NR_Linux; |
| 59 #endif |
| 60 |
| 61 if (sysno >= 1024) |
| 62 sysno = 0; |
| 63 |
| 64 return sysno; |
| 65 } |
| 66 |
47 // Print a seccomp-bpf failure to handle |sysno| to stderr in an | 67 // Print a seccomp-bpf failure to handle |sysno| to stderr in an |
48 // async-signal safe way. | 68 // async-signal safe way. |
49 void PrintSyscallError(uint32_t sysno) { | 69 void PrintSyscallError(uint32_t sysno) { |
50 if (sysno >= 1024) | 70 if (sysno >= 1024) |
51 sysno = 0; | 71 sysno = 0; |
52 // TODO(markus): replace with async-signal safe snprintf when available. | 72 // TODO(markus): replace with async-signal safe snprintf when available. |
53 const size_t kNumDigits = 4; | 73 const size_t kNumDigits = 4; |
54 char sysno_base10[kNumDigits]; | 74 char sysno_base10[kNumDigits]; |
55 uint32_t rem = sysno; | 75 uint32_t rem = sysno; |
56 uint32_t mod = 0; | 76 uint32_t mod = 0; |
57 for (int i = kNumDigits - 1; i >= 0; i--) { | 77 for (int i = kNumDigits - 1; i >= 0; i--) { |
58 mod = rem % 10; | 78 mod = rem % 10; |
59 rem /= 10; | 79 rem /= 10; |
60 sysno_base10[i] = '0' + mod; | 80 sysno_base10[i] = '0' + mod; |
61 } | 81 } |
| 82 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32) |
| 83 static const char kSeccompErrorPrefix[] = |
| 84 __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT |
| 85 " in syscall 4000 + "; |
| 86 #else |
62 static const char kSeccompErrorPrefix[] = | 87 static const char kSeccompErrorPrefix[] = |
63 __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT " in syscall "; | 88 __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT " in syscall "; |
| 89 #endif |
64 static const char kSeccompErrorPostfix[] = "\n"; | 90 static const char kSeccompErrorPostfix[] = "\n"; |
65 WriteToStdErr(kSeccompErrorPrefix, sizeof(kSeccompErrorPrefix) - 1); | 91 WriteToStdErr(kSeccompErrorPrefix, sizeof(kSeccompErrorPrefix) - 1); |
66 WriteToStdErr(sysno_base10, sizeof(sysno_base10)); | 92 WriteToStdErr(sysno_base10, sizeof(sysno_base10)); |
67 WriteToStdErr(kSeccompErrorPostfix, sizeof(kSeccompErrorPostfix) - 1); | 93 WriteToStdErr(kSeccompErrorPostfix, sizeof(kSeccompErrorPostfix) - 1); |
68 } | 94 } |
69 | 95 |
70 } // namespace. | 96 } // namespace. |
71 | 97 |
72 namespace sandbox { | 98 namespace sandbox { |
73 | 99 |
74 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data& args, void* aux) { | 100 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data& args, void* aux) { |
75 uint32_t syscall = args.nr; | 101 uint32_t syscall = SyscallNumberToOffsetFromBase(args.nr); |
76 if (syscall >= 1024) | 102 |
77 syscall = 0; | |
78 PrintSyscallError(syscall); | 103 PrintSyscallError(syscall); |
79 | 104 |
80 // Encode 8-bits of the 1st two arguments too, so we can discern which socket | 105 // Encode 8-bits of the 1st two arguments too, so we can discern which socket |
81 // type, which fcntl, ... etc., without being likely to hit a mapped | 106 // type, which fcntl, ... etc., without being likely to hit a mapped |
82 // address. | 107 // address. |
83 // Do not encode more bits here without thinking about increasing the | 108 // Do not encode more bits here without thinking about increasing the |
84 // likelihood of collision with mapped pages. | 109 // likelihood of collision with mapped pages. |
85 syscall |= ((args.args[0] & 0xffUL) << 12); | 110 syscall |= ((args.args[0] & 0xffUL) << 12); |
86 syscall |= ((args.args[1] & 0xffUL) << 20); | 111 syscall |= ((args.args[1] & 0xffUL) << 20); |
87 // Purposefully dereference the syscall as an address so it'll show up very | 112 // Purposefully dereference the syscall as an address so it'll show up very |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 203 |
179 const char* GetIoctlErrorMessageContentForTests() { | 204 const char* GetIoctlErrorMessageContentForTests() { |
180 return SECCOMP_MESSAGE_IOCTL_CONTENT; | 205 return SECCOMP_MESSAGE_IOCTL_CONTENT; |
181 } | 206 } |
182 | 207 |
183 const char* GetKillErrorMessageContentForTests() { | 208 const char* GetKillErrorMessageContentForTests() { |
184 return SECCOMP_MESSAGE_KILL_CONTENT; | 209 return SECCOMP_MESSAGE_KILL_CONTENT; |
185 } | 210 } |
186 | 211 |
187 } // namespace sandbox. | 212 } // namespace sandbox. |
OLD | NEW |