Chromium Code Reviews| 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 an 1023 on archs other than Mips. | |
|
jln (very slow on Chromium)
2014/06/03 01:00:33
s/an/and/
Also, this statement should be "on all
nedeljko
2014/06/03 15:32:18
Done.
| |
| 51 // On architectures where base value is different than zero (currently only | |
| 52 // Mips), we are truncating valid syscall value to offset from base. | |
| 53 // This function returns values between 0 and 350 on Mips (O32 ABI). | |
| 54 uint32_t SyscallNumberToOffsetFromBase(uint32_t sysno) { | |
| 55 #if defined(__mips__) | |
| 56 // On MIPS syscall numbers are in different range than on x86 and ARM. | |
| 57 // Valid MIPS O32 ABI syscall __NR_syscall will be truncated to zero for | |
| 58 // simlicity. | |
| 59 if (sysno > __NR_Linux && sysno <= __NR_Linux + __NR_Linux_syscalls) | |
| 60 sysno = sysno - __NR_Linux; | |
| 61 else | |
| 62 sysno = 0; | |
| 63 #else | |
| 64 if (sysno >= 1024) | |
| 65 sysno = 0; | |
| 66 #endif | |
| 67 return sysno; | |
| 68 } | |
| 69 | |
| 47 // Print a seccomp-bpf failure to handle |sysno| to stderr in an | 70 // Print a seccomp-bpf failure to handle |sysno| to stderr in an |
| 48 // async-signal safe way. | 71 // async-signal safe way. |
| 49 void PrintSyscallError(uint32_t sysno) { | 72 void PrintSyscallError(uint32_t sysno) { |
| 50 if (sysno >= 1024) | 73 if (sysno >= 1024) |
| 51 sysno = 0; | 74 sysno = 0; |
| 75 | |
| 52 // TODO(markus): replace with async-signal safe snprintf when available. | 76 // TODO(markus): replace with async-signal safe snprintf when available. |
| 53 const size_t kNumDigits = 4; | 77 const size_t kNumDigits = 4; |
| 54 char sysno_base10[kNumDigits]; | 78 char sysno_base10[kNumDigits]; |
| 55 uint32_t rem = sysno; | 79 uint32_t rem = sysno; |
| 56 uint32_t mod = 0; | 80 uint32_t mod = 0; |
| 57 for (int i = kNumDigits - 1; i >= 0; i--) { | 81 for (int i = kNumDigits - 1; i >= 0; i--) { |
| 58 mod = rem % 10; | 82 mod = rem % 10; |
| 59 rem /= 10; | 83 rem /= 10; |
| 60 sysno_base10[i] = '0' + mod; | 84 sysno_base10[i] = '0' + mod; |
| 61 } | 85 } |
| 86 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32) | |
| 87 static const char kSeccompErrorPrefix[] = | |
| 88 __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT | |
| 89 " in syscall 4000 + "; | |
| 90 #else | |
| 62 static const char kSeccompErrorPrefix[] = | 91 static const char kSeccompErrorPrefix[] = |
| 63 __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT " in syscall "; | 92 __FILE__":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT " in syscall "; |
| 93 #endif | |
| 64 static const char kSeccompErrorPostfix[] = "\n"; | 94 static const char kSeccompErrorPostfix[] = "\n"; |
| 65 WriteToStdErr(kSeccompErrorPrefix, sizeof(kSeccompErrorPrefix) - 1); | 95 WriteToStdErr(kSeccompErrorPrefix, sizeof(kSeccompErrorPrefix) - 1); |
| 66 WriteToStdErr(sysno_base10, sizeof(sysno_base10)); | 96 WriteToStdErr(sysno_base10, sizeof(sysno_base10)); |
| 67 WriteToStdErr(kSeccompErrorPostfix, sizeof(kSeccompErrorPostfix) - 1); | 97 WriteToStdErr(kSeccompErrorPostfix, sizeof(kSeccompErrorPostfix) - 1); |
| 68 } | 98 } |
| 69 | 99 |
| 70 } // namespace. | 100 } // namespace. |
| 71 | 101 |
| 72 namespace sandbox { | 102 namespace sandbox { |
| 73 | 103 |
| 74 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data& args, void* aux) { | 104 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data& args, void* aux) { |
| 75 uint32_t syscall = args.nr; | 105 uint32_t syscall = SyscallNumberToOffsetFromBase(args.nr); |
| 76 if (syscall >= 1024) | 106 |
| 77 syscall = 0; | |
| 78 PrintSyscallError(syscall); | 107 PrintSyscallError(syscall); |
| 79 | 108 |
| 80 // Encode 8-bits of the 1st two arguments too, so we can discern which socket | 109 // 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 | 110 // type, which fcntl, ... etc., without being likely to hit a mapped |
| 82 // address. | 111 // address. |
| 83 // Do not encode more bits here without thinking about increasing the | 112 // Do not encode more bits here without thinking about increasing the |
| 84 // likelihood of collision with mapped pages. | 113 // likelihood of collision with mapped pages. |
| 85 syscall |= ((args.args[0] & 0xffUL) << 12); | 114 syscall |= ((args.args[0] & 0xffUL) << 12); |
| 86 syscall |= ((args.args[1] & 0xffUL) << 20); | 115 syscall |= ((args.args[1] & 0xffUL) << 20); |
| 87 // Purposefully dereference the syscall as an address so it'll show up very | 116 // 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 | 207 |
| 179 const char* GetIoctlErrorMessageContentForTests() { | 208 const char* GetIoctlErrorMessageContentForTests() { |
| 180 return SECCOMP_MESSAGE_IOCTL_CONTENT; | 209 return SECCOMP_MESSAGE_IOCTL_CONTENT; |
| 181 } | 210 } |
| 182 | 211 |
| 183 const char* GetKillErrorMessageContentForTests() { | 212 const char* GetKillErrorMessageContentForTests() { |
| 184 return SECCOMP_MESSAGE_KILL_CONTENT; | 213 return SECCOMP_MESSAGE_KILL_CONTENT; |
| 185 } | 214 } |
| 186 | 215 |
| 187 } // namespace sandbox. | 216 } // namespace sandbox. |
| OLD | NEW |