OLD | NEW |
---|---|
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/sandbox_bpf.h" | 7 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.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 |
11 using namespace playground2; | 11 using namespace playground2; |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 const int kExpectedReturnValue = 42; | 15 const int kExpectedReturnValue = 42; |
16 #if defined(__arm__) | |
17 const int kArmPublicSysnoCeiling = __NR_SYSCALL_BASE + 1024; | |
18 #endif | |
16 | 19 |
17 TEST(SandboxBpf, CallSupports) { | 20 TEST(SandboxBpf, CallSupports) { |
18 // We check that we don't crash, but it's ok if the kernel doesn't | 21 // We check that we don't crash, but it's ok if the kernel doesn't |
19 // support it. | 22 // support it. |
20 bool seccomp_bpf_supported = | 23 bool seccomp_bpf_supported = |
21 Sandbox::supportsSeccompSandbox(-1) == Sandbox::STATUS_AVAILABLE; | 24 Sandbox::supportsSeccompSandbox(-1) == Sandbox::STATUS_AVAILABLE; |
22 // We want to log whether or not seccomp BPF is actually supported | 25 // We want to log whether or not seccomp BPF is actually supported |
23 // since actual test coverage depends on it. | 26 // since actual test coverage depends on it. |
24 RecordProperty("SeccompBPFSupported", | 27 RecordProperty("SeccompBPFSupported", |
25 seccomp_bpf_supported ? "true." : "false."); | 28 seccomp_bpf_supported ? "true." : "false."); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 // index of that set + 1 (so that we never return a NUL errno). | 230 // index of that set + 1 (so that we never return a NUL errno). |
228 return ((sysno & ~3) >> 2) % 29 + 1; | 231 return ((sysno & ~3) >> 2) % 29 + 1; |
229 } | 232 } |
230 | 233 |
231 Sandbox::ErrorCode SyntheticPolicy(int sysno) { | 234 Sandbox::ErrorCode SyntheticPolicy(int sysno) { |
232 if (sysno < static_cast<int>(MIN_SYSCALL) || | 235 if (sysno < static_cast<int>(MIN_SYSCALL) || |
233 sysno > static_cast<int>(MAX_SYSCALL)) { | 236 sysno > static_cast<int>(MAX_SYSCALL)) { |
234 // FIXME: we should really not have to do that in a trivial policy. | 237 // FIXME: we should really not have to do that in a trivial policy. |
235 return ENOSYS; | 238 return ENOSYS; |
236 } | 239 } |
240 | |
241 // TODO(jorgelo): remove this restriction once crbug.com/141694 is fixed. | |
242 #if defined(__arm__) | |
243 if (sysno > kArmPublicSysnoCeiling) | |
244 return ENOSYS; | |
245 #endif | |
246 | |
237 if (sysno == __NR_exit_group) { | 247 if (sysno == __NR_exit_group) { |
238 // exit_group() is special, we really need it to work. | 248 // exit_group() is special, we really need it to work. |
239 return Sandbox::SB_ALLOWED; | 249 return Sandbox::SB_ALLOWED; |
240 } else { | 250 } else { |
241 return SysnoToRandomErrno(sysno); | 251 return SysnoToRandomErrno(sysno); |
242 } | 252 } |
243 } | 253 } |
244 | 254 |
245 void SyntheticProcess(void) { | 255 void SyntheticProcess(void) { |
246 // Ensure that that kExpectedReturnValue + syscallnumber + 1 does not int | 256 // Ensure that that kExpectedReturnValue + syscallnumber + 1 does not int |
247 // overflow. | 257 // overflow. |
248 if (std::numeric_limits<int>::max() - kExpectedReturnValue - 1 < | 258 if (std::numeric_limits<int>::max() - kExpectedReturnValue - 1 < |
249 static_cast<int>(MAX_SYSCALL)) { | 259 static_cast<int>(MAX_SYSCALL)) { |
250 ExitGroup(1); | 260 ExitGroup(1); |
251 } | 261 } |
262 | |
263 // TODO(jorgelo): remove this limit once crbug.com/141694 is fixed. | |
264 #if defined(__arm__) | |
265 int sysno_ceiling = kArmPublicSysnoCeiling; | |
jln (very slow on Chromium)
2012/08/09 22:41:23
These should be const.
Jorge Lucangeli Obes
2012/08/09 22:58:26
Done.
| |
266 #else | |
267 int sysno_ceiling = static_cast<int>(MAX_SYSCALL); | |
268 #endif | |
269 | |
252 for (int syscall_number = static_cast<int>(MIN_SYSCALL); | 270 for (int syscall_number = static_cast<int>(MIN_SYSCALL); |
253 syscall_number <= static_cast<int>(MAX_SYSCALL); | 271 syscall_number <= sysno_ceiling; |
254 ++syscall_number) { | 272 ++syscall_number) { |
255 if (syscall_number == __NR_exit_group) { | 273 if (syscall_number == __NR_exit_group) { |
256 // exit_group() is special | 274 // exit_group() is special |
257 continue; | 275 continue; |
258 } | 276 } |
259 errno = 0; | 277 errno = 0; |
260 if (syscall(syscall_number) != -1 || | 278 if (syscall(syscall_number) != -1 || |
261 errno != SysnoToRandomErrno(syscall_number)) { | 279 errno != SysnoToRandomErrno(syscall_number)) { |
262 // Exit with a return value that is different than kExpectedReturnValue | 280 // Exit with a return value that is different than kExpectedReturnValue |
263 // to signal an error. Make it easy to see what syscall_number failed in | 281 // to signal an error. Make it easy to see what syscall_number failed in |
264 // the test report. | 282 // the test report. |
265 ExitGroup(kExpectedReturnValue + syscall_number + 1); | 283 ExitGroup(kExpectedReturnValue + syscall_number + 1); |
266 } | 284 } |
267 } | 285 } |
268 ExitGroup(kExpectedReturnValue); | 286 ExitGroup(kExpectedReturnValue); |
269 } | 287 } |
270 | 288 |
271 TEST(SandboxBpf, SyntheticPolicy) { | 289 TEST(SandboxBpf, SyntheticPolicy) { |
272 TryPolicyInProcess(SyntheticPolicy, SyntheticProcess); | 290 TryPolicyInProcess(SyntheticPolicy, SyntheticProcess); |
273 } | 291 } |
274 | 292 |
275 } // namespace | 293 } // namespace |
OLD | NEW |