Skip to content

Commit 2812640

Browse files
committed
mount-util: add helpers for mount() without following symlinks
1 parent e59d030 commit 2812640

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎src/shared/mount-util.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,60 @@
1919
#include "parse-util.h"
2020
#include "path-util.h"
2121
#include "set.h"
22+
#include "stat-util.h"
2223
#include "stdio-util.h"
2324
#include "string-util.h"
2425
#include "strv.h"
2526

27+
int mount_fd(const char *source,
28+
int target_fd,
29+
const char *filesystemtype,
30+
unsigned long mountflags,
31+
const void *data) {
32+
33+
char path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
34+
35+
xsprintf(path, "/proc/self/fd/%i", target_fd);
36+
if (mount(source, path, filesystemtype, mountflags, data) < 0) {
37+
if (errno != ENOENT)
38+
return -errno;
39+
40+
/* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
41+
* mounted. Check for the latter to generate better error messages. */
42+
if (proc_mounted() == 0)
43+
return -ENOSYS;
44+
45+
return -ENOENT;
46+
}
47+
48+
return 0;
49+
}
50+
51+
int mount_nofollow(
52+
const char *source,
53+
const char *target,
54+
const char *filesystemtype,
55+
unsigned long mountflags,
56+
const void *data) {
57+
58+
_cleanup_close_ int fd = -1;
59+
60+
/* In almost all cases we want to manipulate the mount table without following symlinks, hence
61+
* mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is
62+
* not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early
63+
* initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the
64+
* fs to mount) we can only use traditional mount() directly.
65+
*
66+
* Note that this disables following only for the final component of the target, i.e symlinks within
67+
* the path of the target are honoured, as are symlinks in the source path everywhere. */
68+
69+
fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
70+
if (fd < 0)
71+
return -errno;
72+
73+
return mount_fd(source, fd, filesystemtype, mountflags, data);
74+
}
75+
2676
int umount_recursive(const char *prefix, int flags) {
2777
int n = 0, r;
2878
bool again;

‎src/shared/mount-util.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#define TMPFS_LIMITS_ROOTFS TMPFS_LIMITS_VAR
3333
#define TMPFS_LIMITS_VOLATILE_STATE TMPFS_LIMITS_VAR
3434

35+
int mount_fd(const char *source, int target_fd, const char *filesystemtype, unsigned long mountflags, const void *data);
36+
int mount_nofollow(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data);
37+
3538
int repeat_unmount(const char *path, int flags);
3639
int umount_recursive(const char *target, int flags);
3740
int bind_remount_recursive(const char *prefix, unsigned long new_flags, unsigned long flags_mask, char **deny_list);

0 commit comments

Comments
 (0)