| 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/syscall.h" | 5 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 6 | 6 |
| 7 #include <asm/unistd.h> | 7 #include <asm/unistd.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 10 #include <sys/syscall.h> | 10 #include <sys/syscall.h> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 TEST(Syscall, InvalidCallReturnsENOSYS) { | 36 TEST(Syscall, InvalidCallReturnsENOSYS) { |
| 37 EXPECT_EQ(-ENOSYS, Syscall::InvalidCall()); | 37 EXPECT_EQ(-ENOSYS, Syscall::InvalidCall()); |
| 38 } | 38 } |
| 39 | 39 |
| 40 TEST(Syscall, WellKnownEntryPoint) { | 40 TEST(Syscall, WellKnownEntryPoint) { |
| 41 // Test that Syscall::Call(-1) is handled specially. Don't do this on ARM, | 41 // Test that Syscall::Call(-1) is handled specially. Don't do this on ARM, |
| 42 // where syscall(-1) crashes with SIGILL. Not running the test is fine, as we | 42 // where syscall(-1) crashes with SIGILL. Not running the test is fine, as we |
| 43 // are still testing ARM code in the next set of tests. | 43 // are still testing ARM code in the next set of tests. |
| 44 #if !defined(__arm__) | 44 #if !defined(__arm__) && !defined(__aarch64__) |
| 45 EXPECT_NE(Syscall::Call(-1), syscall(-1)); | 45 EXPECT_NE(Syscall::Call(-1), syscall(-1)); |
| 46 #endif | 46 #endif |
| 47 | 47 |
| 48 // If possible, test that Syscall::Call(-1) returns the address right | 48 // If possible, test that Syscall::Call(-1) returns the address right |
| 49 // after | 49 // after |
| 50 // a kernel entry point. | 50 // a kernel entry point. |
| 51 #if defined(__i386__) | 51 #if defined(__i386__) |
| 52 EXPECT_EQ(0x80CDu, ((uint16_t*)Syscall::Call(-1))[-1]); // INT 0x80 | 52 EXPECT_EQ(0x80CDu, ((uint16_t*)Syscall::Call(-1))[-1]); // INT 0x80 |
| 53 #elif defined(__x86_64__) | 53 #elif defined(__x86_64__) |
| 54 EXPECT_EQ(0x050Fu, ((uint16_t*)Syscall::Call(-1))[-1]); // SYSCALL | 54 EXPECT_EQ(0x050Fu, ((uint16_t*)Syscall::Call(-1))[-1]); // SYSCALL |
| 55 #elif defined(__arm__) | 55 #elif defined(__arm__) |
| 56 #if defined(__thumb__) | 56 #if defined(__thumb__) |
| 57 EXPECT_EQ(0xDF00u, ((uint16_t*)Syscall::Call(-1))[-1]); // SWI 0 | 57 EXPECT_EQ(0xDF00u, ((uint16_t*)Syscall::Call(-1))[-1]); // SWI 0 |
| 58 #else | 58 #else |
| 59 EXPECT_EQ(0xEF000000u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0 | 59 EXPECT_EQ(0xEF000000u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0 |
| 60 #endif | 60 #endif |
| 61 #elif defined(__mips__) | 61 #elif defined(__mips__) |
| 62 // Opcode for MIPS sycall is in the lower 16-bits | 62 // Opcode for MIPS sycall is in the lower 16-bits |
| 63 EXPECT_EQ(0x0cu, (((uint32_t*)Syscall::Call(-1))[-1]) & 0x0000FFFF); | 63 EXPECT_EQ(0x0cu, (((uint32_t*)Syscall::Call(-1))[-1]) & 0x0000FFFF); |
| 64 #elif defined(__aarch64__) |
| 65 EXPECT_EQ(0xD4000001u, ((uint32_t*)Syscall::Call(-1))[-1]); // SVC 0 |
| 64 #else | 66 #else |
| 65 #warning Incomplete test case; need port for target platform | 67 #warning Incomplete test case; need port for target platform |
| 66 #endif | 68 #endif |
| 67 } | 69 } |
| 68 | 70 |
| 69 TEST(Syscall, TrivialSyscallNoArgs) { | 71 TEST(Syscall, TrivialSyscallNoArgs) { |
| 70 // Test that we can do basic system calls | 72 // Test that we can do basic system calls |
| 71 EXPECT_EQ(Syscall::Call(__NR_getpid), syscall(__NR_getpid)); | 73 EXPECT_EQ(Syscall::Call(__NR_getpid), syscall(__NR_getpid)); |
| 72 } | 74 } |
| 73 | 75 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 BPF_ASSERT((*BPF_AUX)[0] == static_cast<uint64_t>(syscall_args[0])); | 148 BPF_ASSERT((*BPF_AUX)[0] == static_cast<uint64_t>(syscall_args[0])); |
| 147 BPF_ASSERT((*BPF_AUX)[1] == static_cast<uint64_t>(syscall_args[1])); | 149 BPF_ASSERT((*BPF_AUX)[1] == static_cast<uint64_t>(syscall_args[1])); |
| 148 BPF_ASSERT((*BPF_AUX)[2] == static_cast<uint64_t>(syscall_args[2])); | 150 BPF_ASSERT((*BPF_AUX)[2] == static_cast<uint64_t>(syscall_args[2])); |
| 149 BPF_ASSERT((*BPF_AUX)[3] == static_cast<uint64_t>(syscall_args[3])); | 151 BPF_ASSERT((*BPF_AUX)[3] == static_cast<uint64_t>(syscall_args[3])); |
| 150 BPF_ASSERT((*BPF_AUX)[4] == static_cast<uint64_t>(syscall_args[4])); | 152 BPF_ASSERT((*BPF_AUX)[4] == static_cast<uint64_t>(syscall_args[4])); |
| 151 BPF_ASSERT((*BPF_AUX)[5] == static_cast<uint64_t>(syscall_args[5])); | 153 BPF_ASSERT((*BPF_AUX)[5] == static_cast<uint64_t>(syscall_args[5])); |
| 152 } | 154 } |
| 153 | 155 |
| 154 TEST(Syscall, ComplexSyscallSixArgs) { | 156 TEST(Syscall, ComplexSyscallSixArgs) { |
| 155 int fd; | 157 int fd; |
| 156 ASSERT_LE(0, fd = Syscall::Call(__NR_open, "/dev/null", O_RDWR, 0L)); | 158 ASSERT_LE(0, |
| 159 fd = Syscall::Call(__NR_openat, AT_FDCWD, "/dev/null", O_RDWR, 0L)); |
| 157 | 160 |
| 158 // Use mmap() to allocate some read-only memory | 161 // Use mmap() to allocate some read-only memory |
| 159 char* addr0; | 162 char* addr0; |
| 160 ASSERT_NE( | 163 ASSERT_NE( |
| 161 (char*)NULL, | 164 (char*)NULL, |
| 162 addr0 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, | 165 addr0 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, |
| 163 (void*)NULL, | 166 (void*)NULL, |
| 164 4096, | 167 4096, |
| 165 PROT_READ, | 168 PROT_READ, |
| 166 MAP_PRIVATE | MAP_ANONYMOUS, | 169 MAP_PRIVATE | MAP_ANONYMOUS, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 179 fd, | 182 fd, |
| 180 0L))); | 183 0L))); |
| 181 ++*addr1; // This should not seg fault | 184 ++*addr1; // This should not seg fault |
| 182 | 185 |
| 183 // Clean up | 186 // Clean up |
| 184 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr1, 4096L)); | 187 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr1, 4096L)); |
| 185 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); | 188 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); |
| 186 | 189 |
| 187 // Check that the offset argument (i.e. the sixth argument) is processed | 190 // Check that the offset argument (i.e. the sixth argument) is processed |
| 188 // correctly. | 191 // correctly. |
| 189 ASSERT_GE(fd = Syscall::Call(__NR_open, "/proc/self/exe", O_RDONLY, 0L), 0); | 192 ASSERT_GE( |
| 193 fd = Syscall::Call(__NR_openat, AT_FDCWD, "/proc/self/exe", O_RDONLY, 0L), |
| 194 0); |
| 190 char* addr2, *addr3; | 195 char* addr2, *addr3; |
| 191 ASSERT_NE((char*)NULL, | 196 ASSERT_NE((char*)NULL, |
| 192 addr2 = reinterpret_cast<char*>(Syscall::Call( | 197 addr2 = reinterpret_cast<char*>(Syscall::Call( |
| 193 kMMapNr, (void*)NULL, 8192L, PROT_READ, MAP_PRIVATE, fd, 0L))); | 198 kMMapNr, (void*)NULL, 8192L, PROT_READ, MAP_PRIVATE, fd, 0L))); |
| 194 ASSERT_NE((char*)NULL, | 199 ASSERT_NE((char*)NULL, |
| 195 addr3 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, | 200 addr3 = reinterpret_cast<char*>(Syscall::Call(kMMapNr, |
| 196 (void*)NULL, | 201 (void*)NULL, |
| 197 4096L, | 202 4096L, |
| 198 PROT_READ, | 203 PROT_READ, |
| 199 MAP_PRIVATE, | 204 MAP_PRIVATE, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 214 | 219 |
| 215 // Clean up | 220 // Clean up |
| 216 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr2, 8192L)); | 221 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr2, 8192L)); |
| 217 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr3, 4096L)); | 222 EXPECT_EQ(0, Syscall::Call(__NR_munmap, addr3, 4096L)); |
| 218 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); | 223 EXPECT_EQ(0, IGNORE_EINTR(Syscall::Call(__NR_close, fd))); |
| 219 } | 224 } |
| 220 | 225 |
| 221 } // namespace | 226 } // namespace |
| 222 | 227 |
| 223 } // namespace sandbox | 228 } // namespace sandbox |
| OLD | NEW |