Debian Codegraph @

src/test/test-luo.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /* Helper for TEST-91-LIVEUPDATE: creates memfds and stores them in the fd store,
4 * creates a LUO session directly via /dev/liveupdate and stores a memfd in it,
5 * or verifies everything after kexec.
6 *
7 * Usage:
8 * test-luo store - create memfds and a LUO session, push all to the fd store
9 * test-luo check - verify fd store content and LUO session memfd after kexec
10 * test-luo store-hijack - store a fd store entry holding a child LUO session named like
11 * PID 1's own ("systemd"), to exercise the serialize-side anti-hijack guard
12 * test-luo check-hijack - verify the hijacking session fd was NOT serialized/restored after kexec
13 */
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include "sd-daemon.h"
20
21 #include "fd-util.h"
22 #include "log.h"
23 #include "luo-util.h"
24 #include "main-func.h"
25 #include "memfd-util.h"
26 #include "parse-util.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "time-util.h"
30
31 #define TEST_DATA_1 "liveupdate-test-data-1"
32 #define TEST_DATA_2 "liveupdate-test-data-2"
33 #define SESSION_MEMFD_DATA "luo-session-memfd-test-data"
34 #define SESSION_MEMFD_TOKEN UINT64_C(42)
35
36 /* Name PID 1 reserves for its own LUO session (must match LUO_SESSION_NAME). A unit that tries to
37 * preserve a child session under this name is an attempt to hijack PID 1's namespace, and the
38 * serialize-side guard in manager_luo_serialize_fd_stores() must refuse to serialize it. */
39 #define HIJACK_SESSION_NAME LUO_SESSION_NAME
40 #define HIJACK_FDNAME "hijackfd"
41 #define HIJACK_MEMFD_DATA "luo-hijack-memfd-test-data"
42 #define HIJACK_MEMFD_TOKEN UINT64_C(99)
43
44 static int do_store(const char *prefix) {
45 _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF;
46 int r;
47
48 fd1 = memfd_new_and_seal("luo-test-1", TEST_DATA_1, strlen(TEST_DATA_1));
49 if (fd1 < 0)
50 return log_error_errno(fd1, "Failed to create memfd 1: %m");
51
52 fd2 = memfd_new_and_seal("luo-test-2", TEST_DATA_2, strlen(TEST_DATA_2));
53 if (fd2 < 0)
54 return log_error_errno(fd2, "Failed to create memfd 2: %m");
55
56 r = sd_pid_notify_with_fds(0, /* unset_environment= */ false, "FDSTORE=1\nFDNAME=testfd1", &fd1, 1);
57 if (r < 0)
58 return log_error_errno(r, "Failed to store memfd 1 in fd store: %m");
59
60 r = sd_pid_notify_with_fds(0, /* unset_environment= */ false, "FDSTORE=1\nFDNAME=testfd2", &fd2, 1);
61 if (r < 0)
62 return log_error_errno(r, "Failed to store memfd 2 in fd store: %m");
63
64 log_info("Stored 2 memfds in fd store.");
65
66 /* Create a LUO session directly via /dev/liveupdate, put a memfd in it, and store the session fd */
67 _cleanup_close_ int device_fd = -EBADF, session_fd = -EBADF, session_memfd = -EBADF;
68 const char *session_name = strjoina(prefix, "-direct");
69
70 device_fd = luo_open_device();
71 if (device_fd < 0)
72 return log_error_errno(device_fd, "Failed to open /dev/liveupdate: %m");
73
74 session_fd = luo_create_session(device_fd, session_name);
75 if (session_fd < 0)
76 return log_error_errno(session_fd, "Failed to create LUO session '%s': %m", session_name);
77
78 session_memfd = memfd_new_and_seal("session-test", SESSION_MEMFD_DATA, strlen(SESSION_MEMFD_DATA));
79 if (session_memfd < 0)
80 return log_error_errno(session_memfd, "Failed to create session memfd: %m");
81
82 r = luo_session_preserve_fd(session_fd, session_memfd, SESSION_MEMFD_TOKEN);
83 if (r < 0)
84 return log_error_errno(r, "Failed to preserve memfd in session: %m");
85
86 r = sd_pid_notifyf_with_fds(0, false, &session_fd, 1, "FDSTORE=1\nFDNAME=%s-direct", prefix);
87 if (r < 0)
88 return log_error_errno(r, "Failed to store session fd in fd store: %m");
89 TAKE_FD(session_fd);
90
91 log_info("Stored LUO session '%s' with memfd in fd store.", session_name);
92
93 /* Wait for PID 1 to actually process all our FDSTORE notifications before we exit, otherwise
94 * the cgroup-based pidref to unit lookup may fail once we're gone, and the fds end up closed. */
95 r = sd_notify_barrier(0, 5 * USEC_PER_SEC);
96 if (r < 0)
97 return log_error_errno(r, "Failed to wait for notification barrier: %m");
98
99 return 0;
100 }
101
102 static int do_check(const char *prefix) {
103 const char *e;
104 _cleanup_strv_free_ char **names = NULL;
105 const char *session_fdname = strjoina(prefix, "-direct");
106 size_t n_fds;
107 int r;
108
109 /* sd_listen_fds_with_names() checks LISTEN_PID which won't match since we're a child process.
110 * Read LISTEN_FDS and LISTEN_FDNAMES directly from the environment instead. */
111 e = getenv("LISTEN_FDS");
112 if (!e)
113 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No LISTEN_FDS environment variable set");
114
115 r = safe_atozu(e, &n_fds);
116 if (r < 0)
117 return log_error_errno(r, "Failed to parse LISTEN_FDS='%s': %m", e);
118 if (n_fds == 0)
119 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No file descriptors in fd store after kexec");
120
121 log_info("Got %zu fd(s) in fd store after kexec.", n_fds);
122
123 /* Parse LISTEN_FDNAMES to match fds by name, not position */
124 e = getenv("LISTEN_FDNAMES");
125 if (!e)
126 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No LISTEN_FDNAMES environment variable set");
127
128 names = strv_split(e, ":");
129 if (!names)
130 return log_oom();
131 assert(n_fds == strv_length(names));
132
133 static const struct {
134 const char *name;
135 const char *expected;
136 } checks[] = {
137 { "testfd1", TEST_DATA_1 },
138 { "testfd2", TEST_DATA_2 },
139 };
140
141 if (n_fds < ELEMENTSOF(checks))
142 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
143 "Not enough fds in fd store after kexec: expected at least %zu, got %zu",
144 ELEMENTSOF(checks), n_fds);
145
146 for (size_t i = 0; i < ELEMENTSOF(checks); i++) {
147 char buf[256];
148 ssize_t n;
149 size_t idx = 0;
150 int fd = -EBADF;
151
152 /* Find the fd by name */
153 STRV_FOREACH(name, names) {
154 if (idx >= n_fds)
155 break;
156 if (streq(*name, checks[i].name)) {
157 fd = SD_LISTEN_FDS_START + idx;
158 break;
159 }
160 idx++;
161 }
162
163 if (fd < 0)
164 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
165 "fd '%s' not found in LISTEN_FDNAMES", checks[i].name);
166
167 /* memfds are sealed; pread() avoids needing a separate lseek() */
168 n = pread(fd, buf, sizeof(buf) - 1, 0);
169 if (n < 0)
170 return log_error_errno(errno, "Failed to read fd %d: %m", fd);
171
172 buf[n] = '\0';
173
174 if (!streq(buf, checks[i].expected))
175 return log_error_errno(
176 SYNTHETIC_ERRNO(EBADMSG),
177 "Content mismatch for '%s': expected '%s', got '%s'",
178 checks[i].name, checks[i].expected, buf);
179
180 /* Remove the fd from the fd store so we don't keep accumulating duplicates across
181 * repeated invocations (and across repeated kexec cycles). */
182 r = sd_pid_notifyf(0, /* unset_environment= */ false,
183 "FDSTOREREMOVE=1\nFDNAME=%s", checks[i].name);
184 if (r < 0)
185 return log_error_errno(r, "Failed to remove fd '%s' from fd store: %m", checks[i].name);
186
187 log_info("Verified fd '%s': content matches.", checks[i].name);
188 }
189
190 log_info("All fd store checks passed.");
191
192 /* Verify the LUO session fd survived and its memfd content is intact */
193 int session_fd = -EBADF;
194 size_t idx = 0;
195 STRV_FOREACH(name, names) {
196 if (idx >= n_fds)
197 break;
198 if (streq(*name, session_fdname)) {
199 session_fd = SD_LISTEN_FDS_START + idx;
200 break;
201 }
202 idx++;
203 }
204
205 if (session_fd < 0)
206 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
207 "LUO session fd '%s' not found in fd store", session_fdname);
208
209 r = fd_is_luo_session(session_fd);
210 if (r < 0)
211 return log_error_errno(r, "Failed to check if fd is LUO session: %m");
212 if (r == 0)
213 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
214 "fd '%s' is not a LUO session!", session_fdname);
215
216 _cleanup_close_ int session_memfd = luo_session_retrieve_fd(session_fd, SESSION_MEMFD_TOKEN);
217 if (session_memfd < 0)
218 return log_error_errno(session_memfd, "Failed to retrieve memfd from session: %m");
219
220 char sbuf[256];
221 ssize_t sn = pread(session_memfd, sbuf, sizeof(sbuf) - 1, 0);
222 if (sn < 0)
223 return log_error_errno(errno, "Failed to read session memfd: %m");
224 sbuf[sn] = '\0';
225
226 if (!streq(sbuf, SESSION_MEMFD_DATA))
227 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
228 "Session memfd content mismatch: expected '%s', got '%s'",
229 SESSION_MEMFD_DATA, sbuf);
230
231 /* Remove the LUO session fd from the fd store as well. */
232 r = sd_pid_notifyf(0, /* unset_environment= */ false,
233 "FDSTOREREMOVE=1\nFDNAME=%s", session_fdname);
234 if (r < 0)
235 return log_error_errno(r, "Failed to remove fd '%s' from fd store: %m", session_fdname);
236
237 log_info("Verified LUO session memfd content matches.");
238
239 /* Wait for PID 1 to actually process all our FDSTORE notifications before we exit, otherwise
240 * the cgroup-based pidref to unit lookup may fail once we're gone, and the fds end up closed. */
241 r = sd_notify_barrier(0, 5 * USEC_PER_SEC);
242 if (r < 0)
243 return log_error_errno(r, "Failed to wait for notification barrier: %m");
244
245 return 0;
246 }
247
248 static int do_store_hijack(void) {
249 int r;
250
251 /* Create a child LUO session named exactly like PID 1's own session, put a memfd in it, and push the
252 * session fd into our fd store. On kexec, PID 1's manager_luo_serialize_fd_stores() must detect the
253 * reserved session name and refuse to serialize this entry (anti-hijack guard). */
254 _cleanup_close_ int device_fd = -EBADF, session_fd = -EBADF, session_memfd = -EBADF;
255
256 device_fd = luo_open_device();
257 if (device_fd < 0)
258 return log_error_errno(device_fd, "Failed to open /dev/liveupdate: %m");
259
260 session_fd = luo_create_session(device_fd, HIJACK_SESSION_NAME);
261 if (session_fd < 0)
262 return log_error_errno(session_fd, "Failed to create hijacking LUO session '%s': %m", HIJACK_SESSION_NAME);
263
264 session_memfd = memfd_new_and_seal("hijack-test", HIJACK_MEMFD_DATA, strlen(HIJACK_MEMFD_DATA));
265 if (session_memfd < 0)
266 return log_error_errno(session_memfd, "Failed to create hijack session memfd: %m");
267
268 r = luo_session_preserve_fd(session_fd, session_memfd, HIJACK_MEMFD_TOKEN);
269 if (r < 0)
270 return log_error_errno(r, "Failed to preserve memfd in hijack session: %m");
271
272 r = sd_pid_notify_with_fds(0, /* unset_environment= */ false, "FDSTORE=1\nFDNAME=" HIJACK_FDNAME, &session_fd, 1);
273 if (r < 0)
274 return log_error_errno(r, "Failed to store hijack session fd in fd store: %m");
275 TAKE_FD(session_fd);
276
277 log_info("Stored hijacking LUO session '%s' with memfd in fd store.", HIJACK_SESSION_NAME);
278
279 /* Wait for PID 1 to actually process the FDSTORE notification before we exit, otherwise
280 * the cgroup-based pidref to unit lookup may fail once we're gone, and the fd ends up closed. */
281 r = sd_notify_barrier(0, 5 * USEC_PER_SEC);
282 if (r < 0)
283 return log_error_errno(r, "Failed to wait for notification barrier: %m");
284
285 return 0;
286 }
287
288 static int do_check_hijack(void) {
289 _cleanup_strv_free_ char **names = NULL;
290 const char *e;
291 size_t n_fds;
292 int r;
293
294 /* The hijacking session fd ("hijackfd") must NOT have survived kexec: PID 1 refused to serialize it
295 * because its session name infringes PID 1's reserved namespace. So it must be absent from
296 * LISTEN_FDNAMES here. */
297 e = getenv("LISTEN_FDS");
298 if (!e) {
299 log_info("No LISTEN_FDS set after kexec, hijack fd correctly not restored.");
300 return 0;
301 }
302
303 r = safe_atozu(e, &n_fds);
304 if (r < 0)
305 return log_error_errno(r, "Failed to parse LISTEN_FDS='%s': %m", e);
306
307 e = getenv("LISTEN_FDNAMES");
308 if (!e) {
309 if (n_fds == 0) {
310 log_info("No fds restored after kexec, hijack fd correctly not restored.");
311 return 0;
312 }
313 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "LISTEN_FDS=%zu but no LISTEN_FDNAMES set", n_fds);
314 }
315
316 names = strv_split(e, ":");
317 if (!names)
318 return log_oom();
319
320 if (strv_contains(names, HIJACK_FDNAME))
321 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
322 "Hijacking session fd '%s' was restored after kexec, anti-hijack guard failed!",
323 HIJACK_FDNAME);
324
325 log_info("Verified hijacking session fd '%s' was not restored after kexec.", HIJACK_FDNAME);
326 return 0;
327 }
328
329 static int run(int argc, char *argv[]) {
330 if (argc < 2 || argc > 3)
331 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Usage: %s store|check|store-hijack|check-hijack [PREFIX]", argv[0]);
332
333 const char *prefix = argc > 2 ? argv[2] : "luosession";
334
335 if (streq(argv[1], "store"))
336 return do_store(prefix);
337 if (streq(argv[1], "check"))
338 return do_check(prefix);
339 if (streq(argv[1], "store-hijack"))
340 return do_store_hijack();
341 if (streq(argv[1], "check-hijack"))
342 return do_check_hijack();
343
344 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command: %s", argv[1]);
345 }
346
347 DEFINE_MAIN_FUNCTION(run);