Commit Graph
20 Commits
Author SHA1 Message Date
Yu Watanabe 7be9657706 rm-rf: also chmod() directory if it cannot be opened
Otherwise, directory with zero access mode cannot be removed.

This is a revised version of 808c8b25ee,
- dropped O_NOFOLLOW from fd_reopen()
- fixed error handling on opening path in rm_rf().
2023-03-31 11:52:03 +09:00
Luca Boccassi cfe9d45207 Revert "rm-rf: also chmod() directory if it cannot be opened"
This reverts commit 808c8b25ee.
2023-03-30 16:48:22 +01:00
Yu Watanabe 808c8b25ee rm-rf: also chmod() directory if it cannot be opened
Otherwise, directory with zero access mode cannot be removed.
2023-03-25 05:55:38 +09:00
Yu Watanabe da19c07198 rm-rf: mask file mode with 07777 when passed to chmod()
No functional change hopefully, just for safety.
2023-03-25 04:50:27 +09:00
Yu Watanabe cd2cd095db rm-rf: fix errno handling 2023-03-25 04:50:27 +09:00
Zbigniew Jędrzejewski-Szmek 5b1cf7a9be shared/rm-rf: loop over nested directories instead of instead of recursing
To remove directory structures, we need to remove the innermost items first,
and then recursively remove higher-level directories. We would recursively
descend into directories and invoke rm_rf_children and rm_rm_children_inner.
This is problematic when too many directories are nested.

Instead, let's create a "TODO" queue. In the the queue, for each level we
hold the DIR* object we were working on, and the name of the directory. This
allows us to leave a partially-processed directory, and restart the removal
loop one level down. When done with the inner directory, we use the name to
unlinkat() it from the parent, and proceed with the removal of other items.

Because the nesting is increased by one level, it is best to view this patch
with -b/--ignore-space-change.

This fixes CVE-2021-3997, https://bugzilla.redhat.com/show_bug.cgi?id=2024639.
The issue was reported and patches reviewed by Qualys Team.
Mauro Matteo Cascella and Riccardo Schirone from Red Hat handled the disclosure.
2021-12-21 14:39:50 +01:00
Zbigniew Jędrzejewski-Szmek 84ced33002 shared/rm_rf: refactor rm_rf() to shorten code a bit 2021-12-21 14:36:56 +01:00
Zbigniew Jędrzejewski-Szmek 3bac86abfa shared/rm_rf: refactor rm_rf_children_inner() to shorten code a bit 2021-12-21 14:36:56 +01:00
Zbigniew Jędrzejewski-Szmek c7f0d9e5ac tree-wide: make FOREACH_DIRENT_ALL define the iterator variable
The variable is not useful outside of the loop (it'll always be null
after the loop is finished), so we can declare it inline in the loop.
This saves one variable declaration and reduces the chances that somebody
tries to use the variable outside of the loop.

For consistency, 'de' is used everywhere for the var name.
2021-12-15 16:19:13 +01:00
Zbigniew Jędrzejewski-Szmek 160dadc035 tmpfiles: 'st' may have been used uninitialized 2021-11-30 22:57:31 +01:00
Lennart Poettering bdfe7ada0d rm-rf: optionally fsync() after removing directory tree 2021-10-11 16:00:34 +02:00
Yu Watanabe bdbb61f69f tree-wide: fix typo 2021-08-18 13:36:14 +02:00
Lennart Poettering 1f0fb7d544 rm-rf: refactor rm_rf_children(), split out body of directory iteration loop
This splits out rm_rf_children_inner() as body of the loop. We can use
that to implement rm_rf_child() for deleting one specific entry in a
directory.
2021-07-30 16:14:37 +02:00
Zbigniew Jędrzejewski-Szmek b25a930f0e basic,shared: move a bunch of files to src/shared/
The goal is to move everything that requires selinux or smack
away from src/basic/. This means that src/basic/label.[ch] must move,
which implies btrfs-util.[ch], copy.[ch], and a bunch of other files
which form a cluster of internal use.

This is just moving text around, so there should be no functional difference.

test-blockdev-util is new, because path_is_encrypted() is moved to
blockdev-util.c, and so far we didn't have any tests for code there.
2021-06-24 10:11:00 +02:00
Kay Sievers a095315b3c build-sys: split internal basic/ library from shared/
basic/      can be used by everything
            cannot use anything outside of basic/

libsystemd/ can use basic/
            cannot use shared/

shared/     can use libsystemd/
2015-06-11 10:52:46 +02:00
Martin Pitt 5d40903401 path-util: Fix path_is_mount_point for files
Commits 27cc6f166 and f25afeb broke path_is_mount_point() for files (such as
/etc/machine-id → /run/machine-id bind mounts) as with the factorization of
fd_is_mount_point() we lost the parent directory. We cannot determine that from
an fd only as openat(fd, "..") only works for directory fds.

Change fd_is_mount_point() to behave like openat(): It now takes a file
descriptor of the containing directory, a file name in it, and flags (which can
be 0 or AT_SYMLINK_FOLLOW). Unlike name_to_handle_at() or openat(), fstatat()
only accepts the inverse flag AT_SYMLINK_NOFOLLOW and complains with EINVAL
about AT_SYMLINK_FOLLOW; so we need to transform the flags for that fallback.

Adjust rm_rf_children() accordingly (only other caller of fd_is_mount_point()
aside from path_is_mount_point()).

Add test cases for files, links, and file bind mounts (the latter will only
work when running as root). Split out a new test_path_is_mount_point() test
case function as it got significantly larger now.
2015-05-29 17:40:12 +02:00
Lennart Poettering d9e2daaf3d btrfs: support recursively removing btrfs snapshots 2015-04-06 11:28:16 +02:00
Lennart Poettering 9e9b663aae rm-rf: add support for recursively removing btrfs subvolumes 2015-04-06 10:57:53 +02:00
Lennart Poettering f25afeb6ab rm-rf: never cross mount points 2015-04-06 10:57:53 +02:00
Lennart Poettering c687863750 util: rework rm_rf() logic
- Move to its own file rm-rf.c

- Change parameters into a single flags parameter

- Remove "honour sticky" logic, it's unused these days
2015-04-06 10:57:53 +02:00