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