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>
10 #include <sys/syscall.h>
11 #include <sys/types.h>
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"
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.
31 const int kMMapNr
= __NR_mmap2
;
33 const int kMMapNr
= __NR_mmap
;
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.
41 EXPECT_NE(Syscall::Call(-1), syscall(-1));
44 // If possible, test that Syscall::Call(-1) returns the address right
46 // a kernel entry point.
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
55 EXPECT_EQ(0xEF000000u
, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0
58 #warning Incomplete test case; need port for target platform
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
) {
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
) {
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
));
93 ErrorCode
CopyAllArgsOnUnamePolicy(SandboxBPF
* sandbox
,
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
);
102 return ErrorCode(ErrorCode::ERR_ALLOWED
);
106 // We are testing Syscall::Call() by making use of a BPF filter that
108 // to inspect the system call arguments that the kernel saw.
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.
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
,
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
) {
149 ASSERT_LE(0, fd
= Syscall::Call(__NR_open
, "/dev/null", O_RDWR
, 0L));
151 // Use mmap() to allocate some read-only memory
155 addr0
= reinterpret_cast<char*>(Syscall::Call(kMMapNr
,
159 MAP_PRIVATE
| MAP_ANONYMOUS
,
163 // Try to replace the existing mapping with a read-write mapping
166 addr1
= reinterpret_cast<char*>(
167 Syscall::Call(kMMapNr
,
170 PROT_READ
| PROT_WRITE
,
171 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
174 ++*addr1
; // This should not seg fault
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
182 ASSERT_GE(fd
= Syscall::Call(__NR_open
, "/proc/self/exe", O_RDONLY
, 0L), 0);
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
,
194 #if defined(__NR_mmap2)
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.
205 EXPECT_EQ(8192, Syscall::Call(__NR_read
, fd
, buf
, 8192L));
206 EXPECT_EQ(0, memcmp(addr2
, buf
, 8192));
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
)));
216 } // namespace sandbox