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 an errno that | |
203 // depends on the syscall number. Unlike the Verifier, this exercises the BPF | |
204 // interpreter in the kernel. | |
205 | |
206 // We try to make sure we exercise optimizations in the BPF compiler. We make | |
207 // sure that the compiler can have an opportunity to coalesce syscalls with | |
208 // contiguous numbers and we also make sure that disjoint sets can return the | |
209 // same errno. | |
palmer
2012/07/09 18:52:18
To be honest, I still don't understand this. Maybe
jln (very slow on Chromium)
2012/07/09 19:46:00
It's not the BPF interpreter, it's the BPF compile
| |
210 int SysnoToRandomErrno(int sysno) { | |
211 // Small contiguous sets of 3 system calls return an errno equal to the | |
212 // index of that set + 1 (so that we never return a NUL errno). | |
213 return ((sysno & ~3) >> 2) % 29 + 1; | |
214 } | |
215 | |
216 Sandbox::ErrorCode SyntheticPolicy(int sysno) { | |
217 if (sysno < static_cast<int>(MIN_SYSCALL) || | |
218 sysno > static_cast<int>(MAX_SYSCALL)) { | |
219 // FIXME: we should really not have to do that in a trivial policy. | |
220 return ENOSYS; | |
221 } | |
222 if (sysno == __NR_exit_group) { | |
223 // exit_group() is special, we really need it to work. | |
224 return Sandbox::SB_ALLOWED; | |
225 } else { | |
226 return SysnoToRandomErrno(sysno); | |
227 } | |
228 } | |
229 | |
230 void SyntheticProcess(void) { | |
231 // Ensure that that kExpectedReturnValue + syscallnumber + 1 does not int | |
232 // overflow. | |
233 if (std::numeric_limits<int>::max() - kExpectedReturnValue - 1 < | |
234 static_cast<int>(MAX_SYSCALL)) { | |
235 ExitGroup(1); | |
236 } | |
237 for (int syscall_number = static_cast<int>(MIN_SYSCALL); | |
238 syscall_number <= static_cast<int>(MAX_SYSCALL); | |
239 ++syscall_number) { | |
240 if (syscall_number == __NR_exit_group) { | |
241 // exit_group() is special | |
242 continue; | |
243 } | |
244 errno = 0; | |
245 if (syscall(syscall_number) != -1 || | |
246 errno != SysnoToRandomErrno(syscall_number)) { | |
247 // Exit with a return value that is different than kExpectedReturnValue | |
248 // to signal an error. Make it easy to see what syscall_number failed in | |
249 // the test report. | |
250 ExitGroup(kExpectedReturnValue + syscall_number + 1); | |
251 } | |
252 } | |
253 ExitGroup(kExpectedReturnValue); | |
254 } | |
255 | |
256 TEST(SandboxBpf, SyntheticPolicy) { | |
257 TryPolicyInProcess(SyntheticPolicy, SyntheticProcess); | |
258 } | |
259 | |
201 } // namespace | 260 } // namespace |
OLD | NEW |