]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/sigbus.c
Merge pull request #33377 from yuwata/strbuf-cleanups
[thirdparty/systemd.git] / src / basic / sigbus.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <signal.h>
5 #include <stddef.h>
6 #include <sys/mman.h>
7
8 #include "macro.h"
9 #include "memory-util.h"
10 #include "missing_syscall.h"
11 #include "process-util.h"
12 #include "sigbus.h"
13 #include "signal-util.h"
14
15 #define SIGBUS_QUEUE_MAX 64
16
17 static struct sigaction old_sigaction;
18 static unsigned n_installed = 0;
19
20 /* We maintain a fixed size list of page addresses that triggered a
21 SIGBUS. We access with list with atomic operations, so that we
22 don't have to deal with locks between signal handler and main
23 programs in possibly multiple threads. */
24
25 static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
26 static volatile sig_atomic_t n_sigbus_queue = 0;
27
28 static void sigbus_push(void *addr) {
29 assert(addr);
30
31 /* Find a free place, increase the number of entries and leave, if we can */
32 for (size_t u = 0; u < SIGBUS_QUEUE_MAX; u++) {
33 /* OK to initialize this here since we haven't started the atomic ops yet */
34 void *tmp = NULL;
35 if (__atomic_compare_exchange_n(&sigbus_queue[u], &tmp, addr, false,
36 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
37 __atomic_fetch_add(&n_sigbus_queue, 1, __ATOMIC_SEQ_CST);
38 return;
39 }
40 }
41
42 /* If we can't, make sure the queue size is out of bounds, to
43 * mark it as overflowed */
44 for (;;) {
45 sig_atomic_t c;
46
47 __atomic_thread_fence(__ATOMIC_SEQ_CST);
48 c = n_sigbus_queue;
49
50 if (c > SIGBUS_QUEUE_MAX) /* already overflowed */
51 return;
52
53 /* OK if we clobber c here, since we either immediately return
54 * or it will be immediately reinitialized on next loop */
55 if (__atomic_compare_exchange_n(&n_sigbus_queue, &c, c + SIGBUS_QUEUE_MAX, false,
56 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
57 return;
58 }
59 }
60
61 int sigbus_pop(void **ret) {
62 assert(ret);
63
64 for (;;) {
65 unsigned u, c;
66
67 __atomic_thread_fence(__ATOMIC_SEQ_CST);
68 c = n_sigbus_queue;
69
70 if (_likely_(c == 0))
71 return 0;
72
73 if (_unlikely_(c > SIGBUS_QUEUE_MAX))
74 return -EOVERFLOW;
75
76 for (u = 0; u < SIGBUS_QUEUE_MAX; u++) {
77 void *addr;
78
79 addr = sigbus_queue[u];
80 if (!addr)
81 continue;
82
83 /* OK if we clobber addr here, since we either immediately return
84 * or it will be immediately reinitialized on next loop */
85 if (__atomic_compare_exchange_n(&sigbus_queue[u], &addr, NULL, false,
86 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
87 __atomic_fetch_sub(&n_sigbus_queue, 1, __ATOMIC_SEQ_CST);
88 /* If we successfully entered this if condition, addr won't
89 * have been modified since its assignment, so safe to use it */
90 *ret = addr;
91 return 1;
92 }
93 }
94 }
95 }
96
97 static void sigbus_handler(int sn, siginfo_t *si, void *data) {
98 unsigned long ul;
99 void *aligned;
100
101 assert(sn == SIGBUS);
102 assert(si);
103
104 if (si->si_code != BUS_ADRERR || !si->si_addr) {
105 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
106 propagate_signal(sn, si);
107 return;
108 }
109
110 ul = (unsigned long) si->si_addr;
111 ul = ul / page_size();
112 ul = ul * page_size();
113 aligned = (void*) ul;
114
115 /* Let's remember which address failed */
116 sigbus_push(aligned);
117
118 /* Replace mapping with an anonymous page, so that the
119 * execution can continue, however with a zeroed out page */
120 assert_se(mmap(aligned, page_size(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == aligned);
121 }
122
123 void sigbus_install(void) {
124 struct sigaction sa = {
125 .sa_sigaction = sigbus_handler,
126 .sa_flags = SA_SIGINFO,
127 };
128
129 /* make sure that sysconf() is not called from a signal handler because
130 * it is not guaranteed to be async-signal-safe since POSIX.1-2008 */
131 (void) page_size();
132
133 n_installed++;
134
135 if (n_installed == 1)
136 assert_se(sigaction(SIGBUS, &sa, &old_sigaction) == 0);
137
138 return;
139 }
140
141 void sigbus_reset(void) {
142
143 if (n_installed <= 0)
144 return;
145
146 n_installed--;
147
148 if (n_installed == 0)
149 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
150
151 return;
152 }