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 "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 5 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
6 #include "sandbox/linux/seccomp-bpf/verifier.h" | 6 #include "sandbox/linux/seccomp-bpf/verifier.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 using namespace playground2; | 9 using namespace playground2; |
10 | 10 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 if (BlacklistNanosleepPolicySigsysAuxData != kExpectedReturnValue) | 191 if (BlacklistNanosleepPolicySigsysAuxData != kExpectedReturnValue) |
192 ExitGroup(1); | 192 ExitGroup(1); |
193 else | 193 else |
194 ExitGroup(kExpectedReturnValue); | 194 ExitGroup(kExpectedReturnValue); |
195 } | 195 } |
196 | 196 |
197 TEST(SandboxBpf, BasicBlacklistWithSigsys) { | 197 TEST(SandboxBpf, BasicBlacklistWithSigsys) { |
198 TryPolicyInProcess(BlacklistNanosleepPolicySigsys, NanosleepProcessSigsys); | 198 TryPolicyInProcess(BlacklistNanosleepPolicySigsys, NanosleepProcessSigsys); |
199 } | 199 } |
200 | 200 |
201 // A more complex, but synthetic policy. This tests the correctness of the BPF | |
202 // program by iterating through all syscalls and checking for a errno that | |
palmer
2012/06/28 19:58:14
Nit: "for an errno" :)
jln (very slow on Chromium)
2012/06/28 20:57:45
Done.
| |
203 // depends on the syscall number. Unlike the Verifier, this exercises the BPF | |
204 // interpreter in the kernel. | |
205 | |
206 int SysnoToRandomErrno(int sysno) { | |
palmer
2012/06/28 19:58:14
I think this only makes sense if you have some con
jln (very slow on Chromium)
2012/06/28 20:57:45
Let me know if this is more clear.
| |
207 // Small contiguous sets of 3 system calls return an errno equal to the | |
208 // index of that set + 1 (so that we never return a NUL errno). | |
209 return ((sysno & ~3) >> 2) % 29 + 1; | |
210 } | |
211 | |
212 Sandbox::ErrorCode SyntheticPolicy(int sysno) { | |
213 if (sysno < static_cast<int>(MIN_SYSCALL) || | |
214 sysno > static_cast<int>(MAX_SYSCALL)) { | |
215 // FIXME: we should really not have to do that in a trivial policy. | |
216 return ENOSYS; | |
217 } | |
218 if (sysno == __NR_exit_group) | |
219 // exit_group() is special, we really need it to work. | |
220 return Sandbox::SB_ALLOWED; | |
221 else { | |
palmer
2012/06/28 19:58:14
Nit: Two-line (one code line) block gets no curly
jln (very slow on Chromium)
2012/06/28 20:57:45
Done.
| |
222 return SysnoToRandomErrno(sysno); | |
223 } | |
224 } | |
225 | |
226 void SyntheticProcess(void) { | |
227 // Ensure that that kExpectedReturnValue + syscallnumber + 1 does not int | |
228 // overflow. | |
229 if (std::numeric_limits<int>::max() - kExpectedReturnValue - 1 < | |
230 static_cast<int>(MAX_SYSCALL)) { | |
231 ExitGroup(1); | |
232 } | |
233 for (int syscall_number = static_cast<int>(MIN_SYSCALL); | |
palmer
2012/06/28 19:58:14
Why are these static_casts necessary? Can we just
jln (very slow on Chromium)
2012/06/28 20:57:45
Perhaps surprisingly, syscalls are actually signed
| |
234 syscall_number <= static_cast<int>(MAX_SYSCALL); | |
235 ++syscall_number) { | |
236 if (syscall_number == __NR_exit_group) { | |
237 // exit_group() is special | |
238 continue; | |
239 } | |
240 errno = 0; | |
241 if (syscall(syscall_number) != -1 || | |
242 errno != SysnoToRandomErrno(syscall_number)) { | |
palmer
2012/06/28 19:58:14
I don't see why this is correct. Shouldn't we be e
jln (very slow on Chromium)
2012/06/28 20:57:45
This is what this is doing, isn't it ? I added a c
| |
243 ExitGroup(kExpectedReturnValue + syscall_number + 1); | |
244 } | |
245 } | |
246 ExitGroup(kExpectedReturnValue); | |
247 } | |
248 | |
249 TEST(SandboxBpf, SyntheticPolicy) { | |
250 TryPolicyInProcess(SyntheticPolicy, SyntheticProcess); | |
251 } | |
252 | |
201 } // namespace | 253 } // namespace |
OLD | NEW |