From 1c3adf3ec249c1df930f10c74707f4f3cc0e49c8 Mon Sep 17 00:00:00 2001 From: "jln@chromium.org" Date: Mon, 11 Mar 2013 23:39:21 +0000 Subject: Android sandbox: workaround for restricted errno values. On Android, errno are only supported up to 255 and are not processed otherwise. Fix a test to work around this issue. BUG=181647,169416 NOTRY=true TBR=markus Review URL: https://chromiumcodereview.appspot.com/12638015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@187410 0039d316-1c4b-4281-b951-d872f2087c98 --- sandbox/linux/seccomp-bpf/errorcode.h | 2 ++ sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc | 38 +++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'sandbox') diff --git a/sandbox/linux/seccomp-bpf/errorcode.h b/sandbox/linux/seccomp-bpf/errorcode.h index 901525c..61ec110 100644 --- a/sandbox/linux/seccomp-bpf/errorcode.h +++ b/sandbox/linux/seccomp-bpf/errorcode.h @@ -34,6 +34,8 @@ class ErrorCode { // indicate success, but it won't actually run the system call. // This is very different from return ERR_ALLOWED. ERR_MIN_ERRNO = 0, + // TODO(markus): Android only supports errno up to 255 + // (crbug.com/181647). ERR_MAX_ERRNO = 4095, }; diff --git a/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc b/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc index b5a62c2..2d775f4 100644 --- a/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc +++ b/sandbox/linux/seccomp-bpf/sandbox_bpf_unittest.cc @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #if defined(ANDROID) // Work-around for buggy headers in Android's NDK @@ -19,6 +21,7 @@ #include #include "base/memory/scoped_ptr.h" +#include "build/build_config.h" #include "sandbox/linux/seccomp-bpf/bpf_tests.h" #include "sandbox/linux/seccomp-bpf/syscall.h" #include "sandbox/linux/seccomp-bpf/trap.h" @@ -44,6 +47,14 @@ namespace { const int kExpectedReturnValue = 42; const char kSandboxDebuggingEnv[] = "CHROME_SANDBOX_DEBUGGING"; +inline bool IsAndroid() { +#if defined(OS_ANDROID) + return true; +#else + return false; +#endif +} + // This test should execute no matter whether we have kernel support. So, // we make it a TEST() instead of a BPF_TEST(). TEST(SandboxBpf, CallSupports) { @@ -214,7 +225,7 @@ ErrorCode ErrnoTestPolicy(Sandbox *, int sysno, void *) { #if defined(__NR_setuid32) case __NR_setuid32: #endif - // Return errno = 1 + // Return errno = 1. return ErrorCode(1); case __NR_setgid: #if defined(__NR_setgid32) @@ -222,6 +233,9 @@ ErrorCode ErrnoTestPolicy(Sandbox *, int sysno, void *) { #endif // Return maximum errno value (typically 4095). return ErrorCode(ErrorCode::ERR_MAX_ERRNO); + case __NR_uname: + // Return errno = 42; + return ErrorCode(42); default: return ErrorCode(ErrorCode::ERR_ALLOWED); } @@ -243,10 +257,28 @@ BPF_TEST(SandboxBpf, ErrnoTest, ErrnoTestPolicy) { BPF_ASSERT(buf[0] == '\x55'); // Verify that we can return the minimum and maximum errno values. + errno = 0; BPF_ASSERT(setuid(0) == -1); BPF_ASSERT(errno == 1); - BPF_ASSERT(setgid(0) == -1); - BPF_ASSERT(errno == ErrorCode::ERR_MAX_ERRNO); + + // On Android, errno is only supported up to 255, otherwise errno + // processing is skipped. + // We work around this (crbug.com/181647). + if (IsAndroid() && setgid(0) != -1) { + errno = 0; + BPF_ASSERT(setgid(0) == -ErrorCode::ERR_MAX_ERRNO); + BPF_ASSERT(errno == 0); + } else { + errno = 0; + BPF_ASSERT(setgid(0) == -1); + BPF_ASSERT(errno == ErrorCode::ERR_MAX_ERRNO); + } + + // Finally, test an errno in between the minimum and maximum. + errno = 0; + struct utsname uts_buf; + BPF_ASSERT(uname(&uts_buf) == -1); + BPF_ASSERT(errno == 42); } // Testing the stacking of two sandboxes -- cgit v1.1