Commit f608caba authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'vfs-6.11.mount' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull vfs mount query updates from Christian Brauner:
 "This contains work to extend the abilities of listmount() and
  statmount() and various fixes and cleanups.

  Features:

   - Allow iterating through mounts via listmount() from newest to
     oldest. This makes it possible for mount(8) to keep iterating the
     mount table in reverse order so it gets newest mounts first.

   - Relax permissions on listmount() and statmount().

     It's not necessary to have capabilities in the initial namespace:
     it is sufficient to have capabilities in the owning namespace of
     the mount namespace we're located in to list unreachable mounts in
     that namespace.

   - Extend both listmount() and statmount() to list and stat mounts in
     foreign mount namespaces.

     Currently the only way to iterate over mount entries in mount
     namespaces that aren't in the caller's mount namespace is by
     crawling through /proc in order to find /proc/<pid>/mountinfo for
     the relevant mount namespace.

     This is both very clumsy and hugely inefficient. So extend struct
     mnt_id_req with a new member that allows to specify the mount
     namespace id of the mount namespace we want to look at.

     Luckily internally we already have most of the infrastructure for
     this so we just need to expose it to userspace. Give userspace a
     way to retrieve the id of a mount namespace via statmount() and
     through a new nsfs ioctl() on mount namespace file descriptor.

     This comes with appropriate selftests.

   - Expose mount options through statmount().

     Currently if userspace wants to get mount options for a mount and
     with statmount(), they still have to open /proc/<pid>/mountinfo to
     parse mount options. Simply the information through statmount()
     directly.

     Afterwards it's possible to only rely on statmount() and
     listmount() to retrieve all and more information than
     /proc/<pid>/mountinfo provides.

     This comes with appropriate selftests.

  Fixes:

   - Avoid copying to userspace under the namespace semaphore in
     listmount.

  Cleanups:

   - Simplify the error handling in listmount by relying on our newly
     added cleanup infrastructure.

   - Refuse invalid mount ids early for both listmount and statmount"

* tag 'vfs-6.11.mount' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs:
  fs: reject invalid last mount id early
  fs: refuse mnt id requests with invalid ids early
  fs: find rootfs mount of the mount namespace
  fs: only copy to userspace on success in listmount()
  sefltests: extend the statmount test for mount options
  fs: use guard for namespace_sem in statmount()
  fs: export mount options via statmount()
  fs: rename show_mnt_opts -> show_vfsmnt_opts
  selftests: add a test for the foreign mnt ns extensions
  fs: add an ioctl to get the mnt ns id from nsfs
  fs: Allow statmount() in foreign mount namespace
  fs: Allow listmount() in foreign mount namespace
  fs: export the mount ns id via statmount
  fs: keep an index of current mount namespaces
  fs: relax permissions for statmount()
  listmount: allow listing in reverse order
  fs: relax permissions for listmount()
  fs: simplify error handling
  fs: don't copy to userspace under namespace semaphore
  path: add cleanup helper
parents 2aae1d67 4bed843b
...@@ -16,6 +16,8 @@ struct mnt_namespace { ...@@ -16,6 +16,8 @@ struct mnt_namespace {
u64 event; u64 event;
unsigned int nr_mounts; /* # of mounts in the namespace */ unsigned int nr_mounts; /* # of mounts in the namespace */
unsigned int pending_mounts; unsigned int pending_mounts;
struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */
refcount_t passive; /* number references not pinning @mounts */
} __randomize_layout; } __randomize_layout;
struct mnt_pcp { struct mnt_pcp {
......
This diff is collapsed.
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/nsfs.h> #include <linux/nsfs.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include "mount.h"
#include "internal.h" #include "internal.h"
static struct vfsmount *nsfs_mnt; static struct vfsmount *nsfs_mnt;
...@@ -143,6 +144,19 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, ...@@ -143,6 +144,19 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
argp = (uid_t __user *) arg; argp = (uid_t __user *) arg;
uid = from_kuid_munged(current_user_ns(), user_ns->owner); uid = from_kuid_munged(current_user_ns(), user_ns->owner);
return put_user(uid, argp); return put_user(uid, argp);
case NS_GET_MNTNS_ID: {
struct mnt_namespace *mnt_ns;
__u64 __user *idp;
__u64 id;
if (ns->ops->type != CLONE_NEWNS)
return -EINVAL;
mnt_ns = container_of(ns, struct mnt_namespace, ns);
idp = (__u64 __user *)arg;
id = mnt_ns->seq;
return put_user(id, idp);
}
default: default:
return -ENOTTY; return -ENOTTY;
} }
......
...@@ -61,7 +61,7 @@ static int show_sb_opts(struct seq_file *m, struct super_block *sb) ...@@ -61,7 +61,7 @@ static int show_sb_opts(struct seq_file *m, struct super_block *sb)
return security_sb_show_options(m, sb); return security_sb_show_options(m, sb);
} }
static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt) static void show_vfsmnt_opts(struct seq_file *m, struct vfsmount *mnt)
{ {
static const struct proc_fs_opts mnt_opts[] = { static const struct proc_fs_opts mnt_opts[] = {
{ MNT_NOSUID, ",nosuid" }, { MNT_NOSUID, ",nosuid" },
...@@ -124,7 +124,7 @@ static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt) ...@@ -124,7 +124,7 @@ static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
err = show_sb_opts(m, sb); err = show_sb_opts(m, sb);
if (err) if (err)
goto out; goto out;
show_mnt_opts(m, mnt); show_vfsmnt_opts(m, mnt);
if (sb->s_op->show_options) if (sb->s_op->show_options)
err = sb->s_op->show_options(m, mnt_path.dentry); err = sb->s_op->show_options(m, mnt_path.dentry);
seq_puts(m, " 0 0\n"); seq_puts(m, " 0 0\n");
...@@ -153,7 +153,7 @@ static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt) ...@@ -153,7 +153,7 @@ static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
goto out; goto out;
seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw"); seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
show_mnt_opts(m, mnt); show_vfsmnt_opts(m, mnt);
/* Tagged fields ("foo:X" or "bar") */ /* Tagged fields ("foo:X" or "bar") */
if (IS_MNT_SHARED(r)) if (IS_MNT_SHARED(r))
......
...@@ -24,4 +24,13 @@ static inline void path_put_init(struct path *path) ...@@ -24,4 +24,13 @@ static inline void path_put_init(struct path *path)
*path = (struct path) { }; *path = (struct path) { };
} }
/*
* Cleanup macro for use with __free(path_put). Avoids dereference and
* copying @path unlike DEFINE_FREE(). path_put() will handle the empty
* path correctly just ensure @path is initialized:
*
* struct path path __free(path_put) = {};
*/
#define __free_path_put path_put
#endif /* _LINUX_PATH_H */ #endif /* _LINUX_PATH_H */
...@@ -154,7 +154,7 @@ struct mount_attr { ...@@ -154,7 +154,7 @@ struct mount_attr {
*/ */
struct statmount { struct statmount {
__u32 size; /* Total size, including strings */ __u32 size; /* Total size, including strings */
__u32 __spare1; __u32 mnt_opts; /* [str] Mount options of the mount */
__u64 mask; /* What results were written */ __u64 mask; /* What results were written */
__u32 sb_dev_major; /* Device ID */ __u32 sb_dev_major; /* Device ID */
__u32 sb_dev_minor; __u32 sb_dev_minor;
...@@ -172,7 +172,8 @@ struct statmount { ...@@ -172,7 +172,8 @@ struct statmount {
__u64 propagate_from; /* Propagation from in current namespace */ __u64 propagate_from; /* Propagation from in current namespace */
__u32 mnt_root; /* [str] Root of mount relative to root of fs */ __u32 mnt_root; /* [str] Root of mount relative to root of fs */
__u32 mnt_point; /* [str] Mountpoint relative to current root */ __u32 mnt_point; /* [str] Mountpoint relative to current root */
__u64 __spare2[50]; __u64 mnt_ns_id; /* ID of the mount namespace */
__u64 __spare2[49];
char str[]; /* Variable size part containing strings */ char str[]; /* Variable size part containing strings */
}; };
...@@ -188,10 +189,12 @@ struct mnt_id_req { ...@@ -188,10 +189,12 @@ struct mnt_id_req {
__u32 spare; __u32 spare;
__u64 mnt_id; __u64 mnt_id;
__u64 param; __u64 param;
__u64 mnt_ns_id;
}; };
/* List of all mnt_id_req versions. */ /* List of all mnt_id_req versions. */
#define MNT_ID_REQ_SIZE_VER0 24 /* sizeof first published struct */ #define MNT_ID_REQ_SIZE_VER0 24 /* sizeof first published struct */
#define MNT_ID_REQ_SIZE_VER1 32 /* sizeof second published struct */
/* /*
* @mask bits for statmount(2) * @mask bits for statmount(2)
...@@ -202,10 +205,13 @@ struct mnt_id_req { ...@@ -202,10 +205,13 @@ struct mnt_id_req {
#define STATMOUNT_MNT_ROOT 0x00000008U /* Want/got mnt_root */ #define STATMOUNT_MNT_ROOT 0x00000008U /* Want/got mnt_root */
#define STATMOUNT_MNT_POINT 0x00000010U /* Want/got mnt_point */ #define STATMOUNT_MNT_POINT 0x00000010U /* Want/got mnt_point */
#define STATMOUNT_FS_TYPE 0x00000020U /* Want/got fs_type */ #define STATMOUNT_FS_TYPE 0x00000020U /* Want/got fs_type */
#define STATMOUNT_MNT_NS_ID 0x00000040U /* Want/got mnt_ns_id */
#define STATMOUNT_MNT_OPTS 0x00000080U /* Want/got mnt_opts */
/* /*
* Special @mnt_id values that can be passed to listmount * Special @mnt_id values that can be passed to listmount
*/ */
#define LSMT_ROOT 0xffffffffffffffff /* root mount */ #define LSMT_ROOT 0xffffffffffffffff /* root mount */
#define LISTMOUNT_REVERSE (1 << 0) /* List later mounts first */
#endif /* _UAPI_LINUX_MOUNT_H */ #endif /* _UAPI_LINUX_MOUNT_H */
...@@ -15,5 +15,7 @@ ...@@ -15,5 +15,7 @@
#define NS_GET_NSTYPE _IO(NSIO, 0x3) #define NS_GET_NSTYPE _IO(NSIO, 0x3)
/* Get owner UID (in the caller's user namespace) for a user namespace */ /* Get owner UID (in the caller's user namespace) for a user namespace */
#define NS_GET_OWNER_UID _IO(NSIO, 0x4) #define NS_GET_OWNER_UID _IO(NSIO, 0x4)
/* Get the id for a mount namespace */
#define NS_GET_MNTNS_ID _IO(NSIO, 0x5)
#endif /* __LINUX_NSFS_H */ #endif /* __LINUX_NSFS_H */
# SPDX-License-Identifier: GPL-2.0-or-later # SPDX-License-Identifier: GPL-2.0-or-later
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
TEST_GEN_PROGS := statmount_test TEST_GEN_PROGS := statmount_test statmount_test_ns
include ../../lib.mk include ../../lib.mk
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __STATMOUNT_H
#define __STATMOUNT_H
#include <stdint.h>
#include <linux/mount.h>
#include <asm/unistd.h>
static inline int statmount(uint64_t mnt_id, uint64_t mnt_ns_id, uint64_t mask,
struct statmount *buf, size_t bufsize,
unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER0,
.mnt_id = mnt_id,
.param = mask,
};
if (mnt_ns_id) {
req.size = MNT_ID_REQ_SIZE_VER1;
req.mnt_ns_id = mnt_ns_id;
}
return syscall(__NR_statmount, &req, buf, bufsize, flags);
}
static ssize_t listmount(uint64_t mnt_id, uint64_t mnt_ns_id,
uint64_t last_mnt_id, uint64_t list[], size_t num,
unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER0,
.mnt_id = mnt_id,
.param = last_mnt_id,
};
if (mnt_ns_id) {
req.size = MNT_ID_REQ_SIZE_VER1;
req.mnt_ns_id = mnt_ns_id;
}
return syscall(__NR_listmount, &req, list, num, flags);
}
#endif /* __STATMOUNT_H */
...@@ -4,17 +4,15 @@ ...@@ -4,17 +4,15 @@
#include <assert.h> #include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <sched.h> #include <sched.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statfs.h> #include <sys/statfs.h>
#include <linux/mount.h>
#include <linux/stat.h> #include <linux/stat.h>
#include <asm/unistd.h>
#include "statmount.h"
#include "../../kselftest.h" #include "../../kselftest.h"
static const char *const known_fs[] = { static const char *const known_fs[] = {
...@@ -36,18 +34,6 @@ static const char *const known_fs[] = { ...@@ -36,18 +34,6 @@ static const char *const known_fs[] = {
"ufs", "v7", "vboxsf", "vfat", "virtiofs", "vxfs", "xenfs", "xfs", "ufs", "v7", "vboxsf", "vfat", "virtiofs", "vxfs", "xenfs", "xfs",
"zonefs", NULL }; "zonefs", NULL };
static int statmount(uint64_t mnt_id, uint64_t mask, struct statmount *buf,
size_t bufsize, unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER0,
.mnt_id = mnt_id,
.param = mask,
};
return syscall(__NR_statmount, &req, buf, bufsize, flags);
}
static struct statmount *statmount_alloc(uint64_t mnt_id, uint64_t mask, unsigned int flags) static struct statmount *statmount_alloc(uint64_t mnt_id, uint64_t mask, unsigned int flags)
{ {
size_t bufsize = 1 << 15; size_t bufsize = 1 << 15;
...@@ -56,7 +42,7 @@ static struct statmount *statmount_alloc(uint64_t mnt_id, uint64_t mask, unsigne ...@@ -56,7 +42,7 @@ static struct statmount *statmount_alloc(uint64_t mnt_id, uint64_t mask, unsigne
int ret; int ret;
for (;;) { for (;;) {
ret = statmount(mnt_id, mask, tmp, bufsize, flags); ret = statmount(mnt_id, 0, mask, tmp, bufsize, flags);
if (ret != -1) if (ret != -1)
break; break;
if (tofree) if (tofree)
...@@ -121,7 +107,7 @@ static char root_mntpoint[] = "/tmp/statmount_test_root.XXXXXX"; ...@@ -121,7 +107,7 @@ static char root_mntpoint[] = "/tmp/statmount_test_root.XXXXXX";
static int orig_root; static int orig_root;
static uint64_t root_id, parent_id; static uint64_t root_id, parent_id;
static uint32_t old_root_id, old_parent_id; static uint32_t old_root_id, old_parent_id;
static FILE *f_mountinfo;
static void cleanup_namespace(void) static void cleanup_namespace(void)
{ {
...@@ -146,7 +132,7 @@ static void setup_namespace(void) ...@@ -146,7 +132,7 @@ static void setup_namespace(void)
uid_t uid = getuid(); uid_t uid = getuid();
gid_t gid = getgid(); gid_t gid = getgid();
ret = unshare(CLONE_NEWNS|CLONE_NEWUSER); ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
if (ret == -1) if (ret == -1)
ksft_exit_fail_msg("unsharing mountns and userns: %s\n", ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
strerror(errno)); strerror(errno));
...@@ -157,6 +143,11 @@ static void setup_namespace(void) ...@@ -157,6 +143,11 @@ static void setup_namespace(void)
sprintf(buf, "0 %d 1", gid); sprintf(buf, "0 %d 1", gid);
write_file("/proc/self/gid_map", buf); write_file("/proc/self/gid_map", buf);
f_mountinfo = fopen("/proc/self/mountinfo", "re");
if (!f_mountinfo)
ksft_exit_fail_msg("failed to open mountinfo: %s\n",
strerror(errno));
ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL); ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
if (ret == -1) if (ret == -1)
ksft_exit_fail_msg("making mount tree private: %s\n", ksft_exit_fail_msg("making mount tree private: %s\n",
...@@ -216,25 +207,13 @@ static int setup_mount_tree(int log2_num) ...@@ -216,25 +207,13 @@ static int setup_mount_tree(int log2_num)
return 0; return 0;
} }
static ssize_t listmount(uint64_t mnt_id, uint64_t last_mnt_id,
uint64_t list[], size_t num, unsigned int flags)
{
struct mnt_id_req req = {
.size = MNT_ID_REQ_SIZE_VER0,
.mnt_id = mnt_id,
.param = last_mnt_id,
};
return syscall(__NR_listmount, &req, list, num, flags);
}
static void test_listmount_empty_root(void) static void test_listmount_empty_root(void)
{ {
ssize_t res; ssize_t res;
const unsigned int size = 32; const unsigned int size = 32;
uint64_t list[size]; uint64_t list[size];
res = listmount(LSMT_ROOT, 0, list, size, 0); res = listmount(LSMT_ROOT, 0, 0, list, size, 0);
if (res == -1) { if (res == -1) {
ksft_test_result_fail("listmount: %s\n", strerror(errno)); ksft_test_result_fail("listmount: %s\n", strerror(errno));
return; return;
...@@ -259,7 +238,7 @@ static void test_statmount_zero_mask(void) ...@@ -259,7 +238,7 @@ static void test_statmount_zero_mask(void)
struct statmount sm; struct statmount sm;
int ret; int ret;
ret = statmount(root_id, 0, &sm, sizeof(sm), 0); ret = statmount(root_id, 0, 0, &sm, sizeof(sm), 0);
if (ret == -1) { if (ret == -1) {
ksft_test_result_fail("statmount zero mask: %s\n", ksft_test_result_fail("statmount zero mask: %s\n",
strerror(errno)); strerror(errno));
...@@ -285,7 +264,7 @@ static void test_statmount_mnt_basic(void) ...@@ -285,7 +264,7 @@ static void test_statmount_mnt_basic(void)
int ret; int ret;
uint64_t mask = STATMOUNT_MNT_BASIC; uint64_t mask = STATMOUNT_MNT_BASIC;
ret = statmount(root_id, mask, &sm, sizeof(sm), 0); ret = statmount(root_id, 0, mask, &sm, sizeof(sm), 0);
if (ret == -1) { if (ret == -1) {
ksft_test_result_fail("statmount mnt basic: %s\n", ksft_test_result_fail("statmount mnt basic: %s\n",
strerror(errno)); strerror(errno));
...@@ -345,7 +324,7 @@ static void test_statmount_sb_basic(void) ...@@ -345,7 +324,7 @@ static void test_statmount_sb_basic(void)
struct statx sx; struct statx sx;
struct statfs sf; struct statfs sf;
ret = statmount(root_id, mask, &sm, sizeof(sm), 0); ret = statmount(root_id, 0, mask, &sm, sizeof(sm), 0);
if (ret == -1) { if (ret == -1) {
ksft_test_result_fail("statmount sb basic: %s\n", ksft_test_result_fail("statmount sb basic: %s\n",
strerror(errno)); strerror(errno));
...@@ -470,6 +449,88 @@ static void test_statmount_fs_type(void) ...@@ -470,6 +449,88 @@ static void test_statmount_fs_type(void)
free(sm); free(sm);
} }
static void test_statmount_mnt_opts(void)
{
struct statmount *sm;
const char *statmount_opts;
char *line = NULL;
size_t len = 0;
sm = statmount_alloc(root_id, STATMOUNT_MNT_BASIC | STATMOUNT_MNT_OPTS,
0);
if (!sm) {
ksft_test_result_fail("statmount mnt opts: %s\n",
strerror(errno));
return;
}
while (getline(&line, &len, f_mountinfo) != -1) {
int i;
char *p, *p2;
unsigned int old_mnt_id;
old_mnt_id = atoi(line);
if (old_mnt_id != sm->mnt_id_old)
continue;
for (p = line, i = 0; p && i < 5; i++)
p = strchr(p + 1, ' ');
if (!p)
continue;
p2 = strchr(p + 1, ' ');
if (!p2)
continue;
*p2 = '\0';
p = strchr(p2 + 1, '-');
if (!p)
continue;
for (p++, i = 0; p && i < 2; i++)
p = strchr(p + 1, ' ');
if (!p)
continue;
p++;
/* skip generic superblock options */
if (strncmp(p, "ro", 2) == 0)
p += 2;
else if (strncmp(p, "rw", 2) == 0)
p += 2;
if (*p == ',')
p++;
if (strncmp(p, "sync", 4) == 0)
p += 4;
if (*p == ',')
p++;
if (strncmp(p, "dirsync", 7) == 0)
p += 7;
if (*p == ',')
p++;
if (strncmp(p, "lazytime", 8) == 0)
p += 8;
if (*p == ',')
p++;
p2 = strrchr(p, '\n');
if (p2)
*p2 = '\0';
statmount_opts = sm->str + sm->mnt_opts;
if (strcmp(statmount_opts, p) != 0)
ksft_test_result_fail(
"unexpected mount options: '%s' != '%s'\n",
statmount_opts, p);
else
ksft_test_result_pass("statmount mount options\n");
free(sm);
free(line);
return;
}
ksft_test_result_fail("didnt't find mount entry\n");
free(sm);
free(line);
}
static void test_statmount_string(uint64_t mask, size_t off, const char *name) static void test_statmount_string(uint64_t mask, size_t off, const char *name)
{ {
struct statmount *sm; struct statmount *sm;
...@@ -506,14 +567,14 @@ static void test_statmount_string(uint64_t mask, size_t off, const char *name) ...@@ -506,14 +567,14 @@ static void test_statmount_string(uint64_t mask, size_t off, const char *name)
exactsize = sm->size; exactsize = sm->size;
shortsize = sizeof(*sm) + i; shortsize = sizeof(*sm) + i;
ret = statmount(root_id, mask, sm, exactsize, 0); ret = statmount(root_id, 0, mask, sm, exactsize, 0);
if (ret == -1) { if (ret == -1) {
ksft_test_result_fail("statmount exact size: %s\n", ksft_test_result_fail("statmount exact size: %s\n",
strerror(errno)); strerror(errno));
goto out; goto out;
} }
errno = 0; errno = 0;
ret = statmount(root_id, mask, sm, shortsize, 0); ret = statmount(root_id, 0, mask, sm, shortsize, 0);
if (ret != -1 || errno != EOVERFLOW) { if (ret != -1 || errno != EOVERFLOW) {
ksft_test_result_fail("should have failed with EOVERFLOW: %s\n", ksft_test_result_fail("should have failed with EOVERFLOW: %s\n",
strerror(errno)); strerror(errno));
...@@ -541,7 +602,7 @@ static void test_listmount_tree(void) ...@@ -541,7 +602,7 @@ static void test_listmount_tree(void)
if (res == -1) if (res == -1)
return; return;
num = res = listmount(LSMT_ROOT, 0, list, size, 0); num = res = listmount(LSMT_ROOT, 0, 0, list, size, 0);
if (res == -1) { if (res == -1) {
ksft_test_result_fail("listmount: %s\n", strerror(errno)); ksft_test_result_fail("listmount: %s\n", strerror(errno));
return; return;
...@@ -553,7 +614,7 @@ static void test_listmount_tree(void) ...@@ -553,7 +614,7 @@ static void test_listmount_tree(void)
} }
for (i = 0; i < size - step;) { for (i = 0; i < size - step;) {
res = listmount(LSMT_ROOT, i ? list2[i - 1] : 0, list2 + i, step, 0); res = listmount(LSMT_ROOT, 0, i ? list2[i - 1] : 0, list2 + i, step, 0);
if (res == -1) if (res == -1)
ksft_test_result_fail("short listmount: %s\n", ksft_test_result_fail("short listmount: %s\n",
strerror(errno)); strerror(errno));
...@@ -585,18 +646,18 @@ int main(void) ...@@ -585,18 +646,18 @@ int main(void)
int ret; int ret;
uint64_t all_mask = STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC | uint64_t all_mask = STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC |
STATMOUNT_PROPAGATE_FROM | STATMOUNT_MNT_ROOT | STATMOUNT_PROPAGATE_FROM | STATMOUNT_MNT_ROOT |
STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE; STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE | STATMOUNT_MNT_NS_ID;
ksft_print_header(); ksft_print_header();
ret = statmount(0, 0, NULL, 0, 0); ret = statmount(0, 0, 0, NULL, 0, 0);
assert(ret == -1); assert(ret == -1);
if (errno == ENOSYS) if (errno == ENOSYS)
ksft_exit_skip("statmount() syscall not supported\n"); ksft_exit_skip("statmount() syscall not supported\n");
setup_namespace(); setup_namespace();
ksft_set_plan(14); ksft_set_plan(15);
test_listmount_empty_root(); test_listmount_empty_root();
test_statmount_zero_mask(); test_statmount_zero_mask();
test_statmount_mnt_basic(); test_statmount_mnt_basic();
...@@ -604,6 +665,7 @@ int main(void) ...@@ -604,6 +665,7 @@ int main(void)
test_statmount_mnt_root(); test_statmount_mnt_root();
test_statmount_mnt_point(); test_statmount_mnt_point();
test_statmount_fs_type(); test_statmount_fs_type();
test_statmount_mnt_opts();
test_statmount_string(STATMOUNT_MNT_ROOT, str_off(mnt_root), "mount root"); test_statmount_string(STATMOUNT_MNT_ROOT, str_off(mnt_root), "mount root");
test_statmount_string(STATMOUNT_MNT_POINT, str_off(mnt_point), "mount point"); test_statmount_string(STATMOUNT_MNT_POINT, str_off(mnt_point), "mount point");
test_statmount_string(STATMOUNT_FS_TYPE, str_off(fs_type), "fs type"); test_statmount_string(STATMOUNT_FS_TYPE, str_off(fs_type), "fs type");
......
// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <linux/nsfs.h>
#include <linux/stat.h>
#include "statmount.h"
#include "../../kselftest.h"
#define NSID_PASS 0
#define NSID_FAIL 1
#define NSID_SKIP 2
#define NSID_ERROR 3
static void handle_result(int ret, const char *testname)
{
if (ret == NSID_PASS)
ksft_test_result_pass("%s\n", testname);
else if (ret == NSID_FAIL)
ksft_test_result_fail("%s\n", testname);
else if (ret == NSID_ERROR)
ksft_exit_fail_msg("%s\n", testname);
else
ksft_test_result_skip("%s\n", testname);
}
static inline int wait_for_pid(pid_t pid)
{
int status, ret;
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
return -1;
}
if (!WIFEXITED(status)) {
ksft_print_msg(
"waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
WIFSIGNALED(status), WTERMSIG(status));
return -1;
}
ret = WEXITSTATUS(status);
return ret;
}
static int get_mnt_ns_id(const char *mnt_ns, uint64_t *mnt_ns_id)
{
int fd = open(mnt_ns, O_RDONLY);
if (fd < 0) {
ksft_print_msg("failed to open for ns %s: %s\n",
mnt_ns, strerror(errno));
sleep(60);
return NSID_ERROR;
}
if (ioctl(fd, NS_GET_MNTNS_ID, mnt_ns_id) < 0) {
ksft_print_msg("failed to get the nsid for ns %s: %s\n",
mnt_ns, strerror(errno));
return NSID_ERROR;
}
close(fd);
return NSID_PASS;
}
static int get_mnt_id(const char *path, uint64_t *mnt_id)
{
struct statx sx;
int ret;
ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
if (ret == -1) {
ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
strerror(errno));
return NSID_ERROR;
}
if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
ksft_print_msg("no unique mount ID available for %s\n", path);
return NSID_ERROR;
}
*mnt_id = sx.stx_mnt_id;
return NSID_PASS;
}
static int write_file(const char *path, const char *val)
{
int fd = open(path, O_WRONLY);
size_t len = strlen(val);
int ret;
if (fd == -1) {
ksft_print_msg("opening %s for write: %s\n", path, strerror(errno));
return NSID_ERROR;
}
ret = write(fd, val, len);
if (ret == -1) {
ksft_print_msg("writing to %s: %s\n", path, strerror(errno));
return NSID_ERROR;
}
if (ret != len) {
ksft_print_msg("short write to %s\n", path);
return NSID_ERROR;
}
ret = close(fd);
if (ret == -1) {
ksft_print_msg("closing %s\n", path);
return NSID_ERROR;
}
return NSID_PASS;
}
static int setup_namespace(void)
{
int ret;
char buf[32];
uid_t uid = getuid();
gid_t gid = getgid();
ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
if (ret == -1)
ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
strerror(errno));
sprintf(buf, "0 %d 1", uid);
ret = write_file("/proc/self/uid_map", buf);
if (ret != NSID_PASS)
return ret;
ret = write_file("/proc/self/setgroups", "deny");
if (ret != NSID_PASS)
return ret;
sprintf(buf, "0 %d 1", gid);
ret = write_file("/proc/self/gid_map", buf);
if (ret != NSID_PASS)
return ret;
ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
if (ret == -1) {
ksft_print_msg("making mount tree private: %s\n",
strerror(errno));
return NSID_ERROR;
}
return NSID_PASS;
}
static int _test_statmount_mnt_ns_id(void)
{
struct statmount sm;
uint64_t mnt_ns_id;
uint64_t root_id;
int ret;
ret = get_mnt_ns_id("/proc/self/ns/mnt", &mnt_ns_id);
if (ret != NSID_PASS)
return ret;
ret = get_mnt_id("/", &root_id);
if (ret != NSID_PASS)
return ret;
ret = statmount(root_id, 0, STATMOUNT_MNT_NS_ID, &sm, sizeof(sm), 0);
if (ret == -1) {
ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
return NSID_ERROR;
}
if (sm.size != sizeof(sm)) {
ksft_print_msg("unexpected size: %u != %u\n", sm.size,
(uint32_t)sizeof(sm));
return NSID_FAIL;
}
if (sm.mask != STATMOUNT_MNT_NS_ID) {
ksft_print_msg("statmount mnt ns id unavailable\n");
return NSID_SKIP;
}
if (sm.mnt_ns_id != mnt_ns_id) {
ksft_print_msg("unexpected mnt ns ID: 0x%llx != 0x%llx\n",
(unsigned long long)sm.mnt_ns_id,
(unsigned long long)mnt_ns_id);
return NSID_FAIL;
}
return NSID_PASS;
}
static void test_statmount_mnt_ns_id(void)
{
pid_t pid;
int ret;
pid = fork();
if (pid < 0)
ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));
/* We're the original pid, wait for the result. */
if (pid != 0) {
ret = wait_for_pid(pid);
handle_result(ret, "test statmount ns id");
return;
}
ret = setup_namespace();
if (ret != NSID_PASS)
exit(ret);
ret = _test_statmount_mnt_ns_id();
exit(ret);
}
static int validate_external_listmount(pid_t pid, uint64_t child_nr_mounts)
{
uint64_t list[256];
uint64_t mnt_ns_id;
uint64_t nr_mounts;
char buf[256];
int ret;
/* Get the mount ns id for our child. */
snprintf(buf, sizeof(buf), "/proc/%lu/ns/mnt", (unsigned long)pid);
ret = get_mnt_ns_id(buf, &mnt_ns_id);
nr_mounts = listmount(LSMT_ROOT, mnt_ns_id, 0, list, 256, 0);
if (nr_mounts == (uint64_t)-1) {
ksft_print_msg("listmount: %s\n", strerror(errno));
return NSID_ERROR;
}
if (nr_mounts != child_nr_mounts) {
ksft_print_msg("listmount results is %zi != %zi\n", nr_mounts,
child_nr_mounts);
return NSID_FAIL;
}
/* Validate that all of our entries match our mnt_ns_id. */
for (int i = 0; i < nr_mounts; i++) {
struct statmount sm;
ret = statmount(list[i], mnt_ns_id, STATMOUNT_MNT_NS_ID, &sm,
sizeof(sm), 0);
if (ret < 0) {
ksft_print_msg("statmount mnt ns id: %s\n", strerror(errno));
return NSID_ERROR;
}
if (sm.mask != STATMOUNT_MNT_NS_ID) {
ksft_print_msg("statmount mnt ns id unavailable\n");
return NSID_SKIP;
}
if (sm.mnt_ns_id != mnt_ns_id) {
ksft_print_msg("listmount gave us the wrong ns id: 0x%llx != 0x%llx\n",
(unsigned long long)sm.mnt_ns_id,
(unsigned long long)mnt_ns_id);
return NSID_FAIL;
}
}
return NSID_PASS;
}
static void test_listmount_ns(void)
{
uint64_t nr_mounts;
char pval;
int child_ready_pipe[2];
int parent_ready_pipe[2];
pid_t pid;
int ret, child_ret;
if (pipe(child_ready_pipe) < 0)
ksft_exit_fail_msg("failed to create the child pipe: %s\n",
strerror(errno));
if (pipe(parent_ready_pipe) < 0)
ksft_exit_fail_msg("failed to create the parent pipe: %s\n",
strerror(errno));
pid = fork();
if (pid < 0)
ksft_exit_fail_msg("failed to fork: %s\n", strerror(errno));
if (pid == 0) {
char cval;
uint64_t list[256];
close(child_ready_pipe[0]);
close(parent_ready_pipe[1]);
ret = setup_namespace();
if (ret != NSID_PASS)
exit(ret);
nr_mounts = listmount(LSMT_ROOT, 0, 0, list, 256, 0);
if (nr_mounts == (uint64_t)-1) {
ksft_print_msg("listmount: %s\n", strerror(errno));
exit(NSID_FAIL);
}
/*
* Tell our parent how many mounts we have, and then wait for it
* to tell us we're done.
*/
write(child_ready_pipe[1], &nr_mounts, sizeof(nr_mounts));
read(parent_ready_pipe[0], &cval, sizeof(cval));
exit(NSID_PASS);
}
close(child_ready_pipe[1]);
close(parent_ready_pipe[0]);
/* Wait until the child has created everything. */
if (read(child_ready_pipe[0], &nr_mounts, sizeof(nr_mounts)) !=
sizeof(nr_mounts))
ret = NSID_ERROR;
ret = validate_external_listmount(pid, nr_mounts);
if (write(parent_ready_pipe[1], &pval, sizeof(pval)) != sizeof(pval))
ret = NSID_ERROR;
child_ret = wait_for_pid(pid);
if (child_ret != NSID_PASS)
ret = child_ret;
handle_result(ret, "test listmount ns id");
}
int main(void)
{
int ret;
ksft_print_header();
ret = statmount(0, 0, 0, NULL, 0, 0);
assert(ret == -1);
if (errno == ENOSYS)
ksft_exit_skip("statmount() syscall not supported\n");
ksft_set_plan(2);
test_statmount_mnt_ns_id();
test_listmount_ns();
if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
ksft_exit_fail();
else
ksft_exit_pass();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment