Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: sandbox/linux/seccomp-bpf/syscall.cc

Issue 11363212: Added support for greylisting of system calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now with more meat Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <asm/unistd.h>
8
9 #include "sandbox/linux/seccomp-bpf/syscall.h"
10
11
12 namespace playground2 {
13
14 asm( // We need to be able to tell the kernel exactly where we made a
15 // system call. The C++ compiler likes to sometimes clone or
16 // inline code, which would inadvertently end up duplicating
17 // the entry point.
18 // "gcc" can suppress code duplication with suitable function
19 // attributes, but "clang" doesn't have this ability.
20 // The "clang" developer mailing list suggested that the correct
21 // and portable solution is a file-scope assembly block.
22 // N.B. We do mark our code as a proper function so that backtraces
23 // work correctly. But we make absolutely no attempt to use the
24 // ABI's calling conventions for passing arguments. We will only
25 // ever be called from assembly code and thus can pick more
26 // suitable calling conventions.
27 #if defined(__i386__)
28 ".text\n"
29 ".align 16, 0x90\n"
30 ".type SyscallAsm, @function\n"
31 "SyscallAsm:.cfi_startproc\n"
32 // Check if "%eax" is negative. If so, do not attempt to make a
33 // system call. Instead, compute the return address that is visible
34 // to the kernel after we execute "int $0x80". This address can be
35 // used as a marker that BPF code inspects.
36 "test %eax, %eax\n"
37 "jge 1f\n"
38 // Always, make sure that our code is position-independent, or
39 // address space randomization might not work on i386. This means,
40 // we can't use "lea", but instead have to rely on "call/pop".
41 "call 0f; .cfi_adjust_cfa_offset 4\n"
42 "0:pop %eax; .cfi_adjust_cfa_offset -4\n"
43 "addl $2f-0b, %eax\n"
44 "ret\n"
45 // Save register that we don't want to clobber. On i386, we need to
46 // save relatively aggressively, as there are a couple or registers
47 // that are used internally (e.g. %ebx for position-independent
48 // code, and %ebp for the frame pointer), and as we need to keep at
49 // least a few registers available for the register allocator.
50 "1:push %esi; .cfi_adjust_cfa_offset 4\n"
51 "push %edi; .cfi_adjust_cfa_offset 4\n"
52 "push %ebx; .cfi_adjust_cfa_offset 4\n"
53 "push %ebp; .cfi_adjust_cfa_offset 4\n"
54 // Copy entries from the array holding the arguments into the
55 // correct CPU registers.
56 "movl 0(%edi), %ebx\n"
57 "movl 4(%edi), %ecx\n"
58 "movl 8(%edi), %edx\n"
59 "movl 12(%edi), %esi\n"
60 "movl 20(%edi), %ebp\n"
61 // Enter the kernel.
62 "int $0x80\n"
63 // This is our "magic" return address that the BPF filter sees.
64 "2:"
65 // Restore any clobbered registers that we didn't declare to the
66 // compiler.
67 "pop %ebp; .cfi_adjust_cfa_offset -4\n"
68 "pop %ebx; .cfi_adjust_cfa_offset -4\n"
69 "pop %edi; .cfi_adjust_cfa_offset -4\n"
70 "pop %esi; .cfi_adjust_cfa_offset -4\n"
71 "ret\n"
72 ".cfi_endproc\n"
73 "9:.size SyscallAsm, 9b-SyscallAsm\n"
74 #elif defined(__x86_64__)
75 ".text\n"
76 ".align 16, 0x90\n"
77 ".type SyscallAsm, @function\n"
78 "SyscallAsm:.cfi_startproc\n"
79 // Check if "%rax" is negative. If so, do not attempt to make a
80 // system call. Instead, compute the return address that is visible
81 // to the kernel after we execute "syscall". This address can be
82 // used as a marker that BPF code inspects.
83 "test %rax, %rax\n"
84 "jge 1f\n"
85 // Always make sure that our code is position-independent, or the
86 // linker will throw a hissy fit on x86-64.
87 "call 0f; .cfi_adjust_cfa_offset 8\n"
88 "0:pop %rax; .cfi_adjust_cfa_offset -8\n"
89 "addq $2f-0b, %rax\n"
90 "ret\n"
91 // We declared all clobbered registers to the compiler. On x86-64,
92 // there really isn't much of a problem with register pressure. So,
93 // we can go ahead and directly copy the entries from the arguments
94 // array into the appropriate CPU registers.
95 "1:movq 0(%r12), %rdi\n"
96 "movq 8(%r12), %rsi\n"
97 "movq 16(%r12), %rdx\n"
98 "movq 24(%r12), %r10\n"
99 "movq 32(%r12), %r8\n"
100 "movq 40(%r12), %r9\n"
101 // Enter the kernel.
102 "syscall\n"
103 // This is our "magic" return address that the BPF filter sees.
104 "2:ret\n"
105 ".cfi_endproc\n"
106 "9:.size SyscallAsm, 9b-SyscallAsm\n"
107 #elif defined(__arm__)
108 // Throughout this file, we use the same mode (ARM vs. thumb)
109 // that the C++ compiler uses. This means, when transfering control
110 // from C++ to assembly code, we do not need to switch modes (e.g.
111 // by using the "bx" instruction). It also means that our assembly
112 // code should not be invoked directly from code that lives in
113 // other compilation units, as we don't bother implementing thumb
114 // interworking. That's OK, as we don't make any of the assembly
115 // symbols public. They are all local to this file.
116 ".text\n"
117 ".align 2\n"
118 ".type SyscallAsm, %function\n"
119 #if defined(__thumb__)
120 ".thumb_func\n"
121 #else
122 ".arm\n"
123 #endif
124 "SyscallAsm:.fnstart\n"
125 "@ args = 0, pretend = 0, frame = 8\n"
126 "@ frame_needed = 1, uses_anonymous_args = 0\n"
127 #if defined(__thumb__)
128 ".cfi_startproc\n"
129 "push {r7, lr}\n"
130 ".cfi_offset 14, -4\n"
131 ".cfi_offset 7, -8\n"
132 "mov r7, sp\n"
133 ".cfi_def_cfa_register 7\n"
134 ".cfi_def_cfa_offset 8\n"
135 #else
136 "stmfd sp!, {fp, lr}\n"
137 "add fp, sp, #4\n"
138 #endif
139 // Check if "r0" is negative. If so, do not attempt to make a
140 // system call. Instead, compute the return address that is visible
141 // to the kernel after we execute "swi 0". This address can be
142 // used as a marker that BPF code inspects.
143 "cmp r0, #0\n"
144 "bge 1f\n"
145 "ldr r0, =2f\n"
146 "b 2f\n"
147 // We declared (almost) all clobbered registers to the compiler. On
148 // ARM there is no particular register pressure. So, we can go
149 // ahead and directly copy the entries from the arguments array
150 // into the appropriate CPU registers.
151 "1:ldr r5, [r6, #20]\n"
152 "ldr r4, [r6, #16]\n"
153 "ldr r3, [r6, #12]\n"
154 "ldr r2, [r6, #8]\n"
155 "ldr r1, [r6, #4]\n"
156 "mov r7, r0\n"
157 "ldr r0, [r6, #0]\n"
158 // Enter the kernel
159 "swi 0\n"
160 // Restore the frame pointer. Also restore the program counter from
161 // the link register; this makes us return to the caller.
162 #if defined(__thumb__)
163 "2:pop {r7, pc}\n"
164 ".cfi_endproc\n"
165 #else
166 "2:ldmfd sp!, {fp, pc}\n"
167 #endif
168 ".fnend\n"
169 "9:.size SyscallAsm, 9b-SyscallAsm\n"
170 #else
171 #error Need to port some assembly code before this code will work
172 #endif
173 ); // asm
174
175 intptr_t Syscall(int nr, ...) {
176 // It is most convenient for the caller to pass a variadic list of arguments.
177 // But this is difficult to handle in assembly code without making
178 // assumptions about internal implementation details of "va_list". So, we
179 // first use C code to copy all the arguments into an array, where they are
180 // easily accessible to asm().
181 // This is preferable over copying them into individual variables, which
182 // can result in too much register pressure.
183 void *args[6];
184 va_list ap;
185
186 // System calls take a system call number (typically passed in %eax or
187 // %rax) and up to six arguments (passed in general-purpose CPU registers).
188 //
189 // On 32bit systems, all variadic arguments are passed on the stack as 32bit
190 // quantities. We can use an arbitrary 32bit type to retrieve them with
191 // va_arg() and then forward them to the kernel in the appropriate CPU
192 // register. We do not need to know whether this is an integer or a pointer
193 // value.
194 //
195 // On 64bit systems, variadic arguments can be either 32bit or 64bit wide,
196 // which would seem to make it more important that we pass the correct type
197 // to va_arg(). And we really can't know what this type is unless we have a
198 // table with function signatures for all system calls.
199 //
200 // Fortunately, on x86-64 this is less critical. The first six function
201 // arguments will be passed in CPU registers, no matter whether they were
202 // named or variadic. This only leaves us with a single argument (if present)
203 // that could be passed on the stack. And since x86-64 is little endian,
204 // it will have the correct value both for 32bit and 64bit quantities.
205 //
206 // N.B. Because of how the x86-64 ABI works, it is possible that 32bit
207 // quantities will have undefined garbage bits in the upper 32 bits of a
208 // 64bit register. This is relatively unlikely for the first five system
209 // call arguments, as the processor does automatic sign extensions and zero
210 // filling so frequently, there rarely is garbage in CPU registers. But it
211 // is quite likely for the last argument, which is passed on the stack.
212 // That's generally OK, because the kernel has the correct function
213 // signatures and knows to only inspect the LSB of a 32bit value.
214 // But callers must be careful in cases, where the compiler cannot tell
215 // the difference (e.g. when passing NULL to any system call, it must
216 // always be cast to a pointer type).
217 // The glibc implementation of syscall() has the exact same issues.
218 // In the unlikely event that this ever becomes a problem, we could add
219 // code that handles six-argument system calls specially. The number of
220 // system calls that take six arguments and expect a 32bit value in the
221 // sixth argument is very limited.
222 va_start(ap, nr);
223 args[0] = va_arg(ap, void *);
224 args[1] = va_arg(ap, void *);
225 args[2] = va_arg(ap, void *);
226 args[3] = va_arg(ap, void *);
227 args[4] = va_arg(ap, void *);
228 args[5] = va_arg(ap, void *);
229 va_end(ap);
230
231 // Invoke our file-scope assembly code. The constraints have been picked
232 // carefully to match what the rest of the assembly code expects in input,
233 // output, and clobbered registers.
234 #if defined(__i386__)
235 intptr_t ret = nr;
236 asm volatile(
237 "call SyscallAsm\n"
238 // N.B. These are not the calling conventions normally used by the ABI.
239 : "=a"(ret)
240 : "0"(ret), "D"(args)
241 : "esp", "memory", "ecx", "edx");
242 #elif defined(__x86_64__)
243 intptr_t ret = nr;
244 {
245 register void **data __asm__("r12") = args;
246 asm volatile(
247 "call SyscallAsm\n"
248 // N.B. These are not the calling conventions normally used by the ABI.
249 : "=a"(ret)
250 : "0"(ret), "r"(data)
251 : "rsp", "memory",
252 "rcx", "rdi", "rsi", "rdx", "r8", "r9", "r10", "r11");
253 }
254 #elif defined(__arm__)
255 intptr_t ret;
256 {
257 register intptr_t inout __asm__("r0") = nr;
258 register void **data __asm__("r6") = args;
259 asm volatile(
260 "bl SyscallAsm\n"
261 // N.B. These are not the calling conventions normally used by the ABI.
262 : "=r"(inout)
263 : "0"(inout), "r"(data)
264 : "lr", "memory", "r1", "r2", "r3", "r4", "r5"
265 #if !defined(__arm__)
266 // In thumb mode, we cannot use "r7" as a general purpose register, as
267 // it is our frame pointer. We have to manually manage and preserve it.
268 // In ARM mode, we have a dedicated frame pointer register and "r7" is
269 // thus available as a general purpose register. We don't preserve it,
270 // but instead mark it as clobbered.
271 , "r7"
272 #endif
273 );
274 ret = inout;
275 }
276 #else
277 #error Need to port some assembly code before this code will work
278 #endif
279 return ret;
280 }
281
282 } // namespace
OLDNEW
« sandbox/linux/seccomp-bpf/sandbox_bpf.h ('K') | « sandbox/linux/seccomp-bpf/syscall.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698