]> git.ipfire.org Git - people/ms/pakfire.git/blame - src/libpakfire/mount.c
jail: Use pivot_root() again instead of chroot()
[people/ms/pakfire.git] / src / libpakfire / mount.c
CommitLineData
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
39static 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",
84e5948e 96 MS_NOSUID|MS_NODEV|MS_STRICTATIME, "mode=1777,size=4096m", },
bf5f9c24 97
bf5f9c24
MT
98 // The end
99 { NULL },
100};
101
f71c82d8
MT
102static const struct pakfire_devnode {
103 const char* path;
104 int major;
105 int minor;
106 mode_t mode;
282b732a 107 int flags;
f71c82d8 108} devnodes[] = {
282b732a
MT
109 { "/dev/null", 1, 3, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 },
110 { "/dev/zero", 1, 5, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 },
111 { "/dev/full", 1, 7, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 },
112 { "/dev/random", 1, 8, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, 0 },
113 { "/dev/urandom", 1, 9, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, 0 },
114 { "/dev/kmsg", 1, 11, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, 0 },
115 { "/dev/tty", 5, 0, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 },
116 { "/dev/console", 5, 1, S_IFCHR|S_IRUSR|S_IWUSR, 0 },
117 { "/dev/rtc0", 252, 0, S_IFCHR|S_IRUSR|S_IWUSR, 0 },
118
119 // Loop Devices
120 { "/dev/loop-control", 10, 237, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
121 { "/dev/loop0", 7, 0, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
122 { "/dev/loop1", 7, 1, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
123 { "/dev/loop2", 7, 2, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
124 { "/dev/loop3", 7, 3, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
125 { "/dev/loop4", 7, 4, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
126 { "/dev/loop5", 7, 5, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
127 { "/dev/loop6", 7, 6, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
128 { "/dev/loop7", 7, 7, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES },
129
f71c82d8
MT
130 { NULL },
131};
dc01afb6 132
f71c82d8
MT
133static const struct pakfire_symlink {
134 const char* target;
135 const char* path;
136} symlinks[] = {
0675c514 137 { "/dev/pts/ptmx", "/dev/ptmx", },
f71c82d8
MT
138 { "/proc/self/fd", "/dev/fd", },
139 { "/proc/self/fd/0", "/dev/stdin" },
140 { "/proc/self/fd/1", "/dev/stdout" },
141 { "/proc/self/fd/2", "/dev/stderr" },
142 { "/proc/kcore", "/dev/core" },
143 { NULL },
144};
dc01afb6 145
14df7388
MT
146static int pakfire_mount_is_mountpoint(struct pakfire* pakfire, const char* path) {
147 // XXX THIS STILL NEEDS TO BE IMPLEMENTED
148 return 1;
149}
150
151int pakfire_mount_make_mounpoint(struct pakfire* pakfire, const char* path) {
152 int r;
153
154 // Check if path already is a mountpoint
155 r = pakfire_mount_is_mountpoint(pakfire, path);
156 switch (r) {
157 // Already is a mountpoint
158 case 0:
159 return 0;
160
161 // Is not a mountpoint
162 case 1:
163 break;
164
165 default:
166 ERROR(pakfire, "Could not determine whether %s is a mountpoint: %m\n", path);
167 return r;
168 }
169
170 // Bind-mount to self
171 r = mount(path, path, NULL, MS_BIND|MS_REC, NULL);
172 if (r) {
173 ERROR(pakfire, "Could not make %s a mountpoint: %m\n", path);
174 return r;
175 }
176
177 return 0;
178}
179
bf5f9c24
MT
180/*
181 Easy way to iterate through all mountpoints
182*/
0ea82518
MT
183static int pakfire_mount_foreach(struct pakfire* pakfire, int direction,
184 int (*callback)(struct pakfire* pakfire, struct libmnt_fs* fs, const void* data),
f71c82d8 185 const void* data) {
bf5f9c24 186 const char* root = pakfire_get_path(pakfire);
ec64a612 187 int r = 0;
bf5f9c24 188
0ea82518
MT
189 struct libmnt_iter* iterator = NULL;
190 struct libmnt_table* tab = NULL;
191 struct libmnt_fs* fs = NULL;
192
193 // Create an iterator
194 iterator = mnt_new_iter(direction);
195 if (!iterator) {
196 ERROR(pakfire, "Could not setup iterator: %m\n");
197 goto ERROR;
198 }
199
200 // Read /proc/mounts
201 tab = mnt_new_table_from_file("/proc/mounts");
202 if (!tab) {
bf5f9c24 203 ERROR(pakfire, "Could not open /proc/mounts: %m\n");
0ea82518 204 goto ERROR;
bf5f9c24
MT
205 }
206
0ea82518
MT
207 while (mnt_table_next_fs(tab, iterator, &fs) == 0) {
208 const char* target = mnt_fs_get_target(fs);
bf5f9c24
MT
209
210 // Ignore any mointpoints that don't belong to us
0ea82518 211 if (!pakfire_string_startswith(target, root))
bf5f9c24
MT
212 continue;
213
bf5f9c24 214 // Call the callback for each relevant mountpoint
0ea82518 215 r = callback(pakfire, fs, data);
bf5f9c24
MT
216 if (r)
217 break;
218 }
219
0ea82518 220ERROR:
bf5f9c24 221 // Tidy up
0ea82518
MT
222 if (fs)
223 mnt_unref_fs(fs);
224 if (tab)
225 mnt_unref_table(tab);
226 if (iterator)
227 mnt_free_iter(iterator);
bf5f9c24
MT
228
229 return r;
230}
231
232static int __pakfire_is_mountpoint(struct pakfire* pakfire,
0ea82518 233 struct libmnt_fs* fs, const void* data) {
bf5f9c24
MT
234 const char* path = (const char*)data;
235
0ea82518 236 return mnt_fs_streq_target(fs, path);
bf5f9c24
MT
237}
238
239int pakfire_is_mountpoint(struct pakfire* pakfire, const char* path) {
0ea82518 240 return pakfire_mount_foreach(pakfire, MNT_ITER_FORWARD,
f71c82d8 241 __pakfire_is_mountpoint, path);
bf5f9c24
MT
242}
243
163851bc 244static int pakfire_mount(struct pakfire* pakfire, const char* source, const char* target,
bf5f9c24
MT
245 const char* fstype, unsigned long mflags, const void* data) {
246 const char* options = (const char*)data;
bf5f9c24
MT
247
248 // Check for some basic inputs
249 if (!source || !target) {
250 errno = EINVAL;
251 return 1;
252 }
253
7f970e6e 254 DEBUG(pakfire, "Mounting %s from %s (%s - %s)\n", target, source, fstype, options);
bf5f9c24 255
7f970e6e
MT
256 // Perform mount()
257 int r = mount(source, target, fstype, mflags, data);
bf5f9c24 258 if (r) {
7f970e6e 259 ERROR(pakfire, "Could not mount %s: %m\n", target);
bf5f9c24
MT
260 }
261
bf5f9c24
MT
262 return r;
263}
264
bf5f9c24 265static int __pakfire_mount_print(struct pakfire* pakfire,
0ea82518
MT
266 struct libmnt_fs* fs, const void* data) {
267 DEBUG(pakfire,
268 " %s %s %s %s\n",
269 mnt_fs_get_source(fs),
270 mnt_fs_get_target(fs),
271 mnt_fs_get_fstype(fs),
272 mnt_fs_get_fs_options(fs)
bf5f9c24
MT
273 );
274
275 return 0;
276}
277
91247a7b
MT
278int pakfire_mount_list(struct pakfire* pakfire) {
279 DEBUG(pakfire, "Mountpoints:\n");
bf5f9c24 280
0ea82518 281 return pakfire_mount_foreach(pakfire, MNT_ITER_FORWARD,
f71c82d8
MT
282 __pakfire_mount_print, NULL);
283}
284
282b732a 285static int pakfire_populate_dev(struct pakfire* pakfire, int flags) {
f71c82d8
MT
286 char path[PATH_MAX];
287
288 // Create device nodes
289 for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) {
290 DEBUG(pakfire, "Creating device node %s\n", devnode->path);
291
282b732a
MT
292 // Check if flags match
293 if (devnode->flags && !(flags & devnode->flags))
294 continue;
295
77e26129
MT
296 int r = pakfire_path(pakfire, path, "%s", devnode->path);
297 if (r)
298 return r;
f71c82d8
MT
299
300 dev_t dev = makedev(devnode->major, devnode->minor);
301
302 r = mknod(path, devnode->mode, dev);
8f1003a3
MT
303
304 // Continue if mknod was successful
305 if (r == 0)
306 continue;
307
308 // If we could not create the device node because of permission issues,
309 // it might be likely that we are running in a user namespace where creating
310 // device nodes is not permitted. Try bind-mounting them.
311 if (errno == EPERM)
312 goto MOUNT;
313
314 // Otherwise log an error and end
315 ERROR(pakfire, "Could not create %s: %m\n", devnode->path);
316 return r;
317
318MOUNT:
319 // Create an empty file
320 r = pakfire_touch(path, 0444);
f71c82d8 321 if (r) {
8f1003a3 322 ERROR(pakfire, "Could not create %s: %m\n", path);
f71c82d8
MT
323 return r;
324 }
8f1003a3
MT
325
326 // Create a bind-mount over the file
64e3b4ff 327 r = pakfire_mount(pakfire, devnode->path, path, "bind", MS_BIND, NULL);
8f1003a3
MT
328 if (r)
329 return r;
f71c82d8
MT
330 }
331
332 // Create symlinks
333 for (const struct pakfire_symlink* s = symlinks; s->target; s++) {
334 DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target);
335
77e26129
MT
336 int r = pakfire_path(pakfire, path, "%s", s->path);
337 if (r)
338 return r;
f71c82d8
MT
339
340 r = symlink(s->target, path);
341 if (r) {
342 ERROR(pakfire, "Could not create symlink %s: %m\n", s->path);
343 return r;
344 }
345 }
346
347 return 0;
bf5f9c24
MT
348}
349
660120b6
MT
350static int pakfire_mount_interpreter(struct pakfire* pakfire) {
351 char target[PATH_MAX];
352
353 // Fetch the target architecture
354 const char* arch = pakfire_get_arch(pakfire);
355
356 // Can we emulate this architecture?
357 char* interpreter = pakfire_arch_find_interpreter(arch);
358
359 // No interpreter required
360 if (!interpreter)
361 return 0;
362
363 DEBUG(pakfire, "Mounting interpreter %s for %s\n", interpreter, arch);
364
365 // Where to mount this?
77e26129
MT
366 int r = pakfire_path(pakfire, target, "%s", interpreter);
367 if (r)
660120b6
MT
368 return r;
369
370 // Create directory
520ce66c 371 r = pakfire_mkparentdir(target, 0755);
660120b6
MT
372 if (r)
373 return r;
374
375 // Create an empty file
376 FILE* f = fopen(target, "w");
377 if (!f)
378 return 1;
379 fclose(f);
380
381 r = pakfire_mount(pakfire, interpreter, target, NULL, MS_BIND|MS_RDONLY, NULL);
382 if (r)
383 ERROR(pakfire, "Could not mount interpreter %s to %s: %m\n", interpreter, target);
384
385 return r;
386}
387
282b732a 388int pakfire_mount_all(struct pakfire* pakfire, int flags) {
bf5f9c24 389 char target[PATH_MAX];
bf5f9c24
MT
390 int r;
391
392 // Fetch Pakfire's root directory
393 const char* root = pakfire_get_path(pakfire);
394
bf5f9c24 395 for (const struct pakfire_mountpoint* mp = mountpoints; mp->source; mp++) {
bf5f9c24
MT
396 // Figure out where to mount
397 r = pakfire_path_join(target, root, mp->target);
56796f84 398 if (r)
bf5f9c24
MT
399 return r;
400
a685a503
MT
401 // Create target if it doesn't exist
402 if (!pakfire_path_exists(target)) {
403 r = pakfire_mkdir(target, 0755);
404 if (r) {
405 ERROR(pakfire, "Could not create %s: %m\n", target);
406 return r;
407 }
408 }
bf5f9c24 409
bf5f9c24 410 // Perform mount()
f71c82d8 411 r = pakfire_mount(pakfire, mp->source, target, mp->fstype, mp->flags, mp->options);
a685a503 412 if (r)
bf5f9c24 413 return r;
bf5f9c24
MT
414 }
415
f71c82d8 416 // Populate /dev
282b732a 417 r = pakfire_populate_dev(pakfire, flags);
f71c82d8
MT
418 if (r)
419 return r;
420
660120b6
MT
421 // Mount the interpreter (if needed)
422 r = pakfire_mount_interpreter(pakfire);
423 if (r)
424 return r;
425
bf5f9c24
MT
426 return 0;
427}
428
061223f7 429int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int flags) {
163851bc
MT
430 struct stat st;
431 char mountpoint[PATH_MAX];
432
433 if (!dst)
434 dst = src;
435
77e26129
MT
436 int r = pakfire_path(pakfire, mountpoint, "%s", dst);
437 if (r)
438 return r;
163851bc
MT
439
440 DEBUG(pakfire, "Bind-mounting %s to %s\n", src, mountpoint);
441
442 r = stat(src, &st);
443 if (r < 0) {
444 ERROR(pakfire, "Could not stat %s: %m\n", src);
445 return 1;
446 }
447
448 // Make sure the mountpoint exists
449 switch (st.st_mode & S_IFMT) {
450 case S_IFDIR:
520ce66c 451 r = pakfire_mkdir(mountpoint, st.st_mode);
163851bc
MT
452 if (r && errno != EEXIST)
453 return r;
454 break;
455
456 case S_IFREG:
457 case S_IFLNK:
458 // Make parent directory
520ce66c 459 r = pakfire_mkparentdir(mountpoint, 0755);
163851bc
MT
460 if (r)
461 return r;
462
463 // Create a file
464 FILE* f = fopen(mountpoint, "w");
465 if (!f)
466 return 1;
467 fclose(f);
468 break;
469
470 default:
471 errno = ENOTSUP;
472 return 1;
473 }
474
976fbbc8
MT
475 // The Linux kernel seems to be quite funny when trying to bind-mount something
476 // as read-only and requires us to mount the source first, and then remount it
477 // again using MS_RDONLY.
478 if (flags & MS_RDONLY) {
479 r = pakfire_mount(pakfire, src, mountpoint, NULL, MS_BIND|MS_REC, NULL);
480 if (r)
481 return r;
482
483 // Add the remount flag
484 flags |= MS_REMOUNT;
485 }
486
163851bc 487 // Perform mount
976fbbc8 488 return pakfire_mount(pakfire, src, mountpoint, NULL, flags|MS_BIND|MS_REC, NULL);
163851bc 489}