]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/import-common.c
Merge pull request #12508 from keszybz/no-root-checks
[thirdparty/systemd.git] / src / import / import-common.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b6e676ce 2
618234a5 3#include <sched.h>
b6e676ce
LP
4#include <sys/prctl.h>
5#include <sys/stat.h>
6#include <unistd.h>
7
e21b7229 8#include "alloc-util.h"
b6e676ce 9#include "btrfs-util.h"
430f0182 10#include "capability-util.h"
e21b7229 11#include "dirent-util.h"
3ffd4af2 12#include "fd-util.h"
e21b7229
LP
13#include "fileio.h"
14#include "fs-util.h"
3ffd4af2 15#include "import-common.h"
e21b7229 16#include "os-util.h"
dccca82b 17#include "process-util.h"
0a8321d3 18#include "selinux-util.h"
24882e06 19#include "signal-util.h"
e4de7287 20#include "tmpfile-util.h"
618234a5 21#include "util.h"
b6e676ce
LP
22
23int import_make_read_only_fd(int fd) {
24 int r;
25
26 assert(fd >= 0);
27
28 /* First, let's make this a read-only subvolume if it refers
29 * to a subvolume */
30 r = btrfs_subvol_set_read_only_fd(fd, true);
4c701096 31 if (IN_SET(r, -ENOTTY, -ENOTDIR, -EINVAL)) {
b6e676ce
LP
32 struct stat st;
33
34 /* This doesn't refer to a subvolume, or the file
35 * system isn't even btrfs. In that, case fall back to
36 * chmod()ing */
37
38 r = fstat(fd, &st);
39 if (r < 0)
40 return log_error_errno(errno, "Failed to stat temporary image: %m");
41
42 /* Drop "w" flag */
43 if (fchmod(fd, st.st_mode & 07555) < 0)
44 return log_error_errno(errno, "Failed to chmod() final image: %m");
45
46 return 0;
47
48 } else if (r < 0)
49 return log_error_errno(r, "Failed to make subvolume read-only: %m");
50
51 return 0;
52}
53
54int import_make_read_only(const char *path) {
55 _cleanup_close_ int fd = 1;
56
57 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
58 if (fd < 0)
59 return log_error_errno(errno, "Failed to open %s: %m", path);
60
61 return import_make_read_only_fd(fd);
62}
63
587fec42 64int import_fork_tar_x(const char *path, pid_t *ret) {
b6e676ce 65 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
0a8321d3 66 bool use_selinux;
b6e676ce
LP
67 pid_t pid;
68 int r;
69
70 assert(path);
71 assert(ret);
72
73 if (pipe2(pipefd, O_CLOEXEC) < 0)
74 return log_error_errno(errno, "Failed to create pipe for tar: %m");
75
0a8321d3
YW
76 use_selinux = mac_selinux_use();
77
b6e1fff1 78 r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
4c253ed1 79 if (r < 0)
b6e1fff1 80 return r;
4c253ed1 81 if (r == 0) {
b6e676ce
LP
82 uint64_t retain =
83 (1ULL << CAP_CHOWN) |
84 (1ULL << CAP_FOWNER) |
85 (1ULL << CAP_FSETID) |
86 (1ULL << CAP_MKNOD) |
87 (1ULL << CAP_SETFCAP) |
88 (1ULL << CAP_DAC_OVERRIDE);
89
90 /* Child */
91
b6e676ce
LP
92 pipefd[1] = safe_close(pipefd[1]);
93
2b33ab09 94 r = rearrange_stdio(pipefd[0], -1, STDERR_FILENO);
046a82c1 95 if (r < 0) {
2b33ab09 96 log_error_errno(r, "Failed to rearrange stdin/stdout: %m");
b6e676ce
LP
97 _exit(EXIT_FAILURE);
98 }
99
b6e676ce
LP
100 if (unshare(CLONE_NEWNET) < 0)
101 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
102
a103496c 103 r = capability_bounding_set_drop(retain, true);
b6e676ce
LP
104 if (r < 0)
105 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
106
0a8321d3
YW
107 execlp("tar", "tar", "--numeric-owner", "-C", path, "-px", "--xattrs", "--xattrs-include=*",
108 use_selinux ? "--selinux" : "--no-selinux", NULL);
b6e676ce
LP
109 log_error_errno(errno, "Failed to execute tar: %m");
110 _exit(EXIT_FAILURE);
111 }
112
b6e676ce
LP
113 *ret = pid;
114
c10d6bdb 115 return TAKE_FD(pipefd[1]);
b6e676ce 116}
587fec42
LP
117
118int import_fork_tar_c(const char *path, pid_t *ret) {
119 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
0a8321d3 120 bool use_selinux;
587fec42
LP
121 pid_t pid;
122 int r;
123
124 assert(path);
125 assert(ret);
126
127 if (pipe2(pipefd, O_CLOEXEC) < 0)
128 return log_error_errno(errno, "Failed to create pipe for tar: %m");
129
0a8321d3
YW
130 use_selinux = mac_selinux_use();
131
b6e1fff1 132 r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
4c253ed1 133 if (r < 0)
b6e1fff1 134 return r;
4c253ed1 135 if (r == 0) {
587fec42
LP
136 uint64_t retain = (1ULL << CAP_DAC_OVERRIDE);
137
138 /* Child */
139
587fec42
LP
140 pipefd[0] = safe_close(pipefd[0]);
141
2b33ab09 142 r = rearrange_stdio(-1, pipefd[1], STDERR_FILENO);
046a82c1 143 if (r < 0) {
2b33ab09 144 log_error_errno(r, "Failed to rearrange stdin/stdout: %m");
587fec42
LP
145 _exit(EXIT_FAILURE);
146 }
147
587fec42
LP
148 if (unshare(CLONE_NEWNET) < 0)
149 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
150
a103496c 151 r = capability_bounding_set_drop(retain, true);
587fec42
LP
152 if (r < 0)
153 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
154
0a8321d3
YW
155 execlp("tar", "tar", "-C", path, "-c", "--xattrs", "--xattrs-include=*",
156 use_selinux ? "--selinux" : "--no-selinux", ".", NULL);
587fec42
LP
157 log_error_errno(errno, "Failed to execute tar: %m");
158 _exit(EXIT_FAILURE);
159 }
160
587fec42
LP
161 *ret = pid;
162
c10d6bdb 163 return TAKE_FD(pipefd[0]);
587fec42 164}
e21b7229
LP
165
166int import_mangle_os_tree(const char *path) {
167 _cleanup_closedir_ DIR *d = NULL, *cd = NULL;
168 _cleanup_free_ char *child = NULL, *t = NULL;
169 const char *joined;
170 struct dirent *de;
171 int r;
172
173 assert(path);
174
175 /* Some tarballs contain a single top-level directory that contains the actual OS directory tree. Try to
176 * recognize this, and move the tree one level up. */
177
178 r = path_is_os_tree(path);
179 if (r < 0)
180 return log_error_errno(r, "Failed to determine whether '%s' is an OS tree: %m", path);
181 if (r > 0) {
182 log_debug("Directory tree '%s' is a valid OS tree.", path);
183 return 0;
184 }
185
186 log_debug("Directory tree '%s' is not recognizable as OS tree, checking whether to rearrange it.", path);
187
188 d = opendir(path);
189 if (!d)
190 return log_error_errno(r, "Failed to open directory '%s': %m", path);
191
192 errno = 0;
193 de = readdir_no_dot(d);
194 if (!de) {
195 if (errno != 0)
196 return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path);
197
198 log_debug("Directory '%s' is empty, leaving it as it is.", path);
199 return 0;
200 }
201
202 child = strdup(de->d_name);
203 if (!child)
204 return log_oom();
205
206 errno = 0;
207 de = readdir_no_dot(d);
208 if (de) {
209 if (errno != 0)
210 return log_error_errno(errno, "Failed to iterate through directory '%s': %m", path);
211
212 log_debug("Directory '%s' does not look like a directory tree, and has multiple children, leaving as it is.", path);
213 return 0;
214 }
215
216 joined = strjoina(path, "/", child);
217 r = path_is_os_tree(joined);
218 if (r == -ENOTDIR) {
219 log_debug("Directory '%s' does not look like a directory tree, and contains a single regular file only, leaving as it is.", path);
220 return 0;
221 }
222 if (r < 0)
223 return log_error_errno(r, "Failed to determine whether '%s' is an OS tree: %m", joined);
224 if (r == 0) {
225 log_debug("Neither '%s' nor '%s' is a valid OS tree, leaving them as they are.", path, joined);
226 return 0;
227 }
228
229 /* Nice, we have checked now:
230 *
231 * 1. The top-level directory does not qualify as OS tree
232 * 1. The top-level directory only contains one item
233 * 2. That item is a directory
234 * 3. And that directory qualifies as OS tree
235 *
236 * Let's now rearrange things, moving everything in the inner directory one level up */
237
238 cd = xopendirat(dirfd(d), child, O_NOFOLLOW);
239 if (!cd)
240 return log_error_errno(errno, "Can't open directory '%s': %m", joined);
241
242 log_info("Rearranging '%s', moving OS tree one directory up.", joined);
243
244 /* Let's rename the child to an unguessable name so that we can be sure all files contained in it can be
245 * safely moved up and won't collide with the name. */
246 r = tempfn_random(child, NULL, &t);
247 if (r < 0)
248 return log_oom();
249 r = rename_noreplace(dirfd(d), child, dirfd(d), t);
250 if (r < 0)
251 return log_error_errno(r, "Unable to rename '%s' to '%s/%s': %m", joined, path, t);
252
253 FOREACH_DIRENT_ALL(de, cd, return log_error_errno(errno, "Failed to iterate through directory '%s': %m", joined)) {
254 if (dot_or_dot_dot(de->d_name))
255 continue;
256
257 r = rename_noreplace(dirfd(cd), de->d_name, dirfd(d), de->d_name);
258 if (r < 0)
259 return log_error_errno(r, "Unable to move '%s/%s/%s' to '%s/%s': %m", path, t, de->d_name, path, de->d_name);
260 }
261
262 if (unlinkat(dirfd(d), t, AT_REMOVEDIR) < 0)
263 return log_error_errno(errno, "Failed to remove temporary directory '%s/%s': %m", path, t);
264
265 log_info("Successfully rearranged OS tree.");
266
267 return 0;
268}