| 1 | |
| 2 | #define _GNU_SOURCE
|
| 3 |
|
| 4 | #include <errno.h>
|
| 5 | #include <fcntl.h>
|
| 6 | #include <limits.h>
|
| 7 | #include <sched.h>
|
| 8 | #include <stdio.h>
|
| 9 | #include <unistd.h>
|
| 10 | #include <sys/fsuid.h>
|
| 11 | #include <sys/stat.h>
|
| 12 | #include <sys/syscall.h>
|
| 13 |
|
| 14 | #include <linux/mount.h>
|
| 15 | #include <linux/types.h>
|
| 16 |
|
| 17 | #include "kselftest_harness.h"
|
| 18 | #include "wrappers.h"
|
| 19 | #include "utils.h"
|
| 20 |
|
| 21 | |
| 22 | * The test mount maps caller-visible ids [0, MAP_RANGE) onto the on-disk range
|
| 23 | * [MAP_HOST, MAP_HOST + MAP_RANGE). An id outside [0, MAP_RANGE) therefore has
|
| 24 | * no mapping in the mount and is not representable in the filesystem.
|
| 25 |
|
| 26 | #define MAP_HOST 10000
|
| 27 | #define MAP_RANGE 10000
|
| 28 | #define UNMAPPED 50000
|
| 29 |
|
| 30 | #ifndef MOUNT_ATTR_IDMAP
|
| 31 | #define MOUNT_ATTR_IDMAP 0x00100000
|
| 32 | #endif
|
| 33 |
|
| 34 | #ifndef __NR_mount_setattr
|
| 35 | #define __NR_mount_setattr 442
|
| 36 | #endif
|
| 37 |
|
| 38 | static inline int sys_mount_setattr(int dfd, const char *path,
|
| 39 | unsigned int flags,
|
| 40 | struct mount_attr *attr, size_t size)
|
| 41 | {
|
| 42 | return syscall(__NR_mount_setattr, dfd, path, flags, attr, size);
|
| 43 | }
|
| 44 |
|
| 45 | |
| 46 | * Clone @path into a detached mount idmapped so that caller-visible ids
|
| 47 | * [0, MAP_RANGE) map onto the on-disk ids [MAP_HOST, MAP_HOST + MAP_RANGE).
|
| 48 | * Returns the mount fd, or -1 if idmapped mounts are not available.
|
| 49 |
|
| 50 | static int idmapped_clone(const char *path)
|
| 51 | {
|
| 52 | struct mount_attr attr = {
|
| 53 | .attr_set = MOUNT_ATTR_IDMAP,
|
| 54 | };
|
| 55 | int fd_tree, userns_fd, ret;
|
| 56 |
|
| 57 | fd_tree = sys_open_tree(AT_FDCWD, path,
|
| 58 | OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
|
| 59 | if (fd_tree < 0)
|
| 60 | return -1;
|
| 61 |
|
| 62 | userns_fd = get_userns_fd(MAP_HOST, 0, MAP_RANGE);
|
| 63 | if (userns_fd < 0) {
|
| 64 | close(fd_tree);
|
| 65 | return -1;
|
| 66 | }
|
| 67 |
|
| 68 | attr.userns_fd = userns_fd;
|
| 69 | ret = sys_mount_setattr(fd_tree, "", AT_EMPTY_PATH, &attr, sizeof(attr));
|
| 70 | close(userns_fd);
|
| 71 | if (ret) {
|
| 72 | close(fd_tree);
|
| 73 | return -1;
|
| 74 | }
|
| 75 |
|
| 76 | return fd_tree;
|
| 77 | }
|
| 78 |
|
| 79 | FIXTURE(idmapped_tmpfile) {
|
| 80 | char dir[64];
|
| 81 | };
|
| 82 |
|
| 83 | FIXTURE_SETUP(idmapped_tmpfile)
|
| 84 | {
|
| 85 |
|
| 86 | ASSERT_EQ(unshare(CLONE_NEWNS), 0);
|
| 87 | ASSERT_EQ(sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL), 0);
|
| 88 | ASSERT_EQ(sys_mount("tmpfs", "/tmp", "tmpfs", 0, NULL), 0);
|
| 89 |
|
| 90 | snprintf(self->dir, sizeof(self->dir), "/tmp/d");
|
| 91 | ASSERT_EQ(mkdir(self->dir, 0777), 0);
|
| 92 |
|
| 93 | ASSERT_EQ(chmod(self->dir, 0777), 0);
|
| 94 | |
| 95 | * The directory itself must be inside the range the mount maps.
|
| 96 | * Left owned by 0 it has no mapping in the mount, and
|
| 97 | * inode_permission() then fails MAY_WRITE via HAS_UNMAPPED_ID()
|
| 98 | * before any of the tmpfile logic is reached, regardless of mode.
|
| 99 |
|
| 100 | ASSERT_EQ(chown(self->dir, MAP_HOST, MAP_HOST), 0);
|
| 101 | }
|
| 102 |
|
| 103 | FIXTURE_TEARDOWN(idmapped_tmpfile)
|
| 104 | {
|
| 105 | }
|
| 106 |
|
| 107 | |
| 108 | * A caller whose fsuid/fsgid have no mapping in the idmapped mount must not be
|
| 109 | * able to create an O_TMPFILE. Without the check in vfs_tmpfile() the inode
|
| 110 | * would be created owned by (uid_t)-1 and could then be linked into the
|
| 111 | * namespace.
|
| 112 |
|
| 113 | TEST_F(idmapped_tmpfile, unmapped_caller_is_refused)
|
| 114 | {
|
| 115 | int mfd, fd;
|
| 116 |
|
| 117 | mfd = idmapped_clone(self->dir);
|
| 118 | if (mfd < 0)
|
| 119 | SKIP(return, "idmapped mounts not supported");
|
| 120 |
|
| 121 |
|
| 122 | setfsgid(UNMAPPED);
|
| 123 | setfsuid(UNMAPPED);
|
| 124 | ASSERT_EQ(setfsuid(-1), UNMAPPED);
|
| 125 |
|
| 126 | fd = openat(mfd, ".", O_TMPFILE | O_WRONLY, 0644);
|
| 127 | ASSERT_LT(fd, 0);
|
| 128 | EXPECT_EQ(errno, EOVERFLOW);
|
| 129 | if (fd >= 0)
|
| 130 | close(fd);
|
| 131 |
|
| 132 | EXPECT_EQ(close(mfd), 0);
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | * A mapped caller can create an O_TMPFILE and link it into the namespace; the
|
| 137 | * ownership round-trips through the mount idmap. This is what makes refusing
|
| 138 | * the unmapped case above necessary in the first place.
|
| 139 |
|
| 140 | TEST_F(idmapped_tmpfile, mapped_caller_creates_and_links)
|
| 141 | {
|
| 142 | char path[PATH_MAX];
|
| 143 | struct stat st;
|
| 144 | int mfd, fd;
|
| 145 |
|
| 146 | mfd = idmapped_clone(self->dir);
|
| 147 | if (mfd < 0)
|
| 148 | SKIP(return, "idmapped mounts not supported");
|
| 149 |
|
| 150 |
|
| 151 | fd = openat(mfd, ".", O_TMPFILE | O_RDWR, 0600);
|
| 152 | ASSERT_GE(fd, 0);
|
| 153 |
|
| 154 | ASSERT_EQ(fstat(fd, &st), 0);
|
| 155 | EXPECT_EQ(st.st_uid, 0);
|
| 156 | EXPECT_EQ(st.st_gid, 0);
|
| 157 |
|
| 158 |
|
| 159 | ASSERT_EQ(linkat(fd, "", mfd, "linked", AT_EMPTY_PATH), 0);
|
| 160 | EXPECT_EQ(close(fd), 0);
|
| 161 |
|
| 162 | ASSERT_EQ(fstatat(mfd, "linked", &st, 0), 0);
|
| 163 | EXPECT_EQ(st.st_uid, 0);
|
| 164 | EXPECT_EQ(st.st_gid, 0);
|
| 165 |
|
| 166 |
|
| 167 | snprintf(path, sizeof(path), "%s/linked", self->dir);
|
| 168 | ASSERT_EQ(stat(path, &st), 0);
|
| 169 | EXPECT_EQ(st.st_uid, MAP_HOST);
|
| 170 | EXPECT_EQ(st.st_gid, MAP_HOST);
|
| 171 |
|
| 172 | EXPECT_EQ(close(mfd), 0);
|
| 173 | }
|
| 174 |
|
| 175 | TEST_HARNESS_MAIN
|