]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-common.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / import / import-common.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sched.h>
4 #include <sys/prctl.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7
8 #include "btrfs-util.h"
9 #include "capability-util.h"
10 #include "fd-util.h"
11 #include "import-common.h"
12 #include "process-util.h"
13 #include "signal-util.h"
14 #include "util.h"
15
16 int import_make_read_only_fd(int fd) {
17 int r;
18
19 assert(fd >= 0);
20
21 /* First, let's make this a read-only subvolume if it refers
22 * to a subvolume */
23 r = btrfs_subvol_set_read_only_fd(fd, true);
24 if (IN_SET(r, -ENOTTY, -ENOTDIR, -EINVAL)) {
25 struct stat st;
26
27 /* This doesn't refer to a subvolume, or the file
28 * system isn't even btrfs. In that, case fall back to
29 * chmod()ing */
30
31 r = fstat(fd, &st);
32 if (r < 0)
33 return log_error_errno(errno, "Failed to stat temporary image: %m");
34
35 /* Drop "w" flag */
36 if (fchmod(fd, st.st_mode & 07555) < 0)
37 return log_error_errno(errno, "Failed to chmod() final image: %m");
38
39 return 0;
40
41 } else if (r < 0)
42 return log_error_errno(r, "Failed to make subvolume read-only: %m");
43
44 return 0;
45 }
46
47 int import_make_read_only(const char *path) {
48 _cleanup_close_ int fd = 1;
49
50 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
51 if (fd < 0)
52 return log_error_errno(errno, "Failed to open %s: %m", path);
53
54 return import_make_read_only_fd(fd);
55 }
56
57 int import_fork_tar_x(const char *path, pid_t *ret) {
58 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
59 pid_t pid;
60 int r;
61
62 assert(path);
63 assert(ret);
64
65 if (pipe2(pipefd, O_CLOEXEC) < 0)
66 return log_error_errno(errno, "Failed to create pipe for tar: %m");
67
68 r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
69 if (r < 0)
70 return r;
71 if (r == 0) {
72 uint64_t retain =
73 (1ULL << CAP_CHOWN) |
74 (1ULL << CAP_FOWNER) |
75 (1ULL << CAP_FSETID) |
76 (1ULL << CAP_MKNOD) |
77 (1ULL << CAP_SETFCAP) |
78 (1ULL << CAP_DAC_OVERRIDE);
79
80 /* Child */
81
82 pipefd[1] = safe_close(pipefd[1]);
83
84 r = rearrange_stdio(pipefd[0], -1, STDERR_FILENO);
85 if (r < 0) {
86 log_error_errno(r, "Failed to rearrange stdin/stdout: %m");
87 _exit(EXIT_FAILURE);
88 }
89
90 if (unshare(CLONE_NEWNET) < 0)
91 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
92
93 r = capability_bounding_set_drop(retain, true);
94 if (r < 0)
95 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
96
97 execlp("tar", "tar", "--numeric-owner", "-C", path, "-px", "--xattrs", "--xattrs-include=*", NULL);
98 log_error_errno(errno, "Failed to execute tar: %m");
99 _exit(EXIT_FAILURE);
100 }
101
102 *ret = pid;
103
104 return TAKE_FD(pipefd[1]);
105 }
106
107 int import_fork_tar_c(const char *path, pid_t *ret) {
108 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
109 pid_t pid;
110 int r;
111
112 assert(path);
113 assert(ret);
114
115 if (pipe2(pipefd, O_CLOEXEC) < 0)
116 return log_error_errno(errno, "Failed to create pipe for tar: %m");
117
118 r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
119 if (r < 0)
120 return r;
121 if (r == 0) {
122 uint64_t retain = (1ULL << CAP_DAC_OVERRIDE);
123
124 /* Child */
125
126 pipefd[0] = safe_close(pipefd[0]);
127
128 r = rearrange_stdio(-1, pipefd[1], STDERR_FILENO);
129 if (r < 0) {
130 log_error_errno(r, "Failed to rearrange stdin/stdout: %m");
131 _exit(EXIT_FAILURE);
132 }
133
134 if (unshare(CLONE_NEWNET) < 0)
135 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
136
137 r = capability_bounding_set_drop(retain, true);
138 if (r < 0)
139 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
140
141 execlp("tar", "tar", "-C", path, "-c", "--xattrs", "--xattrs-include=*", ".", NULL);
142 log_error_errno(errno, "Failed to execute tar: %m");
143 _exit(EXIT_FAILURE);
144 }
145
146 *ret = pid;
147
148 return TAKE_FD(pipefd[0]);
149 }