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

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

Issue 11363212: Added support for greylisting of system calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed reviewer's comments 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
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 <ostream> 5 #include <ostream>
6 6
7 #include "sandbox/linux/seccomp-bpf/bpf_tests.h" 7 #include "sandbox/linux/seccomp-bpf/bpf_tests.h"
8 #include "sandbox/linux/seccomp-bpf/syscall.h"
8 #include "sandbox/linux/seccomp-bpf/verifier.h" 9 #include "sandbox/linux/seccomp-bpf/verifier.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 using namespace playground2; 12 using namespace playground2;
12 13
13 namespace { 14 namespace {
14 15
15 const int kExpectedReturnValue = 42; 16 const int kExpectedReturnValue = 42;
16 17
17 // This test should execute no matter whether we have kernel support. So, 18 // 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); 258 for (int syscall_number = static_cast<int>(__ARM_NR_set_tls + 1);
258 syscall_number <= static_cast<int>(MAX_PRIVATE_SYSCALL); 259 syscall_number <= static_cast<int>(MAX_PRIVATE_SYSCALL);
259 ++syscall_number) { 260 ++syscall_number) {
260 errno = 0; 261 errno = 0;
261 BPF_ASSERT(syscall(syscall_number) == -1); 262 BPF_ASSERT(syscall(syscall_number) == -1);
262 BPF_ASSERT(errno == ArmPrivateSysnoToErrno(syscall_number)); 263 BPF_ASSERT(errno == ArmPrivateSysnoToErrno(syscall_number));
263 } 264 }
264 } 265 }
265 #endif // defined(__arm__) 266 #endif // defined(__arm__)
266 267
268 intptr_t CountSyscalls(const struct arch_seccomp_data& args, void *aux) {
269 // Count all invocations of our callback function.
270 ++*reinterpret_cast<int *>(aux);
271
272 // Verify that within the callback function all filtering is temporarily
273 // disabled.
274 BPF_ASSERT(syscall(__NR_getpid) > 1);
275
276 // Verify that we can now call the underlying system call without causing
277 // infinite recursion.
278 return (intptr_t)(syscall(args.nr, args.args[0], args.args[1],
279 args.args[2], args.args[3],
280 args.args[4], args.args[5]));
281 }
282
283 ErrorCode GreyListedPolicy(int sysno, void *aux) {
284 // The use of UnsafeTrap() causes us to print a warning message. This is
285 // generally desirable, but it results in the unittest failing, as it doesn't
286 // expect any messages on "stderr". So, temporarily disable messages. The
287 // BPF_TEST() is guaranteed to turn messages back on, after the policy
288 // function has completed.
289 Die::SuppressInfoMessages(true);
290
291 // Some system calls must always be allowed, if our policy wants to make
292 // use of UnsafeTrap()
293 if (sysno == __NR_rt_sigprocmask ||
294 sysno == __NR_rt_sigreturn
295 #if defined(__NR_sigprocmask)
296 || sysno == __NR_sigprocmask
297 #endif
298 #if defined(__NR_sigreturn)
299 || sysno == __NR_sigreturn
300 #endif
301 ) {
302 return ErrorCode(ErrorCode::ERR_ALLOWED);
303 } else if (sysno == __NR_getpid) {
304 // Disallow getpid()
305 return ErrorCode(EPERM);
306 } else if (Sandbox::isValidSyscallNumber(sysno)) {
307 // Allow (and count) all other system calls.
308 return Sandbox::UnsafeTrap(CountSyscalls, aux);
309 } else {
310 return ErrorCode(ENOSYS);
311 }
312 }
313
314 BPF_TEST(SandboxBpf, GreyListedPolicy,
315 GreyListedPolicy, int /* BPF_AUX */) {
316 BPF_ASSERT(syscall(__NR_getpid) == -1);
317 BPF_ASSERT(errno == EPERM);
318 BPF_ASSERT(BPF_AUX == 0);
319 BPF_ASSERT(syscall(__NR_geteuid) == syscall(__NR_getuid));
320 BPF_ASSERT(BPF_AUX == 2);
321 }
322
323 intptr_t AllowRedirectedSyscall(const struct arch_seccomp_data& args, void *) {
324 return (intptr_t)(syscall(args.nr,
325 (void *)args.args[0], (void *)args.args[1],
326 (void *)args.args[2], (void *)args.args[3],
327 (void *)args.args[4], (void *)args.args[5]));
328 }
329
330 ErrorCode RedirectAllSyscallsPolicy(int sysno, void *aux) {
331 Die::SuppressInfoMessages(true);
332
333 // Some system calls must always be allowed, if our policy wants to make
334 // use of UnsafeTrap()
335 if (sysno == __NR_rt_sigprocmask ||
336 sysno == __NR_rt_sigreturn
337 #if defined(__NR_sigprocmask)
338 || sysno == __NR_sigprocmask
339 #endif
340 #if defined(__NR_sigreturn)
341 || sysno == __NR_sigreturn
342 #endif
343 ) {
344 return ErrorCode(ErrorCode::ERR_ALLOWED);
345 } else if (Sandbox::isValidSyscallNumber(sysno)) {
346 return Sandbox::UnsafeTrap(AllowRedirectedSyscall, aux);
347 } else {
348 return ErrorCode(ENOSYS);
349 }
350 }
351
352 int bus_handler_fd_ = -1;
353
354 void SigBusHandler(int, siginfo_t *info, void *void_context) {
355 BPF_ASSERT(write(bus_handler_fd_, "\x55", 1) == 1);
356 }
357
jln (very slow on Chromium) 2012/11/20 01:08:31 Could you add a small explaination, saying that SI
358 BPF_TEST(SandboxBpf, SigBus, RedirectAllSyscallsPolicy) {
359 int fds[2];
360 BPF_ASSERT(pipe(fds) == 0);
361 bus_handler_fd_ = fds[1];
362 struct sigaction sa = { };
363 sa.sa_sigaction = SigBusHandler;
364 sa.sa_flags = SA_SIGINFO;
365 BPF_ASSERT(sigaction(SIGBUS, &sa, NULL) == 0);
366 raise(SIGBUS);
367 char c = '\000';
368 BPF_ASSERT(read(fds[0], &c, 1) == 1);
369 BPF_ASSERT(close(fds[0]) == 0);
370 BPF_ASSERT(close(fds[1]) == 0);
371 BPF_ASSERT(c == 0x55);
372 }
373
jln (very slow on Chromium) 2012/11/20 01:08:31 Same thing, please add a small note about what thi
374 BPF_TEST(SandboxBpf, SigMask, RedirectAllSyscallsPolicy) {
375 sigset_t mask0, mask1, mask2;
376 sigemptyset(&mask0);
jln (very slow on Chromium) 2012/11/20 01:08:31 Please add comments, to make this faster to read.
377 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, &mask1));
378 BPF_ASSERT(!sigismember(&mask1, SIGUSR1));
379 sigaddset(&mask0, SIGUSR1);
380 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, NULL));
381 BPF_ASSERT(!sigprocmask(SIG_BLOCK, &mask0, &mask2));
382 BPF_ASSERT( sigismember(&mask2, SIGUSR1));
383 }
384
267 } // namespace 385 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698