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.
5 // Note: any code in this file MUST be async-signal safe.
7 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h"
9 #include <sys/syscall.h>
12 #include "base/logging.h"
13 #include "base/posix/eintr_wrapper.h"
14 #include "build/build_config.h"
15 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
16 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
17 #include "sandbox/linux/seccomp-bpf/syscall.h"
18 #include "sandbox/linux/services/syscall_wrappers.h"
19 #include "sandbox/linux/system_headers/linux_syscalls.h"
22 // __NR_Linux, is defined in <asm/unistd.h>.
23 #include <asm/unistd.h>
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"
35 inline bool IsArchitectureX86_64() {
36 #if defined(__x86_64__)
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
) {
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;
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
) {
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
69 sysno
= sysno
- __NR_Linux
;
78 // Print a seccomp-bpf failure to handle |sysno| to stderr in an
79 // async-signal safe way.
80 void PrintSyscallError(uint32_t sysno
) {
83 // TODO(markus): replace with async-signal safe snprintf when available.
84 const size_t kNumDigits
= 4;
85 char sysno_base10
[kNumDigits
];
88 for (int i
= kNumDigits
- 1; i
>= 0; i
--) {
91 sysno_base10
[i
] = '0' + mod
;
93 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32)
94 static const char kSeccompErrorPrefix
[] = __FILE__
95 ":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT
" in syscall 4000 + ";
97 static const char kSeccompErrorPrefix
[] =
98 __FILE__
":**CRASHING**:" SECCOMP_MESSAGE_COMMON_CONTENT
" in syscall ";
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);
110 intptr_t CrashSIGSYS_Handler(const struct arch_seccomp_data
& args
, void* aux
) {
111 uint32_t syscall
= SyscallNumberToOffsetFromBase(args
.nr
);
113 PrintSyscallError(syscall
);
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
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
);
126 // In case we hit a mapped address, hit the null page with just the syscall,
129 addr
= reinterpret_cast<volatile char*>(syscall
);
135 // TODO(jln): refactor the reporting functions.
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];
145 if (IsArchitectureX86_64()) {
146 addr
= reinterpret_cast<volatile char*>(clone_flags
& 0xFFFFFF);
149 // Hit the NULL page if this fails to fault.
150 addr
= reinterpret_cast<volatile char*>(clone_flags
& 0xFFF);
156 intptr_t SIGSYSPrctlFailure(const struct arch_seccomp_data
& args
,
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);
170 intptr_t SIGSYSIoctlFailure(const struct arch_seccomp_data
& args
,
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);
179 // Hit the NULL page if this fails.
180 addr
= reinterpret_cast<volatile char*>(request
& 0xFFF);
186 intptr_t SIGSYSKillFailure(const struct arch_seccomp_data
& args
,
188 static const char kSeccompKillError
[] =
189 __FILE__
":**CRASHING**:" SECCOMP_MESSAGE_KILL_CONTENT
"\n";
190 WriteToStdErr(kSeccompKillError
, sizeof(kSeccompKillError
) - 1);
191 // Make "pid" volatile so that we can see it on the stack in a minidump.
192 volatile uint64_t my_pid
= sys_getpid();
193 volatile char* addr
= reinterpret_cast<volatile char*>(my_pid
& 0xFFF);
199 intptr_t SIGSYSFutexFailure(const struct arch_seccomp_data
& args
,
201 static const char kSeccompFutexError
[] =
202 __FILE__
":**CRASHING**:" SECCOMP_MESSAGE_FUTEX_CONTENT
"\n";
203 WriteToStdErr(kSeccompFutexError
, sizeof(kSeccompFutexError
) - 1);
204 volatile int futex_op
= args
.args
[1];
205 volatile char* addr
= reinterpret_cast<volatile char*>(futex_op
& 0xFFF);
211 intptr_t SIGSYSSchedHandler(const struct arch_seccomp_data
& args
,
214 case __NR_sched_getaffinity
:
215 case __NR_sched_getattr
:
216 case __NR_sched_getparam
:
217 case __NR_sched_getscheduler
:
218 case __NR_sched_rr_get_interval
:
219 case __NR_sched_setaffinity
:
220 case __NR_sched_setattr
:
221 case __NR_sched_setparam
:
222 case __NR_sched_setscheduler
:
223 const pid_t tid
= sys_gettid();
224 // The first argument is the pid. If is our thread id, then replace it
225 // with 0, which is equivalent and allowed by the policy.
226 if (args
.args
[0] == static_cast<uint64_t>(tid
)) {
227 return Syscall::Call(args
.nr
,
229 static_cast<intptr_t>(args
.args
[1]),
230 static_cast<intptr_t>(args
.args
[2]),
231 static_cast<intptr_t>(args
.args
[3]),
232 static_cast<intptr_t>(args
.args
[4]),
233 static_cast<intptr_t>(args
.args
[5]));
238 CrashSIGSYS_Handler(args
, aux
);
240 // Should never be reached.
245 bpf_dsl::ResultExpr
CrashSIGSYS() {
246 return bpf_dsl::Trap(CrashSIGSYS_Handler
, NULL
);
249 bpf_dsl::ResultExpr
CrashSIGSYSClone() {
250 return bpf_dsl::Trap(SIGSYSCloneFailure
, NULL
);
253 bpf_dsl::ResultExpr
CrashSIGSYSPrctl() {
254 return bpf_dsl::Trap(SIGSYSPrctlFailure
, NULL
);
257 bpf_dsl::ResultExpr
CrashSIGSYSIoctl() {
258 return bpf_dsl::Trap(SIGSYSIoctlFailure
, NULL
);
261 bpf_dsl::ResultExpr
CrashSIGSYSKill() {
262 return bpf_dsl::Trap(SIGSYSKillFailure
, NULL
);
265 bpf_dsl::ResultExpr
CrashSIGSYSFutex() {
266 return bpf_dsl::Trap(SIGSYSFutexFailure
, NULL
);
269 bpf_dsl::ResultExpr
RewriteSchedSIGSYS() {
270 return bpf_dsl::Trap(SIGSYSSchedHandler
, NULL
);
273 const char* GetErrorMessageContentForTests() {
274 return SECCOMP_MESSAGE_COMMON_CONTENT
;
277 const char* GetCloneErrorMessageContentForTests() {
278 return SECCOMP_MESSAGE_CLONE_CONTENT
;
281 const char* GetPrctlErrorMessageContentForTests() {
282 return SECCOMP_MESSAGE_PRCTL_CONTENT
;
285 const char* GetIoctlErrorMessageContentForTests() {
286 return SECCOMP_MESSAGE_IOCTL_CONTENT
;
289 const char* GetKillErrorMessageContentForTests() {
290 return SECCOMP_MESSAGE_KILL_CONTENT
;
293 const char* GetFutexErrorMessageContentForTests() {
294 return SECCOMP_MESSAGE_FUTEX_CONTENT
;
297 } // namespace sandbox.