Debian Codegraph @

src/import/import-common.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <sys/prctl.h>
5 #include <unistd.h>
6
7 #include "sd-event.h"
8
9 #include "capability-util.h"
10 #include "compress.h"
11 #include "dirent-util.h"
12 #include "dissect-image.h"
13 #include "fd-util.h"
14 #include "fs-util.h"
15 #include "import-common.h"
16 #include "libarchive-util.h"
17 #include "log.h"
18 #include "namespace-util.h"
19 #include "nsresource.h"
20 #include "os-util.h"
21 #include "pidref.h"
22 #include "process-util.h"
23 #include "rm-rf.h"
24 #include "selinux-util.h"
25 #include "stat-util.h"
26 #include "tar-util.h"
27 #include "tmpfile-util.h"
28
29 int import_fork_tar_x(int tree_fd, int userns_fd, PidRef *ret_pid) {
30 int r;
31
32 assert(tree_fd >= 0);
33 assert(ret_pid);
34
35 r = DLOPEN_LIBARCHIVE(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
36 if (r < 0)
37 return r;
38
39 TarFlags flags =
40 (userns_fd >= 0 ? TAR_SQUASH_UIDS_ABOVE_64K : 0) |
41 (mac_selinux_use() ? TAR_SELINUX : 0);
42
43 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
44 if (pipe2(pipefd, O_CLOEXEC) < 0)
45 return log_error_errno(errno, "Failed to create pipe for tar: %m");
46
47 (void) fcntl(pipefd[0], F_SETPIPE_SZ, COMPRESS_PIPE_BUFFER_SIZE);
48
49 r = pidref_safe_fork_full(
50 "tar-x",
51 /* stdio_fds= */ NULL,
52 (int[]) { tree_fd, pipefd[0], userns_fd }, userns_fd >= 0 ? 3 : 2,
53 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_REOPEN_LOG,
54 ret_pid);
55 if (r < 0)
56 return r;
57 if (r == 0) {
58 static const uint64_t retain =
59 (1ULL << CAP_CHOWN) |
60 (1ULL << CAP_FOWNER) |
61 (1ULL << CAP_FSETID) |
62 (1ULL << CAP_MKNOD) |
63 (1ULL << CAP_SETFCAP) |
64 (1ULL << CAP_DAC_OVERRIDE) |
65 (1ULL << CAP_DAC_READ_SEARCH);
66
67 /* Child */
68
69 if (userns_fd >= 0) {
70 r = detach_mount_namespace_userns(userns_fd);
71 if (r < 0) {
72 log_error_errno(r, "Failed to join user namespace: %m");
73 _exit(EXIT_FAILURE);
74 }
75 }
76
77 if (unshare(CLONE_NEWNET) < 0)
78 log_debug_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
79
80 r = capability_bounding_set_drop(retain, true);
81 if (r < 0)
82 log_debug_errno(r, "Failed to drop capabilities, ignoring: %m");
83
84 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
85 log_warning_errno(errno, "Failed to enable PR_SET_NO_NEW_PRIVS, ignoring: %m");
86
87 if (tar_x(pipefd[0], tree_fd, flags) < 0)
88 _exit(EXIT_FAILURE);
89
90 _exit(EXIT_SUCCESS);
91 }
92
93 return TAKE_FD(pipefd[1]);
94 }
95
96 int import_fork_tar_c(int tree_fd, int userns_fd, PidRef *ret_pid) {
97 int r;
98
99 assert(tree_fd >= 0);
100 assert(ret_pid);
101
102 r = DLOPEN_LIBARCHIVE(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
103 if (r < 0)
104 return r;
105
106 TarFlags flags =
107 (userns_fd >= 0 ? TAR_SQUASH_UIDS_ABOVE_64K : 0) |
108 (mac_selinux_use() ? TAR_SELINUX : 0);
109
110 _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
111 if (pipe2(pipefd, O_CLOEXEC) < 0)
112 return log_error_errno(errno, "Failed to create pipe for tar: %m");
113
114 (void) fcntl(pipefd[0], F_SETPIPE_SZ, COMPRESS_PIPE_BUFFER_SIZE);
115
116 r = pidref_safe_fork_full(
117 "tar-c",
118 /* stdio_fds= */ NULL,
119 (int[]) { tree_fd, pipefd[1], userns_fd }, userns_fd >= 0 ? 3 : 2,
120 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_REOPEN_LOG,
121 ret_pid);
122 if (r < 0)
123 return r;
124 if (r == 0) {
125 static const uint64_t retain = (1ULL << CAP_DAC_OVERRIDE);
126
127 /* Child */
128
129 if (userns_fd >= 0) {
130 r = detach_mount_namespace_userns(userns_fd);
131 if (r < 0) {
132 log_error_errno(r, "Failed to join user namespace: %m");
133 _exit(EXIT_FAILURE);
134 }
135 }
136
137 if (unshare(CLONE_NEWNET) < 0)
138 log_debug_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
139
140 r = capability_bounding_set_drop(retain, true);
141 if (r < 0)
142 log_debug_errno(r, "Failed to drop capabilities, ignoring: %m");
143
144 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
145 log_warning_errno(errno, "Failed to enable PR_SET_NO_NEW_PRIVS, ignoring: %m");
146
147 if (tar_c(tree_fd, pipefd[1], /* filename= */ NULL, flags) < 0)
148 _exit(EXIT_FAILURE);
149
150 _exit(EXIT_SUCCESS);
151 }
152
153 return TAKE_FD(pipefd[0]);
154 }
155
156 int import_mangle_os_tree_fd(int tree_fd, int userns_fd, ImportFlags flags) {
157 _cleanup_free_ char *child = NULL, *t = NULL, *joined = NULL;
158 _cleanup_closedir_ DIR *d = NULL, *cd = NULL;
159 struct dirent *dent;
160 struct stat st;
161 int r;
162
163 assert(tree_fd >= 0);
164
165 if (FLAGS_SET(flags, IMPORT_FOREIGN_UID) && userns_fd >= 0)
166 return import_mangle_os_tree_fd_foreign(tree_fd, userns_fd);
167
168 /* Some tarballs contain a single top-level directory that contains the actual OS directory tree. Try to
169 * recognize this, and move the tree one level up. */
170
171 _cleanup_free_ char *path = NULL;
172 r = fd_get_path(tree_fd, &path);
173 if (r < 0)
174 return log_error_errno(r, "Failed to determine path of fd: %m");
175
176 r = fd_is_os_tree(tree_fd);
177 if (r < 0)
178 return log_error_errno(r, "Failed to determine whether '%s' is an OS tree: %m", path);
179 if (r > 0) {
180 log_debug("Directory tree '%s' is a valid OS tree.", path);
181 return 0;
182 }
183
184 log_debug("Directory tree '%s' is not recognizable as OS tree, checking whether to rearrange it.", path);
185
186 d = xopendirat(tree_fd, /* path= */ NULL, /* flags= */ 0);
187 if (!d)
188 return log_error_errno(errno, "Failed to open directory '%s': %m", path);
189
190 errno = 0;
191 dent = readdir_no_dot(d);
192 if (!dent) {
193 if (errno != 0)
194 return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path);
195
196 log_debug("Directory '%s' is empty, leaving it as it is.", path);
197 return 0;
198 }
199
200 child = strdup(dent->d_name);
201 if (!child)
202 return log_oom();
203
204 errno = 0;
205 dent = readdir_no_dot(d);
206 if (dent) {
207 log_debug("Directory '%s' does not look like an OS tree, and has multiple children, leaving as it is.", path);
208 return 0;
209 } else if (errno != 0)
210 return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path);
211
212 _cleanup_close_ int child_fd = openat(dirfd(d), child, O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW|O_NONBLOCK);
213 if (child_fd < 0) {
214 if (IN_SET(errno, ENOTDIR, ELOOP)) {
215 log_debug_errno(errno, "Child '%s' of directory '%s' is not a directory, leaving things as they are.", child, path);
216 return 0;
217 }
218
219 return log_debug_errno(errno, "Failed to open file '%s/%s': %m", path, child);
220 }
221
222 if (fstat(child_fd, &st) < 0)
223 return log_debug_errno(errno, "Failed to stat file '%s/%s': %m", path, child);
224
225 joined = path_join(path, child);
226 if (!joined)
227 return log_oom();
228
229 r = fd_is_os_tree(child_fd);
230 if (r < 0)
231 return log_error_errno(r, "Failed to determine whether '%s' is an OS tree: %m", joined);
232 if (r == 0) {
233 log_debug("Neither '%s' nor '%s' is a valid OS tree, leaving them as they are.", path, joined);
234 return 0;
235 }
236
237 /* Nice, we have checked now:
238 *
239 * 1. The top-level directory does not qualify as OS tree
240 * 1. The top-level directory only contains one item
241 * 2. That item is a directory
242 * 3. And that directory qualifies as OS tree
243 *
244 * Let's now rearrange things, moving everything in the inner directory one level up */
245
246 cd = take_fdopendir(&child_fd);
247 if (!cd)
248 return log_error_errno(errno, "Can't open directory '%s': %m", joined);
249
250 log_info("Rearranging '%s', moving OS tree one directory up.", joined);
251
252 /* Let's rename the child to an unguessable name so that we can be sure all files contained in it can be
253 * safely moved up and won't collide with the name. */
254 r = tempfn_random(child, /* extra= */ NULL, &t);
255 if (r < 0)
256 return log_oom();
257 r = rename_noreplace(dirfd(d), child, dirfd(d), t);
258 if (r < 0)
259 return log_error_errno(r, "Unable to rename '%s' to '%s/%s': %m", joined, path, t);
260
261 FOREACH_DIRENT_ALL(de, cd, return log_error_errno(errno, "Failed to iterate through directory '%s': %m", joined)) {
262 if (dot_or_dot_dot(de->d_name))
263 continue;
264
265 r = rename_noreplace(dirfd(cd), de->d_name, dirfd(d), de->d_name);
266 if (r < 0)
267 return log_error_errno(r, "Unable to move '%s/%s/%s' to '%s/%s': %m", path, t, de->d_name, path, de->d_name);
268 }
269
270 if (unlinkat(dirfd(d), t, AT_REMOVEDIR) < 0)
271 return log_error_errno(errno, "Failed to remove temporary directory '%s/%s': %m", path, t);
272
273 r = futimens(dirfd(d), (struct timespec[2]) { st.st_atim, st.st_mtim });
274 if (r < 0)
275 log_debug_errno(errno, "Failed to adjust top-level timestamps '%s', ignoring: %m", path);
276
277 r = fchmod_and_chown(dirfd(d), st.st_mode, st.st_uid, st.st_gid);
278 if (r < 0)
279 return log_error_errno(r, "Failed to adjust top-level directory mode/ownership '%s': %m", path);
280
281 log_info("Successfully rearranged OS tree.");
282 return 0;
283 }
284
285 int import_mangle_os_tree(const char *path, int userns_fd, ImportFlags flags) {
286 assert(path);
287
288 _cleanup_close_ int fd = open(path, O_DIRECTORY|O_CLOEXEC|O_PATH);
289 if (fd < 0)
290 return log_error_errno(errno, "Failed to open '%s': %m", path);
291
292 return import_mangle_os_tree_fd(fd, userns_fd, flags);
293 }
294
295 int import_mangle_os_tree_fd_foreign(
296 int tree_fd,
297 int userns_fd) {
298
299 int r;
300
301 assert(tree_fd >= 0);
302 assert(userns_fd >= 0);
303
304 r = pidref_safe_fork_full(
305 "mangle-tree",
306 /* stdio_fds= */ NULL,
307 (int[]) { userns_fd, tree_fd }, 2,
308 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_REOPEN_LOG|FORK_WAIT,
309 /* ret= */ NULL);
310 if (r < 0)
311 return r;
312 if (r == 0) {
313 /* child */
314
315 r = namespace_enter(
316 /* pidns_fd= */ -EBADF,
317 /* mntns_fd= */ -EBADF,
318 /* netns_fd= */ -EBADF,
319 userns_fd,
320 /* root_fd= */ -EBADF);
321 if (r < 0) {
322 log_error_errno(r, "Failed to join user namespace: %m");
323 _exit(EXIT_FAILURE);
324 }
325
326 r = import_mangle_os_tree_fd(tree_fd, /* userns_fd= */ -EBADF, /* flags= */ 0);
327 if (r < 0) {
328 log_error_errno(r, "Failed to mangle OS tree in foreign UID mode: %m");
329 _exit(EXIT_FAILURE);
330 }
331
332 _exit(EXIT_SUCCESS);
333 }
334
335 return 0;
336
337 }
338
339 bool import_validate_local(const char *name, ImportFlags flags) {
340
341 /* By default we insist on a valid hostname for naming images. But optionally we relax that, in which
342 * case it can be any path name */
343
344 if (FLAGS_SET(flags, IMPORT_DIRECT))
345 return path_is_valid(name);
346
347 return image_name_is_valid(name);
348 }
349
350 static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
351 log_notice("Transfer aborted.");
352 sd_event_exit(sd_event_source_get_event(s), EINTR);
353 return 0;
354 }
355
356 int import_allocate_event_with_signals(sd_event **ret) {
357 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
358 int r;
359
360 assert(ret);
361
362 r = sd_event_default(&event);
363 if (r < 0)
364 return log_error_errno(r, "Failed to allocate event loop: %m");
365
366 (void) sd_event_add_signal(event, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK, interrupt_signal_handler, NULL);
367 (void) sd_event_add_signal(event, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK, interrupt_signal_handler, NULL);
368
369 *ret = TAKE_PTR(event);
370 return 0;
371 }
372
373 int import_make_foreign_userns(int *userns_fd) {
374 assert(userns_fd);
375
376 if (*userns_fd >= 0)
377 return 0;
378
379 *userns_fd = nsresource_allocate_userns(
380 /* vl= */ NULL,
381 /* name= */ NULL,
382 NSRESOURCE_UIDS_64K); /* allocate 64K users */
383 if (*userns_fd < 0)
384 return log_error_errno(*userns_fd, "Failed to allocate transient user namespace: %m");
385
386 return 1;
387 }
388
389 int import_remove_tree(const char *path, int *userns_fd, ImportFlags flags) {
390 int r;
391
392 assert(path);
393 assert(userns_fd);
394
395 /* Try the userns dance first, to remove foreign UID range owned trees */
396 if (FLAGS_SET(flags, IMPORT_FOREIGN_UID)) {
397 r = import_make_foreign_userns(userns_fd);
398 if (r < 0)
399 return r;
400
401 (void) remove_tree_foreign(path, *userns_fd);
402 }
403
404 r = rm_rf(path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_MISSING_OK|REMOVE_CHMOD);
405 if (r < 0)
406 return log_error_errno(r, "Failed to remove '%s': %m", path);
407
408 return 0;
409 }