/src/crosvm/third_party/minijail/signal_handler.c
Line | Count | Source |
1 | | /* Copyright 2012 The ChromiumOS Authors |
2 | | * Use of this source code is governed by a BSD-style license that can be |
3 | | * found in the LICENSE file. |
4 | | */ |
5 | | |
6 | | #include <signal.h> |
7 | | #include <stdlib.h> |
8 | | #include <string.h> |
9 | | #include <unistd.h> |
10 | | |
11 | | #include "signal_handler.h" |
12 | | |
13 | | #include "util.h" |
14 | | |
15 | | /* |
16 | | * si_syscall was added in glibc-2.17+, but Android still uses glibc-2.15 |
17 | | * for its prebuilt binary host toolchains. Add a compat hack for it. |
18 | | */ |
19 | | static int get_si_syscall(const siginfo_t *info) |
20 | 0 | { |
21 | 0 | #if defined(si_syscall) |
22 | 0 | return info->si_syscall; |
23 | | #else |
24 | | typedef struct { |
25 | | void *ip; |
26 | | int nr; |
27 | | unsigned int arch; |
28 | | } local_siginfo_t; |
29 | | |
30 | | union { |
31 | | const siginfo_t *info; |
32 | | const local_siginfo_t *local_info; |
33 | | } local_info = { |
34 | | .info = info, |
35 | | }; |
36 | | return local_info.local_info->nr; |
37 | | #endif |
38 | 0 | } |
39 | | |
40 | | void log_sigsys_handler(int sig attribute_unused, siginfo_t *info, |
41 | | void *void_context attribute_unused) |
42 | 0 | { |
43 | 0 | const char *syscall_name; |
44 | 0 | int nr = get_si_syscall(info); |
45 | 0 | syscall_name = lookup_syscall_name(nr); |
46 | |
|
47 | 0 | if (syscall_name) |
48 | 0 | die("blocked syscall: %s", syscall_name); |
49 | 0 | else |
50 | 0 | die("blocked syscall: %d", nr); |
51 | | |
52 | | /* |
53 | | * We trapped on a syscall that should have killed the process. |
54 | | * This should never ever return, but we're paranoid. |
55 | | */ |
56 | 0 | for (;;) |
57 | 0 | _exit(1); |
58 | 0 | } |
59 | | |
60 | | int install_sigsys_handler() |
61 | 0 | { |
62 | 0 | int ret = 0; |
63 | 0 | struct sigaction act; |
64 | 0 | sigset_t mask; |
65 | |
|
66 | 0 | memset(&act, 0, sizeof(act)); |
67 | 0 | act.sa_sigaction = &log_sigsys_handler; |
68 | 0 | act.sa_flags = SA_SIGINFO; |
69 | |
|
70 | 0 | sigemptyset(&mask); |
71 | 0 | sigaddset(&mask, SIGSYS); |
72 | |
|
73 | 0 | ret = sigaction(SIGSYS, &act, NULL); |
74 | 0 | if (ret < 0) |
75 | 0 | return ret; |
76 | | |
77 | 0 | ret = sigprocmask(SIG_UNBLOCK, &mask, NULL); |
78 | 0 | if (ret < 0) |
79 | 0 | return ret; |
80 | | |
81 | 0 | return 0; |
82 | 0 | } |