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

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

Issue 1739011: Added support for sigreturn() and rt_sigreturn(). On x86-32, this is... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 7 months 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
« no previous file with comments | « sandbox/linux/seccomp/sandbox_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "debug.h" 5 #include "debug.h"
6 #include "sandbox_impl.h" 6 #include "sandbox_impl.h"
7 #include "syscall_table.h" 7 #include "syscall_table.h"
8 8
9 namespace playground { 9 namespace playground {
10 10
(...skipping 28 matching lines...) Expand all
39 ".size playground$sandbox_clone, .-playground$sandbox_clone\n" 39 ".size playground$sandbox_clone, .-playground$sandbox_clone\n"
40 40
41 41
42 // This is the wrapper which is called by the untrusted code, trying to 42 // This is the wrapper which is called by the untrusted code, trying to
43 // make a system call. 43 // make a system call.
44 "playground$syscallWrapper:" 44 "playground$syscallWrapper:"
45 ".internal playground$syscallWrapper\n" 45 ".internal playground$syscallWrapper\n"
46 ".globl playground$syscallWrapper\n" 46 ".globl playground$syscallWrapper\n"
47 ".type playground$syscallWrapper, @function\n" 47 ".type playground$syscallWrapper, @function\n"
48 #if defined(__x86_64__) 48 #if defined(__x86_64__)
49 // Check for rt_sigreturn(). It needs to be handled specially.
50 "cmp $15, %rax\n" // NR_rt_sigreturn
51 "jnz 1f\n"
52 "add $0x90, %rsp\n" // pop return addresses and red zone
53 "0:syscall\n" // rt_sigreturn() is unrestricted
54 "mov $66, %edi\n" // rt_sigreturn() should never return
55 "mov $231, %eax\n" // NR_exit_group
56 "jmp 0b\n"
57
49 // Save all registers 58 // Save all registers
50 "push %rbp\n" 59 "1:push %rbp\n"
51 "mov %rsp, %rbp\n" 60 "mov %rsp, %rbp\n"
52 "push %rbx\n" 61 "push %rbx\n"
53 "push %rcx\n" 62 "push %rcx\n"
54 "push %rdx\n" 63 "push %rdx\n"
55 "push %rsi\n" 64 "push %rsi\n"
56 "push %rdi\n" 65 "push %rdi\n"
57 "push %r8\n" 66 "push %r8\n"
58 "push %r9\n" 67 "push %r9\n"
59 "push %r10\n" 68 "push %r10\n"
60 "push %r11\n" 69 "push %r11\n"
61 "push %r12\n" 70 "push %r12\n"
62 "push %r13\n" 71 "push %r13\n"
63 "push %r14\n" 72 "push %r14\n"
64 "push %r15\n" 73 "push %r15\n"
65 74
66 // Convert from syscall calling conventions to C calling conventions. 75 // Convert from syscall calling conventions to C calling conventions.
67 // System calls have a subtly different register ordering than the user- 76 // System calls have a subtly different register ordering than the user-
68 // space x86-64 ABI. 77 // space x86-64 ABI.
69 "mov %r10, %rcx\n" 78 "mov %r10, %rcx\n"
70 79
71 // Check range of system call 80 // Check range of system call
72 "cmp playground$maxSyscall(%rip), %eax\n" 81 "cmp playground$maxSyscall(%rip), %eax\n"
73 "ja 1f\n" 82 "ja 3f\n"
74 83
75 // Retrieve function call from system call table (c.f. syscall_table.c). 84 // Retrieve function call from system call table (c.f. syscall_table.c).
76 // We have three different types of entries; zero for denied system calls, 85 // We have three different types of entries; zero for denied system calls,
77 // that should be handled by the defaultSystemCallHandler(); minus one 86 // that should be handled by the defaultSystemCallHandler(); minus one
78 // for unrestricted system calls that need to be forwarded to the trusted 87 // for unrestricted system calls that need to be forwarded to the trusted
79 // thread; and function pointers to specific handler functions. 88 // thread; and function pointers to specific handler functions.
80 "mov %rax, %r10\n" 89 "mov %rax, %r10\n"
81 "shl $4, %r10\n" 90 "shl $4, %r10\n"
82 "lea playground$syscallTable(%rip), %r11\n" 91 "lea playground$syscallTable(%rip), %r11\n"
83 "add %r11, %r10\n" 92 "add %r11, %r10\n"
84 "mov 0(%r10), %r10\n" 93 "mov 0(%r10), %r10\n"
85 94
86 // Jump to function if non-null and not UNRESTRICTED_SYSCALL, otherwise 95 // Jump to function if non-null and not UNRESTRICTED_SYSCALL, otherwise
87 // jump to fallback handler. 96 // jump to fallback handler.
88 "cmp $1, %r10\n" 97 "cmp $1, %r10\n"
89 "jbe 1f\n" 98 "jbe 3f\n"
90 "call *%r10\n" 99 "call *%r10\n"
91 "0:" 100 "2:"
92 101
93 // Restore CPU registers, except for %rax which was set by the system call. 102 // Restore CPU registers, except for %rax which was set by the system call.
94 "pop %r15\n" 103 "pop %r15\n"
95 "pop %r14\n" 104 "pop %r14\n"
96 "pop %r13\n" 105 "pop %r13\n"
97 "pop %r12\n" 106 "pop %r12\n"
98 "pop %r11\n" 107 "pop %r11\n"
99 "pop %r10\n" 108 "pop %r10\n"
100 "pop %r9\n" 109 "pop %r9\n"
101 "pop %r8\n" 110 "pop %r8\n"
102 "pop %rdi\n" 111 "pop %rdi\n"
103 "pop %rsi\n" 112 "pop %rsi\n"
104 "pop %rdx\n" 113 "pop %rdx\n"
105 "pop %rcx\n" 114 "pop %rcx\n"
106 "pop %rbx\n" 115 "pop %rbx\n"
107 "pop %rbp\n" 116 "pop %rbp\n"
108 117
109 // Remove fake return address. This is added in the patching code in 118 // Remove fake return address. This is added in the patching code in
110 // library.cc and it makes stack traces a little cleaner. 119 // library.cc and it makes stack traces a little cleaner.
111 "add $8, %rsp\n" 120 "add $8, %rsp\n"
112 121
113 // Return to caller 122 // Return to caller
114 "ret\n" 123 "ret\n"
115 124
116 "1:" 125 "3:"
117 // If we end up calling a specific handler, we don't need to know the 126 // If we end up calling a specific handler, we don't need to know the
118 // system call number. However, in the generic case, we do. Shift 127 // system call number. However, in the generic case, we do. Shift
119 // registers so that the system call number becomes visible as the 128 // registers so that the system call number becomes visible as the
120 // first function argument. 129 // first function argument.
121 "push %r9\n" 130 "push %r9\n"
122 "mov %r8, %r9\n" 131 "mov %r8, %r9\n"
123 "mov %rcx, %r8\n" 132 "mov %rcx, %r8\n"
124 "mov %rdx, %rcx\n" 133 "mov %rdx, %rcx\n"
125 "mov %rsi, %rdx\n" 134 "mov %rsi, %rdx\n"
126 "mov %rdi, %rsi\n" 135 "mov %rdi, %rsi\n"
127 "mov %rax, %rdi\n" 136 "mov %rax, %rdi\n"
128 137
129 // Call default handler. 138 // Call default handler.
130 "call playground$defaultSystemCallHandler\n" 139 "call playground$defaultSystemCallHandler\n"
131 "pop %r9\n" 140 "pop %r9\n"
132 "jmp 0b\n" 141 "jmp 2b\n"
133 #elif defined(__i386__) 142 #elif defined(__i386__)
143 "cmp $119, %eax\n" // NR_sigreturn
144 "jnz 1f\n"
145 "add $0x4, %esp\n" // pop return address
146 "0:int $0x80\n" // sigreturn() is unrestricted
147 "mov $66, %ebx\n" // sigreturn() should never return
148 "mov %ebx, %eax\n" // NR_exit
149 "jmp 0b\n"
150 "1:cmp $173, %eax\n" // NR_rt_sigreturn
151 "jnz 3f\n"
152
153 // Convert rt_sigframe into sigframe, allowing us to call sigreturn().
154 // This is possible since the first part of signal stack frames have
155 // stayed very stable since the earliest kernel versions. While never
156 // officially documented, lots of user space applications rely on this
157 // part of the ABI, and kernel developers have been careful to maintain
158 // backwards compatibility.
159 // In general, the rt_sigframe includes a lot of extra information that
160 // the signal handler can look at. Most notably, this means a complete
161 // siginfo record.
162 // Fortunately though, the kernel doesn't look at any of this extra data
163 // when returning from a signal handler. So, we can safely convert an
164 // rt_sigframe to a legacy sigframe, discarding the extra data in the
165 // process. Interestingly, the legacy signal frame is actually larger than
166 // the rt signal frame, as it includes a lot more padding.
167 "sub $0x1C8, %esp\n" // a legacy signal stack is much larger
168 "mov 0x1CC(%esp), %eax\n" // push signal number
169 "push %eax\n"
170 "lea 0x270(%esp), %esi\n" // copy siginfo register values
171 "lea 0x4(%esp), %edi\n" // into new location
172 "mov $0x16, %ecx\n"
173 "cld\n"
174 "rep movsl\n"
175 "mov 0x2C8(%esp), %ebx\n" // copy first half of signal mask
176 "mov %ebx, 0x54(%esp)\n"
177 "lea 2f, %esi\n"
178 "push %esi\n" // push restorer function
179 "lea 0x2D4(%esp), %edi\n" // patch up retcode magic numbers
180 "movb $2, %cl\n"
181 "rep movsl\n"
182 "ret\n" // return to restorer function
183 "2:pop %eax\n" // remove dummy argument (signo)
184 "mov $119, %eax\n" // NR_sigaction
185 "int $0x80\n"
186
187
134 // Preserve all registers 188 // Preserve all registers
135 "push %ebx\n" 189 "3:push %ebx\n"
136 "push %ecx\n" 190 "push %ecx\n"
137 "push %edx\n" 191 "push %edx\n"
138 "push %esi\n" 192 "push %esi\n"
139 "push %edi\n" 193 "push %edi\n"
140 "push %ebp\n" 194 "push %ebp\n"
141 195
142 // Convert from syscall calling conventions to C calling conventions 196 // Convert from syscall calling conventions to C calling conventions
143 "push %ebp\n" 197 "push %ebp\n"
144 "push %edi\n" 198 "push %edi\n"
145 "push %esi\n" 199 "push %esi\n"
146 "push %edx\n" 200 "push %edx\n"
147 "push %ecx\n" 201 "push %ecx\n"
148 "push %ebx\n" 202 "push %ebx\n"
149 "push %eax\n" 203 "push %eax\n"
150 204
151 // Check range of system call 205 // Check range of system call
152 "cmp playground$maxSyscall, %eax\n" 206 "cmp playground$maxSyscall, %eax\n"
153 "ja 5f\n" 207 "ja 9f\n"
154 208
155 // We often have long sequences of calls to gettimeofday(). This is 209 // We often have long sequences of calls to gettimeofday(). This is
156 // needlessly expensive. Coalesce them into a single call. 210 // needlessly expensive. Coalesce them into a single call.
157 // 211 //
158 // We keep track of state in TLS storage that we can access through 212 // We keep track of state in TLS storage that we can access through
159 // the %fs segment register. See trusted_thread.cc for the exact 213 // the %fs segment register. See trusted_thread.cc for the exact
160 // memory layout. 214 // memory layout.
161 // 215 //
162 // TODO(markus): maybe, we should proactively call gettimeofday() and 216 // TODO(markus): maybe, we should proactively call gettimeofday() and
163 // clock_gettime(), whenever we talk to the trusted thread? 217 // clock_gettime(), whenever we talk to the trusted thread?
164 // or maybe, if we have recently seen requests to compute 218 // or maybe, if we have recently seen requests to compute
165 // the time. There might be a repeated pattern of those. 219 // the time. There might be a repeated pattern of those.
166 "cmp $78, %eax\n" // __NR_gettimeofday 220 "cmp $78, %eax\n" // __NR_gettimeofday
167 "jnz 2f\n" 221 "jnz 6f\n"
168 "cmp %eax, %fs:0x102C-0x58\n" // last system call 222 "cmp %eax, %fs:0x102C-0x58\n" // last system call
169 "jnz 0f\n" 223 "jnz 4f\n"
170 224
171 // This system call and the last system call prior to this one both are 225 // This system call and the last system call prior to this one both are
172 // calls to gettimeofday(). Try to avoid making the new call and just 226 // calls to gettimeofday(). Try to avoid making the new call and just
173 // return the same result as in the previous call. 227 // return the same result as in the previous call.
174 // Just in case the caller is spinning on the result from gettimeofday(), 228 // Just in case the caller is spinning on the result from gettimeofday(),
175 // every so often, call the actual system call. 229 // every so often, call the actual system call.
176 "decl %fs:0x1030-0x58\n" // countdown calls to gettimofday() 230 "decl %fs:0x1030-0x58\n" // countdown calls to gettimofday()
177 "jz 0f\n" 231 "jz 4f\n"
178 232
179 // Atomically read the 64bit word representing last-known timestamp and 233 // Atomically read the 64bit word representing last-known timestamp and
180 // return it to the caller. On x86-32 this is a little more complicated and 234 // return it to the caller. On x86-32 this is a little more complicated and
181 // requires the use of the cmpxchg8b instruction. 235 // requires the use of the cmpxchg8b instruction.
182 "mov %ebx, %eax\n" 236 "mov %ebx, %eax\n"
183 "mov %ecx, %edx\n" 237 "mov %ecx, %edx\n"
184 "lock; cmpxchg8b 100f\n" 238 "lock; cmpxchg8b 100f\n"
185 "mov %eax, 0(%ebx)\n" 239 "mov %eax, 0(%ebx)\n"
186 "mov %edx, 4(%ebx)\n" 240 "mov %edx, 4(%ebx)\n"
187 "xor %eax, %eax\n" 241 "xor %eax, %eax\n"
188 "add $28, %esp\n" 242 "add $28, %esp\n"
189 "jmp 4f\n" 243 "jmp 8f\n"
190 244
191 // This is a call to gettimeofday(), but we don't have a valid cached 245 // This is a call to gettimeofday(), but we don't have a valid cached
192 // result, yet. 246 // result, yet.
193 "0:mov %eax, %fs:0x102C-0x58\n" // remember syscall number 247 "4:mov %eax, %fs:0x102C-0x58\n" // remember syscall number
194 "movl $500, %fs:0x1030-0x58\n" // make system call, each 500 invocations 248 "movl $500, %fs:0x1030-0x58\n" // make system call, each 500 invocations
195 "call playground$defaultSystemCallHandler\n" 249 "call playground$defaultSystemCallHandler\n"
196 250
197 // Returned from gettimeofday(). Remember return value, in case the 251 // Returned from gettimeofday(). Remember return value, in case the
198 // application calls us again right away. 252 // application calls us again right away.
199 // Again, this has to happen atomically and requires cmpxchg8b. 253 // Again, this has to happen atomically and requires cmpxchg8b.
200 "mov 4(%ebx), %ecx\n" 254 "mov 4(%ebx), %ecx\n"
201 "mov 0(%ebx), %ebx\n" 255 "mov 0(%ebx), %ebx\n"
202 "mov 100f, %eax\n" 256 "mov 100f, %eax\n"
203 "mov 101f, %edx\n" 257 "mov 101f, %edx\n"
204 "1:lock; cmpxchg8b 100f\n" 258 "5:lock; cmpxchg8b 100f\n"
205 "jnz 1b\n" 259 "jnz 5b\n"
206 "xor %eax, %eax\n" 260 "xor %eax, %eax\n"
207 "jmp 6f\n" 261 "jmp 10f\n"
208 262
209 // Remember the number of the last system call made. We deliberately do 263 // Remember the number of the last system call made. We deliberately do
210 // not remember calls to gettid(), as we have often seen long sequences 264 // not remember calls to gettid(), as we have often seen long sequences
211 // of calls to just gettimeofday() and gettid(). In that situation, we 265 // of calls to just gettimeofday() and gettid(). In that situation, we
212 // would still like to coalesce the gettimeofday() calls. 266 // would still like to coalesce the gettimeofday() calls.
213 "2:cmp $224, %eax\n" // __NR_gettid 267 "6:cmp $224, %eax\n" // __NR_gettid
214 "jz 3f\n" 268 "jz 7f\n"
215 "mov %eax, %fs:0x102C-0x58\n" // remember syscall number 269 "mov %eax, %fs:0x102C-0x58\n" // remember syscall number
216 270
217 // Retrieve function call from system call table (c.f. syscall_table.c). 271 // Retrieve function call from system call table (c.f. syscall_table.c).
218 // We have three different types of entries; zero for denied system calls, 272 // We have three different types of entries; zero for denied system calls,
219 // that should be handled by the defaultSystemCallHandler(); minus one 273 // that should be handled by the defaultSystemCallHandler(); minus one
220 // for unrestricted system calls that need to be forwarded to the trusted 274 // for unrestricted system calls that need to be forwarded to the trusted
221 // thread; and function pointers to specific handler functions. 275 // thread; and function pointers to specific handler functions.
222 "3:shl $3, %eax\n" 276 "7:shl $3, %eax\n"
223 "lea playground$syscallTable, %ebx\n" 277 "lea playground$syscallTable, %ebx\n"
224 "add %ebx, %eax\n" 278 "add %ebx, %eax\n"
225 "mov 0(%eax), %eax\n" 279 "mov 0(%eax), %eax\n"
226 280
227 // Jump to function if non-null and not UNRESTRICTED_SYSCALL, otherwise 281 // Jump to function if non-null and not UNRESTRICTED_SYSCALL, otherwise
228 // jump to fallback handler. 282 // jump to fallback handler.
229 "cmp $1, %eax\n" 283 "cmp $1, %eax\n"
230 "jbe 5f\n" 284 "jbe 9f\n"
231 "add $4, %esp\n" 285 "add $4, %esp\n"
232 "call *%eax\n" 286 "call *%eax\n"
233 "add $24, %esp\n" 287 "add $24, %esp\n"
234 288
235 // Restore CPU registers, except for %eax which was set by the system call. 289 // Restore CPU registers, except for %eax which was set by the system call.
236 "4:pop %ebp\n" 290 "8:pop %ebp\n"
237 "pop %edi\n" 291 "pop %edi\n"
238 "pop %esi\n" 292 "pop %esi\n"
239 "pop %edx\n" 293 "pop %edx\n"
240 "pop %ecx\n" 294 "pop %ecx\n"
241 "pop %ebx\n" 295 "pop %ebx\n"
242 296
243 // Return to caller 297 // Return to caller
244 "ret\n" 298 "ret\n"
245 299
246 // Call default handler. 300 // Call default handler.
247 "5:call playground$defaultSystemCallHandler\n" 301 "9:call playground$defaultSystemCallHandler\n"
248 "6:add $28, %esp\n" 302 "10:add $28, %esp\n"
249 "jmp 4b\n" 303 "jmp 8b\n"
250 304
251 ".pushsection \".bss\"\n" 305 ".pushsection \".bss\"\n"
252 ".balign 8\n" 306 ".balign 8\n"
253 "100:.byte 0, 0, 0, 0\n" 307 "100:.byte 0, 0, 0, 0\n"
254 "101:.byte 0, 0, 0, 0\n" 308 "101:.byte 0, 0, 0, 0\n"
255 ".popsection\n" 309 ".popsection\n"
256 310
257 #else 311 #else
258 #error Unsupported target platform 312 #error Unsupported target platform
259 #endif 313 #endif
260 ".size playground$syscallWrapper, .-playground$syscallWrapper\n" 314 ".size playground$syscallWrapper, .-playground$syscallWrapper\n"
261 ".popsection\n" 315 ".popsection\n"
262 ); 316 );
263 317
264 318
265 void* Sandbox::defaultSystemCallHandler(int syscallNum, void* arg0, void* arg1, 319 void* Sandbox::defaultSystemCallHandler(int syscallNum, void* arg0, void* arg1,
266 void* arg2, void* arg3, void* arg4, 320 void* arg2, void* arg3, void* arg4,
267 void* arg5) { 321 void* arg5) {
268 // TODO(markus): The following comment is currently not true, we do intercept these system calls. Try to fix that. 322 // TODO(markus): The following comment is currently not true, we do intercept these system calls. Try to fix that.
269 323
270 // We try to avoid intercepting read(), write(), and sigreturn(), as 324 // We try to avoid intercepting read(), and write(), as these system calls
271 // these system calls are not restricted in Seccomp mode. But depending on 325 // are not restricted in Seccomp mode. But depending on the exact
272 // the exact instruction sequence in libc, we might not be able to reliably 326 // instruction sequence in libc, we might not be able to reliably
273 // filter out these system calls at the time when we instrument the code. 327 // filter out these system calls at the time when we instrument the code.
274 SysCalls sys; 328 SysCalls sys;
275 long rc; 329 long rc;
276 long long tm; 330 long long tm;
277 switch (syscallNum) { 331 switch (syscallNum) {
278 case __NR_read: 332 case __NR_read:
279 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call"); 333 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call");
280 rc = sys.read((long)arg0, arg1, (size_t)arg2); 334 rc = sys.read((long)arg0, arg1, (size_t)arg2);
281 break; 335 break;
282 case __NR_write: 336 case __NR_write:
283 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call"); 337 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call");
284 rc = sys.write((long)arg0, arg1, (size_t)arg2); 338 rc = sys.write((long)arg0, arg1, (size_t)arg2);
285 break; 339 break;
286 case __NR_rt_sigreturn:
287 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call");
288 rc = sys.rt_sigreturn((unsigned long)arg0);
289 break;
290 default: 340 default:
291 if (Debug::isEnabled()) { 341 if (Debug::isEnabled()) {
292 // In debug mode, prevent stderr from being closed 342 // In debug mode, prevent stderr from being closed
293 if (syscallNum == __NR_close && arg0 == (void *)2) 343 if (syscallNum == __NR_close && arg0 == (void *)2)
294 return 0; 344 return 0;
295 } 345 }
296 346
297 if ((unsigned)syscallNum <= maxSyscall && 347 if ((unsigned)syscallNum <= maxSyscall &&
298 syscallTable[syscallNum].handler == UNRESTRICTED_SYSCALL) { 348 syscallTable[syscallNum].handler == UNRESTRICTED_SYSCALL) {
299 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call"); 349 Debug::syscall(&tm, syscallNum, "Allowing unrestricted system call");
(...skipping 21 matching lines...) Expand all
321 } 371 }
322 } 372 }
323 if (rc < 0) { 373 if (rc < 0) {
324 rc = -sys.my_errno; 374 rc = -sys.my_errno;
325 } 375 }
326 Debug::elapsed(tm, syscallNum); 376 Debug::elapsed(tm, syscallNum);
327 return (void *)rc; 377 return (void *)rc;
328 } 378 }
329 379
330 } // namespace 380 } // namespace
OLDNEW
« no previous file with comments | « sandbox/linux/seccomp/sandbox_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698