]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/rm-rf.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / basic / rm-rf.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c6878637 2
11c3a366
TA
3#include <errno.h>
4#include <fcntl.h>
5#include <stdbool.h>
6#include <stddef.h>
11c3a366
TA
7#include <unistd.h>
8
265e9be7 9#include "alloc-util.h"
d9e2daaf 10#include "btrfs-util.h"
f0bef277 11#include "cgroup-util.h"
8fb3f009 12#include "dirent-util.h"
3ffd4af2 13#include "fd-util.h"
93cc7779
TA
14#include "log.h"
15#include "macro.h"
049af8ad 16#include "mountpoint-util.h"
07630cea 17#include "path-util.h"
3ffd4af2 18#include "rm-rf.h"
8fcde012 19#include "stat-util.h"
07630cea 20#include "string-util.h"
c6878637 21
f0bef277
EV
22static bool is_physical_fs(const struct statfs *sfs) {
23 return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs);
24}
25
2899fb02
LP
26static int unlinkat_harder(
27 int dfd,
28 const char *filename,
29 int unlink_flags,
30 RemoveFlags remove_flags) {
31
32 struct stat st;
33 int r;
34
35 /* Like unlinkat(), but tries harder: if we get EACCESS we'll try to set the r/w/x bits on the
36 * directory. This is useful if we run unprivileged and have some files where the w bit is
37 * missing. */
38
39 if (unlinkat(dfd, filename, unlink_flags) >= 0)
40 return 0;
41 if (errno != EACCES || !FLAGS_SET(remove_flags, REMOVE_CHMOD))
42 return -errno;
43
44 if (fstat(dfd, &st) < 0)
45 return -errno;
46 if (!S_ISDIR(st.st_mode))
47 return -ENOTDIR;
1d6cc5d0 48 if (FLAGS_SET(st.st_mode, 0700)) /* Already set? */
2899fb02
LP
49 return -EACCES; /* original error */
50 if (st.st_uid != geteuid()) /* this only works if the UID matches ours */
51 return -EACCES;
52
53 if (fchmod(dfd, (st.st_mode | 0700) & 07777) < 0)
54 return -errno;
55
56 if (unlinkat(dfd, filename, unlink_flags) < 0) {
57 r = -errno;
58 /* Try to restore the original access mode if this didn't work */
59 (void) fchmod(dfd, st.st_mode & 07777);
60 return r;
61 }
62
63 return 0;
64}
65
c6878637
LP
66int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
67 _cleanup_closedir_ DIR *d = NULL;
8fb3f009 68 struct dirent *de;
c6878637 69 int ret = 0, r;
f0bef277 70 struct statfs sfs;
c6878637
LP
71
72 assert(fd >= 0);
73
c0ba6b92
LP
74 /* This returns the first error we run into, but nevertheless tries to go on. This closes the passed
75 * fd, in all cases, including on failure.. */
c6878637
LP
76
77 if (!(flags & REMOVE_PHYSICAL)) {
78
f0bef277 79 r = fstatfs(fd, &sfs);
c6878637
LP
80 if (r < 0) {
81 safe_close(fd);
f0bef277 82 return -errno;
c6878637
LP
83 }
84
f0bef277 85 if (is_physical_fs(&sfs)) {
265e9be7
ZJS
86 /* We refuse to clean physical file systems with this call,
87 * unless explicitly requested. This is extra paranoia just
88 * to be sure we never ever remove non-state data. */
89 _cleanup_free_ char *path = NULL;
90
91 (void) fd_get_path(fd, &path);
92 log_error("Attempted to remove disk file system under \"%s\", and we can't allow that.",
93 strna(path));
c6878637 94
c6878637
LP
95 safe_close(fd);
96 return -EPERM;
97 }
98 }
99
100 d = fdopendir(fd);
101 if (!d) {
102 safe_close(fd);
103 return errno == ENOENT ? 0 : -errno;
104 }
105
8fb3f009 106 FOREACH_DIRENT_ALL(de, d, return -errno) {
c6878637
LP
107 bool is_dir;
108 struct stat st;
109
49bfc877 110 if (dot_or_dot_dot(de->d_name))
c6878637
LP
111 continue;
112
9e9b663a
LP
113 if (de->d_type == DT_UNKNOWN ||
114 (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
c6878637
LP
115 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
116 if (ret == 0 && errno != ENOENT)
117 ret = -errno;
118 continue;
119 }
120
121 is_dir = S_ISDIR(st.st_mode);
122 } else
123 is_dir = de->d_type == DT_DIR;
124
125 if (is_dir) {
50b6eec1 126 _cleanup_close_ int subdir_fd = -1;
c6878637 127
f25afeb6 128 /* if root_dev is set, remove subdirectories only if device is same */
c6878637
LP
129 if (root_dev && st.st_dev != root_dev->st_dev)
130 continue;
131
132 subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
133 if (subdir_fd < 0) {
134 if (ret == 0 && errno != ENOENT)
135 ret = -errno;
136 continue;
137 }
138
f25afeb6 139 /* Stop at mount points */
5d409034 140 r = fd_is_mount_point(fd, de->d_name, 0);
f25afeb6
LP
141 if (r < 0) {
142 if (ret == 0 && r != -ENOENT)
143 ret = r;
144
f25afeb6
LP
145 continue;
146 }
50b6eec1 147 if (r > 0)
f25afeb6 148 continue;
f25afeb6 149
9e9b663a 150 if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
9e9b663a
LP
151
152 /* This could be a subvolume, try to remove it */
153
5bcd08db 154 r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
d9e2daaf 155 if (r < 0) {
4c701096 156 if (!IN_SET(r, -ENOTTY, -EINVAL)) {
9e9b663a 157 if (ret == 0)
d9e2daaf 158 ret = r;
9e9b663a 159
9e9b663a
LP
160 continue;
161 }
162
50b6eec1
LP
163 /* ENOTTY, then it wasn't a btrfs subvolume, continue below. */
164 } else
9e9b663a 165 /* It was a subvolume, continue. */
9e9b663a 166 continue;
9e9b663a
LP
167 }
168
50b6eec1 169 /* We pass REMOVE_PHYSICAL here, to avoid doing the fstatfs() to check the file
c6878637 170 * system type again for each directory */
50b6eec1 171 r = rm_rf_children(TAKE_FD(subdir_fd), flags | REMOVE_PHYSICAL, root_dev);
c6878637
LP
172 if (r < 0 && ret == 0)
173 ret = r;
174
2899fb02
LP
175 r = unlinkat_harder(fd, de->d_name, AT_REMOVEDIR, flags);
176 if (r < 0 && r != -ENOENT && ret == 0)
177 ret = r;
c6878637
LP
178
179 } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
180
2899fb02
LP
181 r = unlinkat_harder(fd, de->d_name, 0, flags);
182 if (r < 0 && r != -ENOENT && ret == 0)
183 ret = r;
c6878637
LP
184 }
185 }
8fb3f009 186 return ret;
c6878637
LP
187}
188
189int rm_rf(const char *path, RemoveFlags flags) {
190 int fd, r;
191 struct statfs s;
192
193 assert(path);
194
c2f64c07
LP
195 /* For now, don't support dropping subvols when also only dropping directories, since we can't do
196 * this race-freely. */
197 if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES|REMOVE_SUBVOLUME))
198 return -EINVAL;
199
c0228b4f
LP
200 /* We refuse to clean the root file system with this call. This is extra paranoia to never cause a
201 * really seriously broken system. */
baaa35ad
ZJS
202 if (path_equal_or_files_same(path, "/", AT_SYMLINK_NOFOLLOW))
203 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
204 "Attempted to remove entire root file system (\"%s\"), and we can't allow that.",
205 path);
c6878637 206
d94a24ca 207 if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
d9e2daaf 208 /* Try to remove as subvolume first */
5bcd08db 209 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
d9e2daaf
LP
210 if (r >= 0)
211 return r;
212
c0228b4f
LP
213 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && r == -ENOENT)
214 return 0;
215
4c701096 216 if (!IN_SET(r, -ENOTTY, -EINVAL, -ENOTDIR))
d9e2daaf
LP
217 return r;
218
219 /* Not btrfs or not a subvolume */
220 }
221
c6878637
LP
222 fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
223 if (fd < 0) {
c0228b4f
LP
224 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && errno == ENOENT)
225 return 0;
226
ec2ce0c5 227 if (!IN_SET(errno, ENOTDIR, ELOOP))
c6878637
LP
228 return -errno;
229
c0228b4f
LP
230 if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES))
231 return 0;
c6878637 232
c0228b4f
LP
233 if (FLAGS_SET(flags, REMOVE_ROOT)) {
234
235 if (!FLAGS_SET(flags, REMOVE_PHYSICAL)) {
236 if (statfs(path, &s) < 0)
237 return -errno;
238
239 if (is_physical_fs(&s))
240 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
241 "Attempted to remove files from a disk file system under \"%s\", refusing.",
242 path);
243 }
244
245 if (unlink(path) < 0) {
246 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && errno == ENOENT)
247 return 0;
c6878637 248
c6878637 249 return -errno;
c0228b4f
LP
250 }
251 }
c6878637
LP
252
253 return 0;
254 }
255
256 r = rm_rf_children(fd, flags, NULL);
257
c0228b4f
LP
258 if (FLAGS_SET(flags, REMOVE_ROOT) &&
259 rmdir(path) < 0 &&
260 r >= 0 &&
261 (!FLAGS_SET(flags, REMOVE_MISSING_OK) || errno != ENOENT))
262 r = -errno;
c6878637
LP
263
264 return r;
265}