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
3 // found in the LICENSE file.
5 #include "sandbox/linux/seccomp-bpf/syscall.h"
10 #include "base/logging.h"
11 #include "sandbox/linux/bpf_dsl/seccomp_macros.h"
17 #if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) || \
18 defined(ARCH_CPU_MIPS_FAMILY)
19 // Number that's not currently used by any Linux kernel ABIs.
20 const int kInvalidSyscallNumber
= 0x351d3;
22 #error Unrecognized architecture
25 asm(// We need to be able to tell the kernel exactly where we made a
26 // system call. The C++ compiler likes to sometimes clone or
27 // inline code, which would inadvertently end up duplicating
29 // "gcc" can suppress code duplication with suitable function
30 // attributes, but "clang" doesn't have this ability.
31 // The "clang" developer mailing list suggested that the correct
32 // and portable solution is a file-scope assembly block.
33 // N.B. We do mark our code as a proper function so that backtraces
34 // work correctly. But we make absolutely no attempt to use the
35 // ABI's calling conventions for passing arguments. We will only
36 // ever be called from assembly code and thus can pick more
37 // suitable calling conventions.
41 ".type SyscallAsm, @function\n"
42 "SyscallAsm:.cfi_startproc\n"
43 // Check if "%eax" is negative. If so, do not attempt to make a
44 // system call. Instead, compute the return address that is visible
45 // to the kernel after we execute "int $0x80". This address can be
46 // used as a marker that BPF code inspects.
49 // Always, make sure that our code is position-independent, or
50 // address space randomization might not work on i386. This means,
51 // we can't use "lea", but instead have to rely on "call/pop".
52 "call 0f; .cfi_adjust_cfa_offset 4\n"
53 "0:pop %eax; .cfi_adjust_cfa_offset -4\n"
56 // Save register that we don't want to clobber. On i386, we need to
57 // save relatively aggressively, as there are a couple or registers
58 // that are used internally (e.g. %ebx for position-independent
59 // code, and %ebp for the frame pointer), and as we need to keep at
60 // least a few registers available for the register allocator.
61 "1:push %esi; .cfi_adjust_cfa_offset 4; .cfi_rel_offset esi, 0\n"
62 "push %edi; .cfi_adjust_cfa_offset 4; .cfi_rel_offset edi, 0\n"
63 "push %ebx; .cfi_adjust_cfa_offset 4; .cfi_rel_offset ebx, 0\n"
64 "push %ebp; .cfi_adjust_cfa_offset 4; .cfi_rel_offset ebp, 0\n"
65 // Copy entries from the array holding the arguments into the
66 // correct CPU registers.
67 "movl 0(%edi), %ebx\n"
68 "movl 4(%edi), %ecx\n"
69 "movl 8(%edi), %edx\n"
70 "movl 12(%edi), %esi\n"
71 "movl 20(%edi), %ebp\n"
72 "movl 16(%edi), %edi\n"
75 // This is our "magic" return address that the BPF filter sees.
77 // Restore any clobbered registers that we didn't declare to the
79 "pop %ebp; .cfi_restore ebp; .cfi_adjust_cfa_offset -4\n"
80 "pop %ebx; .cfi_restore ebx; .cfi_adjust_cfa_offset -4\n"
81 "pop %edi; .cfi_restore edi; .cfi_adjust_cfa_offset -4\n"
82 "pop %esi; .cfi_restore esi; .cfi_adjust_cfa_offset -4\n"
85 "9:.size SyscallAsm, 9b-SyscallAsm\n"
86 #elif defined(__x86_64__)
89 ".type SyscallAsm, @function\n"
90 "SyscallAsm:.cfi_startproc\n"
91 // Check if "%rdi" is negative. If so, do not attempt to make a
92 // system call. Instead, compute the return address that is visible
93 // to the kernel after we execute "syscall". This address can be
94 // used as a marker that BPF code inspects.
97 // Always make sure that our code is position-independent, or the
98 // linker will throw a hissy fit on x86-64.
99 "lea 2f(%rip), %rax\n"
101 // Now we load the registers used to pass arguments to the system
102 // call: system call number in %rax, and arguments in %rdi, %rsi,
103 // %rdx, %r10, %r8, %r9. Note: These are all caller-save registers
104 // (only %rbx, %rbp, %rsp, and %r12-%r15 are callee-save), so no
105 // need to worry here about spilling registers or CFI directives.
106 "1:movq %rdi, %rax\n"
107 "movq 0(%rsi), %rdi\n"
108 "movq 16(%rsi), %rdx\n"
109 "movq 24(%rsi), %r10\n"
110 "movq 32(%rsi), %r8\n"
111 "movq 40(%rsi), %r9\n"
112 "movq 8(%rsi), %rsi\n"
115 // This is our "magic" return address that the BPF filter sees.
118 "9:.size SyscallAsm, 9b-SyscallAsm\n"
119 #elif defined(__arm__)
120 // Throughout this file, we use the same mode (ARM vs. thumb)
121 // that the C++ compiler uses. This means, when transfering control
122 // from C++ to assembly code, we do not need to switch modes (e.g.
123 // by using the "bx" instruction). It also means that our assembly
124 // code should not be invoked directly from code that lives in
125 // other compilation units, as we don't bother implementing thumb
126 // interworking. That's OK, as we don't make any of the assembly
127 // symbols public. They are all local to this file.
130 ".type SyscallAsm, %function\n"
131 #if defined(__thumb__)
136 "SyscallAsm:.fnstart\n"
137 "@ args = 0, pretend = 0, frame = 8\n"
138 "@ frame_needed = 1, uses_anonymous_args = 0\n"
139 #if defined(__thumb__)
142 ".cfi_offset 14, -4\n"
143 ".cfi_offset 7, -8\n"
145 ".cfi_def_cfa_register 7\n"
146 ".cfi_def_cfa_offset 8\n"
148 "stmfd sp!, {fp, lr}\n"
151 // Check if "r0" is negative. If so, do not attempt to make a
152 // system call. Instead, compute the return address that is visible
153 // to the kernel after we execute "swi 0". This address can be
154 // used as a marker that BPF code inspects.
159 // We declared (almost) all clobbered registers to the compiler. On
160 // ARM there is no particular register pressure. So, we can go
161 // ahead and directly copy the entries from the arguments array
162 // into the appropriate CPU registers.
163 "1:ldr r5, [r6, #20]\n"
164 "ldr r4, [r6, #16]\n"
165 "ldr r3, [r6, #12]\n"
172 // Restore the frame pointer. Also restore the program counter from
173 // the link register; this makes us return to the caller.
174 #if defined(__thumb__)
178 "2:ldmfd sp!, {fp, pc}\n"
181 "9:.size SyscallAsm, 9b-SyscallAsm\n"
182 #elif defined(__mips__)
185 ".type SyscallAsm, @function\n"
186 "SyscallAsm:.ent SyscallAsm\n"
187 ".frame $sp, 40, $ra\n"
190 "addiu $sp, $sp, -40\n"
192 // Check if "v0" is negative. If so, do not attempt to make a
193 // system call. Instead, compute the return address that is visible
194 // to the kernel after we execute "syscall". This address can be
195 // used as a marker that BPF code inspects.
201 // On MIPS first four arguments go to registers a0 - a3 and any
202 // argument after that goes to stack. We can go ahead and directly
203 // copy the entries from the arguments array into the appropriate
204 // CPU registers and on the stack.
205 "1:lw $a3, 28($a0)\n"
219 // This is our "magic" return address that the BPF filter sees.
220 // Restore the return address from the stack.
221 "2:lw $ra, 36($sp)\n"
223 " addiu $sp, $sp, 40\n"
226 ".size SyscallAsm,.-SyscallAsm\n"
227 #elif defined(__aarch64__)
230 ".type SyscallAsm, %function\n"
237 "1:ldr x5, [x6, #40]\n"
238 "ldr x4, [x6, #32]\n"
239 "ldr x3, [x6, #24]\n"
240 "ldr x2, [x6, #16]\n"
248 ".size SyscallAsm, .-SyscallAsm\n"
252 #if defined(__x86_64__)
254 intptr_t SyscallAsm(intptr_t nr
, const intptr_t args
[6]);
260 intptr_t Syscall::InvalidCall() {
261 // Explicitly pass eight zero arguments just in case.
262 return Call(kInvalidSyscallNumber
, 0, 0, 0, 0, 0, 0, 0, 0);
265 intptr_t Syscall::Call(int nr
,
274 // We rely on "intptr_t" to be the exact size as a "void *". This is
275 // typically true, but just in case, we add a check. The language
276 // specification allows platforms some leeway in cases, where
277 // "sizeof(void *)" is not the same as "sizeof(void (*)())". We expect
278 // that this would only be an issue for IA64, which we are currently not
279 // planning on supporting. And it is even possible that this would work
280 // on IA64, but for lack of actual hardware, I cannot test.
281 static_assert(sizeof(void*) == sizeof(intptr_t),
282 "pointer types and intptr_t must be exactly the same size");
284 // TODO(nedeljko): Enable use of more than six parameters on architectures
285 // where that makes sense.
286 #if defined(__mips__)
287 const intptr_t args
[8] = {p0
, p1
, p2
, p3
, p4
, p5
, p6
, p7
};
289 DCHECK_EQ(p6
, 0) << " Support for syscalls with more than six arguments not "
290 "added for this architecture";
291 DCHECK_EQ(p7
, 0) << " Support for syscalls with more than six arguments not "
292 "added for this architecture";
293 const intptr_t args
[6] = {p0
, p1
, p2
, p3
, p4
, p5
};
294 #endif // defined(__mips__)
296 // Invoke our file-scope assembly code. The constraints have been picked
297 // carefully to match what the rest of the assembly code expects in input,
298 // output, and clobbered registers.
299 #if defined(__i386__)
303 // N.B. These are not the calling conventions normally used by the ABI.
305 : "0"(ret
), "D"(args
)
306 : "cc", "esp", "memory", "ecx", "edx");
307 #elif defined(__x86_64__)
308 intptr_t ret
= SyscallAsm(nr
, args
);
309 #elif defined(__arm__)
312 register intptr_t inout
__asm__("r0") = nr
;
313 register const intptr_t* data
__asm__("r6") = args
;
316 // N.B. These are not the calling conventions normally used by the ABI.
318 : "0"(inout
), "r"(data
)
327 #if !defined(__thumb__)
328 // In thumb mode, we cannot use "r7" as a general purpose register, as
329 // it is our frame pointer. We have to manually manage and preserve
331 // In ARM mode, we have a dedicated frame pointer register and "r7" is
332 // thus available as a general purpose register. We don't preserve it,
333 // but instead mark it as clobbered.
336 #endif // !defined(__thumb__)
340 #elif defined(__mips__)
342 intptr_t ret
= Syscall::SandboxSyscallRaw(nr
, args
, &err_status
);
345 // On error, MIPS returns errno from syscall instead of -errno.
346 // The purpose of this negation is for SandboxSyscall() to behave
347 // more like it would on other architectures.
350 #elif defined(__aarch64__)
353 register intptr_t inout
__asm__("x0") = nr
;
354 register const intptr_t* data
__asm__("x6") = args
;
355 asm volatile("bl SyscallAsm\n"
357 : "0"(inout
), "r"(data
)
358 : "memory", "x1", "x2", "x3", "x4", "x5", "x8", "x30");
363 #error "Unimplemented architecture"
368 void Syscall::PutValueInUcontext(intptr_t ret_val
, ucontext_t
* ctx
) {
369 #if defined(__mips__)
370 // Mips ABI states that on error a3 CPU register has non zero value and if
371 // there is no error, it should be zero.
372 if (ret_val
<= -1 && ret_val
>= -4095) {
373 // |ret_val| followes the Syscall::Call() convention of being -errno on
374 // errors. In order to write correct value to return register this sign
375 // needs to be changed back.
377 SECCOMP_PARM4(ctx
) = 1;
379 SECCOMP_PARM4(ctx
) = 0;
381 SECCOMP_RESULT(ctx
) = static_cast<greg_t
>(ret_val
);
384 #if defined(__mips__)
385 intptr_t Syscall::SandboxSyscallRaw(int nr
,
386 const intptr_t* args
,
388 register intptr_t ret
__asm__("v0") = nr
;
389 // a3 register becomes non zero on error.
390 register intptr_t err_stat
__asm__("a3") = 0;
392 register const intptr_t* data
__asm__("a0") = args
;
394 "la $t9, SyscallAsm\n"
397 : "=r"(ret
), "=r"(err_stat
)
400 // a2 is in the clober list so inline assembly can not change its
402 : "memory", "ra", "t9", "a2");
405 // Set an error status so it can be used outside of this function
410 #endif // defined(__mips__)
412 } // namespace sandbox