Debian Codegraph @

src/shared/coredump-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <elf.h>
4 #include <sys/prctl.h>
5
6 #include "alloc-util.h"
7 #include "coredump-util.h"
8 #include "errno-util.h"
9 #include "extract-word.h"
10 #include "fileio.h"
11 #include "log.h"
12 #include "parse-util.h"
13 #include "stdio-util.h"
14 #include "string-table.h"
15 #include "string-util.h"
16 #include "unaligned.h" /* IWYU pragma: keep */
17 #include "virt.h"
18
19 int set_dumpable(SuidDumpMode mode) {
20 /* Cast mode explicitly to long, because prctl wants longs but is varargs. */
21 return RET_NERRNO(prctl(PR_SET_DUMPABLE, (long) mode));
22 }
23
24 static const char *const coredump_filter_table[_COREDUMP_FILTER_MAX] = {
25 [COREDUMP_FILTER_PRIVATE_ANONYMOUS] = "private-anonymous",
26 [COREDUMP_FILTER_SHARED_ANONYMOUS] = "shared-anonymous",
27 [COREDUMP_FILTER_PRIVATE_FILE_BACKED] = "private-file-backed",
28 [COREDUMP_FILTER_SHARED_FILE_BACKED] = "shared-file-backed",
29 [COREDUMP_FILTER_ELF_HEADERS] = "elf-headers",
30 [COREDUMP_FILTER_PRIVATE_HUGE] = "private-huge",
31 [COREDUMP_FILTER_SHARED_HUGE] = "shared-huge",
32 [COREDUMP_FILTER_PRIVATE_DAX] = "private-dax",
33 [COREDUMP_FILTER_SHARED_DAX] = "shared-dax",
34 };
35
36 DEFINE_STRING_TABLE_LOOKUP(coredump_filter, CoredumpFilter);
37
38 int coredump_filter_mask_from_string(const char *s, uint64_t *ret) {
39 uint64_t m = 0;
40
41 assert(s);
42 assert(ret);
43
44 for (;;) {
45 _cleanup_free_ char *n = NULL;
46 CoredumpFilter v;
47 int r;
48
49 r = extract_first_word(&s, &n, NULL, 0);
50 if (r < 0)
51 return r;
52 if (r == 0)
53 break;
54
55 if (streq(n, "default")) {
56 m |= COREDUMP_FILTER_MASK_DEFAULT;
57 continue;
58 }
59
60 if (streq(n, "all")) {
61 m = COREDUMP_FILTER_MASK_ALL;
62 continue;
63 }
64
65 v = coredump_filter_from_string(n);
66 if (v >= 0) {
67 m |= 1u << v;
68 continue;
69 }
70
71 uint64_t x;
72 r = safe_atoux64(n, &x);
73 if (r < 0)
74 return r;
75
76 m |= x;
77 }
78
79 *ret = m;
80 return 0;
81 }
82
83 #define _DEFINE_PARSE_AUXV(size, type, unaligned_read) \
84 static int parse_auxv##size( \
85 int log_level, \
86 const void *auxv, \
87 size_t size_bytes, \
88 int *at_secure, \
89 uid_t *uid, \
90 uid_t *euid, \
91 gid_t *gid, \
92 gid_t *egid) { \
93 \
94 assert(auxv || size_bytes == 0); \
95 assert(at_secure); \
96 assert(uid); \
97 assert(euid); \
98 assert(gid); \
99 assert(egid); \
100 \
101 if (size_bytes % (2 * sizeof(type)) != 0) \
102 return log_full_errno(log_level, \
103 SYNTHETIC_ERRNO(EIO), \
104 "Incomplete auxv structure (%zu bytes).", \
105 size_bytes); \
106 \
107 size_t words = size_bytes / sizeof(type); \
108 \
109 /* Note that we set output variables even on error. */ \
110 \
111 for (size_t i = 0; i + 1 < words; i += 2) { \
112 type key, val; \
113 \
114 key = unaligned_read((uint8_t*) auxv + i * sizeof(type)); \
115 val = unaligned_read((uint8_t*) auxv + (i + 1) * sizeof(type)); \
116 \
117 switch (key) { \
118 case AT_SECURE: \
119 *at_secure = val != 0; \
120 break; \
121 case AT_UID: \
122 *uid = val; \
123 break; \
124 case AT_EUID: \
125 *euid = val; \
126 break; \
127 case AT_GID: \
128 *gid = val; \
129 break; \
130 case AT_EGID: \
131 *egid = val; \
132 break; \
133 case AT_NULL: \
134 if (val != 0) \
135 goto error; \
136 return 0; \
137 } \
138 } \
139 error: \
140 return log_full_errno(log_level, \
141 SYNTHETIC_ERRNO(ENODATA), \
142 "AT_NULL terminator not found, cannot parse auxv structure."); \
143 }
144
145 #define DEFINE_PARSE_AUXV(size) \
146 _DEFINE_PARSE_AUXV(size, uint##size##_t, unaligned_read_ne##size)
147
148 DEFINE_PARSE_AUXV(32);
149 DEFINE_PARSE_AUXV(64);
150
151 int parse_auxv(int log_level,
152 uint8_t elf_class,
153 const void *auxv,
154 size_t size_bytes,
155 int *at_secure,
156 uid_t *uid,
157 uid_t *euid,
158 gid_t *gid,
159 gid_t *egid) {
160
161 switch (elf_class) {
162 case ELFCLASS64:
163 return parse_auxv64(log_level, auxv, size_bytes, at_secure, uid, euid, gid, egid);
164 case ELFCLASS32:
165 return parse_auxv32(log_level, auxv, size_bytes, at_secure, uid, euid, gid, egid);
166 default:
167 return log_full_errno(log_level, SYNTHETIC_ERRNO(EPROTONOSUPPORT),
168 "Unknown ELF class %d.", elf_class);
169 }
170 }
171
172 int set_coredump_filter(uint64_t value) {
173 char t[HEXADECIMAL_STR_MAX(uint64_t)];
174
175 xsprintf(t, "0x%"PRIx64, value);
176
177 return write_string_file("/proc/self/coredump_filter", t,
178 WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
179 }
180
181 /* Turn off core dumps but only if we're running outside of a container. */
182 void disable_coredumps(void) {
183 int r;
184
185 if (detect_container() > 0)
186 return;
187
188 r = write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", WRITE_STRING_FILE_DISABLE_BUFFER);
189 if (r < 0)
190 log_debug_errno(r, "Failed to turn off coredumps, ignoring: %m");
191 }