Debian Codegraph @

src/shared/rm-rf.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/magic.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "btrfs-util.h"
9 #include "dirent-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "log.h"
14 #include "mountpoint-util.h"
15 #include "path-util.h"
16 #include "rm-rf.h"
17 #include "stat-util.h"
18 #include "string-util.h"
19
20 /* We treat tmpfs/ramfs + cgroupfs as non-physical file systems. cgroupfs is similar to tmpfs in a way
21 * after all: we can create arbitrary directory hierarchies in it, and hence can also use rm_rf() on it
22 * to remove those again. */
23 static bool is_physical_fs(const struct statfs *sfs) {
24 return !is_temporary_fs(sfs) && !is_fs_type(sfs, CGROUP2_SUPER_MAGIC);
25 }
26
27 static int patch_dirfd_mode(
28 int dfd,
29 bool refuse_already_set,
30 mode_t *ret_old_mode) {
31
32 struct stat st;
33 int r;
34
35 assert(dfd >= 0);
36 assert(ret_old_mode);
37
38 if (fstat(dfd, &st) < 0)
39 return -errno;
40 r = stat_verify_directory(&st);
41 if (r < 0)
42 return r;
43
44 if (FLAGS_SET(st.st_mode, 0700)) { /* Already set? */
45 if (refuse_already_set)
46 return -EACCES; /* original error */
47
48 *ret_old_mode = st.st_mode;
49 return 0;
50 }
51
52 if (st.st_uid != geteuid()) /* this only works if the UID matches ours */
53 return -EACCES;
54
55 r = fchmod_opath(dfd, (st.st_mode | 0700) & 07777);
56 if (r < 0)
57 return r;
58
59 *ret_old_mode = st.st_mode;
60 return 1;
61 }
62
63 int unlinkat_harder(int dfd, const char *filename, int unlink_flags, RemoveFlags remove_flags) {
64 mode_t old_mode;
65 int r;
66
67 /* Like unlinkat(), but tries harder: if we get EACCESS we'll try to set the r/w/x bits on the
68 * directory. This is useful if we run unprivileged and have some files where the w bit is
69 * missing. */
70
71 if (unlinkat(dfd, filename, unlink_flags) >= 0)
72 return 0;
73 if (errno != EACCES || !FLAGS_SET(remove_flags, REMOVE_CHMOD))
74 return -errno;
75
76 r = patch_dirfd_mode(dfd, /* refuse_already_set= */ true, &old_mode);
77 if (r < 0)
78 return r;
79
80 if (unlinkat(dfd, filename, unlink_flags) < 0) {
81 r = -errno;
82 /* Try to restore the original access mode if this didn't work */
83 (void) fchmod(dfd, old_mode & 07777);
84 return r;
85 }
86
87 if (FLAGS_SET(remove_flags, REMOVE_CHMOD_RESTORE) && fchmod(dfd, old_mode & 07777) < 0)
88 return -errno;
89
90 /* If this worked, we won't reset the old mode by default, since we'll need it for other entries too,
91 * and we should destroy the whole thing */
92 return 0;
93 }
94
95 int fstatat_harder(int dfd,
96 const char *filename,
97 struct stat *ret,
98 int fstatat_flags,
99 RemoveFlags remove_flags) {
100
101 mode_t old_mode;
102 int r;
103
104 /* Like unlink_harder() but does the same for fstatat() */
105
106 if (fstatat(dfd, filename, ret, fstatat_flags) >= 0)
107 return 0;
108 if (errno != EACCES || !FLAGS_SET(remove_flags, REMOVE_CHMOD))
109 return -errno;
110
111 r = patch_dirfd_mode(dfd, /* refuse_already_set= */ true, &old_mode);
112 if (r < 0)
113 return r;
114
115 if (fstatat(dfd, filename, ret, fstatat_flags) < 0) {
116 r = -errno;
117 (void) fchmod(dfd, old_mode & 07777);
118 return r;
119 }
120
121 if (FLAGS_SET(remove_flags, REMOVE_CHMOD_RESTORE) && fchmod(dfd, old_mode & 07777) < 0)
122 return -errno;
123
124 return 0;
125 }
126
127 static int openat_harder(int dfd, const char *path, int open_flags, RemoveFlags remove_flags, mode_t *ret_old_mode) {
128 _cleanup_close_ int pfd = -EBADF, fd = -EBADF;
129 bool chmod_done = false;
130 mode_t old_mode;
131 int r;
132
133 assert(dfd >= 0 || dfd == AT_FDCWD);
134 assert(path);
135
136 /* Unlike unlink_harder() and fstatat_harder(), this chmod the specified path. */
137
138 if (FLAGS_SET(open_flags, O_PATH) ||
139 !FLAGS_SET(open_flags, O_DIRECTORY) ||
140 !FLAGS_SET(remove_flags, REMOVE_CHMOD)) {
141
142 fd = RET_NERRNO(openat(dfd, path, open_flags));
143 if (fd < 0)
144 return fd;
145
146 if (ret_old_mode) {
147 struct stat st;
148
149 if (fstat(fd, &st) < 0)
150 return -errno;
151
152 *ret_old_mode = st.st_mode;
153 }
154
155 return TAKE_FD(fd);
156 }
157
158 pfd = RET_NERRNO(openat(dfd, path, (open_flags & (O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW)) | O_PATH));
159 if (pfd < 0)
160 return pfd;
161
162 if (FLAGS_SET(remove_flags, REMOVE_CHMOD)) {
163 r = patch_dirfd_mode(pfd, /* refuse_already_set= */ false, &old_mode);
164 if (r < 0)
165 return r;
166
167 chmod_done = r;
168 }
169
170 fd = fd_reopen(pfd, open_flags & ~O_NOFOLLOW);
171 if (fd < 0) {
172 if (chmod_done)
173 (void) fchmod_opath(pfd, old_mode & 07777);
174 return fd;
175 }
176
177 if (ret_old_mode)
178 *ret_old_mode = old_mode;
179
180 return TAKE_FD(fd);
181 }
182
183 static int rm_rf_children_impl(
184 int fd,
185 RemoveFlags flags,
186 const struct stat *root_dev,
187 mode_t old_mode);
188
189 static int rm_rf_inner_child(
190 int fd,
191 const char *fname,
192 int is_dir,
193 RemoveFlags flags,
194 const struct stat *root_dev,
195 bool allow_recursion) {
196
197 struct stat st;
198 int r, q = 0;
199
200 assert(fd >= 0);
201 assert(fname);
202
203 if (is_dir < 0 ||
204 root_dev ||
205 (is_dir > 0 && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
206
207 r = fstatat_harder(fd, fname, &st, AT_SYMLINK_NOFOLLOW, flags);
208 if (r < 0)
209 return r;
210
211 is_dir = S_ISDIR(st.st_mode);
212 }
213
214 if (is_dir) {
215 /* If root_dev is set, remove subdirectories only if device is same */
216 if (root_dev && st.st_dev != root_dev->st_dev)
217 return 0;
218
219 /* Stop at mount points */
220 r = is_mount_point_at(fd, fname, 0);
221 if (r < 0)
222 return r;
223 if (r > 0)
224 return 0;
225
226 if ((flags & REMOVE_SUBVOLUME) && btrfs_might_be_subvol(&st)) {
227 /* This could be a subvolume, try to remove it */
228
229 r = btrfs_subvol_remove_at(fd, fname, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
230 if (r < 0) {
231 if (!IN_SET(r, -ENOTTY, -EINVAL))
232 return r;
233
234 /* ENOTTY, then it wasn't a btrfs subvolume, continue below. */
235 } else
236 /* It was a subvolume, done. */
237 return 1;
238 }
239
240 if (!allow_recursion)
241 return -EISDIR;
242
243 mode_t old_mode;
244 int subdir_fd = openat_harder(fd, fname,
245 O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME,
246 flags, &old_mode);
247 if (subdir_fd < 0)
248 return subdir_fd;
249
250 /* We pass REMOVE_PHYSICAL here, to avoid doing the fstatfs() to check the file system type
251 * again for each directory */
252 q = rm_rf_children_impl(subdir_fd, flags | REMOVE_PHYSICAL, root_dev, old_mode);
253
254 } else if (flags & REMOVE_ONLY_DIRECTORIES)
255 return 0;
256
257 r = unlinkat_harder(fd, fname, is_dir ? AT_REMOVEDIR : 0, flags);
258 if (r < 0)
259 return r;
260 if (q < 0)
261 return q;
262 return 1;
263 }
264
265 typedef struct TodoEntry {
266 DIR *dir; /* A directory that we were operating on. */
267 char *dirname; /* The filename of that directory itself. */
268 mode_t old_mode; /* The original file mode. */
269 } TodoEntry;
270
271 static void free_todo_entries(TodoEntry **todos) {
272 assert(todos);
273
274 for (TodoEntry *x = *todos; x && x->dir; x++) {
275 closedir(x->dir);
276 free(x->dirname);
277 }
278
279 freep(todos);
280 }
281
282 int rm_rf_children(
283 int fd,
284 RemoveFlags flags,
285 const struct stat *root_dev) {
286
287 struct stat st;
288
289 assert(fd >= 0);
290
291 if (fstat(fd, &st) < 0)
292 return -errno;
293
294 return rm_rf_children_impl(fd, flags, root_dev, st.st_mode);
295 }
296
297 static int rm_rf_children_impl(
298 int fd,
299 RemoveFlags flags,
300 const struct stat *root_dev,
301 mode_t old_mode) {
302
303 _cleanup_(free_todo_entries) TodoEntry *todos = NULL;
304 size_t n_todo = 0;
305 _cleanup_free_ char *dirname = NULL; /* Set when we are recursing and want to delete ourselves */
306 int ret = 0, r;
307
308 /* Return the first error we run into, but nevertheless try to go on.
309 * The passed fd is closed in all cases, including on failure. */
310
311 for (;;) { /* This loop corresponds to the directory nesting level. */
312 _cleanup_closedir_ DIR *d = NULL;
313
314 if (n_todo > 0) {
315 /* We know that we are in recursion here, because n_todo is set.
316 * We need to remove the inner directory we were operating on. */
317 assert(dirname);
318 r = unlinkat_harder(dirfd(todos[n_todo-1].dir), dirname, AT_REMOVEDIR, flags);
319 if (r < 0 && r != -ENOENT) {
320 if (ret == 0)
321 ret = r;
322
323 if (FLAGS_SET(flags, REMOVE_CHMOD_RESTORE))
324 (void) fchmodat(dirfd(todos[n_todo-1].dir), dirname, old_mode & 07777, 0);
325 }
326 dirname = mfree(dirname);
327
328 /* And now let's back out one level up */
329 n_todo--;
330 d = TAKE_PTR(todos[n_todo].dir);
331 dirname = TAKE_PTR(todos[n_todo].dirname);
332 old_mode = todos[n_todo].old_mode;
333
334 assert(d);
335 fd = dirfd(d); /* Retrieve the file descriptor from the DIR object */
336 assert(fd >= 0);
337 } else {
338 next_fd:
339 assert(fd >= 0);
340 d = fdopendir(fd);
341 if (!d) {
342 safe_close(fd);
343 return -errno;
344 }
345 fd = dirfd(d); /* We donated the fd to fdopendir(). Let's make sure we sure we have
346 * the right descriptor even if it were to internally invalidate the
347 * one we passed. */
348
349 if (!(flags & REMOVE_PHYSICAL)) {
350 struct statfs sfs;
351
352 if (fstatfs(fd, &sfs) < 0)
353 return -errno;
354
355 if (is_physical_fs(&sfs)) {
356 /* We refuse to clean physical file systems with this call, unless
357 * explicitly requested. This is extra paranoia just to be sure we
358 * never ever remove non-state data. */
359
360 _cleanup_free_ char *path = NULL;
361
362 (void) fd_get_path(fd, &path);
363 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
364 "Attempted to remove disk file system under \"%s\", and we can't allow that.",
365 strna(path));
366 }
367 }
368 }
369
370 FOREACH_DIRENT_ALL(de, d, return -errno) {
371 int is_dir;
372
373 if (dot_or_dot_dot(de->d_name))
374 continue;
375
376 is_dir = de->d_type == DT_UNKNOWN ? -1 : de->d_type == DT_DIR;
377
378 r = rm_rf_inner_child(fd, de->d_name, is_dir, flags, root_dev, false);
379 if (r == -EISDIR) {
380 /* Push the current working state onto the todo list */
381
382 if (!GREEDY_REALLOC0(todos, n_todo + 2))
383 return log_oom();
384
385 _cleanup_free_ char *newdirname = strdup(de->d_name);
386 if (!newdirname)
387 return log_oom();
388
389 mode_t mode;
390 int newfd = openat_harder(fd, de->d_name,
391 O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME,
392 flags, &mode);
393 if (newfd >= 0) {
394 todos[n_todo++] = (TodoEntry) {
395 .dir = TAKE_PTR(d),
396 .dirname = TAKE_PTR(dirname),
397 .old_mode = old_mode
398 };
399
400 fd = newfd;
401 dirname = TAKE_PTR(newdirname);
402 old_mode = mode;
403
404 goto next_fd;
405
406 } else if (newfd != -ENOENT && ret == 0)
407 ret = newfd;
408
409 } else if (r < 0 && r != -ENOENT && ret == 0)
410 ret = r;
411 }
412
413 if (FLAGS_SET(flags, REMOVE_SYNCFS) && syncfs(fd) < 0 && ret >= 0)
414 ret = -errno;
415
416 if (n_todo == 0) {
417 if (FLAGS_SET(flags, REMOVE_CHMOD_RESTORE) &&
418 fchmod(fd, old_mode & 07777) < 0 && ret >= 0)
419 ret = -errno;
420
421 break;
422 }
423 }
424
425 return ret;
426 }
427
428 int rm_rf_at(int dir_fd, const char *path, RemoveFlags flags) {
429 mode_t old_mode;
430 int fd, r, q = 0;
431
432 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
433 assert(path);
434
435 /* For now, don't support dropping subvols when also only dropping directories, since we can't do
436 * this race-freely. */
437 if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES|REMOVE_SUBVOLUME))
438 return -EINVAL;
439
440 /* We refuse to clean the root file system with this call. This is extra paranoia to never cause a
441 * really seriously broken system. */
442 if (path_is_root_at(dir_fd, path) > 0)
443 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
444 "Attempted to remove entire root file system, and we can't allow that.");
445
446 if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
447 /* Try to remove as subvolume first */
448 r = btrfs_subvol_remove_at(dir_fd, path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
449 if (r >= 0)
450 return r;
451
452 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && r == -ENOENT)
453 return 0;
454
455 if (!IN_SET(r, -ENOTTY, -EINVAL, -ENOTDIR, -EPERM, -EACCES))
456 return r;
457
458 /* Not btrfs or not a subvolume, or permissions are not available (but might if we go via unlinkat()) */
459 }
460
461 /* In the next step we'll try to open the directory in order to enumerate its contents. This might
462 * not work due to perms, but we might still be able to delete it, hence let's try that first. */
463 if (FLAGS_SET(flags, REMOVE_ROOT | REMOVE_PHYSICAL))
464 if (unlinkat(dir_fd, path, AT_REMOVEDIR) >= 0)
465 return 0;
466
467 fd = openat_harder(dir_fd, path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME, flags, &old_mode);
468 if (fd >= 0) {
469 /* We have a dir */
470 r = rm_rf_children_impl(fd, flags, NULL, old_mode);
471
472 if (FLAGS_SET(flags, REMOVE_ROOT))
473 q = RET_NERRNO(unlinkat(dir_fd, path, AT_REMOVEDIR));
474 } else {
475 r = fd;
476 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && r == -ENOENT)
477 return 0;
478
479 if (!IN_SET(r, -ENOTDIR, -ELOOP))
480 return r;
481
482 if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES) || !FLAGS_SET(flags, REMOVE_ROOT))
483 return 0;
484
485 if (!FLAGS_SET(flags, REMOVE_PHYSICAL)) {
486 struct statfs s;
487
488 r = xstatfsat(dir_fd, path, &s);
489 if (r < 0)
490 return r;
491 if (is_physical_fs(&s))
492 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
493 "Attempted to remove files from a disk file system under \"%s\", refusing.",
494 path);
495 }
496
497 r = 0;
498 q = RET_NERRNO(unlinkat(dir_fd, path, 0));
499 }
500
501 if (r < 0)
502 return r;
503 if (q < 0 && (q != -ENOENT || !FLAGS_SET(flags, REMOVE_MISSING_OK)))
504 return q;
505 return 0;
506 }
507
508 int rm_rf_child(int fd, const char *name, RemoveFlags flags) {
509
510 /* Removes one specific child of the specified directory */
511
512 if (fd < 0)
513 return -EBADF;
514
515 if (!filename_is_valid(name))
516 return -EINVAL;
517
518 if ((flags & (REMOVE_ROOT|REMOVE_MISSING_OK)) != 0) /* Doesn't really make sense here, we are not supposed to remove 'fd' anyway */
519 return -EINVAL;
520
521 if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES|REMOVE_SUBVOLUME))
522 return -EINVAL;
523
524 return rm_rf_inner_child(fd, name, -1, flags, NULL, true);
525 }
526
527 const char* rm_rf_safe(const char *p) {
528 PROTECT_ERRNO;
529
530 if (!p)
531 return NULL;
532
533 (void) rm_rf(p, REMOVE_ROOT|REMOVE_MISSING_OK|REMOVE_CHMOD);
534 return NULL;
535 }
536
537 char* rm_rf_physical_and_free(char *p) {
538 PROTECT_ERRNO;
539
540 if (!p)
541 return NULL;
542
543 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK|REMOVE_CHMOD);
544 return mfree(p);
545 }
546
547 char* rm_rf_subvolume_and_free(char *p) {
548 PROTECT_ERRNO;
549
550 if (!p)
551 return NULL;
552
553 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_MISSING_OK|REMOVE_CHMOD);
554 return mfree(p);
555 }