Roll DEPS for libelf clang compilation fix.
[chromium-blink-merge.git] / sandbox / linux / seccomp-bpf / syscall_unittest.cc
blob4e142f42376e1809d48b93cab425b02a26ae71f5
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
3 // found in the LICENSE file.
5 #include "sandbox/linux/seccomp-bpf/syscall.h"
7 #include <asm/unistd.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <sys/syscall.h>
11 #include <sys/types.h>
12 #include <unistd.h>
14 #include <vector>
16 #include "base/basictypes.h"
17 #include "base/posix/eintr_wrapper.h"
18 #include "build/build_config.h"
19 #include "sandbox/linux/seccomp-bpf/bpf_tests.h"
20 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
21 #include "sandbox/linux/tests/unit_tests.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace sandbox {
26 namespace {
28 // Different platforms use different symbols for the six-argument version
29 // of the mmap() system call. Test for the correct symbol at compile time.
30 #ifdef __NR_mmap2
31 const int kMMapNr = __NR_mmap2;
32 #else
33 const int kMMapNr = __NR_mmap;
34 #endif
36 TEST(Syscall, WellKnownEntryPoint) {
37 // Test that Syscall::Call(-1) is handled specially. Don't do this on ARM,
38 // where syscall(-1) crashes with SIGILL. Not running the test is fine, as we
39 // are still testing ARM code in the next set of tests.
40 #if !defined(__arm__)
41 EXPECT_NE(Syscall::Call(-1), syscall(-1));
42 #endif
44 // If possible, test that Syscall::Call(-1) returns the address right
45 // after
46 // a kernel entry point.
47 #if defined(__i386__)
48 EXPECT_EQ(0x80CDu, ((uint16_t*)Syscall::Call(-1))[-1]); // INT 0x80
49 #elif defined(__x86_64__)
50 EXPECT_EQ(0x050Fu, ((uint16_t*)Syscall::Call(-1))[-1]); // SYSCALL
51 #elif defined(__arm__)
52 #if defined(__thumb__)
53 EXPECT_EQ(0xDF00u, ((uint16_t*)Syscall::Call(-1))[-1]); // SWI 0
54 #else
55 EXPECT_EQ(0xEF000000u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0
56 #endif
57 #else
58 #warning Incomplete test case; need port for target platform
59 #endif
62 TEST(Syscall, TrivialSyscallNoArgs) {
63 // Test that we can do basic system calls
64 EXPECT_EQ(Syscall::Call(__NR_getpid), syscall(__NR_getpid));
67 TEST(Syscall, TrivialSyscallOneArg) {
68 int new_fd;
69 // Duplicate standard error and close it.
70 ASSERT_GE(new_fd = Syscall::Call(__NR_dup, 2), 0);
71 int close_return_value = IGNORE_EINTR(Syscall::Call(__NR_close, new_fd));
72 ASSERT_EQ(close_return_value, 0);
75 TEST(Syscall, TrivialFailingSyscall) {
76 errno = -42;
77 int ret = Syscall::Call(__NR_dup, -1);
78 ASSERT_EQ(-EBADF, ret);
79 // Verify that Syscall::Call does not touch errno.
80 ASSERT_EQ(-42, errno);
83 // SIGSYS trap handler that will be called on __NR_uname.
84 intptr_t CopySyscallArgsToAux(const struct arch_seccomp_data& args, void* aux) {
85 // |aux| is our BPF_AUX pointer.
86 std::vector<uint64_t>* const seen_syscall_args =
87 static_cast<std::vector<uint64_t>*>(aux);
88 BPF_ASSERT(arraysize(args.args) == 6);
89 seen_syscall_args->assign(args.args, args.args + arraysize(args.args));
90 return -ENOMEM;
93 ErrorCode CopyAllArgsOnUnamePolicy(SandboxBPF* sandbox,
94 int sysno,
95 std::vector<uint64_t>* aux) {
96 if (!SandboxBPF::IsValidSyscallNumber(sysno)) {
97 return ErrorCode(ENOSYS);
99 if (sysno == __NR_uname) {
100 return sandbox->Trap(CopySyscallArgsToAux, aux);
101 } else {
102 return ErrorCode(ErrorCode::ERR_ALLOWED);
106 // We are testing Syscall::Call() by making use of a BPF filter that
107 // allows us
108 // to inspect the system call arguments that the kernel saw.
109 BPF_TEST(Syscall,
110 SyntheticSixArgs,
111 CopyAllArgsOnUnamePolicy,
112 std::vector<uint64_t> /* (*BPF_AUX) */) {
113 const int kExpectedValue = 42;
114 // In this test we only pass integers to the kernel. We might want to make
115 // additional tests to try other types. What we will see depends on
116 // implementation details of kernel BPF filters and we will need to document
117 // the expected behavior very clearly.
118 int syscall_args[6];
119 for (size_t i = 0; i < arraysize(syscall_args); ++i) {
120 syscall_args[i] = kExpectedValue + i;
123 // We could use pretty much any system call we don't need here. uname() is
124 // nice because it doesn't have any dangerous side effects.
125 BPF_ASSERT(Syscall::Call(__NR_uname,
126 syscall_args[0],
127 syscall_args[1],
128 syscall_args[2],
129 syscall_args[3],
130 syscall_args[4],
131 syscall_args[5]) == -ENOMEM);
133 // We expect the trap handler to have copied the 6 arguments.
134 BPF_ASSERT(BPF_AUX->size() == 6);
136 // Don't loop here so that we can see which argument does cause the failure
137 // easily from the failing line.
138 // uint64_t is the type passed to our SIGSYS handler.
139 BPF_ASSERT((*BPF_AUX)[0] == static_cast<uint64_t>(syscall_args[0]));
140 BPF_ASSERT((*BPF_AUX)[1] == static_cast<uint64_t>(syscall_args[1]));
141 BPF_ASSERT((*BPF_AUX)[2] == static_cast<uint64_t>(syscall_args[2]));
142 BPF_ASSERT((*BPF_AUX)[3] == static_cast<uint64_t>(syscall_args[3]));
143 BPF_ASSERT((*BPF_AUX)[4] == static_cast<uint64_t>(syscall_args[4]));
144 BPF_ASSERT((*BPF_AUX)[5] == static_cast<uint64_t>(syscall_args[5]));
147 TEST(Syscall, ComplexSyscallSixArgs) {
148 int fd;
149 ASSERT_LE(0, fd = Syscall::Call(__NR_open, "/dev/null", O_RDWR, 0L));
151 // Use mmap() to allocate some read-only memory
152 char* addr0;
153 ASSERT_NE(
154 (char*)NULL,
155 addr0 = reinterpret_cast<char*>(Syscall::Call(kMMapNr,
156 (void*)NULL,
157 4096,
158 PROT_READ,
159 MAP_PRIVATE | MAP_ANONYMOUS,
161 0L)));
163 // Try to replace the existing mapping with a read-write mapping
164 char* addr1;
165 ASSERT_EQ(addr0,
166 addr1 = reinterpret_cast<char*>(
167 Syscall::Call(kMMapNr,
168 addr0,
169 4096L,
170 PROT_READ | PROT_WRITE,
171 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
173 0L)));
174 ++*addr1; // This should not seg fault
176 // Clean up
177 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr1, 4096L));
178 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd)));
180 // Check that the offset argument (i.e. the sixth argument) is processed
181 // correctly.
182 ASSERT_GE(fd = Syscall::Call(__NR_open, "/proc/self/exe", O_RDONLY, 0L), 0);
183 char* addr2, *addr3;
184 ASSERT_NE((char*)NULL,
185 addr2 = reinterpret_cast<char*>(Syscall::Call(
186 kMMapNr, (void*)NULL, 8192L, PROT_READ, MAP_PRIVATE, fd, 0L)));
187 ASSERT_NE((char*)NULL,
188 addr3 = reinterpret_cast<char*>(Syscall::Call(kMMapNr,
189 (void*)NULL,
190 4096L,
191 PROT_READ,
192 MAP_PRIVATE,
194 #if defined(__NR_mmap2)
196 #else
197 4096L
198 #endif
199 )));
200 EXPECT_EQ(0, memcmp(addr2 + 4096, addr3, 4096));
202 // Just to be absolutely on the safe side, also verify that the file
203 // contents matches what we are getting from a read() operation.
204 char buf[8192];
205 EXPECT_EQ(8192, Syscall::Call(__NR_read, fd, buf, 8192L));
206 EXPECT_EQ(0, memcmp(addr2, buf, 8192));
208 // Clean up
209 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr2, 8192L));
210 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr3, 4096L));
211 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd)));
214 } // namespace
216 } // namespace sandbox