OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include <fcntl.h> | |
6 | |
7 #include "library.h" | |
8 #include "sandbox.h" | |
9 #include "test_runner.h" | |
10 | |
11 | |
12 extern "C" int my_getpid(void); | |
13 extern char my_getpid_end[]; | |
14 | |
15 void patch_range(char *start, char *end) { | |
16 int maps_fd; | |
17 CHECK_SUCCEEDS((maps_fd = open("/proc/self/maps", O_RDONLY, 0)) >= 0); | |
18 playground::Maps maps(maps_fd); | |
19 playground::Library library; | |
20 library.setLibraryInfo(&maps); | |
21 char *extra_space = NULL; | |
22 int extra_size = 0; | |
23 char *page_start = (char *) ((uintptr_t) start & ~(getpagesize() - 1)); | |
Markus (顧孟勤)
2011/11/18 18:16:46
You should probably round "end" up to the next pag
| |
24 CHECK_SUCCEEDS(mprotect(page_start, end - page_start, | |
25 PROT_READ | PROT_WRITE | PROT_EXEC) == 0); | |
26 library.patchSystemCallsInRange(start, end, &extra_space, &extra_size); | |
27 CHECK_SUCCEEDS(close(maps_fd) == 0); | |
28 } | |
29 | |
30 TEST(test_patching_syscall) { | |
31 int pid = getpid(); | |
32 CHECK(my_getpid() == pid); | |
33 char *func = (char *) my_getpid; | |
34 char *func_end = my_getpid_end; | |
35 patch_range(func, func_end); | |
36 #if defined(__x86_64__) | |
37 CHECK(func[0] == '\xe9'); // e9 XX XX XX XX jmp X | |
38 CHECK(func[5] == '\x90'); // 90 nop | |
39 CHECK(func[6] == '\x90'); // 90 nop | |
40 CHECK(func[7] == '\xc3'); // c3 ret (unmodified) | |
41 #elif defined(__i386__) | |
42 CHECK(func[0] == '\x68'); // 68 XX XX XX XX push $X | |
43 CHECK(func[5] == '\xc3'); // c3 ret | |
44 CHECK(func[6] == '\x90'); // 90 nop | |
45 CHECK(func[7] == '\xc3'); // c3 ret (unmodified) | |
46 #else | |
47 # error Unsupported target platform | |
48 #endif | |
49 StartSeccompSandbox(); | |
50 CHECK(my_getpid() == pid); | |
51 } | |
OLD | NEW |