1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2022 Pakfire development team #
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. #
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. #
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/>. #
19 #############################################################################*/
22 #include <linux/limits.h>
24 #include <sys/mount.h>
26 #include <sys/sysmacros.h>
27 #include <sys/types.h>
30 #include <libmount/libmount.h>
32 #include <pakfire/arch.h>
33 #include <pakfire/logging.h>
34 #include <pakfire/pakfire.h>
35 #include <pakfire/path.h>
36 #include <pakfire/mount.h>
37 #include <pakfire/string.h>
38 #include <pakfire/util.h>
40 static const struct pakfire_mountpoint
{
47 // Mount a new instance of /proc
48 { "pakfire_proc", "proc", "proc",
49 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
, },
51 // Make /proc/sys read-only (except /proc/sys/net)
52 { "/proc/sys", "proc/sys", "bind", MS_BIND
|MS_REC
, NULL
, },
53 { "/proc/sys/net", "proc/sys/net", "bind", MS_BIND
|MS_REC
, NULL
, },
54 { "/proc/sys", "proc/sys", "bind",
55 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
57 // Deny write access to /proc/sysrq-trigger (can be used to restart the host)
58 { "/proc/sysrq-trigger", "proc/sysrq-trigger", "bind", MS_BIND
|MS_REC
, NULL
, },
59 { "/proc/sysrq-trigger", "proc/sysrq-trigger", "bind",
60 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
62 // Make /proc/irq read-only
63 { "/proc/irq", "proc/irq", "bind", MS_BIND
|MS_REC
, NULL
, },
64 { "/proc/irq", "proc/irq", "bind",
65 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
67 // Make /proc/bus read-only
68 { "/proc/bus", "proc/bus", "bind", MS_BIND
|MS_REC
, NULL
, },
69 { "/proc/bus", "proc/bus", "bind",
70 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
72 // Bind-Mount /sys ready-only
73 { "/sys", "sys", "bind", MS_BIND
|MS_REC
, NULL
, },
74 { "/sys", "sys", "bind",
75 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
78 { "pakfire_dev", "dev", "tmpfs", MS_NOSUID
|MS_NOEXEC
,
79 "mode=0755,size=4m,nr_inodes=64k", },
80 { "pakfire_dev_pts", "dev/pts", "devpts", MS_NOSUID
|MS_NOEXEC
,
81 "newinstance,ptmxmode=0666,mode=620", },
83 // Create a new /dev/shm
84 { "pakfire_dev_shm", "dev/shm", "tmpfs",
85 MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, "mode=1777,size=1024m", },
88 { "mqueue", "dev/mqueue", "mqueue",
89 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
},
92 { "pakfire_run", "run", "tmpfs", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
93 "mode=755,size=256m,nr_inodes=1k", },
96 { "pakfire_tmp", "tmp", "tmpfs",
97 MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, "mode=1777,size=4096m", },
103 static const struct pakfire_devnode
{
110 { "/dev/null", 1, 3, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
111 { "/dev/zero", 1, 5, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
112 { "/dev/full", 1, 7, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
113 { "/dev/random", 1, 8, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
114 { "/dev/urandom", 1, 9, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
115 { "/dev/kmsg", 1, 11, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
116 { "/dev/tty", 5, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
117 { "/dev/console", 5, 1, S_IFCHR
|S_IRUSR
|S_IWUSR
, 0 },
118 { "/dev/rtc0", 252, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
, 0 },
121 { "/dev/loop-control", 10, 237, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
122 { "/dev/loop0", 7, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
123 { "/dev/loop1", 7, 1, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
124 { "/dev/loop2", 7, 2, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
125 { "/dev/loop3", 7, 3, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
126 { "/dev/loop4", 7, 4, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
127 { "/dev/loop5", 7, 5, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
128 { "/dev/loop6", 7, 6, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
129 { "/dev/loop7", 7, 7, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
134 static const struct pakfire_symlink
{
138 { "/dev/pts/ptmx", "/dev/ptmx", },
139 { "/proc/self/fd", "/dev/fd", },
140 { "/proc/self/fd/0", "/dev/stdin" },
141 { "/proc/self/fd/1", "/dev/stdout" },
142 { "/proc/self/fd/2", "/dev/stderr" },
143 { "/proc/kcore", "/dev/core" },
147 int pakfire_mount_change_propagation(struct pakfire
* pakfire
, int propagation
, const char* path
) {
148 DEBUG(pakfire
, "Changing mount propagation on %s\n", path
);
150 int r
= mount(NULL
, path
, NULL
, propagation
|MS_REC
, NULL
);
152 ERROR(pakfire
, "Failed to change mount propagation on %s: %m\n", path
);
157 static int pakfire_mount_is_mountpoint(struct pakfire
* pakfire
, const char* path
) {
158 // XXX THIS STILL NEEDS TO BE IMPLEMENTED
162 int pakfire_mount_make_mounpoint(struct pakfire
* pakfire
, const char* path
) {
165 // Check if path already is a mountpoint
166 r
= pakfire_mount_is_mountpoint(pakfire
, path
);
168 // Already is a mountpoint
172 // Is not a mountpoint
177 ERROR(pakfire
, "Could not determine whether %s is a mountpoint: %m\n", path
);
181 // Bind-mount to self
182 r
= mount(path
, path
, NULL
, MS_BIND
|MS_REC
, NULL
);
184 ERROR(pakfire
, "Could not make %s a mountpoint: %m\n", path
);
192 Easy way to iterate through all mountpoints
194 static int pakfire_mount_foreach(struct pakfire
* pakfire
, int direction
,
195 int (*callback
)(struct pakfire
* pakfire
, struct libmnt_fs
* fs
, const void* data
),
197 const char* root
= pakfire_get_path(pakfire
);
200 struct libmnt_iter
* iterator
= NULL
;
201 struct libmnt_table
* tab
= NULL
;
202 struct libmnt_fs
* fs
= NULL
;
204 // Create an iterator
205 iterator
= mnt_new_iter(direction
);
207 ERROR(pakfire
, "Could not setup iterator: %m\n");
212 tab
= mnt_new_table_from_file("/proc/mounts");
214 ERROR(pakfire
, "Could not open /proc/mounts: %m\n");
218 while (mnt_table_next_fs(tab
, iterator
, &fs
) == 0) {
219 const char* target
= mnt_fs_get_target(fs
);
221 // Ignore any mointpoints that don't belong to us
222 if (!pakfire_string_startswith(target
, root
))
225 // Call the callback for each relevant mountpoint
226 r
= callback(pakfire
, fs
, data
);
236 mnt_unref_table(tab
);
238 mnt_free_iter(iterator
);
243 static int __pakfire_is_mountpoint(struct pakfire
* pakfire
,
244 struct libmnt_fs
* fs
, const void* data
) {
245 const char* path
= (const char*)data
;
247 return mnt_fs_streq_target(fs
, path
);
250 int pakfire_is_mountpoint(struct pakfire
* pakfire
, const char* path
) {
251 return pakfire_mount_foreach(pakfire
, MNT_ITER_FORWARD
,
252 __pakfire_is_mountpoint
, path
);
255 static int pakfire_mount(struct pakfire
* pakfire
, const char* source
, const char* target
,
256 const char* fstype
, unsigned long mflags
, const void* data
) {
257 const char* options
= (const char*)data
;
259 // Check for some basic inputs
260 if (!source
|| !target
) {
265 DEBUG(pakfire
, "Mounting %s from %s (%s - %s)\n", target
, source
, fstype
, options
);
268 int r
= mount(source
, target
, fstype
, mflags
, data
);
270 ERROR(pakfire
, "Could not mount %s: %m\n", target
);
276 static int __pakfire_mount_print(struct pakfire
* pakfire
,
277 struct libmnt_fs
* fs
, const void* data
) {
280 mnt_fs_get_source(fs
),
281 mnt_fs_get_target(fs
),
282 mnt_fs_get_fstype(fs
),
283 mnt_fs_get_fs_options(fs
)
289 int pakfire_mount_list(struct pakfire
* pakfire
) {
290 DEBUG(pakfire
, "Mountpoints:\n");
292 return pakfire_mount_foreach(pakfire
, MNT_ITER_FORWARD
,
293 __pakfire_mount_print
, NULL
);
296 static int pakfire_populate_dev(struct pakfire
* pakfire
, int flags
) {
299 // Create device nodes
300 for (const struct pakfire_devnode
* devnode
= devnodes
; devnode
->path
; devnode
++) {
301 DEBUG(pakfire
, "Creating device node %s\n", devnode
->path
);
303 // Check if flags match
304 if (devnode
->flags
&& !(flags
& devnode
->flags
))
307 int r
= pakfire_path(pakfire
, path
, "%s", devnode
->path
);
311 dev_t dev
= makedev(devnode
->major
, devnode
->minor
);
313 r
= mknod(path
, devnode
->mode
, dev
);
315 // Continue if mknod was successful
319 // If we could not create the device node because of permission issues,
320 // it might be likely that we are running in a user namespace where creating
321 // device nodes is not permitted. Try bind-mounting them.
325 // Otherwise log an error and end
326 ERROR(pakfire
, "Could not create %s: %m\n", devnode
->path
);
330 // Create an empty file
331 r
= pakfire_touch(path
, 0444);
333 ERROR(pakfire
, "Could not create %s: %m\n", path
);
337 // Create a bind-mount over the file
338 r
= pakfire_mount(pakfire
, devnode
->path
, path
, "bind", MS_BIND
, NULL
);
344 for (const struct pakfire_symlink
* s
= symlinks
; s
->target
; s
++) {
345 DEBUG(pakfire
, "Creating symlink %s -> %s\n", s
->path
, s
->target
);
347 int r
= pakfire_path(pakfire
, path
, "%s", s
->path
);
351 r
= symlink(s
->target
, path
);
353 ERROR(pakfire
, "Could not create symlink %s: %m\n", s
->path
);
361 static int pakfire_mount_interpreter(struct pakfire
* pakfire
) {
362 char target
[PATH_MAX
];
364 // Fetch the target architecture
365 const char* arch
= pakfire_get_effective_arch(pakfire
);
367 // Can we emulate this architecture?
368 char* interpreter
= pakfire_arch_find_interpreter(arch
);
370 // No interpreter required
374 DEBUG(pakfire
, "Mounting interpreter %s for %s\n", interpreter
, arch
);
376 // Where to mount this?
377 int r
= pakfire_path(pakfire
, target
, "%s", interpreter
);
382 r
= pakfire_mkparentdir(target
, 0755);
386 // Create an empty file
387 FILE* f
= fopen(target
, "w");
392 r
= pakfire_mount(pakfire
, interpreter
, target
, NULL
, MS_BIND
|MS_RDONLY
, NULL
);
394 ERROR(pakfire
, "Could not mount interpreter %s to %s: %m\n", interpreter
, target
);
399 int pakfire_mount_all(struct pakfire
* pakfire
, int flags
) {
400 char target
[PATH_MAX
];
403 // Fetch Pakfire's root directory
404 const char* root
= pakfire_get_path(pakfire
);
406 for (const struct pakfire_mountpoint
* mp
= mountpoints
; mp
->source
; mp
++) {
407 // Figure out where to mount
408 r
= pakfire_path_append(target
, root
, mp
->target
);
412 // Create target if it doesn't exist
413 if (!pakfire_path_exists(target
)) {
414 r
= pakfire_mkdir(target
, 0755);
416 ERROR(pakfire
, "Could not create %s: %m\n", target
);
422 r
= pakfire_mount(pakfire
, mp
->source
, target
, mp
->fstype
, mp
->flags
, mp
->options
);
428 r
= pakfire_populate_dev(pakfire
, flags
);
432 // Mount the interpreter (if needed)
433 r
= pakfire_mount_interpreter(pakfire
);
440 int pakfire_make_ramdisk(struct pakfire
* pakfire
, char* path
, const char* args
) {
443 // Create a new temporary directory
444 char* p
= pakfire_mkdtemp(path
);
449 r
= pakfire_mount(pakfire
, "pakfire_ramdisk", p
, "tmpfs", 0, args
);
451 ERROR_ERRNO(pakfire
, r
, "Could not mount ramdisk at %s (%s): %m\n", p
, args
);
455 DEBUG(pakfire
, "Ramdisk mounted at %s (%s)\n", p
, args
);
460 int pakfire_bind(struct pakfire
* pakfire
, const char* src
, const char* dst
, int flags
) {
462 char mountpoint
[PATH_MAX
];
467 int r
= pakfire_path(pakfire
, mountpoint
, "%s", dst
);
471 DEBUG(pakfire
, "Bind-mounting %s to %s\n", src
, mountpoint
);
475 ERROR(pakfire
, "Could not stat %s: %m\n", src
);
479 // Make sure the mountpoint exists
480 switch (st
.st_mode
& S_IFMT
) {
482 r
= pakfire_mkdir(mountpoint
, st
.st_mode
);
483 if (r
&& errno
!= EEXIST
)
489 // Make parent directory
490 r
= pakfire_mkparentdir(mountpoint
, 0755);
495 FILE* f
= fopen(mountpoint
, "w");
506 // The Linux kernel seems to be quite funny when trying to bind-mount something
507 // as read-only and requires us to mount the source first, and then remount it
508 // again using MS_RDONLY.
509 if (flags
& MS_RDONLY
) {
510 r
= pakfire_mount(pakfire
, src
, mountpoint
, "bind", MS_BIND
|MS_REC
, NULL
);
514 // Add the remount flag
519 return pakfire_mount(pakfire
, src
, mountpoint
, "bind", flags
|MS_BIND
|MS_REC
, NULL
);