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>
29 #include <pakfire/arch.h>
30 #include <pakfire/logging.h>
31 #include <pakfire/pakfire.h>
32 #include <pakfire/parse.h>
33 #include <pakfire/path.h>
34 #include <pakfire/mount.h>
35 #include <pakfire/string.h>
36 #include <pakfire/util.h>
38 static const struct pakfire_mountpoint
{
45 // Mount a new instance of /proc
46 { "pakfire_proc", "proc", "proc",
47 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
, },
49 // Make /proc/sys read-only (except /proc/sys/net)
50 { "/proc/sys", "proc/sys", "bind", MS_BIND
|MS_REC
, NULL
, },
51 { "/proc/sys/net", "proc/sys/net", "bind", MS_BIND
|MS_REC
, NULL
, },
52 { "/proc/sys", "proc/sys", "bind",
53 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
55 // Deny write access to /proc/sysrq-trigger (can be used to restart the host)
56 { "/proc/sysrq-trigger", "proc/sysrq-trigger", "bind", MS_BIND
|MS_REC
, NULL
, },
57 { "/proc/sysrq-trigger", "proc/sysrq-trigger", "bind",
58 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
60 // Make /proc/irq read-only
61 { "/proc/irq", "proc/irq", "bind", MS_BIND
|MS_REC
, NULL
, },
62 { "/proc/irq", "proc/irq", "bind",
63 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
65 // Make /proc/bus read-only
66 { "/proc/bus", "proc/bus", "bind", MS_BIND
|MS_REC
, NULL
, },
67 { "/proc/bus", "proc/bus", "bind",
68 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
70 // Bind-Mount /sys ready-only
71 { "/sys", "sys", "bind", MS_BIND
|MS_REC
, NULL
, },
72 { "/sys", "sys", "bind",
73 MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
, },
76 { "pakfire_dev", "dev", "tmpfs", MS_NOSUID
|MS_NOEXEC
,
77 "mode=0755,size=4m,nr_inodes=64k", },
78 { "pakfire_dev_pts", "dev/pts", "devpts", MS_NOSUID
|MS_NOEXEC
,
79 "newinstance,ptmxmode=0666,mode=620", },
81 // Create a new /dev/shm
82 { "pakfire_dev_shm", "dev/shm", "tmpfs",
83 MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, "mode=1777,size=1024m", },
86 { "mqueue", "dev/mqueue", "mqueue",
87 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
},
90 { "pakfire_run", "run", "tmpfs", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
91 "mode=755,size=256m,nr_inodes=1k", },
94 { "pakfire_tmp", "tmp", "tmpfs",
95 MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, "mode=1777,size=4096m", },
101 static const struct pakfire_devnode
{
108 { "/dev/null", 1, 3, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
109 { "/dev/zero", 1, 5, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
110 { "/dev/full", 1, 7, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
111 { "/dev/random", 1, 8, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
112 { "/dev/urandom", 1, 9, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
113 { "/dev/kmsg", 1, 11, S_IFCHR
|S_IRUSR
|S_IRGRP
|S_IROTH
, 0 },
114 { "/dev/tty", 5, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
, 0 },
115 { "/dev/console", 5, 1, S_IFCHR
|S_IRUSR
|S_IWUSR
, 0 },
116 { "/dev/rtc0", 252, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
, 0 },
119 { "/dev/loop-control", 10, 237, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
120 { "/dev/loop0", 7, 0, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
121 { "/dev/loop1", 7, 1, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
122 { "/dev/loop2", 7, 2, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
123 { "/dev/loop3", 7, 3, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
124 { "/dev/loop4", 7, 4, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
125 { "/dev/loop5", 7, 5, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
126 { "/dev/loop6", 7, 6, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
127 { "/dev/loop7", 7, 7, S_IFCHR
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
, PAKFIRE_MOUNT_LOOP_DEVICES
},
132 static const struct pakfire_symlink
{
136 { "/dev/pts/ptmx", "/dev/ptmx", },
137 { "/proc/self/fd", "/dev/fd", },
138 { "/proc/self/fd/0", "/dev/stdin" },
139 { "/proc/self/fd/1", "/dev/stdout" },
140 { "/proc/self/fd/2", "/dev/stderr" },
141 { "/proc/kcore", "/dev/core" },
145 int pakfire_mount_change_propagation(struct pakfire_ctx
* ctx
, const char* path
, int propagation
) {
146 CTX_DEBUG(ctx
, "Changing mount propagation on %s\n", path
);
148 int r
= mount(NULL
, path
, NULL
, propagation
|MS_REC
, NULL
);
150 CTX_ERROR(ctx
, "Failed to change mount propagation on %s: %m\n", path
);
155 static int pakfire_mount_is_mountpoint(struct pakfire
* pakfire
, const char* path
) {
156 // XXX THIS STILL NEEDS TO BE IMPLEMENTED
160 int pakfire_mount_make_mounpoint(struct pakfire
* pakfire
, const char* path
) {
163 // Check if path already is a mountpoint
164 r
= pakfire_mount_is_mountpoint(pakfire
, path
);
166 // Already is a mountpoint
170 // Is not a mountpoint
175 ERROR(pakfire
, "Could not determine whether %s is a mountpoint: %m\n", path
);
179 // Bind-mount to self
180 r
= mount(path
, path
, NULL
, MS_BIND
|MS_REC
, NULL
);
182 ERROR(pakfire
, "Could not make %s a mountpoint: %m\n", path
);
189 static int pakfire_mount(struct pakfire
* pakfire
, const char* source
, const char* target
,
190 const char* fstype
, unsigned long mflags
, const void* data
) {
191 const char* options
= (const char*)data
;
193 // Check for some basic inputs
194 if (!source
|| !target
) {
199 DEBUG(pakfire
, "Mounting %s from %s (%s - %s)\n", target
, source
, fstype
, options
);
202 int r
= mount(source
, target
, fstype
, mflags
, data
);
204 ERROR(pakfire
, "Could not mount %s: %m\n", target
);
210 static int __pakfire_mount_list(char* line
, size_t length
, void* data
) {
211 struct pakfire_ctx
* ctx
= data
;
213 // Send the line to the logger
214 CTX_DEBUG(ctx
, " %.*s", (int)length
, line
);
219 int pakfire_mount_list(struct pakfire_ctx
* ctx
) {
220 CTX_DEBUG(ctx
, "Mountpoints:\n");
222 return pakfire_parse_file("/proc/self/mounts", __pakfire_mount_list
, ctx
);
225 static int pakfire_populate_dev(struct pakfire
* pakfire
, int flags
) {
228 // Create device nodes
229 for (const struct pakfire_devnode
* devnode
= devnodes
; devnode
->path
; devnode
++) {
230 DEBUG(pakfire
, "Creating device node %s\n", devnode
->path
);
232 // Check if flags match
233 if (devnode
->flags
&& !(flags
& devnode
->flags
))
236 int r
= pakfire_path(pakfire
, path
, "%s", devnode
->path
);
240 dev_t dev
= makedev(devnode
->major
, devnode
->minor
);
242 r
= mknod(path
, devnode
->mode
, dev
);
244 // Continue if mknod was successful
248 // If we could not create the device node because of permission issues,
249 // it might be likely that we are running in a user namespace where creating
250 // device nodes is not permitted. Try bind-mounting them.
254 // Otherwise log an error and end
255 ERROR(pakfire
, "Could not create %s: %m\n", devnode
->path
);
259 // Create an empty file
260 r
= pakfire_touch(path
, 0444);
262 ERROR(pakfire
, "Could not create %s: %m\n", path
);
266 // Create a bind-mount over the file
267 r
= pakfire_mount(pakfire
, devnode
->path
, path
, "bind", MS_BIND
, NULL
);
273 for (const struct pakfire_symlink
* s
= symlinks
; s
->target
; s
++) {
274 DEBUG(pakfire
, "Creating symlink %s -> %s\n", s
->path
, s
->target
);
276 int r
= pakfire_path(pakfire
, path
, "%s", s
->path
);
280 r
= symlink(s
->target
, path
);
282 ERROR(pakfire
, "Could not create symlink %s: %m\n", s
->path
);
290 static int pakfire_mount_interpreter(struct pakfire
* pakfire
) {
291 char target
[PATH_MAX
];
293 // Fetch the target architecture
294 const char* arch
= pakfire_get_effective_arch(pakfire
);
296 // Can we emulate this architecture?
297 char* interpreter
= pakfire_arch_find_interpreter(arch
);
299 // No interpreter required
303 DEBUG(pakfire
, "Mounting interpreter %s for %s\n", interpreter
, arch
);
305 // Where to mount this?
306 int r
= pakfire_path(pakfire
, target
, "%s", interpreter
);
311 r
= pakfire_mkparentdir(target
, 0755);
315 // Create an empty file
316 FILE* f
= fopen(target
, "w");
321 r
= pakfire_mount(pakfire
, interpreter
, target
, NULL
, MS_BIND
|MS_RDONLY
, NULL
);
323 ERROR(pakfire
, "Could not mount interpreter %s to %s: %m\n", interpreter
, target
);
328 int pakfire_mount_all(struct pakfire
* pakfire
, int flags
) {
329 char target
[PATH_MAX
];
332 // Fetch Pakfire's root directory
333 const char* root
= pakfire_get_path(pakfire
);
335 for (const struct pakfire_mountpoint
* mp
= mountpoints
; mp
->source
; mp
++) {
336 // Figure out where to mount
337 r
= pakfire_path_append(target
, root
, mp
->target
);
341 // Create target if it doesn't exist
342 if (!pakfire_path_exists(target
)) {
343 r
= pakfire_mkdir(target
, 0755);
345 ERROR(pakfire
, "Could not create %s: %m\n", target
);
351 r
= pakfire_mount(pakfire
, mp
->source
, target
, mp
->fstype
, mp
->flags
, mp
->options
);
357 r
= pakfire_populate_dev(pakfire
, flags
);
361 // Mount the interpreter (if needed)
362 r
= pakfire_mount_interpreter(pakfire
);
369 int pakfire_make_ramdisk(struct pakfire
* pakfire
, char* path
, const char* args
) {
372 // Create a new temporary directory
373 char* p
= pakfire_mkdtemp(path
);
378 r
= pakfire_mount(pakfire
, "pakfire_ramdisk", p
, "tmpfs", 0, args
);
380 ERROR_ERRNO(pakfire
, r
, "Could not mount ramdisk at %s (%s): %m\n", p
, args
);
384 DEBUG(pakfire
, "Ramdisk mounted at %s (%s)\n", p
, args
);
389 int pakfire_bind(struct pakfire
* pakfire
, const char* src
, const char* dst
, int flags
) {
391 char mountpoint
[PATH_MAX
];
396 int r
= pakfire_path(pakfire
, mountpoint
, "%s", dst
);
400 DEBUG(pakfire
, "Bind-mounting %s to %s\n", src
, mountpoint
);
404 ERROR(pakfire
, "Could not stat %s: %m\n", src
);
408 // Make sure the mountpoint exists
409 switch (st
.st_mode
& S_IFMT
) {
411 r
= pakfire_mkdir(mountpoint
, st
.st_mode
);
412 if (r
&& errno
!= EEXIST
)
418 // Make parent directory
419 r
= pakfire_mkparentdir(mountpoint
, 0755);
424 FILE* f
= fopen(mountpoint
, "w");
435 // The Linux kernel seems to be quite funny when trying to bind-mount something
436 // as read-only and requires us to mount the source first, and then remount it
437 // again using MS_RDONLY.
438 if (flags
& MS_RDONLY
) {
439 r
= pakfire_mount(pakfire
, src
, mountpoint
, "bind", MS_BIND
|MS_REC
, NULL
);
443 // Add the remount flag
448 return pakfire_mount(pakfire
, src
, mountpoint
, "bind", flags
|MS_BIND
|MS_REC
, NULL
);