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

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

Issue 11419121: SECCOMP-BPF: Added support for greylisting of system calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More unittest coverage 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
« no previous file with comments | « sandbox/linux/seccomp-bpf/sandbox_bpf.cc ('k') | sandbox/linux/seccomp-bpf/syscall.h » ('j') | 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) 2012 The Chromium Authors. All rights reserved. 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 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 <sys/prctl.h>
6 #include <sys/utsname.h>
7
5 #include <ostream> 8 #include <ostream>
6 9
7 #include "sandbox/linux/seccomp-bpf/bpf_tests.h" 10 #include "sandbox/linux/seccomp-bpf/bpf_tests.h"
11 #include "sandbox/linux/seccomp-bpf/syscall.h"
8 #include "sandbox/linux/seccomp-bpf/verifier.h" 12 #include "sandbox/linux/seccomp-bpf/verifier.h"
9 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
10 14
11 using namespace playground2; 15 using namespace playground2;
12 16
13 namespace { 17 namespace {
14 18
15 const int kExpectedReturnValue = 42; 19 const int kExpectedReturnValue = 42;
16 20
17 // This test should execute no matter whether we have kernel support. So, 21 // This test should execute no matter whether we have kernel support. So,
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 for (int syscall_number = static_cast<int>(__ARM_NR_set_tls + 1); 261 for (int syscall_number = static_cast<int>(__ARM_NR_set_tls + 1);
258 syscall_number <= static_cast<int>(MAX_PRIVATE_SYSCALL); 262 syscall_number <= static_cast<int>(MAX_PRIVATE_SYSCALL);
259 ++syscall_number) { 263 ++syscall_number) {
260 errno = 0; 264 errno = 0;
261 BPF_ASSERT(syscall(syscall_number) == -1); 265 BPF_ASSERT(syscall(syscall_number) == -1);
262 BPF_ASSERT(errno == ArmPrivateSysnoToErrno(syscall_number)); 266 BPF_ASSERT(errno == ArmPrivateSysnoToErrno(syscall_number));
263 } 267 }
264 } 268 }
265 #endif // defined(__arm__) 269 #endif // defined(__arm__)
266 270
271 intptr_t CountSyscalls(const struct arch_seccomp_data& args, void *aux) {
272 // Count all invocations of our callback function.
273 ++*reinterpret_cast<int *>(aux);
274
275 // Verify that within the callback function all filtering is temporarily
276 // disabled.
277 BPF_ASSERT(syscall(__NR_getpid) > 1);
278
279 // Verify that we can now call the underlying system call without causing
280 // infinite recursion.
281 return Sandbox::ForwardSyscall(args);
282 }
283
284 ErrorCode GreyListedPolicy(int sysno, void *aux) {
285 // The use of UnsafeTrap() causes us to print a warning message. This is
286 // generally desirable, but it results in the unittest failing, as it doesn't
287 // expect any messages on "stderr". So, temporarily disable messages. The
288 // BPF_TEST() is guaranteed to turn messages back on, after the policy
289 // function has completed.
290 Die::SuppressInfoMessages(true);
291
292 // Some system calls must always be allowed, if our policy wants to make
293 // use of UnsafeTrap()
294 if (sysno == __NR_rt_sigprocmask ||
295 sysno == __NR_rt_sigreturn
296 #if defined(__NR_sigprocmask)
297 || sysno == __NR_sigprocmask
298 #endif
299 #if defined(__NR_sigreturn)
300 || sysno == __NR_sigreturn
301 #endif
302 ) {
303 return ErrorCode(ErrorCode::ERR_ALLOWED);
304 } else if (sysno == __NR_getpid) {
305 // Disallow getpid()
306 return ErrorCode(EPERM);
307 } else if (Sandbox::isValidSyscallNumber(sysno)) {
308 // Allow (and count) all other system calls.
309 return Sandbox::UnsafeTrap(CountSyscalls, aux);
310 } else {
311 return ErrorCode(ENOSYS);
312 }
313 }
314
315 BPF_TEST(SandboxBpf, GreyListedPolicy,
316 GreyListedPolicy, int /* BPF_AUX */) {
317 BPF_ASSERT(syscall(__NR_getpid) == -1);
318 BPF_ASSERT(errno == EPERM);
319 BPF_ASSERT(BPF_AUX == 0);
320 BPF_ASSERT(syscall(__NR_geteuid) == syscall(__NR_getuid));
321 BPF_ASSERT(BPF_AUX == 2);
322 char name[17] = { };
323 BPF_ASSERT(!syscall(__NR_prctl, PR_GET_NAME, name, (void *)NULL,
324 (void *)NULL, (void *)NULL));
325 BPF_ASSERT(BPF_AUX == 3);
326 BPF_ASSERT(*name);
327 }
328
329 intptr_t PrctlHandler(const struct arch_seccomp_data& args, void *) {
330 if (args.args[0] == PR_CAPBSET_DROP &&
331 static_cast<int>(args.args[1]) == -1) {
332 // prctl(PR_CAPBSET_DROP, -1) is never valid. The kernel will always
333 // return an error. But our handler allows this call.
334 return 0;
335 } else {
336 return Sandbox::ForwardSyscall(args);
337 }
338 }
339
340 ErrorCode PrctlPolicy(int sysno, void *aux) {
341 Die::SuppressInfoMessages(true);
342
343 // Some system calls must always be allowed, if our policy wants to make
344 // use of UnsafeTrap()
jln (very slow on Chromium) 2012/11/22 01:12:39 Let's get rid of this big chunk since the default
345 if (sysno == __NR_rt_sigprocmask ||
346 sysno == __NR_rt_sigreturn
347 #if defined(__NR_sigprocmask)
348 || sysno == __NR_sigprocmask
349 #endif
350 #if defined(__NR_sigreturn)
351 || sysno == __NR_sigreturn
352 #endif
353 ) {
354 return ErrorCode(ErrorCode::ERR_ALLOWED);
355 } else if (sysno == __NR_prctl) {
356 // Handle prctl() inside an UnsafeTrap()
357 return Sandbox::UnsafeTrap(PrctlHandler, NULL);
358 } else if (Sandbox::isValidSyscallNumber(sysno)) {
359 // Allow all other system calls.
360 return ErrorCode(ErrorCode::ERR_ALLOWED);
361 } else {
362 return ErrorCode(ENOSYS);
363 }
364 }
365
366 BPF_TEST(SandboxBpf, ForwardSyscall, PrctlPolicy) {
367 // This call should never be allowed. But our policy will intercept it and
368 // let it pass successfully.
369 BPF_ASSERT(!prctl(PR_CAPBSET_DROP, -1, (void *)NULL, (void *)NULL,
370 (void *)NULL));
371
372 // Verify that the call will fail, if it makes it all the way to the kernel.
373 BPF_ASSERT(prctl(PR_CAPBSET_DROP, -2, (void *)NULL, (void *)NULL,
374 (void *)NULL) == -1);
375
376 // And verify that other uses of prctl() work just fine.
377 char name[17] = { };
378 BPF_ASSERT(!syscall(__NR_prctl, PR_GET_NAME, name, (void *)NULL,
379 (void *)NULL, (void *)NULL));
380 BPF_ASSERT(*name);
381
382 // Finally, verify that system calls other than prctl() are completely
383 // unaffected by our policy.
384 struct utsname uts = { };
385 BPF_ASSERT(!uname(&uts));
386 BPF_ASSERT(!strcmp(uts.sysname, "Linux"));
387 }
388
389 intptr_t AllowRedirectedSyscall(const struct arch_seccomp_data& args, void *) {
390 return Sandbox::ForwardSyscall(args);
391 }
392
393 ErrorCode RedirectAllSyscallsPolicy(int sysno, void *aux) {
394 Die::SuppressInfoMessages(true);
395
396 // Some system calls must always be allowed, if our policy wants to make
397 // use of UnsafeTrap()
398 if (sysno == __NR_rt_sigprocmask ||
399 sysno == __NR_rt_sigreturn
400 #if defined(__NR_sigprocmask)
401 || sysno == __NR_sigprocmask
402 #endif
403 #if defined(__NR_sigreturn)
404 || sysno == __NR_sigreturn
405 #endif
406 ) {
407 return ErrorCode(ErrorCode::ERR_ALLOWED);
408 } else if (Sandbox::isValidSyscallNumber(sysno)) {
409 return Sandbox::UnsafeTrap(AllowRedirectedSyscall, aux);
410 } else {
411 return ErrorCode(ENOSYS);
412 }
413 }
414
415 int bus_handler_fd_ = -1;
416
417 void SigBusHandler(int, siginfo_t *info, void *void_context) {
418 BPF_ASSERT(write(bus_handler_fd_, "\x55", 1) == 1);
419 }
420
421 BPF_TEST(SandboxBpf, SigBus, RedirectAllSyscallsPolicy) {
422 // We use the SIGBUS bit in the signal mask as a thread-local boolean
423 // value in the implementation of UnsafeTrap(). This is obviously a bit
424 // of a hack that could conceivably interfere with code that uses SIGBUS
425 // in more traditional ways. This test verifies that basic functionality
426 // of SIGBUS is not impacted, but it is certainly possibly to construe
427 // more complex uses of signals where our use of the SIGBUS mask is not
428 // 100% transparent. This is expected behavior.
429 int fds[2];
430 BPF_ASSERT(pipe(fds) == 0);
431 bus_handler_fd_ = fds[1];
432 struct sigaction sa = { };
433 sa.sa_sigaction = SigBusHandler;
434 sa.sa_flags = SA_SIGINFO;
435 BPF_ASSERT(sigaction(SIGBUS, &sa, NULL) == 0);
436 raise(SIGBUS);
437 char c = '\000';
438 BPF_ASSERT(read(fds[0], &c, 1) == 1);
439 BPF_ASSERT(close(fds[0]) == 0);
440 BPF_ASSERT(close(fds[1]) == 0);
441 BPF_ASSERT(c == 0x55);
442 }
443
444 BPF_TEST(SandboxBpf, SigMask, RedirectAllSyscallsPolicy) {
445 // Signal masks are potentially tricky to handle. For instance, if we
446 // ever tried to update them from inside a Trap() or UnsafeTrap() handler,
447 // the call to sigreturn() at the end of the signal handler would undo
448 // all of our efforts. So, it makes sense to test that sigprocmask()
449 // works, even if we have a policy in place that makes use of UnsafeTrap().
450 // In practice, this works because we force sigprocmask() to be handled
451 // entirely in the kernel.
452 sigset_t mask0, mask1, mask2;
453
454 // Call sigprocmask() to verify that SIGUSR1 wasn't blocked, if we didn't
455 // change the mask (it shouldn't have been, as it isn't blocked by default
456 // in POSIX).
457 sigemptyset(&mask0);
458 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, &mask1));
459 BPF_ASSERT(!sigismember(&mask1, SIGUSR1));
460
461 // Try again, and this time we verify that we can block it. This
462 // requires a second call to sigprocmask().
463 sigaddset(&mask0, SIGUSR1);
464 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, NULL));
465 BPF_ASSERT(!sigprocmask(SIG_BLOCK, NULL, &mask2));
466 BPF_ASSERT( sigismember(&mask2, SIGUSR1));
467 }
468
469 BPF_TEST(SandboxBpf, UnsafeTrapWithErrno, RedirectAllSyscallsPolicy) {
470 // An UnsafeTrap() (or for that matter, a Trap()) has to report error
471 // conditions by returning an exit code in the range -1..-4096. This
472 // should happen automatically if using ForwardSyscall(). If the TrapFnc()
473 // uses some other method to make system calls, then it is responsible
474 // for computing the correct return code.
475 // This test verifies that ForwardSyscall() does the correct thing.
476
477 // The glibc system wrapper will ultimately set errno for us. So, from normal
478 // userspace, all of this should be completely transparent.
479 errno = 0;
480 BPF_ASSERT(close(-1) == -1);
481 BPF_ASSERT(errno == EBADF);
482
483 // Explicitly avoid the glibc wrapper. This is not normally the way anybody
484 // would make system calls, but it allows us to verify that we don't
485 // accidentally mess with errno, when we shouldn't.
486 errno = 0;
487 struct arch_seccomp_data args = { };
488 args.nr = __NR_close;
489 args.args[0] = -1;
490 BPF_ASSERT(Sandbox::ForwardSyscall(args) == -EBADF);
491 BPF_ASSERT(errno == 0);
492 }
493
267 } // namespace 494 } // namespace
OLDNEW
« no previous file with comments | « sandbox/linux/seccomp-bpf/sandbox_bpf.cc ('k') | sandbox/linux/seccomp-bpf/syscall.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698