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

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

Issue 10833044: Refactored ErrorCode into it's own class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased after we moved the unittest framework into its own CL Created 8 years, 3 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
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/verifier.h" 8 #include "sandbox/linux/seccomp-bpf/verifier.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
(...skipping 22 matching lines...) Expand all
33 << "\n"; 33 << "\n";
34 } 34 }
35 35
36 SANDBOX_TEST(SandboxBpf, CallSupportsTwice) { 36 SANDBOX_TEST(SandboxBpf, CallSupportsTwice) {
37 Sandbox::supportsSeccompSandbox(-1); 37 Sandbox::supportsSeccompSandbox(-1);
38 Sandbox::supportsSeccompSandbox(-1); 38 Sandbox::supportsSeccompSandbox(-1);
39 } 39 }
40 40
41 // A simple blacklist test 41 // A simple blacklist test
42 42
43 Sandbox::ErrorCode BlacklistNanosleepPolicy(int sysno) { 43 ErrorCode BlacklistNanosleepPolicy(int sysno) {
44 if (sysno < static_cast<int>(MIN_SYSCALL) || 44 if (sysno < static_cast<int>(MIN_SYSCALL) ||
45 sysno > static_cast<int>(MAX_SYSCALL)) { 45 sysno > static_cast<int>(MAX_SYSCALL)) {
46 // FIXME: we should really not have to do that in a trivial policy 46 // FIXME: we should really not have to do that in a trivial policy
47 return ENOSYS; 47 return ErrorCode(ENOSYS);
48 } 48 }
49 switch (sysno) { 49 switch (sysno) {
50 case __NR_nanosleep: 50 case __NR_nanosleep:
51 return EACCES; 51 return ErrorCode(EACCES);
52 default: 52 default:
53 return Sandbox::SB_ALLOWED; 53 return ErrorCode(ErrorCode::ERR_ALLOWED);
54 } 54 }
55 } 55 }
56 56
57 BPF_TEST(SandboxBpf, ApplyBasicBlacklistPolicy, BlacklistNanosleepPolicy) { 57 BPF_TEST(SandboxBpf, ApplyBasicBlacklistPolicy, BlacklistNanosleepPolicy) {
58 // nanosleep() should be denied 58 // nanosleep() should be denied
59 const struct timespec ts = {0, 0}; 59 const struct timespec ts = {0, 0};
60 errno = 0; 60 errno = 0;
61 BPF_ASSERT(syscall(__NR_nanosleep, &ts, NULL) == -1); 61 BPF_ASSERT(syscall(__NR_nanosleep, &ts, NULL) == -1);
62 BPF_ASSERT(errno == EACCES); 62 BPF_ASSERT(errno == EACCES);
63 } 63 }
64 64
65 // Now do a simple whitelist test 65 // Now do a simple whitelist test
66 66
67 Sandbox::ErrorCode WhitelistGetpidPolicy(int sysno) { 67 ErrorCode WhitelistGetpidPolicy(int sysno) {
68 switch (sysno) { 68 switch (sysno) {
69 case __NR_getpid: 69 case __NR_getpid:
70 case __NR_exit_group: 70 case __NR_exit_group:
71 return Sandbox::SB_ALLOWED; 71 return ErrorCode(ErrorCode::ERR_ALLOWED);
72 default: 72 default:
73 return ENOMEM; 73 return ErrorCode(ENOMEM);
74 } 74 }
75 } 75 }
76 76
77 BPF_TEST(SandboxBpf, ApplyBasicWhitelistPolicy, WhitelistGetpidPolicy) { 77 BPF_TEST(SandboxBpf, ApplyBasicWhitelistPolicy, WhitelistGetpidPolicy) {
78 // getpid() should be allowed 78 // getpid() should be allowed
79 errno = 0; 79 errno = 0;
80 BPF_ASSERT(syscall(__NR_getpid) > 0); 80 BPF_ASSERT(syscall(__NR_getpid) > 0);
81 BPF_ASSERT(errno == 0); 81 BPF_ASSERT(errno == 0);
82 82
83 // getpgid() should be denied 83 // getpgid() should be denied
84 BPF_ASSERT(getpgid(0) == -1); 84 BPF_ASSERT(getpgid(0) == -1);
85 BPF_ASSERT(errno == ENOMEM); 85 BPF_ASSERT(errno == ENOMEM);
86 } 86 }
87 87
88 // A simple blacklist policy, with a SIGSYS handler 88 // A simple blacklist policy, with a SIGSYS handler
89 89
90 // TODO: provide an API to provide the auxiliary data pointer 90 // TODO: provide an API to provide the auxiliary data pointer
91 // to the evaluator 91 // to the evaluator
92 92
93 static int BlacklistNanosleepPolicySigsysAuxData; 93 static int BlacklistNanosleepPolicySigsysAuxData;
94 94
95 intptr_t EnomemHandler(const struct arch_seccomp_data& args, void *aux) { 95 intptr_t EnomemHandler(const struct arch_seccomp_data& args, void *aux) {
96 // We also check that the auxiliary data is correct 96 // We also check that the auxiliary data is correct
97 SANDBOX_ASSERT(aux); 97 SANDBOX_ASSERT(aux);
98 *(static_cast<int*>(aux)) = kExpectedReturnValue; 98 *(static_cast<int*>(aux)) = kExpectedReturnValue;
99 return -ENOMEM; 99 return -ENOMEM;
100 } 100 }
101 101
102 Sandbox::ErrorCode BlacklistNanosleepPolicySigsys(int sysno) { 102 ErrorCode BlacklistNanosleepPolicySigsys(int sysno) {
103 if (sysno < static_cast<int>(MIN_SYSCALL) || 103 if (sysno < static_cast<int>(MIN_SYSCALL) ||
104 sysno > static_cast<int>(MAX_SYSCALL)) { 104 sysno > static_cast<int>(MAX_SYSCALL)) {
105 // FIXME: we should really not have to do that in a trivial policy 105 // FIXME: we should really not have to do that in a trivial policy
106 return ENOSYS; 106 return ErrorCode(ENOSYS);
107 } 107 }
108 switch (sysno) { 108 switch (sysno) {
109 case __NR_nanosleep: 109 case __NR_nanosleep:
110 return Sandbox::ErrorCode(EnomemHandler, 110 return Sandbox::Trap(EnomemHandler,
111 static_cast<void *>(&BlacklistNanosleepPolicySigsysAuxData)); 111 static_cast<void *>(&BlacklistNanosleepPolicySigsysAuxData));
112 default: 112 default:
113 return Sandbox::SB_ALLOWED; 113 return ErrorCode(ErrorCode::ERR_ALLOWED);
114 } 114 }
115 } 115 }
116 116
117 BPF_TEST(SandboxBpf, BasicBlacklistWithSigsys, 117 BPF_TEST(SandboxBpf, BasicBlacklistWithSigsys,
118 BlacklistNanosleepPolicySigsys) { 118 BlacklistNanosleepPolicySigsys) {
119 // getpid() should work properly 119 // getpid() should work properly
120 errno = 0; 120 errno = 0;
121 BPF_ASSERT(syscall(__NR_getpid) > 0); 121 BPF_ASSERT(syscall(__NR_getpid) > 0);
122 BPF_ASSERT(errno == 0); 122 BPF_ASSERT(errno == 0);
123 123
(...skipping 16 matching lines...) Expand all
140 // We try to make sure we exercise optimizations in the BPF compiler. We make 140 // We try to make sure we exercise optimizations in the BPF compiler. We make
141 // sure that the compiler can have an opportunity to coalesce syscalls with 141 // sure that the compiler can have an opportunity to coalesce syscalls with
142 // contiguous numbers and we also make sure that disjoint sets can return the 142 // contiguous numbers and we also make sure that disjoint sets can return the
143 // same errno. 143 // same errno.
144 int SysnoToRandomErrno(int sysno) { 144 int SysnoToRandomErrno(int sysno) {
145 // Small contiguous sets of 3 system calls return an errno equal to the 145 // Small contiguous sets of 3 system calls return an errno equal to the
146 // index of that set + 1 (so that we never return a NUL errno). 146 // index of that set + 1 (so that we never return a NUL errno).
147 return ((sysno & ~3) >> 2) % 29 + 1; 147 return ((sysno & ~3) >> 2) % 29 + 1;
148 } 148 }
149 149
150 Sandbox::ErrorCode SyntheticPolicy(int sysno) { 150 ErrorCode SyntheticPolicy(int sysno) {
151 if (sysno < static_cast<int>(MIN_SYSCALL) || 151 if (sysno < static_cast<int>(MIN_SYSCALL) ||
152 sysno > static_cast<int>(MAX_SYSCALL)) { 152 sysno > static_cast<int>(MAX_SYSCALL)) {
153 // FIXME: we should really not have to do that in a trivial policy. 153 // FIXME: we should really not have to do that in a trivial policy.
154 return ENOSYS; 154 return ErrorCode(ENOSYS);
155 } 155 }
156 156
157 // TODO(jorgelo): remove this restriction once crbug.com/141694 is fixed. 157 // TODO(jorgelo): remove this restriction once crbug.com/141694 is fixed.
158 #if defined(__arm__) 158 #if defined(__arm__)
159 if (sysno > kArmPublicSysnoCeiling) 159 if (sysno > kArmPublicSysnoCeiling)
160 return ENOSYS; 160 return ENOSYS;
161 #endif 161 #endif
162 162
163 // TODO(markus): allow calls to write(). This should start working as soon 163 // TODO(markus): allow calls to write(). This should start working as soon
164 // as we switch to the new code generator. Currently we are blowing up, 164 // as we switch to the new code generator. Currently we are blowing up,
165 // because our jumptable is getting too big. 165 // because our jumptable is getting too big.
166 if (sysno == __NR_exit_group /* || sysno == __NR_write */) { 166 if (sysno == __NR_exit_group /* || sysno == __NR_write */) {
167 // exit_group() is special, we really need it to work. 167 // exit_group() is special, we really need it to work.
168 // write() is needed for BPF_ASSERT() to report a useful error message. 168 return ErrorCode(ErrorCode::ERR_ALLOWED);
169 return Sandbox::SB_ALLOWED;
170 } else { 169 } else {
171 return SysnoToRandomErrno(sysno); 170 return ErrorCode(SysnoToRandomErrno(sysno));
172 } 171 }
173 } 172 }
174 173
175 BPF_TEST(SandboxBpf, SyntheticPolicy, SyntheticPolicy) { 174 BPF_TEST(SandboxBpf, SyntheticPolicy, SyntheticPolicy) {
176 // Ensure that that kExpectedReturnValue + syscallnumber + 1 does not int 175 // Ensure that that kExpectedReturnValue + syscallnumber + 1 does not int
177 // overflow. 176 // overflow.
178 BPF_ASSERT( 177 BPF_ASSERT(
179 std::numeric_limits<int>::max() - kExpectedReturnValue - 1 >= 178 std::numeric_limits<int>::max() - kExpectedReturnValue - 1 >=
180 static_cast<int>(MAX_SYSCALL)); 179 static_cast<int>(MAX_SYSCALL));
181 180
(...skipping 12 matching lines...) Expand all
194 // exit_group() is special 193 // exit_group() is special
195 continue; 194 continue;
196 } 195 }
197 errno = 0; 196 errno = 0;
198 BPF_ASSERT(syscall(syscall_number) == -1); 197 BPF_ASSERT(syscall(syscall_number) == -1);
199 BPF_ASSERT(errno == SysnoToRandomErrno(syscall_number)); 198 BPF_ASSERT(errno == SysnoToRandomErrno(syscall_number));
200 } 199 }
201 } 200 }
202 201
203 } // namespace 202 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698