]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/rm-rf.c
Merge pull request #17185 from yuwata/ethtool-update
[thirdparty/systemd.git] / src / basic / rm-rf.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "btrfs-util.h"
11 #include "cgroup-util.h"
12 #include "dirent-util.h"
13 #include "fd-util.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "mountpoint-util.h"
17 #include "path-util.h"
18 #include "rm-rf.h"
19 #include "stat-util.h"
20 #include "string-util.h"
21
22 static bool is_physical_fs(const struct statfs *sfs) {
23 return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs);
24 }
25
26 static 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;
48 if (FLAGS_SET(st.st_mode, 0700)) /* Already set? */
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
66 int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
67 _cleanup_closedir_ DIR *d = NULL;
68 struct dirent *de;
69 int ret = 0, r;
70 struct statfs sfs;
71
72 assert(fd >= 0);
73
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.. */
76
77 if (!(flags & REMOVE_PHYSICAL)) {
78
79 r = fstatfs(fd, &sfs);
80 if (r < 0) {
81 safe_close(fd);
82 return -errno;
83 }
84
85 if (is_physical_fs(&sfs)) {
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));
94
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
106 FOREACH_DIRENT_ALL(de, d, return -errno) {
107 bool is_dir;
108 struct stat st;
109
110 if (dot_or_dot_dot(de->d_name))
111 continue;
112
113 if (de->d_type == DT_UNKNOWN ||
114 (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
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) {
126 _cleanup_close_ int subdir_fd = -1;
127
128 /* if root_dev is set, remove subdirectories only if device is same */
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
139 /* Stop at mount points */
140 r = fd_is_mount_point(fd, de->d_name, 0);
141 if (r < 0) {
142 if (ret == 0 && r != -ENOENT)
143 ret = r;
144
145 continue;
146 }
147 if (r > 0)
148 continue;
149
150 if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
151
152 /* This could be a subvolume, try to remove it */
153
154 r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
155 if (r < 0) {
156 if (!IN_SET(r, -ENOTTY, -EINVAL)) {
157 if (ret == 0)
158 ret = r;
159
160 continue;
161 }
162
163 /* ENOTTY, then it wasn't a btrfs subvolume, continue below. */
164 } else
165 /* It was a subvolume, continue. */
166 continue;
167 }
168
169 /* We pass REMOVE_PHYSICAL here, to avoid doing the fstatfs() to check the file
170 * system type again for each directory */
171 r = rm_rf_children(TAKE_FD(subdir_fd), flags | REMOVE_PHYSICAL, root_dev);
172 if (r < 0 && ret == 0)
173 ret = r;
174
175 r = unlinkat_harder(fd, de->d_name, AT_REMOVEDIR, flags);
176 if (r < 0 && r != -ENOENT && ret == 0)
177 ret = r;
178
179 } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
180
181 r = unlinkat_harder(fd, de->d_name, 0, flags);
182 if (r < 0 && r != -ENOENT && ret == 0)
183 ret = r;
184 }
185 }
186 return ret;
187 }
188
189 int rm_rf(const char *path, RemoveFlags flags) {
190 int fd, r;
191 struct statfs s;
192
193 assert(path);
194
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
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. */
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);
206
207 if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) {
208 /* Try to remove as subvolume first */
209 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
210 if (r >= 0)
211 return r;
212
213 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && r == -ENOENT)
214 return 0;
215
216 if (!IN_SET(r, -ENOTTY, -EINVAL, -ENOTDIR))
217 return r;
218
219 /* Not btrfs or not a subvolume */
220 }
221
222 fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
223 if (fd < 0) {
224 if (FLAGS_SET(flags, REMOVE_MISSING_OK) && errno == ENOENT)
225 return 0;
226
227 if (!IN_SET(errno, ENOTDIR, ELOOP))
228 return -errno;
229
230 if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES))
231 return 0;
232
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;
248
249 return -errno;
250 }
251 }
252
253 return 0;
254 }
255
256 r = rm_rf_children(fd, flags, NULL);
257
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;
263
264 return r;
265 }