OLD | NEW |
---|---|
(Empty) | |
1 /* | |
jcgregorio
2014/04/09 14:10:31
Deleting this file.
| |
2 * syscall reporting example for seccomp | |
3 * | |
4 * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org> | |
5 * Authors: | |
6 * Will Drewry <wad@chromium.org> | |
7 * Kees Cook <keescook@chromium.org> | |
8 * | |
9 * Use of this source code is governed by a BSD-style license that can be | |
10 * found in the LICENSE file. | |
11 */ | |
12 #include <execinfo.h> | |
13 #include <map> | |
14 | |
15 #include "syscall_reporter.h" | |
16 #include "syscall_names.h" | |
17 | |
18 const char * const msg_needed = "Looks like you also need syscall: "; | |
19 | |
20 /* Since "sprintf" is technically not signal-safe, reimplement %d here. */ | |
mtklein
2014/04/08 19:00:23
Badass
| |
21 static void write_uint(char *buf, unsigned int val) | |
22 { | |
23 int width = 0; | |
24 unsigned int tens; | |
25 | |
26 if (val == 0) { | |
27 strcpy(buf, "0"); | |
28 return; | |
29 } | |
30 for (tens = val; tens; tens /= 10) | |
31 ++ width; | |
32 buf[width] = '\0'; | |
33 for (tens = val; tens; tens /= 10) | |
34 buf[--width] = '0' + (tens % 10); | |
35 } | |
36 | |
37 void tracer() { | |
38 int j, nptrs; | |
39 #define SIZE 100 | |
mtklein
2014/04/08 19:00:23
I'm getting the impression that this file was just
| |
40 void *buffer[100]; | |
41 char **strings; | |
42 | |
43 nptrs = backtrace(buffer, SIZE); | |
44 printf("backtrace() returned %d addresses\n", nptrs); | |
45 | |
46 /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) | |
47 would produce similar output to the following: */ | |
48 | |
49 strings = backtrace_symbols(buffer, nptrs); | |
50 if (strings == NULL) { | |
51 perror("backtrace_symbols"); | |
52 exit(EXIT_FAILURE); | |
53 } | |
54 | |
55 for (j = 0; j < nptrs; j++) | |
56 printf("%s\n", strings[j]); | |
57 | |
58 free(strings); | |
59 } | |
60 | |
61 static void reporter(int nr, siginfo_t *info, void *void_context) | |
62 { | |
mtklein
2014/04/08 19:00:23
This code has some of the oddest formatting I've e
| |
63 char buf[128]; | |
64 ucontext_t *ctx = (ucontext_t *)(void_context); | |
65 unsigned int syscall; | |
66 if (info->si_code != SYS_SECCOMP) | |
67 return; | |
68 if (!ctx) | |
69 return; | |
70 syscall = ctx->uc_mcontext.gregs[REG_SYSCALL]; | |
71 strcpy(buf, msg_needed); | |
72 std::map<int, const char*>::iterator i; | |
73 i = syscall_names.find(syscall); | |
74 if (i != syscall_names.end()) { | |
75 strcat(buf, syscall_names[syscall]); | |
76 strcat(buf, "("); | |
77 } else { | |
78 strcat(buf, "Unknown call?!?"); | |
79 } | |
80 write_uint(buf + strlen(buf), syscall); | |
81 if (i != syscall_names.end()) { | |
82 strcat(buf, ")"); | |
83 } | |
84 strcat(buf, "\n"); | |
85 int ret = write(STDOUT_FILENO, buf, strlen(buf)); | |
mtklein
2014/04/08 19:00:23
WTF do these next 4 lines do?
| |
86 tracer(); | |
87 ret = ret; | |
mtklein
2014/04/08 19:00:23
Particularly WTF. Is there some implicit register
| |
88 _exit(1); | |
89 } | |
90 | |
91 int install_syscall_reporter(void) { | |
92 struct sigaction act; | |
93 syscall_names_init(); | |
94 sigset_t mask; | |
95 memset(&act, 0, sizeof(act)); | |
96 sigemptyset(&mask); | |
97 sigaddset(&mask, SIGSYS); | |
98 | |
99 act.sa_sigaction = &reporter; | |
100 act.sa_flags = SA_SIGINFO; | |
101 if (sigaction(SIGSYS, &act, NULL) < 0) { | |
102 perror("sigaction"); | |
103 return -1; | |
104 } | |
105 if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) { | |
106 perror("sigprocmask"); | |
107 return -1; | |
108 } | |
109 return 0; | |
110 } | |
OLD | NEW |