]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-common.c
c24a0b0c86b94c6939ffdcaff425c47523a9a2e2
[thirdparty/systemd.git] / src / import / import-common.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2015 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sched.h>
22 #include <sys/prctl.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include "btrfs-util.h"
27 #include "capability-util.h"
28 #include "fd-util.h"
29 #include "import-common.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "util.h"
33
34 int import_make_read_only_fd(int fd) {
35 int r;
36
37 assert(fd >= 0);
38
39 /* First, let's make this a read-only subvolume if it refers
40 * to a subvolume */
41 r = btrfs_subvol_set_read_only_fd(fd, true);
42 if (IN_SET(r, -ENOTTY, -ENOTDIR, -EINVAL)) {
43 struct stat st;
44
45 /* This doesn't refer to a subvolume, or the file
46 * system isn't even btrfs. In that, case fall back to
47 * chmod()ing */
48
49 r = fstat(fd, &st);
50 if (r < 0)
51 return log_error_errno(errno, "Failed to stat temporary image: %m");
52
53 /* Drop "w" flag */
54 if (fchmod(fd, st.st_mode & 07555) < 0)
55 return log_error_errno(errno, "Failed to chmod() final image: %m");
56
57 return 0;
58
59 } else if (r < 0)
60 return log_error_errno(r, "Failed to make subvolume read-only: %m");
61
62 return 0;
63 }
64
65 int import_make_read_only(const char *path) {
66 _cleanup_close_ int fd = 1;
67
68 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
69 if (fd < 0)
70 return log_error_errno(errno, "Failed to open %s: %m", path);
71
72 return import_make_read_only_fd(fd);
73 }
74
75 int import_fork_tar_x(const char *path, pid_t *ret) {
76 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
77 pid_t pid;
78 int r;
79
80 assert(path);
81 assert(ret);
82
83 if (pipe2(pipefd, O_CLOEXEC) < 0)
84 return log_error_errno(errno, "Failed to create pipe for tar: %m");
85
86 r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
87 if (r < 0)
88 return r;
89 if (r == 0) {
90 int null_fd;
91 uint64_t retain =
92 (1ULL << CAP_CHOWN) |
93 (1ULL << CAP_FOWNER) |
94 (1ULL << CAP_FSETID) |
95 (1ULL << CAP_MKNOD) |
96 (1ULL << CAP_SETFCAP) |
97 (1ULL << CAP_DAC_OVERRIDE);
98
99 /* Child */
100
101 pipefd[1] = safe_close(pipefd[1]);
102
103 r = move_fd(pipefd[0], STDIN_FILENO, false);
104 if (r < 0) {
105 log_error_errno(r, "Failed to move fd: %m");
106 _exit(EXIT_FAILURE);
107 }
108
109 null_fd = open("/dev/null", O_WRONLY|O_NOCTTY);
110 if (null_fd < 0) {
111 log_error_errno(errno, "Failed to open /dev/null: %m");
112 _exit(EXIT_FAILURE);
113 }
114
115 r = move_fd(null_fd, STDOUT_FILENO, false);
116 if (r < 0) {
117 log_error_errno(r, "Failed to move fd: %m");
118 _exit(EXIT_FAILURE);
119 }
120
121 stdio_unset_cloexec();
122
123 if (unshare(CLONE_NEWNET) < 0)
124 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
125
126 r = capability_bounding_set_drop(retain, true);
127 if (r < 0)
128 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
129
130 execlp("tar", "tar", "--numeric-owner", "-C", path, "-px", "--xattrs", "--xattrs-include=*", NULL);
131 log_error_errno(errno, "Failed to execute tar: %m");
132 _exit(EXIT_FAILURE);
133 }
134
135 pipefd[0] = safe_close(pipefd[0]);
136 r = pipefd[1];
137 pipefd[1] = -1;
138
139 *ret = pid;
140
141 return r;
142 }
143
144 int import_fork_tar_c(const char *path, pid_t *ret) {
145 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
146 pid_t pid;
147 int r;
148
149 assert(path);
150 assert(ret);
151
152 if (pipe2(pipefd, O_CLOEXEC) < 0)
153 return log_error_errno(errno, "Failed to create pipe for tar: %m");
154
155 r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
156 if (r < 0)
157 return r;
158 if (r == 0) {
159 int null_fd;
160 uint64_t retain = (1ULL << CAP_DAC_OVERRIDE);
161
162 /* Child */
163
164 pipefd[0] = safe_close(pipefd[0]);
165
166 r = move_fd(pipefd[1], STDOUT_FILENO, false);
167 if (r < 0) {
168 log_error_errno(r, "Failed to move fd: %m");
169 _exit(EXIT_FAILURE);
170 }
171
172 null_fd = open("/dev/null", O_RDONLY|O_NOCTTY);
173 if (null_fd < 0) {
174 log_error_errno(errno, "Failed to open /dev/null: %m");
175 _exit(EXIT_FAILURE);
176 }
177
178 r = move_fd(null_fd, STDIN_FILENO, false);
179 if (r < 0) {
180 log_error_errno(errno, "Failed to move fd: %m");
181 _exit(EXIT_FAILURE);
182 }
183
184 stdio_unset_cloexec();
185
186 if (unshare(CLONE_NEWNET) < 0)
187 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
188
189 r = capability_bounding_set_drop(retain, true);
190 if (r < 0)
191 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
192
193 execlp("tar", "tar", "-C", path, "-c", "--xattrs", "--xattrs-include=*", ".", NULL);
194 log_error_errno(errno, "Failed to execute tar: %m");
195 _exit(EXIT_FAILURE);
196 }
197
198 pipefd[1] = safe_close(pipefd[1]);
199 r = pipefd[0];
200 pipefd[0] = -1;
201
202 *ret = pid;
203
204 return r;
205 }