]>
Commit | Line | Data |
---|---|---|
bf5f9c24 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2022 Pakfire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <errno.h> | |
22 | #include <linux/limits.h> | |
bf5f9c24 MT |
23 | #include <stddef.h> |
24 | #include <sys/mount.h> | |
25 | #include <sys/stat.h> | |
f71c82d8 | 26 | #include <sys/sysmacros.h> |
bf5f9c24 MT |
27 | #include <sys/types.h> |
28 | ||
29 | // libmount | |
30 | #include <libmount/libmount.h> | |
31 | ||
660120b6 | 32 | #include <pakfire/arch.h> |
bf5f9c24 MT |
33 | #include <pakfire/logging.h> |
34 | #include <pakfire/pakfire.h> | |
35 | #include <pakfire/mount.h> | |
d973a13d | 36 | #include <pakfire/string.h> |
bf5f9c24 MT |
37 | #include <pakfire/util.h> |
38 | ||
39 | static const struct pakfire_mountpoint { | |
40 | const char* source; | |
41 | const char* target; | |
42 | const char* fstype; | |
43 | int flags; | |
44 | const char* options; | |
bf5f9c24 | 45 | } mountpoints[] = { |
9ab7dd21 MT |
46 | // Mount a new instance of /proc |
47 | { "pakfire_proc", "proc", "proc", | |
48 | MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL, }, | |
bf5f9c24 | 49 | |
9ab7dd21 | 50 | // Make /proc/sys read-only (except /proc/sys/net) |
cc752ada MT |
51 | { "/proc/sys", "proc/sys", "bind", MS_BIND|MS_REC, NULL, }, |
52 | { "/proc/sys/net", "proc/sys/net", "bind", MS_BIND|MS_REC, NULL, }, | |
9ab7dd21 MT |
53 | { "/proc/sys", "proc/sys", "bind", |
54 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL, }, | |
bf5f9c24 | 55 | |
9ab7dd21 | 56 | // Deny write access to /proc/sysrq-trigger (can be used to restart the host) |
cc752ada | 57 | { "/proc/sysrq-trigger", "proc/sysrq-trigger", "bind", MS_BIND|MS_REC, NULL, }, |
9ab7dd21 MT |
58 | { "/proc/sysrq-trigger", "proc/sysrq-trigger", "bind", |
59 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL, }, | |
60 | ||
61 | // Make /proc/irq read-only | |
cc752ada | 62 | { "/proc/irq", "proc/irq", "bind", MS_BIND|MS_REC, NULL, }, |
9ab7dd21 MT |
63 | { "/proc/irq", "proc/irq", "bind", |
64 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL, }, | |
65 | ||
66 | // Make /proc/bus read-only | |
cc752ada | 67 | { "/proc/bus", "proc/bus", "bind", MS_BIND|MS_REC, NULL, }, |
9ab7dd21 MT |
68 | { "/proc/bus", "proc/bus", "bind", |
69 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL, }, | |
70 | ||
71 | // Bind-Mount /sys ready-only | |
cc752ada | 72 | { "/sys", "sys", "bind", MS_BIND|MS_REC, NULL, }, |
9ab7dd21 MT |
73 | { "/sys", "sys", "bind", |
74 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL, }, | |
bf5f9c24 MT |
75 | |
76 | // Create a new /dev | |
cc752ada | 77 | { "pakfire_dev", "dev", "tmpfs", MS_NOSUID|MS_NOEXEC, |
9ab7dd21 | 78 | "mode=0755,size=4m,nr_inodes=64k", }, |
0675c514 MT |
79 | { "pakfire_dev_pts", "dev/pts", "devpts", MS_NOSUID|MS_NOEXEC, |
80 | "newinstance,ptmxmode=0666,mode=620", }, | |
cc752ada | 81 | |
99affb62 MT |
82 | // Create a new /dev/shm |
83 | { "pakfire_dev_shm", "dev/shm", "tmpfs", | |
84 | MS_NOSUID|MS_NODEV|MS_STRICTATIME, "mode=1777,size=1024m", }, | |
85 | ||
cc752ada MT |
86 | // Mount /dev/mqueue |
87 | { "mqueue", "dev/mqueue", "mqueue", | |
88 | MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL }, | |
bf5f9c24 MT |
89 | |
90 | // Create a new /run | |
b1a6d98c MT |
91 | { "pakfire_run", "run", "tmpfs", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
92 | "mode=755,size=256m,nr_inodes=1k", }, | |
93 | ||
94 | // Create a new /tmp | |
95 | { "pakfire_tmp", "tmp", "tmpfs", | |
96 | MS_NOSUID|MS_NODEV|MS_STRICTATIME, "mode=1777,size=1024m", }, | |
bf5f9c24 | 97 | |
bf5f9c24 MT |
98 | // The end |
99 | { NULL }, | |
100 | }; | |
101 | ||
f71c82d8 MT |
102 | static const struct pakfire_devnode { |
103 | const char* path; | |
104 | int major; | |
105 | int minor; | |
106 | mode_t mode; | |
107 | } devnodes[] = { | |
108 | { "/dev/null", 1, 3, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, }, | |
109 | { "/dev/zero", 1, 5, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, }, | |
110 | { "/dev/full", 1, 7, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, }, | |
111 | { "/dev/random", 1, 8, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, }, | |
112 | { "/dev/urandom", 1, 9, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, }, | |
113 | { "/dev/kmsg", 1, 11, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, }, | |
114 | { "/dev/tty", 5, 0, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, }, | |
115 | { "/dev/console", 5, 1, S_IFCHR|S_IRUSR|S_IWUSR, }, | |
f71c82d8 MT |
116 | { "/dev/rtc0", 252, 0, S_IFCHR|S_IRUSR|S_IWUSR, }, |
117 | { NULL }, | |
118 | }; | |
dc01afb6 | 119 | |
f71c82d8 MT |
120 | static const struct pakfire_symlink { |
121 | const char* target; | |
122 | const char* path; | |
123 | } symlinks[] = { | |
0675c514 | 124 | { "/dev/pts/ptmx", "/dev/ptmx", }, |
f71c82d8 MT |
125 | { "/proc/self/fd", "/dev/fd", }, |
126 | { "/proc/self/fd/0", "/dev/stdin" }, | |
127 | { "/proc/self/fd/1", "/dev/stdout" }, | |
128 | { "/proc/self/fd/2", "/dev/stderr" }, | |
129 | { "/proc/kcore", "/dev/core" }, | |
130 | { NULL }, | |
131 | }; | |
dc01afb6 | 132 | |
bf5f9c24 MT |
133 | /* |
134 | Easy way to iterate through all mountpoints | |
135 | */ | |
0ea82518 MT |
136 | static int pakfire_mount_foreach(struct pakfire* pakfire, int direction, |
137 | int (*callback)(struct pakfire* pakfire, struct libmnt_fs* fs, const void* data), | |
f71c82d8 | 138 | const void* data) { |
bf5f9c24 | 139 | const char* root = pakfire_get_path(pakfire); |
ec64a612 | 140 | int r = 0; |
bf5f9c24 | 141 | |
0ea82518 MT |
142 | struct libmnt_iter* iterator = NULL; |
143 | struct libmnt_table* tab = NULL; | |
144 | struct libmnt_fs* fs = NULL; | |
145 | ||
146 | // Create an iterator | |
147 | iterator = mnt_new_iter(direction); | |
148 | if (!iterator) { | |
149 | ERROR(pakfire, "Could not setup iterator: %m\n"); | |
150 | goto ERROR; | |
151 | } | |
152 | ||
153 | // Read /proc/mounts | |
154 | tab = mnt_new_table_from_file("/proc/mounts"); | |
155 | if (!tab) { | |
bf5f9c24 | 156 | ERROR(pakfire, "Could not open /proc/mounts: %m\n"); |
0ea82518 | 157 | goto ERROR; |
bf5f9c24 MT |
158 | } |
159 | ||
0ea82518 MT |
160 | while (mnt_table_next_fs(tab, iterator, &fs) == 0) { |
161 | const char* target = mnt_fs_get_target(fs); | |
bf5f9c24 MT |
162 | |
163 | // Ignore any mointpoints that don't belong to us | |
0ea82518 | 164 | if (!pakfire_string_startswith(target, root)) |
bf5f9c24 MT |
165 | continue; |
166 | ||
bf5f9c24 | 167 | // Call the callback for each relevant mountpoint |
0ea82518 | 168 | r = callback(pakfire, fs, data); |
bf5f9c24 MT |
169 | if (r) |
170 | break; | |
171 | } | |
172 | ||
0ea82518 | 173 | ERROR: |
bf5f9c24 | 174 | // Tidy up |
0ea82518 MT |
175 | if (fs) |
176 | mnt_unref_fs(fs); | |
177 | if (tab) | |
178 | mnt_unref_table(tab); | |
179 | if (iterator) | |
180 | mnt_free_iter(iterator); | |
bf5f9c24 MT |
181 | |
182 | return r; | |
183 | } | |
184 | ||
185 | static int __pakfire_is_mountpoint(struct pakfire* pakfire, | |
0ea82518 | 186 | struct libmnt_fs* fs, const void* data) { |
bf5f9c24 MT |
187 | const char* path = (const char*)data; |
188 | ||
0ea82518 | 189 | return mnt_fs_streq_target(fs, path); |
bf5f9c24 MT |
190 | } |
191 | ||
192 | int pakfire_is_mountpoint(struct pakfire* pakfire, const char* path) { | |
0ea82518 | 193 | return pakfire_mount_foreach(pakfire, MNT_ITER_FORWARD, |
f71c82d8 | 194 | __pakfire_is_mountpoint, path); |
bf5f9c24 MT |
195 | } |
196 | ||
163851bc | 197 | static int pakfire_mount(struct pakfire* pakfire, const char* source, const char* target, |
bf5f9c24 MT |
198 | const char* fstype, unsigned long mflags, const void* data) { |
199 | const char* options = (const char*)data; | |
bf5f9c24 MT |
200 | |
201 | // Check for some basic inputs | |
202 | if (!source || !target) { | |
203 | errno = EINVAL; | |
204 | return 1; | |
205 | } | |
206 | ||
7f970e6e | 207 | DEBUG(pakfire, "Mounting %s from %s (%s - %s)\n", target, source, fstype, options); |
bf5f9c24 | 208 | |
7f970e6e MT |
209 | // Perform mount() |
210 | int r = mount(source, target, fstype, mflags, data); | |
bf5f9c24 | 211 | if (r) { |
7f970e6e | 212 | ERROR(pakfire, "Could not mount %s: %m\n", target); |
bf5f9c24 MT |
213 | } |
214 | ||
bf5f9c24 MT |
215 | return r; |
216 | } | |
217 | ||
bf5f9c24 | 218 | static int __pakfire_mount_print(struct pakfire* pakfire, |
0ea82518 MT |
219 | struct libmnt_fs* fs, const void* data) { |
220 | DEBUG(pakfire, | |
221 | " %s %s %s %s\n", | |
222 | mnt_fs_get_source(fs), | |
223 | mnt_fs_get_target(fs), | |
224 | mnt_fs_get_fstype(fs), | |
225 | mnt_fs_get_fs_options(fs) | |
bf5f9c24 MT |
226 | ); |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
91247a7b MT |
231 | int pakfire_mount_list(struct pakfire* pakfire) { |
232 | DEBUG(pakfire, "Mountpoints:\n"); | |
bf5f9c24 | 233 | |
0ea82518 | 234 | return pakfire_mount_foreach(pakfire, MNT_ITER_FORWARD, |
f71c82d8 MT |
235 | __pakfire_mount_print, NULL); |
236 | } | |
237 | ||
238 | static int pakfire_populate_dev(struct pakfire* pakfire) { | |
239 | char path[PATH_MAX]; | |
240 | ||
241 | // Create device nodes | |
242 | for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) { | |
243 | DEBUG(pakfire, "Creating device node %s\n", devnode->path); | |
244 | ||
77e26129 MT |
245 | int r = pakfire_path(pakfire, path, "%s", devnode->path); |
246 | if (r) | |
247 | return r; | |
f71c82d8 MT |
248 | |
249 | dev_t dev = makedev(devnode->major, devnode->minor); | |
250 | ||
251 | r = mknod(path, devnode->mode, dev); | |
8f1003a3 MT |
252 | |
253 | // Continue if mknod was successful | |
254 | if (r == 0) | |
255 | continue; | |
256 | ||
257 | // If we could not create the device node because of permission issues, | |
258 | // it might be likely that we are running in a user namespace where creating | |
259 | // device nodes is not permitted. Try bind-mounting them. | |
260 | if (errno == EPERM) | |
261 | goto MOUNT; | |
262 | ||
263 | // Otherwise log an error and end | |
264 | ERROR(pakfire, "Could not create %s: %m\n", devnode->path); | |
265 | return r; | |
266 | ||
267 | MOUNT: | |
268 | // Create an empty file | |
269 | r = pakfire_touch(path, 0444); | |
f71c82d8 | 270 | if (r) { |
8f1003a3 | 271 | ERROR(pakfire, "Could not create %s: %m\n", path); |
f71c82d8 MT |
272 | return r; |
273 | } | |
8f1003a3 MT |
274 | |
275 | // Create a bind-mount over the file | |
64e3b4ff | 276 | r = pakfire_mount(pakfire, devnode->path, path, "bind", MS_BIND, NULL); |
8f1003a3 MT |
277 | if (r) |
278 | return r; | |
f71c82d8 MT |
279 | } |
280 | ||
281 | // Create symlinks | |
282 | for (const struct pakfire_symlink* s = symlinks; s->target; s++) { | |
283 | DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target); | |
284 | ||
77e26129 MT |
285 | int r = pakfire_path(pakfire, path, "%s", s->path); |
286 | if (r) | |
287 | return r; | |
f71c82d8 MT |
288 | |
289 | r = symlink(s->target, path); | |
290 | if (r) { | |
291 | ERROR(pakfire, "Could not create symlink %s: %m\n", s->path); | |
292 | return r; | |
293 | } | |
294 | } | |
295 | ||
296 | return 0; | |
bf5f9c24 MT |
297 | } |
298 | ||
660120b6 MT |
299 | static int pakfire_mount_interpreter(struct pakfire* pakfire) { |
300 | char target[PATH_MAX]; | |
301 | ||
302 | // Fetch the target architecture | |
303 | const char* arch = pakfire_get_arch(pakfire); | |
304 | ||
305 | // Can we emulate this architecture? | |
306 | char* interpreter = pakfire_arch_find_interpreter(arch); | |
307 | ||
308 | // No interpreter required | |
309 | if (!interpreter) | |
310 | return 0; | |
311 | ||
312 | DEBUG(pakfire, "Mounting interpreter %s for %s\n", interpreter, arch); | |
313 | ||
314 | // Where to mount this? | |
77e26129 MT |
315 | int r = pakfire_path(pakfire, target, "%s", interpreter); |
316 | if (r) | |
660120b6 MT |
317 | return r; |
318 | ||
319 | // Create directory | |
520ce66c | 320 | r = pakfire_mkparentdir(target, 0755); |
660120b6 MT |
321 | if (r) |
322 | return r; | |
323 | ||
324 | // Create an empty file | |
325 | FILE* f = fopen(target, "w"); | |
326 | if (!f) | |
327 | return 1; | |
328 | fclose(f); | |
329 | ||
330 | r = pakfire_mount(pakfire, interpreter, target, NULL, MS_BIND|MS_RDONLY, NULL); | |
331 | if (r) | |
332 | ERROR(pakfire, "Could not mount interpreter %s to %s: %m\n", interpreter, target); | |
333 | ||
334 | return r; | |
335 | } | |
336 | ||
f71c82d8 | 337 | int pakfire_mount_all(struct pakfire* pakfire) { |
bf5f9c24 | 338 | char target[PATH_MAX]; |
bf5f9c24 MT |
339 | int r; |
340 | ||
341 | // Fetch Pakfire's root directory | |
342 | const char* root = pakfire_get_path(pakfire); | |
343 | ||
bf5f9c24 | 344 | for (const struct pakfire_mountpoint* mp = mountpoints; mp->source; mp++) { |
bf5f9c24 MT |
345 | // Figure out where to mount |
346 | r = pakfire_path_join(target, root, mp->target); | |
56796f84 | 347 | if (r) |
bf5f9c24 MT |
348 | return r; |
349 | ||
a685a503 MT |
350 | // Create target if it doesn't exist |
351 | if (!pakfire_path_exists(target)) { | |
352 | r = pakfire_mkdir(target, 0755); | |
353 | if (r) { | |
354 | ERROR(pakfire, "Could not create %s: %m\n", target); | |
355 | return r; | |
356 | } | |
357 | } | |
bf5f9c24 | 358 | |
bf5f9c24 | 359 | // Perform mount() |
f71c82d8 | 360 | r = pakfire_mount(pakfire, mp->source, target, mp->fstype, mp->flags, mp->options); |
a685a503 | 361 | if (r) |
bf5f9c24 | 362 | return r; |
bf5f9c24 MT |
363 | } |
364 | ||
f71c82d8 MT |
365 | // Populate /dev |
366 | r = pakfire_populate_dev(pakfire); | |
367 | if (r) | |
368 | return r; | |
369 | ||
660120b6 MT |
370 | // Mount the interpreter (if needed) |
371 | r = pakfire_mount_interpreter(pakfire); | |
372 | if (r) | |
373 | return r; | |
374 | ||
bf5f9c24 MT |
375 | return 0; |
376 | } | |
377 | ||
061223f7 | 378 | int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int flags) { |
163851bc MT |
379 | struct stat st; |
380 | char mountpoint[PATH_MAX]; | |
381 | ||
382 | if (!dst) | |
383 | dst = src; | |
384 | ||
77e26129 MT |
385 | int r = pakfire_path(pakfire, mountpoint, "%s", dst); |
386 | if (r) | |
387 | return r; | |
163851bc MT |
388 | |
389 | DEBUG(pakfire, "Bind-mounting %s to %s\n", src, mountpoint); | |
390 | ||
391 | r = stat(src, &st); | |
392 | if (r < 0) { | |
393 | ERROR(pakfire, "Could not stat %s: %m\n", src); | |
394 | return 1; | |
395 | } | |
396 | ||
397 | // Make sure the mountpoint exists | |
398 | switch (st.st_mode & S_IFMT) { | |
399 | case S_IFDIR: | |
520ce66c | 400 | r = pakfire_mkdir(mountpoint, st.st_mode); |
163851bc MT |
401 | if (r && errno != EEXIST) |
402 | return r; | |
403 | break; | |
404 | ||
405 | case S_IFREG: | |
406 | case S_IFLNK: | |
407 | // Make parent directory | |
520ce66c | 408 | r = pakfire_mkparentdir(mountpoint, 0755); |
163851bc MT |
409 | if (r) |
410 | return r; | |
411 | ||
412 | // Create a file | |
413 | FILE* f = fopen(mountpoint, "w"); | |
414 | if (!f) | |
415 | return 1; | |
416 | fclose(f); | |
417 | break; | |
418 | ||
419 | default: | |
420 | errno = ENOTSUP; | |
421 | return 1; | |
422 | } | |
423 | ||
424 | // Perform mount | |
cc6e2264 | 425 | return pakfire_mount(pakfire, src, mountpoint, NULL, flags|MS_REC|MS_BIND, NULL); |
163851bc | 426 | } |