1e1199815SMickaël Salaün // SPDX-License-Identifier: GPL-2.0 2e1199815SMickaël Salaün /* 3e1199815SMickaël Salaün * Landlock tests - Filesystem 4e1199815SMickaël Salaün * 5e1199815SMickaël Salaün * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net> 6e1199815SMickaël Salaün * Copyright © 2020 ANSSI 7e1199815SMickaël Salaün * Copyright © 2020-2021 Microsoft Corporation 8e1199815SMickaël Salaün */ 9e1199815SMickaël Salaün 10e1199815SMickaël Salaün #define _GNU_SOURCE 11e1199815SMickaël Salaün #include <fcntl.h> 12e1199815SMickaël Salaün #include <linux/landlock.h> 13e1199815SMickaël Salaün #include <sched.h> 14e1199815SMickaël Salaün #include <string.h> 15e1199815SMickaël Salaün #include <sys/capability.h> 16e1199815SMickaël Salaün #include <sys/mount.h> 17e1199815SMickaël Salaün #include <sys/prctl.h> 18e1199815SMickaël Salaün #include <sys/sendfile.h> 19e1199815SMickaël Salaün #include <sys/stat.h> 20e1199815SMickaël Salaün #include <sys/sysmacros.h> 21e1199815SMickaël Salaün #include <unistd.h> 22e1199815SMickaël Salaün 23e1199815SMickaël Salaün #include "common.h" 24e1199815SMickaël Salaün 2587129ef1SMickaël Salaün #ifndef renameat2 2687129ef1SMickaël Salaün int renameat2(int olddirfd, const char *oldpath, int newdirfd, 2787129ef1SMickaël Salaün const char *newpath, unsigned int flags) 2887129ef1SMickaël Salaün { 2987129ef1SMickaël Salaün return syscall(__NR_renameat2, olddirfd, oldpath, newdirfd, newpath, 3087129ef1SMickaël Salaün flags); 3187129ef1SMickaël Salaün } 3287129ef1SMickaël Salaün #endif 3387129ef1SMickaël Salaün 3487129ef1SMickaël Salaün #ifndef RENAME_EXCHANGE 3587129ef1SMickaël Salaün #define RENAME_EXCHANGE (1 << 1) 3687129ef1SMickaël Salaün #endif 3787129ef1SMickaël Salaün 38e1199815SMickaël Salaün #define TMP_DIR "tmp" 39e1199815SMickaël Salaün #define BINARY_PATH "./true" 40e1199815SMickaël Salaün 41e1199815SMickaël Salaün /* Paths (sibling number and depth) */ 42e1199815SMickaël Salaün static const char dir_s1d1[] = TMP_DIR "/s1d1"; 43e1199815SMickaël Salaün static const char file1_s1d1[] = TMP_DIR "/s1d1/f1"; 44e1199815SMickaël Salaün static const char file2_s1d1[] = TMP_DIR "/s1d1/f2"; 45e1199815SMickaël Salaün static const char dir_s1d2[] = TMP_DIR "/s1d1/s1d2"; 46e1199815SMickaël Salaün static const char file1_s1d2[] = TMP_DIR "/s1d1/s1d2/f1"; 47e1199815SMickaël Salaün static const char file2_s1d2[] = TMP_DIR "/s1d1/s1d2/f2"; 48e1199815SMickaël Salaün static const char dir_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3"; 49e1199815SMickaël Salaün static const char file1_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f1"; 50e1199815SMickaël Salaün static const char file2_s1d3[] = TMP_DIR "/s1d1/s1d2/s1d3/f2"; 51e1199815SMickaël Salaün 52e1199815SMickaël Salaün static const char dir_s2d1[] = TMP_DIR "/s2d1"; 53e1199815SMickaël Salaün static const char file1_s2d1[] = TMP_DIR "/s2d1/f1"; 54e1199815SMickaël Salaün static const char dir_s2d2[] = TMP_DIR "/s2d1/s2d2"; 55e1199815SMickaël Salaün static const char file1_s2d2[] = TMP_DIR "/s2d1/s2d2/f1"; 56e1199815SMickaël Salaün static const char dir_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3"; 57e1199815SMickaël Salaün static const char file1_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f1"; 58e1199815SMickaël Salaün static const char file2_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f2"; 59e1199815SMickaël Salaün 60e1199815SMickaël Salaün static const char dir_s3d1[] = TMP_DIR "/s3d1"; 61e1199815SMickaël Salaün /* dir_s3d2 is a mount point. */ 62e1199815SMickaël Salaün static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2"; 63e1199815SMickaël Salaün static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3"; 64e1199815SMickaël Salaün 65e1199815SMickaël Salaün /* 66e1199815SMickaël Salaün * layout1 hierarchy: 67e1199815SMickaël Salaün * 68e1199815SMickaël Salaün * tmp 69e1199815SMickaël Salaün * ├── s1d1 70e1199815SMickaël Salaün * │ ├── f1 71e1199815SMickaël Salaün * │ ├── f2 72e1199815SMickaël Salaün * │ └── s1d2 73e1199815SMickaël Salaün * │ ├── f1 74e1199815SMickaël Salaün * │ ├── f2 75e1199815SMickaël Salaün * │ └── s1d3 76e1199815SMickaël Salaün * │ ├── f1 77e1199815SMickaël Salaün * │ └── f2 78e1199815SMickaël Salaün * ├── s2d1 79e1199815SMickaël Salaün * │ ├── f1 80e1199815SMickaël Salaün * │ └── s2d2 81e1199815SMickaël Salaün * │ ├── f1 82e1199815SMickaël Salaün * │ └── s2d3 83e1199815SMickaël Salaün * │ ├── f1 84e1199815SMickaël Salaün * │ └── f2 85e1199815SMickaël Salaün * └── s3d1 86e1199815SMickaël Salaün * └── s3d2 87e1199815SMickaël Salaün * └── s3d3 88e1199815SMickaël Salaün */ 89e1199815SMickaël Salaün 90e1199815SMickaël Salaün static void mkdir_parents(struct __test_metadata *const _metadata, 91e1199815SMickaël Salaün const char *const path) 92e1199815SMickaël Salaün { 93e1199815SMickaël Salaün char *walker; 94e1199815SMickaël Salaün const char *parent; 95e1199815SMickaël Salaün int i, err; 96e1199815SMickaël Salaün 97e1199815SMickaël Salaün ASSERT_NE(path[0], '\0'); 98e1199815SMickaël Salaün walker = strdup(path); 99e1199815SMickaël Salaün ASSERT_NE(NULL, walker); 100e1199815SMickaël Salaün parent = walker; 101e1199815SMickaël Salaün for (i = 1; walker[i]; i++) { 102e1199815SMickaël Salaün if (walker[i] != '/') 103e1199815SMickaël Salaün continue; 104e1199815SMickaël Salaün walker[i] = '\0'; 105e1199815SMickaël Salaün err = mkdir(parent, 0700); 106371183faSMickaël Salaün ASSERT_FALSE(err && errno != EEXIST) 107371183faSMickaël Salaün { 108371183faSMickaël Salaün TH_LOG("Failed to create directory \"%s\": %s", parent, 109371183faSMickaël Salaün strerror(errno)); 110e1199815SMickaël Salaün } 111e1199815SMickaël Salaün walker[i] = '/'; 112e1199815SMickaël Salaün } 113e1199815SMickaël Salaün free(walker); 114e1199815SMickaël Salaün } 115e1199815SMickaël Salaün 116e1199815SMickaël Salaün static void create_directory(struct __test_metadata *const _metadata, 117e1199815SMickaël Salaün const char *const path) 118e1199815SMickaël Salaün { 119e1199815SMickaël Salaün mkdir_parents(_metadata, path); 120371183faSMickaël Salaün ASSERT_EQ(0, mkdir(path, 0700)) 121371183faSMickaël Salaün { 122e1199815SMickaël Salaün TH_LOG("Failed to create directory \"%s\": %s", path, 123e1199815SMickaël Salaün strerror(errno)); 124e1199815SMickaël Salaün } 125e1199815SMickaël Salaün } 126e1199815SMickaël Salaün 127e1199815SMickaël Salaün static void create_file(struct __test_metadata *const _metadata, 128e1199815SMickaël Salaün const char *const path) 129e1199815SMickaël Salaün { 130e1199815SMickaël Salaün mkdir_parents(_metadata, path); 131371183faSMickaël Salaün ASSERT_EQ(0, mknod(path, S_IFREG | 0700, 0)) 132371183faSMickaël Salaün { 133e1199815SMickaël Salaün TH_LOG("Failed to create file \"%s\": %s", path, 134e1199815SMickaël Salaün strerror(errno)); 135e1199815SMickaël Salaün } 136e1199815SMickaël Salaün } 137e1199815SMickaël Salaün 138e1199815SMickaël Salaün static int remove_path(const char *const path) 139e1199815SMickaël Salaün { 140e1199815SMickaël Salaün char *walker; 141e1199815SMickaël Salaün int i, ret, err = 0; 142e1199815SMickaël Salaün 143e1199815SMickaël Salaün walker = strdup(path); 144e1199815SMickaël Salaün if (!walker) { 145e1199815SMickaël Salaün err = ENOMEM; 146e1199815SMickaël Salaün goto out; 147e1199815SMickaël Salaün } 148e1199815SMickaël Salaün if (unlink(path) && rmdir(path)) { 149e1199815SMickaël Salaün if (errno != ENOENT) 150e1199815SMickaël Salaün err = errno; 151e1199815SMickaël Salaün goto out; 152e1199815SMickaël Salaün } 153e1199815SMickaël Salaün for (i = strlen(walker); i > 0; i--) { 154e1199815SMickaël Salaün if (walker[i] != '/') 155e1199815SMickaël Salaün continue; 156e1199815SMickaël Salaün walker[i] = '\0'; 157e1199815SMickaël Salaün ret = rmdir(walker); 158e1199815SMickaël Salaün if (ret) { 159e1199815SMickaël Salaün if (errno != ENOTEMPTY && errno != EBUSY) 160e1199815SMickaël Salaün err = errno; 161e1199815SMickaël Salaün goto out; 162e1199815SMickaël Salaün } 163e1199815SMickaël Salaün if (strcmp(walker, TMP_DIR) == 0) 164e1199815SMickaël Salaün goto out; 165e1199815SMickaël Salaün } 166e1199815SMickaël Salaün 167e1199815SMickaël Salaün out: 168e1199815SMickaël Salaün free(walker); 169e1199815SMickaël Salaün return err; 170e1199815SMickaël Salaün } 171e1199815SMickaël Salaün 172e1199815SMickaël Salaün static void prepare_layout(struct __test_metadata *const _metadata) 173e1199815SMickaël Salaün { 174e1199815SMickaël Salaün disable_caps(_metadata); 175e1199815SMickaël Salaün umask(0077); 176e1199815SMickaël Salaün create_directory(_metadata, TMP_DIR); 177e1199815SMickaël Salaün 178e1199815SMickaël Salaün /* 179e1199815SMickaël Salaün * Do not pollute the rest of the system: creates a private mount point 180e1199815SMickaël Salaün * for tests relying on pivot_root(2) and move_mount(2). 181e1199815SMickaël Salaün */ 182e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 183e1199815SMickaël Salaün ASSERT_EQ(0, unshare(CLONE_NEWNS)); 184e1199815SMickaël Salaün ASSERT_EQ(0, mount("tmp", TMP_DIR, "tmpfs", 0, "size=4m,mode=700")); 185e1199815SMickaël Salaün ASSERT_EQ(0, mount(NULL, TMP_DIR, NULL, MS_PRIVATE | MS_REC, NULL)); 186e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 187e1199815SMickaël Salaün } 188e1199815SMickaël Salaün 189e1199815SMickaël Salaün static void cleanup_layout(struct __test_metadata *const _metadata) 190e1199815SMickaël Salaün { 191e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 192e1199815SMickaël Salaün EXPECT_EQ(0, umount(TMP_DIR)); 193e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 194e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(TMP_DIR)); 195e1199815SMickaël Salaün } 196e1199815SMickaël Salaün 197e1199815SMickaël Salaün static void create_layout1(struct __test_metadata *const _metadata) 198e1199815SMickaël Salaün { 199e1199815SMickaël Salaün create_file(_metadata, file1_s1d1); 200e1199815SMickaël Salaün create_file(_metadata, file1_s1d2); 201e1199815SMickaël Salaün create_file(_metadata, file1_s1d3); 202e1199815SMickaël Salaün create_file(_metadata, file2_s1d1); 203e1199815SMickaël Salaün create_file(_metadata, file2_s1d2); 204e1199815SMickaël Salaün create_file(_metadata, file2_s1d3); 205e1199815SMickaël Salaün 206e1199815SMickaël Salaün create_file(_metadata, file1_s2d1); 207e1199815SMickaël Salaün create_file(_metadata, file1_s2d2); 208e1199815SMickaël Salaün create_file(_metadata, file1_s2d3); 209e1199815SMickaël Salaün create_file(_metadata, file2_s2d3); 210e1199815SMickaël Salaün 211e1199815SMickaël Salaün create_directory(_metadata, dir_s3d2); 212e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 213e1199815SMickaël Salaün ASSERT_EQ(0, mount("tmp", dir_s3d2, "tmpfs", 0, "size=4m,mode=700")); 214e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 215e1199815SMickaël Salaün 216e1199815SMickaël Salaün ASSERT_EQ(0, mkdir(dir_s3d3, 0700)); 217e1199815SMickaël Salaün } 218e1199815SMickaël Salaün 219e1199815SMickaël Salaün static void remove_layout1(struct __test_metadata *const _metadata) 220e1199815SMickaël Salaün { 221e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file2_s1d3)); 222e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file2_s1d2)); 223e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file2_s1d1)); 224e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file1_s1d3)); 225e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file1_s1d2)); 226e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file1_s1d1)); 227e1199815SMickaël Salaün 228e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file2_s2d3)); 229e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file1_s2d3)); 230e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file1_s2d2)); 231e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(file1_s2d1)); 232e1199815SMickaël Salaün 233e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(dir_s3d3)); 234e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 235e1199815SMickaël Salaün umount(dir_s3d2); 236e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 237e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(dir_s3d2)); 238e1199815SMickaël Salaün } 239e1199815SMickaël Salaün 2404598d9abSMickaël Salaün /* clang-format off */ 2414598d9abSMickaël Salaün FIXTURE(layout1) {}; 2424598d9abSMickaël Salaün /* clang-format on */ 243e1199815SMickaël Salaün 244e1199815SMickaël Salaün FIXTURE_SETUP(layout1) 245e1199815SMickaël Salaün { 246e1199815SMickaël Salaün prepare_layout(_metadata); 247e1199815SMickaël Salaün 248e1199815SMickaël Salaün create_layout1(_metadata); 249e1199815SMickaël Salaün } 250e1199815SMickaël Salaün 251e1199815SMickaël Salaün FIXTURE_TEARDOWN(layout1) 252e1199815SMickaël Salaün { 253e1199815SMickaël Salaün remove_layout1(_metadata); 254e1199815SMickaël Salaün 255e1199815SMickaël Salaün cleanup_layout(_metadata); 256e1199815SMickaël Salaün } 257e1199815SMickaël Salaün 258e1199815SMickaël Salaün /* 259e1199815SMickaël Salaün * This helper enables to use the ASSERT_* macros and print the line number 260e1199815SMickaël Salaün * pointing to the test caller. 261e1199815SMickaël Salaün */ 262371183faSMickaël Salaün static int test_open_rel(const int dirfd, const char *const path, 263371183faSMickaël Salaün const int flags) 264e1199815SMickaël Salaün { 265e1199815SMickaël Salaün int fd; 266e1199815SMickaël Salaün 267e1199815SMickaël Salaün /* Works with file and directories. */ 268e1199815SMickaël Salaün fd = openat(dirfd, path, flags | O_CLOEXEC); 269e1199815SMickaël Salaün if (fd < 0) 270e1199815SMickaël Salaün return errno; 271e1199815SMickaël Salaün /* 272e1199815SMickaël Salaün * Mixing error codes from close(2) and open(2) should not lead to any 273e1199815SMickaël Salaün * (access type) confusion for this test. 274e1199815SMickaël Salaün */ 275e1199815SMickaël Salaün if (close(fd) != 0) 276e1199815SMickaël Salaün return errno; 277e1199815SMickaël Salaün return 0; 278e1199815SMickaël Salaün } 279e1199815SMickaël Salaün 280e1199815SMickaël Salaün static int test_open(const char *const path, const int flags) 281e1199815SMickaël Salaün { 282e1199815SMickaël Salaün return test_open_rel(AT_FDCWD, path, flags); 283e1199815SMickaël Salaün } 284e1199815SMickaël Salaün 285e1199815SMickaël Salaün TEST_F_FORK(layout1, no_restriction) 286e1199815SMickaël Salaün { 287e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 288e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 289e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file2_s1d1, O_RDONLY)); 290e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 291e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 292e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file2_s1d2, O_RDONLY)); 293e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 294e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 295e1199815SMickaël Salaün 296e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY)); 297e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY)); 298e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY)); 299e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 300e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d3, O_RDONLY)); 301e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d3, O_RDONLY)); 302e1199815SMickaël Salaün 303e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY)); 304e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 305e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY)); 306e1199815SMickaël Salaün } 307e1199815SMickaël Salaün 308e1199815SMickaël Salaün TEST_F_FORK(layout1, inval) 309e1199815SMickaël Salaün { 310e1199815SMickaël Salaün struct landlock_path_beneath_attr path_beneath = { 311e1199815SMickaël Salaün .allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | 312e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 313e1199815SMickaël Salaün .parent_fd = -1, 314e1199815SMickaël Salaün }; 315e1199815SMickaël Salaün struct landlock_ruleset_attr ruleset_attr = { 316e1199815SMickaël Salaün .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE | 317e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 318e1199815SMickaël Salaün }; 319e1199815SMickaël Salaün int ruleset_fd; 320e1199815SMickaël Salaün 321371183faSMickaël Salaün path_beneath.parent_fd = 322371183faSMickaël Salaün open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 323e1199815SMickaël Salaün ASSERT_LE(0, path_beneath.parent_fd); 324e1199815SMickaël Salaün 325e1199815SMickaël Salaün ruleset_fd = open(dir_s1d1, O_PATH | O_DIRECTORY | O_CLOEXEC); 326e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 327e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 328e1199815SMickaël Salaün &path_beneath, 0)); 329e1199815SMickaël Salaün /* Returns EBADF because ruleset_fd is not a landlock-ruleset FD. */ 330e1199815SMickaël Salaün ASSERT_EQ(EBADF, errno); 331e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 332e1199815SMickaël Salaün 333e1199815SMickaël Salaün ruleset_fd = open(dir_s1d1, O_DIRECTORY | O_CLOEXEC); 334e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 335e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 336e1199815SMickaël Salaün &path_beneath, 0)); 337e1199815SMickaël Salaün /* Returns EBADFD because ruleset_fd is not a valid ruleset. */ 338e1199815SMickaël Salaün ASSERT_EQ(EBADFD, errno); 339e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 340e1199815SMickaël Salaün 341e1199815SMickaël Salaün /* Gets a real ruleset. */ 342371183faSMickaël Salaün ruleset_fd = 343371183faSMickaël Salaün landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 344e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 345e1199815SMickaël Salaün ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 346e1199815SMickaël Salaün &path_beneath, 0)); 347e1199815SMickaël Salaün ASSERT_EQ(0, close(path_beneath.parent_fd)); 348e1199815SMickaël Salaün 349e1199815SMickaël Salaün /* Tests without O_PATH. */ 350e1199815SMickaël Salaün path_beneath.parent_fd = open(dir_s1d2, O_DIRECTORY | O_CLOEXEC); 351e1199815SMickaël Salaün ASSERT_LE(0, path_beneath.parent_fd); 352e1199815SMickaël Salaün ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 353e1199815SMickaël Salaün &path_beneath, 0)); 354e1199815SMickaël Salaün ASSERT_EQ(0, close(path_beneath.parent_fd)); 355e1199815SMickaël Salaün 356e1199815SMickaël Salaün /* Tests with a ruleset FD. */ 357e1199815SMickaël Salaün path_beneath.parent_fd = ruleset_fd; 358e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 359e1199815SMickaël Salaün &path_beneath, 0)); 360e1199815SMickaël Salaün ASSERT_EQ(EBADFD, errno); 361e1199815SMickaël Salaün 362e1199815SMickaël Salaün /* Checks unhandled allowed_access. */ 363371183faSMickaël Salaün path_beneath.parent_fd = 364371183faSMickaël Salaün open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 365e1199815SMickaël Salaün ASSERT_LE(0, path_beneath.parent_fd); 366e1199815SMickaël Salaün 367e1199815SMickaël Salaün /* Test with legitimate values. */ 368e1199815SMickaël Salaün path_beneath.allowed_access |= LANDLOCK_ACCESS_FS_EXECUTE; 369e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 370e1199815SMickaël Salaün &path_beneath, 0)); 371e1199815SMickaël Salaün ASSERT_EQ(EINVAL, errno); 372e1199815SMickaël Salaün path_beneath.allowed_access &= ~LANDLOCK_ACCESS_FS_EXECUTE; 373e1199815SMickaël Salaün 374e1199815SMickaël Salaün /* Test with unknown (64-bits) value. */ 375e1199815SMickaël Salaün path_beneath.allowed_access |= (1ULL << 60); 376e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 377e1199815SMickaël Salaün &path_beneath, 0)); 378e1199815SMickaël Salaün ASSERT_EQ(EINVAL, errno); 379e1199815SMickaël Salaün path_beneath.allowed_access &= ~(1ULL << 60); 380e1199815SMickaël Salaün 381e1199815SMickaël Salaün /* Test with no access. */ 382e1199815SMickaël Salaün path_beneath.allowed_access = 0; 383e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 384e1199815SMickaël Salaün &path_beneath, 0)); 385e1199815SMickaël Salaün ASSERT_EQ(ENOMSG, errno); 386e1199815SMickaël Salaün path_beneath.allowed_access &= ~(1ULL << 60); 387e1199815SMickaël Salaün 388e1199815SMickaël Salaün ASSERT_EQ(0, close(path_beneath.parent_fd)); 389e1199815SMickaël Salaün 390e1199815SMickaël Salaün /* Enforces the ruleset. */ 391e1199815SMickaël Salaün ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); 392e1199815SMickaël Salaün ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)); 393e1199815SMickaël Salaün 394e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 395e1199815SMickaël Salaün } 396e1199815SMickaël Salaün 3974598d9abSMickaël Salaün /* clang-format off */ 3984598d9abSMickaël Salaün 399e1199815SMickaël Salaün #define ACCESS_FILE ( \ 400e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_EXECUTE | \ 401e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE | \ 402e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE) 403e1199815SMickaël Salaün 404e1199815SMickaël Salaün #define ACCESS_LAST LANDLOCK_ACCESS_FS_MAKE_SYM 405e1199815SMickaël Salaün 406e1199815SMickaël Salaün #define ACCESS_ALL ( \ 407e1199815SMickaël Salaün ACCESS_FILE | \ 408e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_READ_DIR | \ 409e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_REMOVE_DIR | \ 410e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_REMOVE_FILE | \ 411e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_MAKE_CHAR | \ 412e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_MAKE_DIR | \ 413e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_MAKE_REG | \ 414e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_MAKE_SOCK | \ 415e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_MAKE_FIFO | \ 416e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_MAKE_BLOCK | \ 417e1199815SMickaël Salaün ACCESS_LAST) 418e1199815SMickaël Salaün 4194598d9abSMickaël Salaün /* clang-format on */ 4204598d9abSMickaël Salaün 421*d18955d0SMickaël Salaün TEST_F_FORK(layout1, file_and_dir_access_rights) 422e1199815SMickaël Salaün { 423e1199815SMickaël Salaün __u64 access; 424e1199815SMickaël Salaün int err; 425*d18955d0SMickaël Salaün struct landlock_path_beneath_attr path_beneath_file = {}, 426*d18955d0SMickaël Salaün path_beneath_dir = {}; 427e1199815SMickaël Salaün struct landlock_ruleset_attr ruleset_attr = { 428e1199815SMickaël Salaün .handled_access_fs = ACCESS_ALL, 429e1199815SMickaël Salaün }; 430371183faSMickaël Salaün const int ruleset_fd = 431371183faSMickaël Salaün landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 432e1199815SMickaël Salaün 433e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 434e1199815SMickaël Salaün 435e1199815SMickaël Salaün /* Tests access rights for files. */ 436*d18955d0SMickaël Salaün path_beneath_file.parent_fd = open(file1_s1d2, O_PATH | O_CLOEXEC); 437*d18955d0SMickaël Salaün ASSERT_LE(0, path_beneath_file.parent_fd); 438*d18955d0SMickaël Salaün 439*d18955d0SMickaël Salaün /* Tests access rights for directories. */ 440*d18955d0SMickaël Salaün path_beneath_dir.parent_fd = 441*d18955d0SMickaël Salaün open(dir_s1d2, O_PATH | O_DIRECTORY | O_CLOEXEC); 442*d18955d0SMickaël Salaün ASSERT_LE(0, path_beneath_dir.parent_fd); 443*d18955d0SMickaël Salaün 444e1199815SMickaël Salaün for (access = 1; access <= ACCESS_LAST; access <<= 1) { 445*d18955d0SMickaël Salaün path_beneath_dir.allowed_access = access; 446*d18955d0SMickaël Salaün ASSERT_EQ(0, landlock_add_rule(ruleset_fd, 447*d18955d0SMickaël Salaün LANDLOCK_RULE_PATH_BENEATH, 448*d18955d0SMickaël Salaün &path_beneath_dir, 0)); 449*d18955d0SMickaël Salaün 450*d18955d0SMickaël Salaün path_beneath_file.allowed_access = access; 451e1199815SMickaël Salaün err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 452*d18955d0SMickaël Salaün &path_beneath_file, 0); 453*d18955d0SMickaël Salaün if (access & ACCESS_FILE) { 454e1199815SMickaël Salaün ASSERT_EQ(0, err); 455e1199815SMickaël Salaün } else { 456e1199815SMickaël Salaün ASSERT_EQ(-1, err); 457e1199815SMickaël Salaün ASSERT_EQ(EINVAL, errno); 458e1199815SMickaël Salaün } 459e1199815SMickaël Salaün } 460*d18955d0SMickaël Salaün ASSERT_EQ(0, close(path_beneath_file.parent_fd)); 461*d18955d0SMickaël Salaün ASSERT_EQ(0, close(path_beneath_dir.parent_fd)); 462*d18955d0SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 463e1199815SMickaël Salaün } 464e1199815SMickaël Salaün 465c56b3bf5SMickaël Salaün TEST_F_FORK(layout1, unknown_access_rights) 466c56b3bf5SMickaël Salaün { 467c56b3bf5SMickaël Salaün __u64 access_mask; 468c56b3bf5SMickaël Salaün 469c56b3bf5SMickaël Salaün for (access_mask = 1ULL << 63; access_mask != ACCESS_LAST; 470c56b3bf5SMickaël Salaün access_mask >>= 1) { 471c56b3bf5SMickaël Salaün struct landlock_ruleset_attr ruleset_attr = { 472c56b3bf5SMickaël Salaün .handled_access_fs = access_mask, 473c56b3bf5SMickaël Salaün }; 474c56b3bf5SMickaël Salaün 475c56b3bf5SMickaël Salaün ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 476c56b3bf5SMickaël Salaün sizeof(ruleset_attr), 0)); 477c56b3bf5SMickaël Salaün ASSERT_EQ(EINVAL, errno); 478c56b3bf5SMickaël Salaün } 479c56b3bf5SMickaël Salaün } 480c56b3bf5SMickaël Salaün 481e1199815SMickaël Salaün static void add_path_beneath(struct __test_metadata *const _metadata, 482e1199815SMickaël Salaün const int ruleset_fd, const __u64 allowed_access, 483e1199815SMickaël Salaün const char *const path) 484e1199815SMickaël Salaün { 485e1199815SMickaël Salaün struct landlock_path_beneath_attr path_beneath = { 486e1199815SMickaël Salaün .allowed_access = allowed_access, 487e1199815SMickaël Salaün }; 488e1199815SMickaël Salaün 489e1199815SMickaël Salaün path_beneath.parent_fd = open(path, O_PATH | O_CLOEXEC); 490371183faSMickaël Salaün ASSERT_LE(0, path_beneath.parent_fd) 491371183faSMickaël Salaün { 492e1199815SMickaël Salaün TH_LOG("Failed to open directory \"%s\": %s", path, 493e1199815SMickaël Salaün strerror(errno)); 494e1199815SMickaël Salaün } 495e1199815SMickaël Salaün ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 496371183faSMickaël Salaün &path_beneath, 0)) 497371183faSMickaël Salaün { 498e1199815SMickaël Salaün TH_LOG("Failed to update the ruleset with \"%s\": %s", path, 499e1199815SMickaël Salaün strerror(errno)); 500e1199815SMickaël Salaün } 501e1199815SMickaël Salaün ASSERT_EQ(0, close(path_beneath.parent_fd)); 502e1199815SMickaël Salaün } 503e1199815SMickaël Salaün 504e1199815SMickaël Salaün struct rule { 505e1199815SMickaël Salaün const char *path; 506e1199815SMickaël Salaün __u64 access; 507e1199815SMickaël Salaün }; 508e1199815SMickaël Salaün 5094598d9abSMickaël Salaün /* clang-format off */ 5104598d9abSMickaël Salaün 511e1199815SMickaël Salaün #define ACCESS_RO ( \ 512e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE | \ 513e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_READ_DIR) 514e1199815SMickaël Salaün 515e1199815SMickaël Salaün #define ACCESS_RW ( \ 516e1199815SMickaël Salaün ACCESS_RO | \ 517e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE) 518e1199815SMickaël Salaün 5194598d9abSMickaël Salaün /* clang-format on */ 5204598d9abSMickaël Salaün 521e1199815SMickaël Salaün static int create_ruleset(struct __test_metadata *const _metadata, 522371183faSMickaël Salaün const __u64 handled_access_fs, 523371183faSMickaël Salaün const struct rule rules[]) 524e1199815SMickaël Salaün { 525e1199815SMickaël Salaün int ruleset_fd, i; 526e1199815SMickaël Salaün struct landlock_ruleset_attr ruleset_attr = { 527e1199815SMickaël Salaün .handled_access_fs = handled_access_fs, 528e1199815SMickaël Salaün }; 529e1199815SMickaël Salaün 530371183faSMickaël Salaün ASSERT_NE(NULL, rules) 531371183faSMickaël Salaün { 532e1199815SMickaël Salaün TH_LOG("No rule list"); 533e1199815SMickaël Salaün } 534371183faSMickaël Salaün ASSERT_NE(NULL, rules[0].path) 535371183faSMickaël Salaün { 536e1199815SMickaël Salaün TH_LOG("Empty rule list"); 537e1199815SMickaël Salaün } 538e1199815SMickaël Salaün 539371183faSMickaël Salaün ruleset_fd = 540371183faSMickaël Salaün landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 541371183faSMickaël Salaün ASSERT_LE(0, ruleset_fd) 542371183faSMickaël Salaün { 543e1199815SMickaël Salaün TH_LOG("Failed to create a ruleset: %s", strerror(errno)); 544e1199815SMickaël Salaün } 545e1199815SMickaël Salaün 546e1199815SMickaël Salaün for (i = 0; rules[i].path; i++) { 547e1199815SMickaël Salaün add_path_beneath(_metadata, ruleset_fd, rules[i].access, 548e1199815SMickaël Salaün rules[i].path); 549e1199815SMickaël Salaün } 550e1199815SMickaël Salaün return ruleset_fd; 551e1199815SMickaël Salaün } 552e1199815SMickaël Salaün 553e1199815SMickaël Salaün static void enforce_ruleset(struct __test_metadata *const _metadata, 554e1199815SMickaël Salaün const int ruleset_fd) 555e1199815SMickaël Salaün { 556e1199815SMickaël Salaün ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)); 557371183faSMickaël Salaün ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)) 558371183faSMickaël Salaün { 559e1199815SMickaël Salaün TH_LOG("Failed to enforce ruleset: %s", strerror(errno)); 560e1199815SMickaël Salaün } 561e1199815SMickaël Salaün } 562e1199815SMickaël Salaün 563e1199815SMickaël Salaün TEST_F_FORK(layout1, proc_nsfs) 564e1199815SMickaël Salaün { 565e1199815SMickaël Salaün const struct rule rules[] = { 566e1199815SMickaël Salaün { 567e1199815SMickaël Salaün .path = "/dev/null", 568e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 569e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 570e1199815SMickaël Salaün }, 571135464f9SMickaël Salaün {}, 572e1199815SMickaël Salaün }; 573e1199815SMickaël Salaün struct landlock_path_beneath_attr path_beneath; 574371183faSMickaël Salaün const int ruleset_fd = create_ruleset( 575371183faSMickaël Salaün _metadata, rules[0].access | LANDLOCK_ACCESS_FS_READ_DIR, 576371183faSMickaël Salaün rules); 577e1199815SMickaël Salaün 578e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 579e1199815SMickaël Salaün ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY)); 580e1199815SMickaël Salaün 581e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 582e1199815SMickaël Salaün 583e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 584e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/dev", O_RDONLY)); 585e1199815SMickaël Salaün ASSERT_EQ(0, test_open("/dev/null", O_RDONLY)); 586e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/dev/full", O_RDONLY)); 587e1199815SMickaël Salaün 588e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/proc", O_RDONLY)); 589e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/proc/self", O_RDONLY)); 590e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/proc/self/ns", O_RDONLY)); 591e1199815SMickaël Salaün /* 592e1199815SMickaël Salaün * Because nsfs is an internal filesystem, /proc/self/ns/mnt is a 593e1199815SMickaël Salaün * disconnected path. Such path cannot be identified and must then be 594e1199815SMickaël Salaün * allowed. 595e1199815SMickaël Salaün */ 596e1199815SMickaël Salaün ASSERT_EQ(0, test_open("/proc/self/ns/mnt", O_RDONLY)); 597e1199815SMickaël Salaün 598e1199815SMickaël Salaün /* 599e1199815SMickaël Salaün * Checks that it is not possible to add nsfs-like filesystem 600e1199815SMickaël Salaün * references to a ruleset. 601e1199815SMickaël Salaün */ 602e1199815SMickaël Salaün path_beneath.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | 603e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 604e1199815SMickaël Salaün path_beneath.parent_fd = open("/proc/self/ns/mnt", O_PATH | O_CLOEXEC); 605e1199815SMickaël Salaün ASSERT_LE(0, path_beneath.parent_fd); 606e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, 607e1199815SMickaël Salaün &path_beneath, 0)); 608e1199815SMickaël Salaün ASSERT_EQ(EBADFD, errno); 609e1199815SMickaël Salaün ASSERT_EQ(0, close(path_beneath.parent_fd)); 610e1199815SMickaël Salaün } 611e1199815SMickaël Salaün 612371183faSMickaël Salaün TEST_F_FORK(layout1, unpriv) 613371183faSMickaël Salaün { 614e1199815SMickaël Salaün const struct rule rules[] = { 615e1199815SMickaël Salaün { 616e1199815SMickaël Salaün .path = dir_s1d2, 617e1199815SMickaël Salaün .access = ACCESS_RO, 618e1199815SMickaël Salaün }, 619135464f9SMickaël Salaün {}, 620e1199815SMickaël Salaün }; 621e1199815SMickaël Salaün int ruleset_fd; 622e1199815SMickaël Salaün 623e1199815SMickaël Salaün drop_caps(_metadata); 624e1199815SMickaël Salaün 625e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules); 626e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 627e1199815SMickaël Salaün ASSERT_EQ(-1, landlock_restrict_self(ruleset_fd, 0)); 628e1199815SMickaël Salaün ASSERT_EQ(EPERM, errno); 629e1199815SMickaël Salaün 630e1199815SMickaël Salaün /* enforce_ruleset() calls prctl(no_new_privs). */ 631e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 632e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 633e1199815SMickaël Salaün } 634e1199815SMickaël Salaün 635e1199815SMickaël Salaün TEST_F_FORK(layout1, effective_access) 636e1199815SMickaël Salaün { 637e1199815SMickaël Salaün const struct rule rules[] = { 638e1199815SMickaël Salaün { 639e1199815SMickaël Salaün .path = dir_s1d2, 640e1199815SMickaël Salaün .access = ACCESS_RO, 641e1199815SMickaël Salaün }, 642e1199815SMickaël Salaün { 643e1199815SMickaël Salaün .path = file1_s2d2, 644e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 645e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 646e1199815SMickaël Salaün }, 647135464f9SMickaël Salaün {}, 648e1199815SMickaël Salaün }; 649e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 650e1199815SMickaël Salaün char buf; 651e1199815SMickaël Salaün int reg_fd; 652e1199815SMickaël Salaün 653e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 654e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 655e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 656e1199815SMickaël Salaün 657e1199815SMickaël Salaün /* Tests on a directory. */ 658e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 659e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 660e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 661e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 662e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 663e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 664e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 665e1199815SMickaël Salaün 666e1199815SMickaël Salaün /* Tests on a file. */ 667e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY)); 668e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 669e1199815SMickaël Salaün 670e1199815SMickaël Salaün /* Checks effective read and write actions. */ 671e1199815SMickaël Salaün reg_fd = open(file1_s2d2, O_RDWR | O_CLOEXEC); 672e1199815SMickaël Salaün ASSERT_LE(0, reg_fd); 673e1199815SMickaël Salaün ASSERT_EQ(1, write(reg_fd, ".", 1)); 674e1199815SMickaël Salaün ASSERT_LE(0, lseek(reg_fd, 0, SEEK_SET)); 675e1199815SMickaël Salaün ASSERT_EQ(1, read(reg_fd, &buf, 1)); 676e1199815SMickaël Salaün ASSERT_EQ('.', buf); 677e1199815SMickaël Salaün ASSERT_EQ(0, close(reg_fd)); 678e1199815SMickaël Salaün 679e1199815SMickaël Salaün /* Just in case, double-checks effective actions. */ 680e1199815SMickaël Salaün reg_fd = open(file1_s2d2, O_RDONLY | O_CLOEXEC); 681e1199815SMickaël Salaün ASSERT_LE(0, reg_fd); 682e1199815SMickaël Salaün ASSERT_EQ(-1, write(reg_fd, &buf, 1)); 683e1199815SMickaël Salaün ASSERT_EQ(EBADF, errno); 684e1199815SMickaël Salaün ASSERT_EQ(0, close(reg_fd)); 685e1199815SMickaël Salaün } 686e1199815SMickaël Salaün 687e1199815SMickaël Salaün TEST_F_FORK(layout1, unhandled_access) 688e1199815SMickaël Salaün { 689e1199815SMickaël Salaün const struct rule rules[] = { 690e1199815SMickaël Salaün { 691e1199815SMickaël Salaün .path = dir_s1d2, 692e1199815SMickaël Salaün .access = ACCESS_RO, 693e1199815SMickaël Salaün }, 694135464f9SMickaël Salaün {}, 695e1199815SMickaël Salaün }; 696e1199815SMickaël Salaün /* Here, we only handle read accesses, not write accesses. */ 697e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RO, rules); 698e1199815SMickaël Salaün 699e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 700e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 701e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 702e1199815SMickaël Salaün 703e1199815SMickaël Salaün /* 704e1199815SMickaël Salaün * Because the policy does not handle LANDLOCK_ACCESS_FS_WRITE_FILE, 705e1199815SMickaël Salaün * opening for write-only should be allowed, but not read-write. 706e1199815SMickaël Salaün */ 707e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d1, O_WRONLY)); 708e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 709e1199815SMickaël Salaün 710e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY)); 711e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR)); 712e1199815SMickaël Salaün } 713e1199815SMickaël Salaün 714e1199815SMickaël Salaün TEST_F_FORK(layout1, ruleset_overlap) 715e1199815SMickaël Salaün { 716e1199815SMickaël Salaün const struct rule rules[] = { 717e1199815SMickaël Salaün /* These rules should be ORed among them. */ 718e1199815SMickaël Salaün { 719e1199815SMickaël Salaün .path = dir_s1d2, 720e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 721e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 722e1199815SMickaël Salaün }, 723e1199815SMickaël Salaün { 724e1199815SMickaël Salaün .path = dir_s1d2, 725e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 726e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_READ_DIR, 727e1199815SMickaël Salaün }, 728135464f9SMickaël Salaün {}, 729e1199815SMickaël Salaün }; 730e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 731e1199815SMickaël Salaün 732e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 733e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 734e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 735e1199815SMickaël Salaün 736e1199815SMickaël Salaün /* Checks s1d1 hierarchy. */ 737e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 738e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 739e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 740e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 741e1199815SMickaël Salaün 742e1199815SMickaël Salaün /* Checks s1d2 hierarchy. */ 743e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 744e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_WRONLY)); 745e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR)); 746e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 747e1199815SMickaël Salaün 748e1199815SMickaël Salaün /* Checks s1d3 hierarchy. */ 749e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 750e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_WRONLY)); 751e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 752e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 753e1199815SMickaël Salaün } 754e1199815SMickaël Salaün 755e1199815SMickaël Salaün TEST_F_FORK(layout1, non_overlapping_accesses) 756e1199815SMickaël Salaün { 757e1199815SMickaël Salaün const struct rule layer1[] = { 758e1199815SMickaël Salaün { 759e1199815SMickaël Salaün .path = dir_s1d2, 760e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_MAKE_REG, 761e1199815SMickaël Salaün }, 762135464f9SMickaël Salaün {}, 763e1199815SMickaël Salaün }; 764e1199815SMickaël Salaün const struct rule layer2[] = { 765e1199815SMickaël Salaün { 766e1199815SMickaël Salaün .path = dir_s1d3, 767e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 768e1199815SMickaël Salaün }, 769135464f9SMickaël Salaün {}, 770e1199815SMickaël Salaün }; 771e1199815SMickaël Salaün int ruleset_fd; 772e1199815SMickaël Salaün 773e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 774e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 775e1199815SMickaël Salaün 776371183faSMickaël Salaün ruleset_fd = 777371183faSMickaël Salaün create_ruleset(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, layer1); 778e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 779e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 780e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 781e1199815SMickaël Salaün 782e1199815SMickaël Salaün ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0)); 783e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 784e1199815SMickaël Salaün ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0)); 785e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 786e1199815SMickaël Salaün 787e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_REMOVE_FILE, 788e1199815SMickaël Salaün layer2); 789e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 790e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 791e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 792e1199815SMickaël Salaün 793e1199815SMickaël Salaün /* Unchanged accesses for file creation. */ 794e1199815SMickaël Salaün ASSERT_EQ(-1, mknod(file1_s1d1, S_IFREG | 0700, 0)); 795e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 796e1199815SMickaël Salaün ASSERT_EQ(0, mknod(file1_s1d2, S_IFREG | 0700, 0)); 797e1199815SMickaël Salaün 798e1199815SMickaël Salaün /* Checks file removing. */ 799e1199815SMickaël Salaün ASSERT_EQ(-1, unlink(file1_s1d2)); 800e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 801e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 802e1199815SMickaël Salaün } 803e1199815SMickaël Salaün 804e1199815SMickaël Salaün TEST_F_FORK(layout1, interleaved_masked_accesses) 805e1199815SMickaël Salaün { 806e1199815SMickaël Salaün /* 807e1199815SMickaël Salaün * Checks overly restrictive rules: 808e1199815SMickaël Salaün * layer 1: allows R s1d1/s1d2/s1d3/file1 809e1199815SMickaël Salaün * layer 2: allows RW s1d1/s1d2/s1d3 810e1199815SMickaël Salaün * allows W s1d1/s1d2 811e1199815SMickaël Salaün * denies R s1d1/s1d2 812e1199815SMickaël Salaün * layer 3: allows R s1d1 813e1199815SMickaël Salaün * layer 4: allows R s1d1/s1d2 814e1199815SMickaël Salaün * denies W s1d1/s1d2 815e1199815SMickaël Salaün * layer 5: allows R s1d1/s1d2 816e1199815SMickaël Salaün * layer 6: allows X ---- 817e1199815SMickaël Salaün * layer 7: allows W s1d1/s1d2 818e1199815SMickaël Salaün * denies R s1d1/s1d2 819e1199815SMickaël Salaün */ 820e1199815SMickaël Salaün const struct rule layer1_read[] = { 821e1199815SMickaël Salaün /* Allows read access to file1_s1d3 with the first layer. */ 822e1199815SMickaël Salaün { 823e1199815SMickaël Salaün .path = file1_s1d3, 824e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 825e1199815SMickaël Salaün }, 826135464f9SMickaël Salaün {}, 827e1199815SMickaël Salaün }; 828e1199815SMickaël Salaün /* First rule with write restrictions. */ 829e1199815SMickaël Salaün const struct rule layer2_read_write[] = { 830e1199815SMickaël Salaün /* Start by granting read-write access via its parent directory... */ 831e1199815SMickaël Salaün { 832e1199815SMickaël Salaün .path = dir_s1d3, 833e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 834e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 835e1199815SMickaël Salaün }, 836e1199815SMickaël Salaün /* ...but also denies read access via its grandparent directory. */ 837e1199815SMickaël Salaün { 838e1199815SMickaël Salaün .path = dir_s1d2, 839e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 840e1199815SMickaël Salaün }, 841135464f9SMickaël Salaün {}, 842e1199815SMickaël Salaün }; 843e1199815SMickaël Salaün const struct rule layer3_read[] = { 844e1199815SMickaël Salaün /* Allows read access via its great-grandparent directory. */ 845e1199815SMickaël Salaün { 846e1199815SMickaël Salaün .path = dir_s1d1, 847e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 848e1199815SMickaël Salaün }, 849135464f9SMickaël Salaün {}, 850e1199815SMickaël Salaün }; 851e1199815SMickaël Salaün const struct rule layer4_read_write[] = { 852e1199815SMickaël Salaün /* 853e1199815SMickaël Salaün * Try to confuse the deny access by denying write (but not 854e1199815SMickaël Salaün * read) access via its grandparent directory. 855e1199815SMickaël Salaün */ 856e1199815SMickaël Salaün { 857e1199815SMickaël Salaün .path = dir_s1d2, 858e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 859e1199815SMickaël Salaün }, 860135464f9SMickaël Salaün {}, 861e1199815SMickaël Salaün }; 862e1199815SMickaël Salaün const struct rule layer5_read[] = { 863e1199815SMickaël Salaün /* 864e1199815SMickaël Salaün * Try to override layer2's deny read access by explicitly 865e1199815SMickaël Salaün * allowing read access via file1_s1d3's grandparent. 866e1199815SMickaël Salaün */ 867e1199815SMickaël Salaün { 868e1199815SMickaël Salaün .path = dir_s1d2, 869e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 870e1199815SMickaël Salaün }, 871135464f9SMickaël Salaün {}, 872e1199815SMickaël Salaün }; 873e1199815SMickaël Salaün const struct rule layer6_execute[] = { 874e1199815SMickaël Salaün /* 875e1199815SMickaël Salaün * Restricts an unrelated file hierarchy with a new access 876e1199815SMickaël Salaün * (non-overlapping) type. 877e1199815SMickaël Salaün */ 878e1199815SMickaël Salaün { 879e1199815SMickaël Salaün .path = dir_s2d1, 880e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_EXECUTE, 881e1199815SMickaël Salaün }, 882135464f9SMickaël Salaün {}, 883e1199815SMickaël Salaün }; 884e1199815SMickaël Salaün const struct rule layer7_read_write[] = { 885e1199815SMickaël Salaün /* 886e1199815SMickaël Salaün * Finally, denies read access to file1_s1d3 via its 887e1199815SMickaël Salaün * grandparent. 888e1199815SMickaël Salaün */ 889e1199815SMickaël Salaün { 890e1199815SMickaël Salaün .path = dir_s1d2, 891e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 892e1199815SMickaël Salaün }, 893135464f9SMickaël Salaün {}, 894e1199815SMickaël Salaün }; 895e1199815SMickaël Salaün int ruleset_fd; 896e1199815SMickaël Salaün 897e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, 898e1199815SMickaël Salaün layer1_read); 899e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 900e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 901e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 902e1199815SMickaël Salaün 903e1199815SMickaël Salaün /* Checks that read access is granted for file1_s1d3 with layer 1. */ 904e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 905e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 906e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY)); 907e1199815SMickaël Salaün 908371183faSMickaël Salaün ruleset_fd = create_ruleset(_metadata, 909371183faSMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE | 910371183faSMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 911371183faSMickaël Salaün layer2_read_write); 912e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 913e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 914e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 915e1199815SMickaël Salaün 916e1199815SMickaël Salaün /* Checks that previous access rights are unchanged with layer 2. */ 917e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 918e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 919e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY)); 920e1199815SMickaël Salaün 921e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, 922e1199815SMickaël Salaün layer3_read); 923e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 924e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 925e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 926e1199815SMickaël Salaün 927e1199815SMickaël Salaün /* Checks that previous access rights are unchanged with layer 3. */ 928e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDWR)); 929e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 930e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file2_s1d3, O_WRONLY)); 931e1199815SMickaël Salaün 932e1199815SMickaël Salaün /* This time, denies write access for the file hierarchy. */ 933371183faSMickaël Salaün ruleset_fd = create_ruleset(_metadata, 934371183faSMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE | 935371183faSMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 936371183faSMickaël Salaün layer4_read_write); 937e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 938e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 939e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 940e1199815SMickaël Salaün 941e1199815SMickaël Salaün /* 942e1199815SMickaël Salaün * Checks that the only change with layer 4 is that write access is 943e1199815SMickaël Salaün * denied. 944e1199815SMickaël Salaün */ 945e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 946e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 947e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 948e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 949e1199815SMickaël Salaün 950e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_FILE, 951e1199815SMickaël Salaün layer5_read); 952e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 953e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 954e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 955e1199815SMickaël Salaün 956e1199815SMickaël Salaün /* Checks that previous access rights are unchanged with layer 5. */ 957e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 958e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 959e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 960e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 961e1199815SMickaël Salaün 962e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_EXECUTE, 963e1199815SMickaël Salaün layer6_execute); 964e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 965e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 966e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 967e1199815SMickaël Salaün 968e1199815SMickaël Salaün /* Checks that previous access rights are unchanged with layer 6. */ 969e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 970e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 971e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 972e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 973e1199815SMickaël Salaün 974371183faSMickaël Salaün ruleset_fd = create_ruleset(_metadata, 975371183faSMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE | 976371183faSMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 977371183faSMickaël Salaün layer7_read_write); 978e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 979e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 980e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 981e1199815SMickaël Salaün 982e1199815SMickaël Salaün /* Checks read access is now denied with layer 7. */ 983e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY)); 984e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 985e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_WRONLY)); 986e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file2_s1d3, O_RDONLY)); 987e1199815SMickaël Salaün } 988e1199815SMickaël Salaün 989e1199815SMickaël Salaün TEST_F_FORK(layout1, inherit_subset) 990e1199815SMickaël Salaün { 991e1199815SMickaël Salaün const struct rule rules[] = { 992e1199815SMickaël Salaün { 993e1199815SMickaël Salaün .path = dir_s1d2, 994e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 995e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_READ_DIR, 996e1199815SMickaël Salaün }, 997135464f9SMickaël Salaün {}, 998e1199815SMickaël Salaün }; 999e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1000e1199815SMickaël Salaün 1001e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1002e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1003e1199815SMickaël Salaün 1004e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1005e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1006e1199815SMickaël Salaün 1007e1199815SMickaël Salaün /* Write access is forbidden. */ 1008e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1009e1199815SMickaël Salaün /* Readdir access is allowed. */ 1010e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1011e1199815SMickaël Salaün 1012e1199815SMickaël Salaün /* Write access is forbidden. */ 1013e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1014e1199815SMickaël Salaün /* Readdir access is allowed. */ 1015e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1016e1199815SMickaël Salaün 1017e1199815SMickaël Salaün /* 1018e1199815SMickaël Salaün * Tests shared rule extension: the following rules should not grant 1019e1199815SMickaël Salaün * any new access, only remove some. Once enforced, these rules are 1020e1199815SMickaël Salaün * ANDed with the previous ones. 1021e1199815SMickaël Salaün */ 1022e1199815SMickaël Salaün add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE, 1023e1199815SMickaël Salaün dir_s1d2); 1024e1199815SMickaël Salaün /* 1025e1199815SMickaël Salaün * According to ruleset_fd, dir_s1d2 should now have the 1026e1199815SMickaël Salaün * LANDLOCK_ACCESS_FS_READ_FILE and LANDLOCK_ACCESS_FS_WRITE_FILE 1027e1199815SMickaël Salaün * access rights (even if this directory is opened a second time). 1028e1199815SMickaël Salaün * However, when enforcing this updated ruleset, the ruleset tied to 1029e1199815SMickaël Salaün * the current process (i.e. its domain) will still only have the 1030e1199815SMickaël Salaün * dir_s1d2 with LANDLOCK_ACCESS_FS_READ_FILE and 1031e1199815SMickaël Salaün * LANDLOCK_ACCESS_FS_READ_DIR accesses, but 1032e1199815SMickaël Salaün * LANDLOCK_ACCESS_FS_WRITE_FILE must not be allowed because it would 1033e1199815SMickaël Salaün * be a privilege escalation. 1034e1199815SMickaël Salaün */ 1035e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1036e1199815SMickaël Salaün 1037e1199815SMickaël Salaün /* Same tests and results as above. */ 1038e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1039e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1040e1199815SMickaël Salaün 1041e1199815SMickaël Salaün /* It is still forbidden to write in file1_s1d2. */ 1042e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1043e1199815SMickaël Salaün /* Readdir access is still allowed. */ 1044e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1045e1199815SMickaël Salaün 1046e1199815SMickaël Salaün /* It is still forbidden to write in file1_s1d3. */ 1047e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1048e1199815SMickaël Salaün /* Readdir access is still allowed. */ 1049e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1050e1199815SMickaël Salaün 1051e1199815SMickaël Salaün /* 1052e1199815SMickaël Salaün * Try to get more privileges by adding new access rights to the parent 1053e1199815SMickaël Salaün * directory: dir_s1d1. 1054e1199815SMickaël Salaün */ 1055e1199815SMickaël Salaün add_path_beneath(_metadata, ruleset_fd, ACCESS_RW, dir_s1d1); 1056e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1057e1199815SMickaël Salaün 1058e1199815SMickaël Salaün /* Same tests and results as above. */ 1059e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1060e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1061e1199815SMickaël Salaün 1062e1199815SMickaël Salaün /* It is still forbidden to write in file1_s1d2. */ 1063e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1064e1199815SMickaël Salaün /* Readdir access is still allowed. */ 1065e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1066e1199815SMickaël Salaün 1067e1199815SMickaël Salaün /* It is still forbidden to write in file1_s1d3. */ 1068e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1069e1199815SMickaël Salaün /* Readdir access is still allowed. */ 1070e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1071e1199815SMickaël Salaün 1072e1199815SMickaël Salaün /* 1073e1199815SMickaël Salaün * Now, dir_s1d3 get a new rule tied to it, only allowing 1074e1199815SMickaël Salaün * LANDLOCK_ACCESS_FS_WRITE_FILE. The (kernel internal) difference is 1075e1199815SMickaël Salaün * that there was no rule tied to it before. 1076e1199815SMickaël Salaün */ 1077e1199815SMickaël Salaün add_path_beneath(_metadata, ruleset_fd, LANDLOCK_ACCESS_FS_WRITE_FILE, 1078e1199815SMickaël Salaün dir_s1d3); 1079e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1080e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1081e1199815SMickaël Salaün 1082e1199815SMickaël Salaün /* 1083e1199815SMickaël Salaün * Same tests and results as above, except for open(dir_s1d3) which is 1084e1199815SMickaël Salaün * now denied because the new rule mask the rule previously inherited 1085e1199815SMickaël Salaün * from dir_s1d2. 1086e1199815SMickaël Salaün */ 1087e1199815SMickaël Salaün 1088e1199815SMickaël Salaün /* Same tests and results as above. */ 1089e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 1090e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 1091e1199815SMickaël Salaün 1092e1199815SMickaël Salaün /* It is still forbidden to write in file1_s1d2. */ 1093e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 1094e1199815SMickaël Salaün /* Readdir access is still allowed. */ 1095e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1096e1199815SMickaël Salaün 1097e1199815SMickaël Salaün /* It is still forbidden to write in file1_s1d3. */ 1098e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 1099e1199815SMickaël Salaün /* 1100e1199815SMickaël Salaün * Readdir of dir_s1d3 is still allowed because of the OR policy inside 1101e1199815SMickaël Salaün * the same layer. 1102e1199815SMickaël Salaün */ 1103e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1104e1199815SMickaël Salaün } 1105e1199815SMickaël Salaün 1106e1199815SMickaël Salaün TEST_F_FORK(layout1, inherit_superset) 1107e1199815SMickaël Salaün { 1108e1199815SMickaël Salaün const struct rule rules[] = { 1109e1199815SMickaël Salaün { 1110e1199815SMickaël Salaün .path = dir_s1d3, 1111e1199815SMickaël Salaün .access = ACCESS_RO, 1112e1199815SMickaël Salaün }, 1113135464f9SMickaël Salaün {}, 1114e1199815SMickaël Salaün }; 1115e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1116e1199815SMickaël Salaün 1117e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1118e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1119e1199815SMickaël Salaün 1120e1199815SMickaël Salaün /* Readdir access is denied for dir_s1d2. */ 1121e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1122e1199815SMickaël Salaün /* Readdir access is allowed for dir_s1d3. */ 1123e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1124e1199815SMickaël Salaün /* File access is allowed for file1_s1d3. */ 1125e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1126e1199815SMickaël Salaün 1127e1199815SMickaël Salaün /* Now dir_s1d2, parent of dir_s1d3, gets a new rule tied to it. */ 1128371183faSMickaël Salaün add_path_beneath(_metadata, ruleset_fd, 1129371183faSMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE | 1130371183faSMickaël Salaün LANDLOCK_ACCESS_FS_READ_DIR, 1131371183faSMickaël Salaün dir_s1d2); 1132e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1133e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1134e1199815SMickaël Salaün 1135e1199815SMickaël Salaün /* Readdir access is still denied for dir_s1d2. */ 1136e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 1137e1199815SMickaël Salaün /* Readdir access is still allowed for dir_s1d3. */ 1138e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 1139e1199815SMickaël Salaün /* File access is still allowed for file1_s1d3. */ 1140e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1141e1199815SMickaël Salaün } 1142e1199815SMickaël Salaün 1143e1199815SMickaël Salaün TEST_F_FORK(layout1, max_layers) 1144e1199815SMickaël Salaün { 1145e1199815SMickaël Salaün int i, err; 1146e1199815SMickaël Salaün const struct rule rules[] = { 1147e1199815SMickaël Salaün { 1148e1199815SMickaël Salaün .path = dir_s1d2, 1149e1199815SMickaël Salaün .access = ACCESS_RO, 1150e1199815SMickaël Salaün }, 1151135464f9SMickaël Salaün {}, 1152e1199815SMickaël Salaün }; 1153e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1154e1199815SMickaël Salaün 1155e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1156e1199815SMickaël Salaün for (i = 0; i < 64; i++) 1157e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1158e1199815SMickaël Salaün 1159e1199815SMickaël Salaün for (i = 0; i < 2; i++) { 1160e1199815SMickaël Salaün err = landlock_restrict_self(ruleset_fd, 0); 1161e1199815SMickaël Salaün ASSERT_EQ(-1, err); 1162e1199815SMickaël Salaün ASSERT_EQ(E2BIG, errno); 1163e1199815SMickaël Salaün } 1164e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1165e1199815SMickaël Salaün } 1166e1199815SMickaël Salaün 1167e1199815SMickaël Salaün TEST_F_FORK(layout1, empty_or_same_ruleset) 1168e1199815SMickaël Salaün { 1169e1199815SMickaël Salaün struct landlock_ruleset_attr ruleset_attr = {}; 1170e1199815SMickaël Salaün int ruleset_fd; 1171e1199815SMickaël Salaün 1172e1199815SMickaël Salaün /* Tests empty handled_access_fs. */ 1173371183faSMickaël Salaün ruleset_fd = 1174371183faSMickaël Salaün landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 1175e1199815SMickaël Salaün ASSERT_LE(-1, ruleset_fd); 1176e1199815SMickaël Salaün ASSERT_EQ(ENOMSG, errno); 1177e1199815SMickaël Salaün 1178e1199815SMickaël Salaün /* Enforces policy which deny read access to all files. */ 1179e1199815SMickaël Salaün ruleset_attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE; 1180371183faSMickaël Salaün ruleset_fd = 1181371183faSMickaël Salaün landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 1182e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1183e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1184e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1185e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1186e1199815SMickaël Salaün 1187e1199815SMickaël Salaün /* Nests a policy which deny read access to all directories. */ 1188e1199815SMickaël Salaün ruleset_attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR; 1189371183faSMickaël Salaün ruleset_fd = 1190371183faSMickaël Salaün landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); 1191e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1192e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1193e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 1194e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 1195e1199815SMickaël Salaün 1196e1199815SMickaël Salaün /* Enforces a second time with the same ruleset. */ 1197e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1198e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1199e1199815SMickaël Salaün } 1200e1199815SMickaël Salaün 1201e1199815SMickaël Salaün TEST_F_FORK(layout1, rule_on_mountpoint) 1202e1199815SMickaël Salaün { 1203e1199815SMickaël Salaün const struct rule rules[] = { 1204e1199815SMickaël Salaün { 1205e1199815SMickaël Salaün .path = dir_s1d1, 1206e1199815SMickaël Salaün .access = ACCESS_RO, 1207e1199815SMickaël Salaün }, 1208e1199815SMickaël Salaün { 1209e1199815SMickaël Salaün /* dir_s3d2 is a mount point. */ 1210e1199815SMickaël Salaün .path = dir_s3d2, 1211e1199815SMickaël Salaün .access = ACCESS_RO, 1212e1199815SMickaël Salaün }, 1213135464f9SMickaël Salaün {}, 1214e1199815SMickaël Salaün }; 1215e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1216e1199815SMickaël Salaün 1217e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1218e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1219e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1220e1199815SMickaël Salaün 1221e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1222e1199815SMickaël Salaün 1223e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY)); 1224e1199815SMickaël Salaün 1225e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s3d1, O_RDONLY)); 1226e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 1227e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY)); 1228e1199815SMickaël Salaün } 1229e1199815SMickaël Salaün 1230e1199815SMickaël Salaün TEST_F_FORK(layout1, rule_over_mountpoint) 1231e1199815SMickaël Salaün { 1232e1199815SMickaël Salaün const struct rule rules[] = { 1233e1199815SMickaël Salaün { 1234e1199815SMickaël Salaün .path = dir_s1d1, 1235e1199815SMickaël Salaün .access = ACCESS_RO, 1236e1199815SMickaël Salaün }, 1237e1199815SMickaël Salaün { 1238e1199815SMickaël Salaün /* dir_s3d2 is a mount point. */ 1239e1199815SMickaël Salaün .path = dir_s3d1, 1240e1199815SMickaël Salaün .access = ACCESS_RO, 1241e1199815SMickaël Salaün }, 1242135464f9SMickaël Salaün {}, 1243e1199815SMickaël Salaün }; 1244e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1245e1199815SMickaël Salaün 1246e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1247e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1248e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1249e1199815SMickaël Salaün 1250e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1251e1199815SMickaël Salaün 1252e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY)); 1253e1199815SMickaël Salaün 1254e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY)); 1255e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d2, O_RDONLY)); 1256e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d3, O_RDONLY)); 1257e1199815SMickaël Salaün } 1258e1199815SMickaël Salaün 1259e1199815SMickaël Salaün /* 1260e1199815SMickaël Salaün * This test verifies that we can apply a landlock rule on the root directory 1261e1199815SMickaël Salaün * (which might require special handling). 1262e1199815SMickaël Salaün */ 1263e1199815SMickaël Salaün TEST_F_FORK(layout1, rule_over_root_allow_then_deny) 1264e1199815SMickaël Salaün { 1265e1199815SMickaël Salaün struct rule rules[] = { 1266e1199815SMickaël Salaün { 1267e1199815SMickaël Salaün .path = "/", 1268e1199815SMickaël Salaün .access = ACCESS_RO, 1269e1199815SMickaël Salaün }, 1270135464f9SMickaël Salaün {}, 1271e1199815SMickaël Salaün }; 1272e1199815SMickaël Salaün int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1273e1199815SMickaël Salaün 1274e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1275e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1276e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1277e1199815SMickaël Salaün 1278e1199815SMickaël Salaün /* Checks allowed access. */ 1279e1199815SMickaël Salaün ASSERT_EQ(0, test_open("/", O_RDONLY)); 1280e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1281e1199815SMickaël Salaün 1282e1199815SMickaël Salaün rules[0].access = LANDLOCK_ACCESS_FS_READ_FILE; 1283e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1284e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1285e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1286e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1287e1199815SMickaël Salaün 1288e1199815SMickaël Salaün /* Checks denied access (on a directory). */ 1289e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 1290e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 1291e1199815SMickaël Salaün } 1292e1199815SMickaël Salaün 1293e1199815SMickaël Salaün TEST_F_FORK(layout1, rule_over_root_deny) 1294e1199815SMickaël Salaün { 1295e1199815SMickaël Salaün const struct rule rules[] = { 1296e1199815SMickaël Salaün { 1297e1199815SMickaël Salaün .path = "/", 1298e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 1299e1199815SMickaël Salaün }, 1300135464f9SMickaël Salaün {}, 1301e1199815SMickaël Salaün }; 1302e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1303e1199815SMickaël Salaün 1304e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1305e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1306e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1307e1199815SMickaël Salaün 1308e1199815SMickaël Salaün /* Checks denied access (on a directory). */ 1309e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 1310e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY)); 1311e1199815SMickaël Salaün } 1312e1199815SMickaël Salaün 1313e1199815SMickaël Salaün TEST_F_FORK(layout1, rule_inside_mount_ns) 1314e1199815SMickaël Salaün { 1315e1199815SMickaël Salaün const struct rule rules[] = { 1316e1199815SMickaël Salaün { 1317e1199815SMickaël Salaün .path = "s3d3", 1318e1199815SMickaël Salaün .access = ACCESS_RO, 1319e1199815SMickaël Salaün }, 1320135464f9SMickaël Salaün {}, 1321e1199815SMickaël Salaün }; 1322e1199815SMickaël Salaün int ruleset_fd; 1323e1199815SMickaël Salaün 1324e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 132587129ef1SMickaël Salaün ASSERT_EQ(0, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3)) 1326371183faSMickaël Salaün { 1327e1199815SMickaël Salaün TH_LOG("Failed to pivot root: %s", strerror(errno)); 1328e1199815SMickaël Salaün }; 1329e1199815SMickaël Salaün ASSERT_EQ(0, chdir("/")); 1330e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 1331e1199815SMickaël Salaün 1332e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1333e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1334e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1335e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1336e1199815SMickaël Salaün 1337e1199815SMickaël Salaün ASSERT_EQ(0, test_open("s3d3", O_RDONLY)); 1338e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open("/", O_RDONLY)); 1339e1199815SMickaël Salaün } 1340e1199815SMickaël Salaün 1341e1199815SMickaël Salaün TEST_F_FORK(layout1, mount_and_pivot) 1342e1199815SMickaël Salaün { 1343e1199815SMickaël Salaün const struct rule rules[] = { 1344e1199815SMickaël Salaün { 1345e1199815SMickaël Salaün .path = dir_s3d2, 1346e1199815SMickaël Salaün .access = ACCESS_RO, 1347e1199815SMickaël Salaün }, 1348135464f9SMickaël Salaün {}, 1349e1199815SMickaël Salaün }; 1350e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1351e1199815SMickaël Salaün 1352e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1353e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1354e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1355e1199815SMickaël Salaün 1356e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 1357e1199815SMickaël Salaün ASSERT_EQ(-1, mount(NULL, dir_s3d2, NULL, MS_RDONLY, NULL)); 1358e1199815SMickaël Salaün ASSERT_EQ(EPERM, errno); 135987129ef1SMickaël Salaün ASSERT_EQ(-1, syscall(__NR_pivot_root, dir_s3d2, dir_s3d3)); 1360e1199815SMickaël Salaün ASSERT_EQ(EPERM, errno); 1361e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 1362e1199815SMickaël Salaün } 1363e1199815SMickaël Salaün 1364e1199815SMickaël Salaün TEST_F_FORK(layout1, move_mount) 1365e1199815SMickaël Salaün { 1366e1199815SMickaël Salaün const struct rule rules[] = { 1367e1199815SMickaël Salaün { 1368e1199815SMickaël Salaün .path = dir_s3d2, 1369e1199815SMickaël Salaün .access = ACCESS_RO, 1370e1199815SMickaël Salaün }, 1371135464f9SMickaël Salaün {}, 1372e1199815SMickaël Salaün }; 1373e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1374e1199815SMickaël Salaün 1375e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1376e1199815SMickaël Salaün 1377e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 137887129ef1SMickaël Salaün ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD, 1379371183faSMickaël Salaün dir_s1d2, 0)) 1380371183faSMickaël Salaün { 1381e1199815SMickaël Salaün TH_LOG("Failed to move mount: %s", strerror(errno)); 1382e1199815SMickaël Salaün } 1383e1199815SMickaël Salaün 138487129ef1SMickaël Salaün ASSERT_EQ(0, syscall(__NR_move_mount, AT_FDCWD, dir_s1d2, AT_FDCWD, 1385e1199815SMickaël Salaün dir_s3d2, 0)); 1386e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 1387e1199815SMickaël Salaün 1388e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1389e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1390e1199815SMickaël Salaün 1391e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 139287129ef1SMickaël Salaün ASSERT_EQ(-1, syscall(__NR_move_mount, AT_FDCWD, dir_s3d2, AT_FDCWD, 1393e1199815SMickaël Salaün dir_s1d2, 0)); 1394e1199815SMickaël Salaün ASSERT_EQ(EPERM, errno); 1395e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 1396e1199815SMickaël Salaün } 1397e1199815SMickaël Salaün 1398e1199815SMickaël Salaün TEST_F_FORK(layout1, release_inodes) 1399e1199815SMickaël Salaün { 1400e1199815SMickaël Salaün const struct rule rules[] = { 1401e1199815SMickaël Salaün { 1402e1199815SMickaël Salaün .path = dir_s1d1, 1403e1199815SMickaël Salaün .access = ACCESS_RO, 1404e1199815SMickaël Salaün }, 1405e1199815SMickaël Salaün { 1406e1199815SMickaël Salaün .path = dir_s3d2, 1407e1199815SMickaël Salaün .access = ACCESS_RO, 1408e1199815SMickaël Salaün }, 1409e1199815SMickaël Salaün { 1410e1199815SMickaël Salaün .path = dir_s3d3, 1411e1199815SMickaël Salaün .access = ACCESS_RO, 1412e1199815SMickaël Salaün }, 1413135464f9SMickaël Salaün {}, 1414e1199815SMickaël Salaün }; 1415e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, ACCESS_RW, rules); 1416e1199815SMickaël Salaün 1417e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1418e1199815SMickaël Salaün /* Unmount a file hierarchy while it is being used by a ruleset. */ 1419e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 1420e1199815SMickaël Salaün ASSERT_EQ(0, umount(dir_s3d2)); 1421e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 1422e1199815SMickaël Salaün 1423e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1424e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1425e1199815SMickaël Salaün 1426e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 1427e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s3d2, O_RDONLY)); 1428e1199815SMickaël Salaün /* This dir_s3d3 would not be allowed and does not exist anyway. */ 1429e1199815SMickaël Salaün ASSERT_EQ(ENOENT, test_open(dir_s3d3, O_RDONLY)); 1430e1199815SMickaël Salaün } 1431e1199815SMickaël Salaün 1432e1199815SMickaël Salaün enum relative_access { 1433e1199815SMickaël Salaün REL_OPEN, 1434e1199815SMickaël Salaün REL_CHDIR, 1435e1199815SMickaël Salaün REL_CHROOT_ONLY, 1436e1199815SMickaël Salaün REL_CHROOT_CHDIR, 1437e1199815SMickaël Salaün }; 1438e1199815SMickaël Salaün 1439e1199815SMickaël Salaün static void test_relative_path(struct __test_metadata *const _metadata, 1440e1199815SMickaël Salaün const enum relative_access rel) 1441e1199815SMickaël Salaün { 1442e1199815SMickaël Salaün /* 1443e1199815SMickaël Salaün * Common layer to check that chroot doesn't ignore it (i.e. a chroot 1444e1199815SMickaël Salaün * is not a disconnected root directory). 1445e1199815SMickaël Salaün */ 1446e1199815SMickaël Salaün const struct rule layer1_base[] = { 1447e1199815SMickaël Salaün { 1448e1199815SMickaël Salaün .path = TMP_DIR, 1449e1199815SMickaël Salaün .access = ACCESS_RO, 1450e1199815SMickaël Salaün }, 1451135464f9SMickaël Salaün {}, 1452e1199815SMickaël Salaün }; 1453e1199815SMickaël Salaün const struct rule layer2_subs[] = { 1454e1199815SMickaël Salaün { 1455e1199815SMickaël Salaün .path = dir_s1d2, 1456e1199815SMickaël Salaün .access = ACCESS_RO, 1457e1199815SMickaël Salaün }, 1458e1199815SMickaël Salaün { 1459e1199815SMickaël Salaün .path = dir_s2d2, 1460e1199815SMickaël Salaün .access = ACCESS_RO, 1461e1199815SMickaël Salaün }, 1462135464f9SMickaël Salaün {}, 1463e1199815SMickaël Salaün }; 1464e1199815SMickaël Salaün int dirfd, ruleset_fd; 1465e1199815SMickaël Salaün 1466e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base); 1467e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1468e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1469e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1470e1199815SMickaël Salaün 1471e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_subs); 1472e1199815SMickaël Salaün 1473e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1474e1199815SMickaël Salaün switch (rel) { 1475e1199815SMickaël Salaün case REL_OPEN: 1476e1199815SMickaël Salaün case REL_CHDIR: 1477e1199815SMickaël Salaün break; 1478e1199815SMickaël Salaün case REL_CHROOT_ONLY: 1479e1199815SMickaël Salaün ASSERT_EQ(0, chdir(dir_s2d2)); 1480e1199815SMickaël Salaün break; 1481e1199815SMickaël Salaün case REL_CHROOT_CHDIR: 1482e1199815SMickaël Salaün ASSERT_EQ(0, chdir(dir_s1d2)); 1483e1199815SMickaël Salaün break; 1484e1199815SMickaël Salaün default: 1485e1199815SMickaël Salaün ASSERT_TRUE(false); 1486e1199815SMickaël Salaün return; 1487e1199815SMickaël Salaün } 1488e1199815SMickaël Salaün 1489e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_CHROOT); 1490e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1491e1199815SMickaël Salaün 1492e1199815SMickaël Salaün switch (rel) { 1493e1199815SMickaël Salaün case REL_OPEN: 1494e1199815SMickaël Salaün dirfd = open(dir_s1d2, O_DIRECTORY); 1495e1199815SMickaël Salaün ASSERT_LE(0, dirfd); 1496e1199815SMickaël Salaün break; 1497e1199815SMickaël Salaün case REL_CHDIR: 1498e1199815SMickaël Salaün ASSERT_EQ(0, chdir(dir_s1d2)); 1499e1199815SMickaël Salaün dirfd = AT_FDCWD; 1500e1199815SMickaël Salaün break; 1501e1199815SMickaël Salaün case REL_CHROOT_ONLY: 1502e1199815SMickaël Salaün /* Do chroot into dir_s1d2 (relative to dir_s2d2). */ 1503371183faSMickaël Salaün ASSERT_EQ(0, chroot("../../s1d1/s1d2")) 1504371183faSMickaël Salaün { 1505e1199815SMickaël Salaün TH_LOG("Failed to chroot: %s", strerror(errno)); 1506e1199815SMickaël Salaün } 1507e1199815SMickaël Salaün dirfd = AT_FDCWD; 1508e1199815SMickaël Salaün break; 1509e1199815SMickaël Salaün case REL_CHROOT_CHDIR: 1510e1199815SMickaël Salaün /* Do chroot into dir_s1d2. */ 1511371183faSMickaël Salaün ASSERT_EQ(0, chroot(".")) 1512371183faSMickaël Salaün { 1513e1199815SMickaël Salaün TH_LOG("Failed to chroot: %s", strerror(errno)); 1514e1199815SMickaël Salaün } 1515e1199815SMickaël Salaün dirfd = AT_FDCWD; 1516e1199815SMickaël Salaün break; 1517e1199815SMickaël Salaün } 1518e1199815SMickaël Salaün 1519e1199815SMickaël Salaün ASSERT_EQ((rel == REL_CHROOT_CHDIR) ? 0 : EACCES, 1520e1199815SMickaël Salaün test_open_rel(dirfd, "..", O_RDONLY)); 1521e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, ".", O_RDONLY)); 1522e1199815SMickaël Salaün 1523e1199815SMickaël Salaün if (rel == REL_CHROOT_ONLY) { 1524e1199815SMickaël Salaün /* The current directory is dir_s2d2. */ 1525e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "./s2d3", O_RDONLY)); 1526e1199815SMickaël Salaün } else { 1527e1199815SMickaël Salaün /* The current directory is dir_s1d2. */ 1528e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "./s1d3", O_RDONLY)); 1529e1199815SMickaël Salaün } 1530e1199815SMickaël Salaün 1531e1199815SMickaël Salaün if (rel == REL_CHROOT_ONLY || rel == REL_CHROOT_CHDIR) { 1532e1199815SMickaël Salaün /* Checks the root dir_s1d2. */ 1533e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "/..", O_RDONLY)); 1534e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "/", O_RDONLY)); 1535e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "/f1", O_RDONLY)); 1536e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "/s1d3", O_RDONLY)); 1537e1199815SMickaël Salaün } 1538e1199815SMickaël Salaün 1539e1199815SMickaël Salaün if (rel != REL_CHROOT_CHDIR) { 1540e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s1d1", O_RDONLY)); 1541e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2", O_RDONLY)); 1542371183faSMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "../../s1d1/s1d2/s1d3", 1543371183faSMickaël Salaün O_RDONLY)); 1544e1199815SMickaël Salaün 1545e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open_rel(dirfd, "../../s2d1", O_RDONLY)); 1546e1199815SMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2", O_RDONLY)); 1547371183faSMickaël Salaün ASSERT_EQ(0, test_open_rel(dirfd, "../../s2d1/s2d2/s2d3", 1548371183faSMickaël Salaün O_RDONLY)); 1549e1199815SMickaël Salaün } 1550e1199815SMickaël Salaün 1551e1199815SMickaël Salaün if (rel == REL_OPEN) 1552e1199815SMickaël Salaün ASSERT_EQ(0, close(dirfd)); 1553e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1554e1199815SMickaël Salaün } 1555e1199815SMickaël Salaün 1556e1199815SMickaël Salaün TEST_F_FORK(layout1, relative_open) 1557e1199815SMickaël Salaün { 1558e1199815SMickaël Salaün test_relative_path(_metadata, REL_OPEN); 1559e1199815SMickaël Salaün } 1560e1199815SMickaël Salaün 1561e1199815SMickaël Salaün TEST_F_FORK(layout1, relative_chdir) 1562e1199815SMickaël Salaün { 1563e1199815SMickaël Salaün test_relative_path(_metadata, REL_CHDIR); 1564e1199815SMickaël Salaün } 1565e1199815SMickaël Salaün 1566e1199815SMickaël Salaün TEST_F_FORK(layout1, relative_chroot_only) 1567e1199815SMickaël Salaün { 1568e1199815SMickaël Salaün test_relative_path(_metadata, REL_CHROOT_ONLY); 1569e1199815SMickaël Salaün } 1570e1199815SMickaël Salaün 1571e1199815SMickaël Salaün TEST_F_FORK(layout1, relative_chroot_chdir) 1572e1199815SMickaël Salaün { 1573e1199815SMickaël Salaün test_relative_path(_metadata, REL_CHROOT_CHDIR); 1574e1199815SMickaël Salaün } 1575e1199815SMickaël Salaün 1576e1199815SMickaël Salaün static void copy_binary(struct __test_metadata *const _metadata, 1577e1199815SMickaël Salaün const char *const dst_path) 1578e1199815SMickaël Salaün { 1579e1199815SMickaël Salaün int dst_fd, src_fd; 1580e1199815SMickaël Salaün struct stat statbuf; 1581e1199815SMickaël Salaün 1582e1199815SMickaël Salaün dst_fd = open(dst_path, O_WRONLY | O_TRUNC | O_CLOEXEC); 1583371183faSMickaël Salaün ASSERT_LE(0, dst_fd) 1584371183faSMickaël Salaün { 1585371183faSMickaël Salaün TH_LOG("Failed to open \"%s\": %s", dst_path, strerror(errno)); 1586e1199815SMickaël Salaün } 1587e1199815SMickaël Salaün src_fd = open(BINARY_PATH, O_RDONLY | O_CLOEXEC); 1588371183faSMickaël Salaün ASSERT_LE(0, src_fd) 1589371183faSMickaël Salaün { 1590e1199815SMickaël Salaün TH_LOG("Failed to open \"" BINARY_PATH "\": %s", 1591e1199815SMickaël Salaün strerror(errno)); 1592e1199815SMickaël Salaün } 1593e1199815SMickaël Salaün ASSERT_EQ(0, fstat(src_fd, &statbuf)); 1594371183faSMickaël Salaün ASSERT_EQ(statbuf.st_size, 1595371183faSMickaël Salaün sendfile(dst_fd, src_fd, 0, statbuf.st_size)); 1596e1199815SMickaël Salaün ASSERT_EQ(0, close(src_fd)); 1597e1199815SMickaël Salaün ASSERT_EQ(0, close(dst_fd)); 1598e1199815SMickaël Salaün } 1599e1199815SMickaël Salaün 1600371183faSMickaël Salaün static void test_execute(struct __test_metadata *const _metadata, const int err, 1601371183faSMickaël Salaün const char *const path) 1602e1199815SMickaël Salaün { 1603e1199815SMickaël Salaün int status; 1604e1199815SMickaël Salaün char *const argv[] = { (char *)path, NULL }; 1605e1199815SMickaël Salaün const pid_t child = fork(); 1606e1199815SMickaël Salaün 1607e1199815SMickaël Salaün ASSERT_LE(0, child); 1608e1199815SMickaël Salaün if (child == 0) { 1609371183faSMickaël Salaün ASSERT_EQ(err ? -1 : 0, execve(path, argv, NULL)) 1610371183faSMickaël Salaün { 1611e1199815SMickaël Salaün TH_LOG("Failed to execute \"%s\": %s", path, 1612e1199815SMickaël Salaün strerror(errno)); 1613e1199815SMickaël Salaün }; 1614e1199815SMickaël Salaün ASSERT_EQ(err, errno); 1615e1199815SMickaël Salaün _exit(_metadata->passed ? 2 : 1); 1616e1199815SMickaël Salaün return; 1617e1199815SMickaël Salaün } 1618e1199815SMickaël Salaün ASSERT_EQ(child, waitpid(child, &status, 0)); 1619e1199815SMickaël Salaün ASSERT_EQ(1, WIFEXITED(status)); 1620371183faSMickaël Salaün ASSERT_EQ(err ? 2 : 0, WEXITSTATUS(status)) 1621371183faSMickaël Salaün { 1622e1199815SMickaël Salaün TH_LOG("Unexpected return code for \"%s\": %s", path, 1623e1199815SMickaël Salaün strerror(errno)); 1624e1199815SMickaël Salaün }; 1625e1199815SMickaël Salaün } 1626e1199815SMickaël Salaün 1627e1199815SMickaël Salaün TEST_F_FORK(layout1, execute) 1628e1199815SMickaël Salaün { 1629e1199815SMickaël Salaün const struct rule rules[] = { 1630e1199815SMickaël Salaün { 1631e1199815SMickaël Salaün .path = dir_s1d2, 1632e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_EXECUTE, 1633e1199815SMickaël Salaün }, 1634135464f9SMickaël Salaün {}, 1635e1199815SMickaël Salaün }; 1636371183faSMickaël Salaün const int ruleset_fd = 1637371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1638e1199815SMickaël Salaün 1639e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1640e1199815SMickaël Salaün copy_binary(_metadata, file1_s1d1); 1641e1199815SMickaël Salaün copy_binary(_metadata, file1_s1d2); 1642e1199815SMickaël Salaün copy_binary(_metadata, file1_s1d3); 1643e1199815SMickaël Salaün 1644e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1645e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1646e1199815SMickaël Salaün 1647e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 1648e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 1649e1199815SMickaël Salaün test_execute(_metadata, EACCES, file1_s1d1); 1650e1199815SMickaël Salaün 1651e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 1652e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 1653e1199815SMickaël Salaün test_execute(_metadata, 0, file1_s1d2); 1654e1199815SMickaël Salaün 1655e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 1656e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 1657e1199815SMickaël Salaün test_execute(_metadata, 0, file1_s1d3); 1658e1199815SMickaël Salaün } 1659e1199815SMickaël Salaün 1660e1199815SMickaël Salaün TEST_F_FORK(layout1, link) 1661e1199815SMickaël Salaün { 1662e1199815SMickaël Salaün const struct rule rules[] = { 1663e1199815SMickaël Salaün { 1664e1199815SMickaël Salaün .path = dir_s1d2, 1665e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_MAKE_REG, 1666e1199815SMickaël Salaün }, 1667135464f9SMickaël Salaün {}, 1668e1199815SMickaël Salaün }; 1669371183faSMickaël Salaün const int ruleset_fd = 1670371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1671e1199815SMickaël Salaün 1672e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1673e1199815SMickaël Salaün 1674e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 1675e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 1676e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 1677e1199815SMickaël Salaün 1678e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1679e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1680e1199815SMickaël Salaün 1681e1199815SMickaël Salaün ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 1682e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1683e1199815SMickaël Salaün /* Denies linking because of reparenting. */ 1684e1199815SMickaël Salaün ASSERT_EQ(-1, link(file1_s2d1, file1_s1d2)); 1685e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1686e1199815SMickaël Salaün ASSERT_EQ(-1, link(file2_s1d2, file1_s1d3)); 1687e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1688e1199815SMickaël Salaün 1689e1199815SMickaël Salaün ASSERT_EQ(0, link(file2_s1d2, file1_s1d2)); 1690e1199815SMickaël Salaün ASSERT_EQ(0, link(file2_s1d3, file1_s1d3)); 1691e1199815SMickaël Salaün } 1692e1199815SMickaël Salaün 1693e1199815SMickaël Salaün TEST_F_FORK(layout1, rename_file) 1694e1199815SMickaël Salaün { 1695e1199815SMickaël Salaün const struct rule rules[] = { 1696e1199815SMickaël Salaün { 1697e1199815SMickaël Salaün .path = dir_s1d3, 1698e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 1699e1199815SMickaël Salaün }, 1700e1199815SMickaël Salaün { 1701e1199815SMickaël Salaün .path = dir_s2d2, 1702e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 1703e1199815SMickaël Salaün }, 1704135464f9SMickaël Salaün {}, 1705e1199815SMickaël Salaün }; 1706371183faSMickaël Salaün const int ruleset_fd = 1707371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1708e1199815SMickaël Salaün 1709e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1710e1199815SMickaël Salaün 1711e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 1712e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 1713e1199815SMickaël Salaün 1714e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1715e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1716e1199815SMickaël Salaün 1717e1199815SMickaël Salaün /* 1718e1199815SMickaël Salaün * Tries to replace a file, from a directory that allows file removal, 1719e1199815SMickaël Salaün * but to a different directory (which also allows file removal). 1720e1199815SMickaël Salaün */ 1721e1199815SMickaël Salaün ASSERT_EQ(-1, rename(file1_s2d3, file1_s1d3)); 1722e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1723e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, file1_s1d3, 1724e1199815SMickaël Salaün RENAME_EXCHANGE)); 1725e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1726e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d3, AT_FDCWD, dir_s1d3, 1727e1199815SMickaël Salaün RENAME_EXCHANGE)); 1728e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1729e1199815SMickaël Salaün 1730e1199815SMickaël Salaün /* 1731e1199815SMickaël Salaün * Tries to replace a file, from a directory that denies file removal, 1732e1199815SMickaël Salaün * to a different directory (which allows file removal). 1733e1199815SMickaël Salaün */ 1734e1199815SMickaël Salaün ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3)); 1735e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1736e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, file1_s1d3, 1737e1199815SMickaël Salaün RENAME_EXCHANGE)); 1738e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1739e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s1d3, 1740e1199815SMickaël Salaün RENAME_EXCHANGE)); 1741e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1742e1199815SMickaël Salaün 1743e1199815SMickaël Salaün /* Exchanges files and directories that partially allow removal. */ 1744e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d2, AT_FDCWD, file1_s2d1, 1745e1199815SMickaël Salaün RENAME_EXCHANGE)); 1746e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1747e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d1, AT_FDCWD, dir_s2d2, 1748e1199815SMickaël Salaün RENAME_EXCHANGE)); 1749e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1750e1199815SMickaël Salaün 1751e1199815SMickaël Salaün /* Renames files with different parents. */ 1752e1199815SMickaël Salaün ASSERT_EQ(-1, rename(file1_s2d2, file1_s1d2)); 1753e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1754e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 1755e1199815SMickaël Salaün ASSERT_EQ(-1, rename(file1_s2d1, file1_s1d3)); 1756e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1757e1199815SMickaël Salaün 1758e1199815SMickaël Salaün /* Exchanges and renames files with same parent. */ 1759e1199815SMickaël Salaün ASSERT_EQ(0, renameat2(AT_FDCWD, file2_s2d3, AT_FDCWD, file1_s2d3, 1760e1199815SMickaël Salaün RENAME_EXCHANGE)); 1761e1199815SMickaël Salaün ASSERT_EQ(0, rename(file2_s2d3, file1_s2d3)); 1762e1199815SMickaël Salaün 1763e1199815SMickaël Salaün /* Exchanges files and directories with same parent, twice. */ 1764e1199815SMickaël Salaün ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3, 1765e1199815SMickaël Salaün RENAME_EXCHANGE)); 1766e1199815SMickaël Salaün ASSERT_EQ(0, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s2d3, 1767e1199815SMickaël Salaün RENAME_EXCHANGE)); 1768e1199815SMickaël Salaün } 1769e1199815SMickaël Salaün 1770e1199815SMickaël Salaün TEST_F_FORK(layout1, rename_dir) 1771e1199815SMickaël Salaün { 1772e1199815SMickaël Salaün const struct rule rules[] = { 1773e1199815SMickaël Salaün { 1774e1199815SMickaël Salaün .path = dir_s1d2, 1775e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_DIR, 1776e1199815SMickaël Salaün }, 1777e1199815SMickaël Salaün { 1778e1199815SMickaël Salaün .path = dir_s2d1, 1779e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_DIR, 1780e1199815SMickaël Salaün }, 1781135464f9SMickaël Salaün {}, 1782e1199815SMickaël Salaün }; 1783371183faSMickaël Salaün const int ruleset_fd = 1784371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1785e1199815SMickaël Salaün 1786e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1787e1199815SMickaël Salaün 1788e1199815SMickaël Salaün /* Empties dir_s1d3 to allow renaming. */ 1789e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 1790e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d3)); 1791e1199815SMickaël Salaün 1792e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1793e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1794e1199815SMickaël Salaün 1795e1199815SMickaël Salaün /* Exchanges and renames directory to a different parent. */ 1796e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s2d3, AT_FDCWD, dir_s1d3, 1797e1199815SMickaël Salaün RENAME_EXCHANGE)); 1798e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1799e1199815SMickaël Salaün ASSERT_EQ(-1, rename(dir_s2d3, dir_s1d3)); 1800e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1801e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s2d2, AT_FDCWD, dir_s1d3, 1802e1199815SMickaël Salaün RENAME_EXCHANGE)); 1803e1199815SMickaël Salaün ASSERT_EQ(EXDEV, errno); 1804e1199815SMickaël Salaün 1805e1199815SMickaël Salaün /* 1806e1199815SMickaël Salaün * Exchanges directory to the same parent, which doesn't allow 1807e1199815SMickaël Salaün * directory removal. 1808e1199815SMickaël Salaün */ 1809e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, dir_s1d1, AT_FDCWD, dir_s2d1, 1810e1199815SMickaël Salaün RENAME_EXCHANGE)); 1811e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1812e1199815SMickaël Salaün ASSERT_EQ(-1, renameat2(AT_FDCWD, file1_s1d1, AT_FDCWD, dir_s1d2, 1813e1199815SMickaël Salaün RENAME_EXCHANGE)); 1814e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1815e1199815SMickaël Salaün 1816e1199815SMickaël Salaün /* 1817e1199815SMickaël Salaün * Exchanges and renames directory to the same parent, which allows 1818e1199815SMickaël Salaün * directory removal. 1819e1199815SMickaël Salaün */ 1820e1199815SMickaël Salaün ASSERT_EQ(0, renameat2(AT_FDCWD, dir_s1d3, AT_FDCWD, file1_s1d2, 1821e1199815SMickaël Salaün RENAME_EXCHANGE)); 1822e1199815SMickaël Salaün ASSERT_EQ(0, unlink(dir_s1d3)); 1823e1199815SMickaël Salaün ASSERT_EQ(0, mkdir(dir_s1d3, 0700)); 1824e1199815SMickaël Salaün ASSERT_EQ(0, rename(file1_s1d2, dir_s1d3)); 1825e1199815SMickaël Salaün ASSERT_EQ(0, rmdir(dir_s1d3)); 1826e1199815SMickaël Salaün } 1827e1199815SMickaël Salaün 1828e1199815SMickaël Salaün TEST_F_FORK(layout1, remove_dir) 1829e1199815SMickaël Salaün { 1830e1199815SMickaël Salaün const struct rule rules[] = { 1831e1199815SMickaël Salaün { 1832e1199815SMickaël Salaün .path = dir_s1d2, 1833e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_DIR, 1834e1199815SMickaël Salaün }, 1835135464f9SMickaël Salaün {}, 1836e1199815SMickaël Salaün }; 1837371183faSMickaël Salaün const int ruleset_fd = 1838371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1839e1199815SMickaël Salaün 1840e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1841e1199815SMickaël Salaün 1842e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 1843e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 1844e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 1845e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d3)); 1846e1199815SMickaël Salaün 1847e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1848e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1849e1199815SMickaël Salaün 1850e1199815SMickaël Salaün ASSERT_EQ(0, rmdir(dir_s1d3)); 1851e1199815SMickaël Salaün ASSERT_EQ(0, mkdir(dir_s1d3, 0700)); 1852e1199815SMickaël Salaün ASSERT_EQ(0, unlinkat(AT_FDCWD, dir_s1d3, AT_REMOVEDIR)); 1853e1199815SMickaël Salaün 1854e1199815SMickaël Salaün /* dir_s1d2 itself cannot be removed. */ 1855e1199815SMickaël Salaün ASSERT_EQ(-1, rmdir(dir_s1d2)); 1856e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1857e1199815SMickaël Salaün ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d2, AT_REMOVEDIR)); 1858e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1859e1199815SMickaël Salaün ASSERT_EQ(-1, rmdir(dir_s1d1)); 1860e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1861e1199815SMickaël Salaün ASSERT_EQ(-1, unlinkat(AT_FDCWD, dir_s1d1, AT_REMOVEDIR)); 1862e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1863e1199815SMickaël Salaün } 1864e1199815SMickaël Salaün 1865e1199815SMickaël Salaün TEST_F_FORK(layout1, remove_file) 1866e1199815SMickaël Salaün { 1867e1199815SMickaël Salaün const struct rule rules[] = { 1868e1199815SMickaël Salaün { 1869e1199815SMickaël Salaün .path = dir_s1d2, 1870e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_REMOVE_FILE, 1871e1199815SMickaël Salaün }, 1872135464f9SMickaël Salaün {}, 1873e1199815SMickaël Salaün }; 1874371183faSMickaël Salaün const int ruleset_fd = 1875371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1876e1199815SMickaël Salaün 1877e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1878e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1879e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1880e1199815SMickaël Salaün 1881e1199815SMickaël Salaün ASSERT_EQ(-1, unlink(file1_s1d1)); 1882e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1883e1199815SMickaël Salaün ASSERT_EQ(-1, unlinkat(AT_FDCWD, file1_s1d1, 0)); 1884e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1885e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 1886e1199815SMickaël Salaün ASSERT_EQ(0, unlinkat(AT_FDCWD, file1_s1d3, 0)); 1887e1199815SMickaël Salaün } 1888e1199815SMickaël Salaün 1889e1199815SMickaël Salaün static void test_make_file(struct __test_metadata *const _metadata, 1890371183faSMickaël Salaün const __u64 access, const mode_t mode, 1891371183faSMickaël Salaün const dev_t dev) 1892e1199815SMickaël Salaün { 1893e1199815SMickaël Salaün const struct rule rules[] = { 1894e1199815SMickaël Salaün { 1895e1199815SMickaël Salaün .path = dir_s1d2, 1896e1199815SMickaël Salaün .access = access, 1897e1199815SMickaël Salaün }, 1898135464f9SMickaël Salaün {}, 1899e1199815SMickaël Salaün }; 1900e1199815SMickaël Salaün const int ruleset_fd = create_ruleset(_metadata, access, rules); 1901e1199815SMickaël Salaün 1902e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1903e1199815SMickaël Salaün 1904e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 1905e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d1)); 1906371183faSMickaël Salaün ASSERT_EQ(0, mknod(file2_s1d1, mode | 0400, dev)) 1907371183faSMickaël Salaün { 1908371183faSMickaël Salaün TH_LOG("Failed to make file \"%s\": %s", file2_s1d1, 1909371183faSMickaël Salaün strerror(errno)); 1910e1199815SMickaël Salaün }; 1911e1199815SMickaël Salaün 1912e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 1913e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d2)); 1914e1199815SMickaël Salaün 1915e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 1916e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d3)); 1917e1199815SMickaël Salaün 1918e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 1919e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 1920e1199815SMickaël Salaün 1921e1199815SMickaël Salaün ASSERT_EQ(-1, mknod(file1_s1d1, mode | 0400, dev)); 1922e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1923e1199815SMickaël Salaün ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 1924e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1925e1199815SMickaël Salaün ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1)); 1926e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 1927e1199815SMickaël Salaün 1928371183faSMickaël Salaün ASSERT_EQ(0, mknod(file1_s1d2, mode | 0400, dev)) 1929371183faSMickaël Salaün { 1930371183faSMickaël Salaün TH_LOG("Failed to make file \"%s\": %s", file1_s1d2, 1931371183faSMickaël Salaün strerror(errno)); 1932e1199815SMickaël Salaün }; 1933e1199815SMickaël Salaün ASSERT_EQ(0, link(file1_s1d2, file2_s1d2)); 1934e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d2)); 1935e1199815SMickaël Salaün ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2)); 1936e1199815SMickaël Salaün 1937e1199815SMickaël Salaün ASSERT_EQ(0, mknod(file1_s1d3, mode | 0400, dev)); 1938e1199815SMickaël Salaün ASSERT_EQ(0, link(file1_s1d3, file2_s1d3)); 1939e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d3)); 1940e1199815SMickaël Salaün ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3)); 1941e1199815SMickaël Salaün } 1942e1199815SMickaël Salaün 1943e1199815SMickaël Salaün TEST_F_FORK(layout1, make_char) 1944e1199815SMickaël Salaün { 1945e1199815SMickaël Salaün /* Creates a /dev/null device. */ 1946e1199815SMickaël Salaün set_cap(_metadata, CAP_MKNOD); 1947e1199815SMickaël Salaün test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_CHAR, S_IFCHR, 1948e1199815SMickaël Salaün makedev(1, 3)); 1949e1199815SMickaël Salaün } 1950e1199815SMickaël Salaün 1951e1199815SMickaël Salaün TEST_F_FORK(layout1, make_block) 1952e1199815SMickaël Salaün { 1953e1199815SMickaël Salaün /* Creates a /dev/loop0 device. */ 1954e1199815SMickaël Salaün set_cap(_metadata, CAP_MKNOD); 1955e1199815SMickaël Salaün test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_BLOCK, S_IFBLK, 1956e1199815SMickaël Salaün makedev(7, 0)); 1957e1199815SMickaël Salaün } 1958e1199815SMickaël Salaün 1959e1199815SMickaël Salaün TEST_F_FORK(layout1, make_reg_1) 1960e1199815SMickaël Salaün { 1961e1199815SMickaël Salaün test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, S_IFREG, 0); 1962e1199815SMickaël Salaün } 1963e1199815SMickaël Salaün 1964e1199815SMickaël Salaün TEST_F_FORK(layout1, make_reg_2) 1965e1199815SMickaël Salaün { 1966e1199815SMickaël Salaün test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_REG, 0, 0); 1967e1199815SMickaël Salaün } 1968e1199815SMickaël Salaün 1969e1199815SMickaël Salaün TEST_F_FORK(layout1, make_sock) 1970e1199815SMickaël Salaün { 1971e1199815SMickaël Salaün test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_SOCK, S_IFSOCK, 0); 1972e1199815SMickaël Salaün } 1973e1199815SMickaël Salaün 1974e1199815SMickaël Salaün TEST_F_FORK(layout1, make_fifo) 1975e1199815SMickaël Salaün { 1976e1199815SMickaël Salaün test_make_file(_metadata, LANDLOCK_ACCESS_FS_MAKE_FIFO, S_IFIFO, 0); 1977e1199815SMickaël Salaün } 1978e1199815SMickaël Salaün 1979e1199815SMickaël Salaün TEST_F_FORK(layout1, make_sym) 1980e1199815SMickaël Salaün { 1981e1199815SMickaël Salaün const struct rule rules[] = { 1982e1199815SMickaël Salaün { 1983e1199815SMickaël Salaün .path = dir_s1d2, 1984e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_MAKE_SYM, 1985e1199815SMickaël Salaün }, 1986135464f9SMickaël Salaün {}, 1987e1199815SMickaël Salaün }; 1988371183faSMickaël Salaün const int ruleset_fd = 1989371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 1990e1199815SMickaël Salaün 1991e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 1992e1199815SMickaël Salaün 1993e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 1994e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d1)); 1995e1199815SMickaël Salaün ASSERT_EQ(0, symlink("none", file2_s1d1)); 1996e1199815SMickaël Salaün 1997e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 1998e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d2)); 1999e1199815SMickaël Salaün 2000e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 2001e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d3)); 2002e1199815SMickaël Salaün 2003e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2004e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2005e1199815SMickaël Salaün 2006e1199815SMickaël Salaün ASSERT_EQ(-1, symlink("none", file1_s1d1)); 2007e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 2008e1199815SMickaël Salaün ASSERT_EQ(-1, link(file2_s1d1, file1_s1d1)); 2009e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 2010e1199815SMickaël Salaün ASSERT_EQ(-1, rename(file2_s1d1, file1_s1d1)); 2011e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 2012e1199815SMickaël Salaün 2013e1199815SMickaël Salaün ASSERT_EQ(0, symlink("none", file1_s1d2)); 2014e1199815SMickaël Salaün ASSERT_EQ(0, link(file1_s1d2, file2_s1d2)); 2015e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d2)); 2016e1199815SMickaël Salaün ASSERT_EQ(0, rename(file1_s1d2, file2_s1d2)); 2017e1199815SMickaël Salaün 2018e1199815SMickaël Salaün ASSERT_EQ(0, symlink("none", file1_s1d3)); 2019e1199815SMickaël Salaün ASSERT_EQ(0, link(file1_s1d3, file2_s1d3)); 2020e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file2_s1d3)); 2021e1199815SMickaël Salaün ASSERT_EQ(0, rename(file1_s1d3, file2_s1d3)); 2022e1199815SMickaël Salaün } 2023e1199815SMickaël Salaün 2024e1199815SMickaël Salaün TEST_F_FORK(layout1, make_dir) 2025e1199815SMickaël Salaün { 2026e1199815SMickaël Salaün const struct rule rules[] = { 2027e1199815SMickaël Salaün { 2028e1199815SMickaël Salaün .path = dir_s1d2, 2029e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_MAKE_DIR, 2030e1199815SMickaël Salaün }, 2031135464f9SMickaël Salaün {}, 2032e1199815SMickaël Salaün }; 2033371183faSMickaël Salaün const int ruleset_fd = 2034371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 2035e1199815SMickaël Salaün 2036e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2037e1199815SMickaël Salaün 2038e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d1)); 2039e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 2040e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d3)); 2041e1199815SMickaël Salaün 2042e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2043e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2044e1199815SMickaël Salaün 2045e1199815SMickaël Salaün /* Uses file_* as directory names. */ 2046e1199815SMickaël Salaün ASSERT_EQ(-1, mkdir(file1_s1d1, 0700)); 2047e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 2048e1199815SMickaël Salaün ASSERT_EQ(0, mkdir(file1_s1d2, 0700)); 2049e1199815SMickaël Salaün ASSERT_EQ(0, mkdir(file1_s1d3, 0700)); 2050e1199815SMickaël Salaün } 2051e1199815SMickaël Salaün 2052e1199815SMickaël Salaün static int open_proc_fd(struct __test_metadata *const _metadata, const int fd, 2053e1199815SMickaël Salaün const int open_flags) 2054e1199815SMickaël Salaün { 2055e1199815SMickaël Salaün static const char path_template[] = "/proc/self/fd/%d"; 2056e1199815SMickaël Salaün char procfd_path[sizeof(path_template) + 10]; 2057371183faSMickaël Salaün const int procfd_path_size = 2058371183faSMickaël Salaün snprintf(procfd_path, sizeof(procfd_path), path_template, fd); 2059e1199815SMickaël Salaün 2060e1199815SMickaël Salaün ASSERT_LT(procfd_path_size, sizeof(procfd_path)); 2061e1199815SMickaël Salaün return open(procfd_path, open_flags); 2062e1199815SMickaël Salaün } 2063e1199815SMickaël Salaün 2064e1199815SMickaël Salaün TEST_F_FORK(layout1, proc_unlinked_file) 2065e1199815SMickaël Salaün { 2066e1199815SMickaël Salaün const struct rule rules[] = { 2067e1199815SMickaël Salaün { 2068e1199815SMickaël Salaün .path = file1_s1d2, 2069e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2070e1199815SMickaël Salaün }, 2071135464f9SMickaël Salaün {}, 2072e1199815SMickaël Salaün }; 2073e1199815SMickaël Salaün int reg_fd, proc_fd; 2074371183faSMickaël Salaün const int ruleset_fd = create_ruleset( 2075371183faSMickaël Salaün _metadata, 2076371183faSMickaël Salaün LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, 2077371183faSMickaël Salaün rules); 2078e1199815SMickaël Salaün 2079e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2080e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2081e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2082e1199815SMickaël Salaün 2083e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDWR)); 2084e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 2085e1199815SMickaël Salaün reg_fd = open(file1_s1d2, O_RDONLY | O_CLOEXEC); 2086e1199815SMickaël Salaün ASSERT_LE(0, reg_fd); 2087e1199815SMickaël Salaün ASSERT_EQ(0, unlink(file1_s1d2)); 2088e1199815SMickaël Salaün 2089e1199815SMickaël Salaün proc_fd = open_proc_fd(_metadata, reg_fd, O_RDONLY | O_CLOEXEC); 2090e1199815SMickaël Salaün ASSERT_LE(0, proc_fd); 2091e1199815SMickaël Salaün ASSERT_EQ(0, close(proc_fd)); 2092e1199815SMickaël Salaün 2093e1199815SMickaël Salaün proc_fd = open_proc_fd(_metadata, reg_fd, O_RDWR | O_CLOEXEC); 2094371183faSMickaël Salaün ASSERT_EQ(-1, proc_fd) 2095371183faSMickaël Salaün { 2096371183faSMickaël Salaün TH_LOG("Successfully opened /proc/self/fd/%d: %s", reg_fd, 2097371183faSMickaël Salaün strerror(errno)); 2098e1199815SMickaël Salaün } 2099e1199815SMickaël Salaün ASSERT_EQ(EACCES, errno); 2100e1199815SMickaël Salaün 2101e1199815SMickaël Salaün ASSERT_EQ(0, close(reg_fd)); 2102e1199815SMickaël Salaün } 2103e1199815SMickaël Salaün 2104e1199815SMickaël Salaün TEST_F_FORK(layout1, proc_pipe) 2105e1199815SMickaël Salaün { 2106e1199815SMickaël Salaün int proc_fd; 2107e1199815SMickaël Salaün int pipe_fds[2]; 2108e1199815SMickaël Salaün char buf = '\0'; 2109e1199815SMickaël Salaün const struct rule rules[] = { 2110e1199815SMickaël Salaün { 2111e1199815SMickaël Salaün .path = dir_s1d2, 2112e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2113e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2114e1199815SMickaël Salaün }, 2115135464f9SMickaël Salaün {}, 2116e1199815SMickaël Salaün }; 2117e1199815SMickaël Salaün /* Limits read and write access to files tied to the filesystem. */ 2118371183faSMickaël Salaün const int ruleset_fd = 2119371183faSMickaël Salaün create_ruleset(_metadata, rules[0].access, rules); 2120e1199815SMickaël Salaün 2121e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2122e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2123e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2124e1199815SMickaël Salaün 2125e1199815SMickaël Salaün /* Checks enforcement for normal files. */ 2126e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDWR)); 2127e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDWR)); 2128e1199815SMickaël Salaün 2129e1199815SMickaël Salaün /* Checks access to pipes through FD. */ 2130e1199815SMickaël Salaün ASSERT_EQ(0, pipe2(pipe_fds, O_CLOEXEC)); 2131371183faSMickaël Salaün ASSERT_EQ(1, write(pipe_fds[1], ".", 1)) 2132371183faSMickaël Salaün { 2133e1199815SMickaël Salaün TH_LOG("Failed to write in pipe: %s", strerror(errno)); 2134e1199815SMickaël Salaün } 2135e1199815SMickaël Salaün ASSERT_EQ(1, read(pipe_fds[0], &buf, 1)); 2136e1199815SMickaël Salaün ASSERT_EQ('.', buf); 2137e1199815SMickaël Salaün 2138e1199815SMickaël Salaün /* Checks write access to pipe through /proc/self/fd . */ 2139e1199815SMickaël Salaün proc_fd = open_proc_fd(_metadata, pipe_fds[1], O_WRONLY | O_CLOEXEC); 2140e1199815SMickaël Salaün ASSERT_LE(0, proc_fd); 2141371183faSMickaël Salaün ASSERT_EQ(1, write(proc_fd, ".", 1)) 2142371183faSMickaël Salaün { 2143e1199815SMickaël Salaün TH_LOG("Failed to write through /proc/self/fd/%d: %s", 2144e1199815SMickaël Salaün pipe_fds[1], strerror(errno)); 2145e1199815SMickaël Salaün } 2146e1199815SMickaël Salaün ASSERT_EQ(0, close(proc_fd)); 2147e1199815SMickaël Salaün 2148e1199815SMickaël Salaün /* Checks read access to pipe through /proc/self/fd . */ 2149e1199815SMickaël Salaün proc_fd = open_proc_fd(_metadata, pipe_fds[0], O_RDONLY | O_CLOEXEC); 2150e1199815SMickaël Salaün ASSERT_LE(0, proc_fd); 2151e1199815SMickaël Salaün buf = '\0'; 2152371183faSMickaël Salaün ASSERT_EQ(1, read(proc_fd, &buf, 1)) 2153371183faSMickaël Salaün { 2154e1199815SMickaël Salaün TH_LOG("Failed to read through /proc/self/fd/%d: %s", 2155e1199815SMickaël Salaün pipe_fds[1], strerror(errno)); 2156e1199815SMickaël Salaün } 2157e1199815SMickaël Salaün ASSERT_EQ(0, close(proc_fd)); 2158e1199815SMickaël Salaün 2159e1199815SMickaël Salaün ASSERT_EQ(0, close(pipe_fds[0])); 2160e1199815SMickaël Salaün ASSERT_EQ(0, close(pipe_fds[1])); 2161e1199815SMickaël Salaün } 2162e1199815SMickaël Salaün 21634598d9abSMickaël Salaün /* clang-format off */ 21644598d9abSMickaël Salaün FIXTURE(layout1_bind) {}; 21654598d9abSMickaël Salaün /* clang-format on */ 2166e1199815SMickaël Salaün 2167e1199815SMickaël Salaün FIXTURE_SETUP(layout1_bind) 2168e1199815SMickaël Salaün { 2169e1199815SMickaël Salaün prepare_layout(_metadata); 2170e1199815SMickaël Salaün 2171e1199815SMickaël Salaün create_layout1(_metadata); 2172e1199815SMickaël Salaün 2173e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2174e1199815SMickaël Salaün ASSERT_EQ(0, mount(dir_s1d2, dir_s2d2, NULL, MS_BIND, NULL)); 2175e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2176e1199815SMickaël Salaün } 2177e1199815SMickaël Salaün 2178e1199815SMickaël Salaün FIXTURE_TEARDOWN(layout1_bind) 2179e1199815SMickaël Salaün { 2180e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2181e1199815SMickaël Salaün EXPECT_EQ(0, umount(dir_s2d2)); 2182e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2183e1199815SMickaël Salaün 2184e1199815SMickaël Salaün remove_layout1(_metadata); 2185e1199815SMickaël Salaün 2186e1199815SMickaël Salaün cleanup_layout(_metadata); 2187e1199815SMickaël Salaün } 2188e1199815SMickaël Salaün 2189e1199815SMickaël Salaün static const char bind_dir_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3"; 2190e1199815SMickaël Salaün static const char bind_file1_s1d3[] = TMP_DIR "/s2d1/s2d2/s1d3/f1"; 2191e1199815SMickaël Salaün 2192e1199815SMickaël Salaün /* 2193e1199815SMickaël Salaün * layout1_bind hierarchy: 2194e1199815SMickaël Salaün * 2195e1199815SMickaël Salaün * tmp 2196e1199815SMickaël Salaün * ├── s1d1 2197e1199815SMickaël Salaün * │ ├── f1 2198e1199815SMickaël Salaün * │ ├── f2 2199e1199815SMickaël Salaün * │ └── s1d2 2200e1199815SMickaël Salaün * │ ├── f1 2201e1199815SMickaël Salaün * │ ├── f2 2202e1199815SMickaël Salaün * │ └── s1d3 2203e1199815SMickaël Salaün * │ ├── f1 2204e1199815SMickaël Salaün * │ └── f2 2205e1199815SMickaël Salaün * ├── s2d1 2206e1199815SMickaël Salaün * │ ├── f1 2207e1199815SMickaël Salaün * │ └── s2d2 2208e1199815SMickaël Salaün * │ ├── f1 2209e1199815SMickaël Salaün * │ ├── f2 2210e1199815SMickaël Salaün * │ └── s1d3 2211e1199815SMickaël Salaün * │ ├── f1 2212e1199815SMickaël Salaün * │ └── f2 2213e1199815SMickaël Salaün * └── s3d1 2214e1199815SMickaël Salaün * └── s3d2 2215e1199815SMickaël Salaün * └── s3d3 2216e1199815SMickaël Salaün */ 2217e1199815SMickaël Salaün 2218e1199815SMickaël Salaün TEST_F_FORK(layout1_bind, no_restriction) 2219e1199815SMickaël Salaün { 2220e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY)); 2221e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 2222e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY)); 2223e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 2224e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d3, O_RDONLY)); 2225e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 2226e1199815SMickaël Salaün 2227e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY)); 2228e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d1, O_RDONLY)); 2229e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY)); 2230e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d2, O_RDONLY)); 2231e1199815SMickaël Salaün ASSERT_EQ(ENOENT, test_open(dir_s2d3, O_RDONLY)); 2232e1199815SMickaël Salaün ASSERT_EQ(ENOENT, test_open(file1_s2d3, O_RDONLY)); 2233e1199815SMickaël Salaün 2234e1199815SMickaël Salaün ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY)); 2235e1199815SMickaël Salaün ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY)); 2236e1199815SMickaël Salaün 2237e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s3d1, O_RDONLY)); 2238e1199815SMickaël Salaün } 2239e1199815SMickaël Salaün 2240e1199815SMickaël Salaün TEST_F_FORK(layout1_bind, same_content_same_file) 2241e1199815SMickaël Salaün { 2242e1199815SMickaël Salaün /* 2243e1199815SMickaël Salaün * Sets access right on parent directories of both source and 2244e1199815SMickaël Salaün * destination mount points. 2245e1199815SMickaël Salaün */ 2246e1199815SMickaël Salaün const struct rule layer1_parent[] = { 2247e1199815SMickaël Salaün { 2248e1199815SMickaël Salaün .path = dir_s1d1, 2249e1199815SMickaël Salaün .access = ACCESS_RO, 2250e1199815SMickaël Salaün }, 2251e1199815SMickaël Salaün { 2252e1199815SMickaël Salaün .path = dir_s2d1, 2253e1199815SMickaël Salaün .access = ACCESS_RW, 2254e1199815SMickaël Salaün }, 2255135464f9SMickaël Salaün {}, 2256e1199815SMickaël Salaün }; 2257e1199815SMickaël Salaün /* 2258e1199815SMickaël Salaün * Sets access rights on the same bind-mounted directories. The result 2259e1199815SMickaël Salaün * should be ACCESS_RW for both directories, but not both hierarchies 2260e1199815SMickaël Salaün * because of the first layer. 2261e1199815SMickaël Salaün */ 2262e1199815SMickaël Salaün const struct rule layer2_mount_point[] = { 2263e1199815SMickaël Salaün { 2264e1199815SMickaël Salaün .path = dir_s1d2, 2265e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2266e1199815SMickaël Salaün }, 2267e1199815SMickaël Salaün { 2268e1199815SMickaël Salaün .path = dir_s2d2, 2269e1199815SMickaël Salaün .access = ACCESS_RW, 2270e1199815SMickaël Salaün }, 2271135464f9SMickaël Salaün {}, 2272e1199815SMickaël Salaün }; 2273e1199815SMickaël Salaün /* Only allow read-access to the s1d3 hierarchies. */ 2274e1199815SMickaël Salaün const struct rule layer3_source[] = { 2275e1199815SMickaël Salaün { 2276e1199815SMickaël Salaün .path = dir_s1d3, 2277e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2278e1199815SMickaël Salaün }, 2279135464f9SMickaël Salaün {}, 2280e1199815SMickaël Salaün }; 2281e1199815SMickaël Salaün /* Removes all access rights. */ 2282e1199815SMickaël Salaün const struct rule layer4_destination[] = { 2283e1199815SMickaël Salaün { 2284e1199815SMickaël Salaün .path = bind_file1_s1d3, 2285e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_WRITE_FILE, 2286e1199815SMickaël Salaün }, 2287135464f9SMickaël Salaün {}, 2288e1199815SMickaël Salaün }; 2289e1199815SMickaël Salaün int ruleset_fd; 2290e1199815SMickaël Salaün 2291e1199815SMickaël Salaün /* Sets rules for the parent directories. */ 2292e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_parent); 2293e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2294e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2295e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2296e1199815SMickaël Salaün 2297e1199815SMickaël Salaün /* Checks source hierarchy. */ 2298e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d1, O_RDONLY)); 2299e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 2300e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 2301e1199815SMickaël Salaün 2302e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 2303e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 2304e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 2305e1199815SMickaël Salaün 2306e1199815SMickaël Salaün /* Checks destination hierarchy. */ 2307e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d1, O_RDWR)); 2308e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY)); 2309e1199815SMickaël Salaün 2310e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR)); 2311e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY)); 2312e1199815SMickaël Salaün 2313e1199815SMickaël Salaün /* Sets rules for the mount points. */ 2314e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_mount_point); 2315e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2316e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2317e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2318e1199815SMickaël Salaün 2319e1199815SMickaël Salaün /* Checks source hierarchy. */ 2320e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_RDONLY)); 2321e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d1, O_WRONLY)); 2322e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d1, O_RDONLY | O_DIRECTORY)); 2323e1199815SMickaël Salaün 2324e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d2, O_RDONLY)); 2325e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 2326e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 2327e1199815SMickaël Salaün 2328e1199815SMickaël Salaün /* Checks destination hierarchy. */ 2329e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s2d1, O_RDONLY)); 2330e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s2d1, O_WRONLY)); 2331e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s2d1, O_RDONLY | O_DIRECTORY)); 2332e1199815SMickaël Salaün 2333e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s2d2, O_RDWR)); 2334e1199815SMickaël Salaün ASSERT_EQ(0, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY)); 2335e1199815SMickaël Salaün ASSERT_EQ(0, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY)); 2336e1199815SMickaël Salaün 2337e1199815SMickaël Salaün /* Sets a (shared) rule only on the source. */ 2338e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3_source); 2339e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2340e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2341e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2342e1199815SMickaël Salaün 2343e1199815SMickaël Salaün /* Checks source hierarchy. */ 2344e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_RDONLY)); 2345e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d2, O_WRONLY)); 2346e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d2, O_RDONLY | O_DIRECTORY)); 2347e1199815SMickaël Salaün 2348e1199815SMickaël Salaün ASSERT_EQ(0, test_open(file1_s1d3, O_RDONLY)); 2349e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 2350e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s1d3, O_RDONLY | O_DIRECTORY)); 2351e1199815SMickaël Salaün 2352e1199815SMickaël Salaün /* Checks destination hierarchy. */ 2353e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s2d2, O_RDONLY)); 2354e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s2d2, O_WRONLY)); 2355e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(dir_s2d2, O_RDONLY | O_DIRECTORY)); 2356e1199815SMickaël Salaün 2357e1199815SMickaël Salaün ASSERT_EQ(0, test_open(bind_file1_s1d3, O_RDONLY)); 2358e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY)); 2359e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(bind_dir_s1d3, O_RDONLY | O_DIRECTORY)); 2360e1199815SMickaël Salaün 2361e1199815SMickaël Salaün /* Sets a (shared) rule only on the destination. */ 2362e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer4_destination); 2363e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2364e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2365e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2366e1199815SMickaël Salaün 2367e1199815SMickaël Salaün /* Checks source hierarchy. */ 2368e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_RDONLY)); 2369e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(file1_s1d3, O_WRONLY)); 2370e1199815SMickaël Salaün 2371e1199815SMickaël Salaün /* Checks destination hierarchy. */ 2372e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_RDONLY)); 2373e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(bind_file1_s1d3, O_WRONLY)); 2374e1199815SMickaël Salaün } 2375e1199815SMickaël Salaün 2376e1199815SMickaël Salaün #define LOWER_BASE TMP_DIR "/lower" 2377e1199815SMickaël Salaün #define LOWER_DATA LOWER_BASE "/data" 2378e1199815SMickaël Salaün static const char lower_fl1[] = LOWER_DATA "/fl1"; 2379e1199815SMickaël Salaün static const char lower_dl1[] = LOWER_DATA "/dl1"; 2380e1199815SMickaël Salaün static const char lower_dl1_fl2[] = LOWER_DATA "/dl1/fl2"; 2381e1199815SMickaël Salaün static const char lower_fo1[] = LOWER_DATA "/fo1"; 2382e1199815SMickaël Salaün static const char lower_do1[] = LOWER_DATA "/do1"; 2383e1199815SMickaël Salaün static const char lower_do1_fo2[] = LOWER_DATA "/do1/fo2"; 2384e1199815SMickaël Salaün static const char lower_do1_fl3[] = LOWER_DATA "/do1/fl3"; 2385e1199815SMickaël Salaün 2386e1199815SMickaël Salaün static const char (*lower_base_files[])[] = { 2387e1199815SMickaël Salaün &lower_fl1, 2388e1199815SMickaël Salaün &lower_fo1, 2389135464f9SMickaël Salaün NULL, 2390e1199815SMickaël Salaün }; 2391e1199815SMickaël Salaün static const char (*lower_base_directories[])[] = { 2392e1199815SMickaël Salaün &lower_dl1, 2393e1199815SMickaël Salaün &lower_do1, 2394135464f9SMickaël Salaün NULL, 2395e1199815SMickaël Salaün }; 2396e1199815SMickaël Salaün static const char (*lower_sub_files[])[] = { 2397e1199815SMickaël Salaün &lower_dl1_fl2, 2398e1199815SMickaël Salaün &lower_do1_fo2, 2399e1199815SMickaël Salaün &lower_do1_fl3, 2400135464f9SMickaël Salaün NULL, 2401e1199815SMickaël Salaün }; 2402e1199815SMickaël Salaün 2403e1199815SMickaël Salaün #define UPPER_BASE TMP_DIR "/upper" 2404e1199815SMickaël Salaün #define UPPER_DATA UPPER_BASE "/data" 2405e1199815SMickaël Salaün #define UPPER_WORK UPPER_BASE "/work" 2406e1199815SMickaël Salaün static const char upper_fu1[] = UPPER_DATA "/fu1"; 2407e1199815SMickaël Salaün static const char upper_du1[] = UPPER_DATA "/du1"; 2408e1199815SMickaël Salaün static const char upper_du1_fu2[] = UPPER_DATA "/du1/fu2"; 2409e1199815SMickaël Salaün static const char upper_fo1[] = UPPER_DATA "/fo1"; 2410e1199815SMickaël Salaün static const char upper_do1[] = UPPER_DATA "/do1"; 2411e1199815SMickaël Salaün static const char upper_do1_fo2[] = UPPER_DATA "/do1/fo2"; 2412e1199815SMickaël Salaün static const char upper_do1_fu3[] = UPPER_DATA "/do1/fu3"; 2413e1199815SMickaël Salaün 2414e1199815SMickaël Salaün static const char (*upper_base_files[])[] = { 2415e1199815SMickaël Salaün &upper_fu1, 2416e1199815SMickaël Salaün &upper_fo1, 2417135464f9SMickaël Salaün NULL, 2418e1199815SMickaël Salaün }; 2419e1199815SMickaël Salaün static const char (*upper_base_directories[])[] = { 2420e1199815SMickaël Salaün &upper_du1, 2421e1199815SMickaël Salaün &upper_do1, 2422135464f9SMickaël Salaün NULL, 2423e1199815SMickaël Salaün }; 2424e1199815SMickaël Salaün static const char (*upper_sub_files[])[] = { 2425e1199815SMickaël Salaün &upper_du1_fu2, 2426e1199815SMickaël Salaün &upper_do1_fo2, 2427e1199815SMickaël Salaün &upper_do1_fu3, 2428135464f9SMickaël Salaün NULL, 2429e1199815SMickaël Salaün }; 2430e1199815SMickaël Salaün 2431e1199815SMickaël Salaün #define MERGE_BASE TMP_DIR "/merge" 2432e1199815SMickaël Salaün #define MERGE_DATA MERGE_BASE "/data" 2433e1199815SMickaël Salaün static const char merge_fl1[] = MERGE_DATA "/fl1"; 2434e1199815SMickaël Salaün static const char merge_dl1[] = MERGE_DATA "/dl1"; 2435e1199815SMickaël Salaün static const char merge_dl1_fl2[] = MERGE_DATA "/dl1/fl2"; 2436e1199815SMickaël Salaün static const char merge_fu1[] = MERGE_DATA "/fu1"; 2437e1199815SMickaël Salaün static const char merge_du1[] = MERGE_DATA "/du1"; 2438e1199815SMickaël Salaün static const char merge_du1_fu2[] = MERGE_DATA "/du1/fu2"; 2439e1199815SMickaël Salaün static const char merge_fo1[] = MERGE_DATA "/fo1"; 2440e1199815SMickaël Salaün static const char merge_do1[] = MERGE_DATA "/do1"; 2441e1199815SMickaël Salaün static const char merge_do1_fo2[] = MERGE_DATA "/do1/fo2"; 2442e1199815SMickaël Salaün static const char merge_do1_fl3[] = MERGE_DATA "/do1/fl3"; 2443e1199815SMickaël Salaün static const char merge_do1_fu3[] = MERGE_DATA "/do1/fu3"; 2444e1199815SMickaël Salaün 2445e1199815SMickaël Salaün static const char (*merge_base_files[])[] = { 2446e1199815SMickaël Salaün &merge_fl1, 2447e1199815SMickaël Salaün &merge_fu1, 2448e1199815SMickaël Salaün &merge_fo1, 2449135464f9SMickaël Salaün NULL, 2450e1199815SMickaël Salaün }; 2451e1199815SMickaël Salaün static const char (*merge_base_directories[])[] = { 2452e1199815SMickaël Salaün &merge_dl1, 2453e1199815SMickaël Salaün &merge_du1, 2454e1199815SMickaël Salaün &merge_do1, 2455135464f9SMickaël Salaün NULL, 2456e1199815SMickaël Salaün }; 2457e1199815SMickaël Salaün static const char (*merge_sub_files[])[] = { 2458371183faSMickaël Salaün &merge_dl1_fl2, &merge_du1_fu2, &merge_do1_fo2, 2459371183faSMickaël Salaün &merge_do1_fl3, &merge_do1_fu3, NULL, 2460e1199815SMickaël Salaün }; 2461e1199815SMickaël Salaün 2462e1199815SMickaël Salaün /* 2463e1199815SMickaël Salaün * layout2_overlay hierarchy: 2464e1199815SMickaël Salaün * 2465e1199815SMickaël Salaün * tmp 2466e1199815SMickaël Salaün * ├── lower 2467e1199815SMickaël Salaün * │ └── data 2468e1199815SMickaël Salaün * │ ├── dl1 2469e1199815SMickaël Salaün * │ │ └── fl2 2470e1199815SMickaël Salaün * │ ├── do1 2471e1199815SMickaël Salaün * │ │ ├── fl3 2472e1199815SMickaël Salaün * │ │ └── fo2 2473e1199815SMickaël Salaün * │ ├── fl1 2474e1199815SMickaël Salaün * │ └── fo1 2475e1199815SMickaël Salaün * ├── merge 2476e1199815SMickaël Salaün * │ └── data 2477e1199815SMickaël Salaün * │ ├── dl1 2478e1199815SMickaël Salaün * │ │ └── fl2 2479e1199815SMickaël Salaün * │ ├── do1 2480e1199815SMickaël Salaün * │ │ ├── fl3 2481e1199815SMickaël Salaün * │ │ ├── fo2 2482e1199815SMickaël Salaün * │ │ └── fu3 2483e1199815SMickaël Salaün * │ ├── du1 2484e1199815SMickaël Salaün * │ │ └── fu2 2485e1199815SMickaël Salaün * │ ├── fl1 2486e1199815SMickaël Salaün * │ ├── fo1 2487e1199815SMickaël Salaün * │ └── fu1 2488e1199815SMickaël Salaün * └── upper 2489e1199815SMickaël Salaün * ├── data 2490e1199815SMickaël Salaün * │ ├── do1 2491e1199815SMickaël Salaün * │ │ ├── fo2 2492e1199815SMickaël Salaün * │ │ └── fu3 2493e1199815SMickaël Salaün * │ ├── du1 2494e1199815SMickaël Salaün * │ │ └── fu2 2495e1199815SMickaël Salaün * │ ├── fo1 2496e1199815SMickaël Salaün * │ └── fu1 2497e1199815SMickaël Salaün * └── work 2498e1199815SMickaël Salaün * └── work 2499e1199815SMickaël Salaün */ 2500e1199815SMickaël Salaün 25014598d9abSMickaël Salaün /* clang-format off */ 25024598d9abSMickaël Salaün FIXTURE(layout2_overlay) {}; 25034598d9abSMickaël Salaün /* clang-format on */ 2504e1199815SMickaël Salaün 2505e1199815SMickaël Salaün FIXTURE_SETUP(layout2_overlay) 2506e1199815SMickaël Salaün { 2507e1199815SMickaël Salaün prepare_layout(_metadata); 2508e1199815SMickaël Salaün 2509e1199815SMickaël Salaün create_directory(_metadata, LOWER_BASE); 2510e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2511e1199815SMickaël Salaün /* Creates tmpfs mount points to get deterministic overlayfs. */ 2512e1199815SMickaël Salaün ASSERT_EQ(0, mount("tmp", LOWER_BASE, "tmpfs", 0, "size=4m,mode=700")); 2513e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2514e1199815SMickaël Salaün create_file(_metadata, lower_fl1); 2515e1199815SMickaël Salaün create_file(_metadata, lower_dl1_fl2); 2516e1199815SMickaël Salaün create_file(_metadata, lower_fo1); 2517e1199815SMickaël Salaün create_file(_metadata, lower_do1_fo2); 2518e1199815SMickaël Salaün create_file(_metadata, lower_do1_fl3); 2519e1199815SMickaël Salaün 2520e1199815SMickaël Salaün create_directory(_metadata, UPPER_BASE); 2521e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2522e1199815SMickaël Salaün ASSERT_EQ(0, mount("tmp", UPPER_BASE, "tmpfs", 0, "size=4m,mode=700")); 2523e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2524e1199815SMickaël Salaün create_file(_metadata, upper_fu1); 2525e1199815SMickaël Salaün create_file(_metadata, upper_du1_fu2); 2526e1199815SMickaël Salaün create_file(_metadata, upper_fo1); 2527e1199815SMickaël Salaün create_file(_metadata, upper_do1_fo2); 2528e1199815SMickaël Salaün create_file(_metadata, upper_do1_fu3); 2529e1199815SMickaël Salaün ASSERT_EQ(0, mkdir(UPPER_WORK, 0700)); 2530e1199815SMickaël Salaün 2531e1199815SMickaël Salaün create_directory(_metadata, MERGE_DATA); 2532e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2533e1199815SMickaël Salaün set_cap(_metadata, CAP_DAC_OVERRIDE); 2534e1199815SMickaël Salaün ASSERT_EQ(0, mount("overlay", MERGE_DATA, "overlay", 0, 2535371183faSMickaël Salaün "lowerdir=" LOWER_DATA ",upperdir=" UPPER_DATA 2536e1199815SMickaël Salaün ",workdir=" UPPER_WORK)); 2537e1199815SMickaël Salaün clear_cap(_metadata, CAP_DAC_OVERRIDE); 2538e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2539e1199815SMickaël Salaün } 2540e1199815SMickaël Salaün 2541e1199815SMickaël Salaün FIXTURE_TEARDOWN(layout2_overlay) 2542e1199815SMickaël Salaün { 2543e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(lower_do1_fl3)); 2544e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(lower_dl1_fl2)); 2545e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(lower_fl1)); 2546e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(lower_do1_fo2)); 2547e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(lower_fo1)); 2548e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2549e1199815SMickaël Salaün EXPECT_EQ(0, umount(LOWER_BASE)); 2550e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2551e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(LOWER_BASE)); 2552e1199815SMickaël Salaün 2553e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(upper_do1_fu3)); 2554e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(upper_du1_fu2)); 2555e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(upper_fu1)); 2556e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(upper_do1_fo2)); 2557e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(upper_fo1)); 2558e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(UPPER_WORK "/work")); 2559e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2560e1199815SMickaël Salaün EXPECT_EQ(0, umount(UPPER_BASE)); 2561e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2562e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(UPPER_BASE)); 2563e1199815SMickaël Salaün 2564e1199815SMickaël Salaün set_cap(_metadata, CAP_SYS_ADMIN); 2565e1199815SMickaël Salaün EXPECT_EQ(0, umount(MERGE_DATA)); 2566e1199815SMickaël Salaün clear_cap(_metadata, CAP_SYS_ADMIN); 2567e1199815SMickaël Salaün EXPECT_EQ(0, remove_path(MERGE_DATA)); 2568e1199815SMickaël Salaün 2569e1199815SMickaël Salaün cleanup_layout(_metadata); 2570e1199815SMickaël Salaün } 2571e1199815SMickaël Salaün 2572e1199815SMickaël Salaün TEST_F_FORK(layout2_overlay, no_restriction) 2573e1199815SMickaël Salaün { 2574e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_fl1, O_RDONLY)); 2575e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_dl1, O_RDONLY)); 2576e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_dl1_fl2, O_RDONLY)); 2577e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_fo1, O_RDONLY)); 2578e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_do1, O_RDONLY)); 2579e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_do1_fo2, O_RDONLY)); 2580e1199815SMickaël Salaün ASSERT_EQ(0, test_open(lower_do1_fl3, O_RDONLY)); 2581e1199815SMickaël Salaün 2582e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_fu1, O_RDONLY)); 2583e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_du1, O_RDONLY)); 2584e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_du1_fu2, O_RDONLY)); 2585e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_fo1, O_RDONLY)); 2586e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_do1, O_RDONLY)); 2587e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_do1_fo2, O_RDONLY)); 2588e1199815SMickaël Salaün ASSERT_EQ(0, test_open(upper_do1_fu3, O_RDONLY)); 2589e1199815SMickaël Salaün 2590e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_fl1, O_RDONLY)); 2591e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_dl1, O_RDONLY)); 2592e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_dl1_fl2, O_RDONLY)); 2593e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_fu1, O_RDONLY)); 2594e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_du1, O_RDONLY)); 2595e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_du1_fu2, O_RDONLY)); 2596e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_fo1, O_RDONLY)); 2597e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_do1, O_RDONLY)); 2598e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_do1_fo2, O_RDONLY)); 2599e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_do1_fl3, O_RDONLY)); 2600e1199815SMickaël Salaün ASSERT_EQ(0, test_open(merge_do1_fu3, O_RDONLY)); 2601e1199815SMickaël Salaün } 2602e1199815SMickaël Salaün 2603e1199815SMickaël Salaün #define for_each_path(path_list, path_entry, i) \ 2604e1199815SMickaël Salaün for (i = 0, path_entry = *path_list[i]; path_list[i]; \ 2605e1199815SMickaël Salaün path_entry = *path_list[++i]) 2606e1199815SMickaël Salaün 2607e1199815SMickaël Salaün TEST_F_FORK(layout2_overlay, same_content_different_file) 2608e1199815SMickaël Salaün { 2609e1199815SMickaël Salaün /* Sets access right on parent directories of both layers. */ 2610e1199815SMickaël Salaün const struct rule layer1_base[] = { 2611e1199815SMickaël Salaün { 2612e1199815SMickaël Salaün .path = LOWER_BASE, 2613e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2614e1199815SMickaël Salaün }, 2615e1199815SMickaël Salaün { 2616e1199815SMickaël Salaün .path = UPPER_BASE, 2617e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2618e1199815SMickaël Salaün }, 2619e1199815SMickaël Salaün { 2620e1199815SMickaël Salaün .path = MERGE_BASE, 2621e1199815SMickaël Salaün .access = ACCESS_RW, 2622e1199815SMickaël Salaün }, 2623135464f9SMickaël Salaün {}, 2624e1199815SMickaël Salaün }; 2625e1199815SMickaël Salaün const struct rule layer2_data[] = { 2626e1199815SMickaël Salaün { 2627e1199815SMickaël Salaün .path = LOWER_DATA, 2628e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2629e1199815SMickaël Salaün }, 2630e1199815SMickaël Salaün { 2631e1199815SMickaël Salaün .path = UPPER_DATA, 2632e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2633e1199815SMickaël Salaün }, 2634e1199815SMickaël Salaün { 2635e1199815SMickaël Salaün .path = MERGE_DATA, 2636e1199815SMickaël Salaün .access = ACCESS_RW, 2637e1199815SMickaël Salaün }, 2638135464f9SMickaël Salaün {}, 2639e1199815SMickaël Salaün }; 2640e1199815SMickaël Salaün /* Sets access right on directories inside both layers. */ 2641e1199815SMickaël Salaün const struct rule layer3_subdirs[] = { 2642e1199815SMickaël Salaün { 2643e1199815SMickaël Salaün .path = lower_dl1, 2644e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2645e1199815SMickaël Salaün }, 2646e1199815SMickaël Salaün { 2647e1199815SMickaël Salaün .path = lower_do1, 2648e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2649e1199815SMickaël Salaün }, 2650e1199815SMickaël Salaün { 2651e1199815SMickaël Salaün .path = upper_du1, 2652e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2653e1199815SMickaël Salaün }, 2654e1199815SMickaël Salaün { 2655e1199815SMickaël Salaün .path = upper_do1, 2656e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2657e1199815SMickaël Salaün }, 2658e1199815SMickaël Salaün { 2659e1199815SMickaël Salaün .path = merge_dl1, 2660e1199815SMickaël Salaün .access = ACCESS_RW, 2661e1199815SMickaël Salaün }, 2662e1199815SMickaël Salaün { 2663e1199815SMickaël Salaün .path = merge_du1, 2664e1199815SMickaël Salaün .access = ACCESS_RW, 2665e1199815SMickaël Salaün }, 2666e1199815SMickaël Salaün { 2667e1199815SMickaël Salaün .path = merge_do1, 2668e1199815SMickaël Salaün .access = ACCESS_RW, 2669e1199815SMickaël Salaün }, 2670135464f9SMickaël Salaün {}, 2671e1199815SMickaël Salaün }; 2672e1199815SMickaël Salaün /* Tighten access rights to the files. */ 2673e1199815SMickaël Salaün const struct rule layer4_files[] = { 2674e1199815SMickaël Salaün { 2675e1199815SMickaël Salaün .path = lower_dl1_fl2, 2676e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2677e1199815SMickaël Salaün }, 2678e1199815SMickaël Salaün { 2679e1199815SMickaël Salaün .path = lower_do1_fo2, 2680e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2681e1199815SMickaël Salaün }, 2682e1199815SMickaël Salaün { 2683e1199815SMickaël Salaün .path = lower_do1_fl3, 2684e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2685e1199815SMickaël Salaün }, 2686e1199815SMickaël Salaün { 2687e1199815SMickaël Salaün .path = upper_du1_fu2, 2688e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2689e1199815SMickaël Salaün }, 2690e1199815SMickaël Salaün { 2691e1199815SMickaël Salaün .path = upper_do1_fo2, 2692e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2693e1199815SMickaël Salaün }, 2694e1199815SMickaël Salaün { 2695e1199815SMickaël Salaün .path = upper_do1_fu3, 2696e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE, 2697e1199815SMickaël Salaün }, 2698e1199815SMickaël Salaün { 2699e1199815SMickaël Salaün .path = merge_dl1_fl2, 2700e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2701e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2702e1199815SMickaël Salaün }, 2703e1199815SMickaël Salaün { 2704e1199815SMickaël Salaün .path = merge_du1_fu2, 2705e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2706e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2707e1199815SMickaël Salaün }, 2708e1199815SMickaël Salaün { 2709e1199815SMickaël Salaün .path = merge_do1_fo2, 2710e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2711e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2712e1199815SMickaël Salaün }, 2713e1199815SMickaël Salaün { 2714e1199815SMickaël Salaün .path = merge_do1_fl3, 2715e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2716e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2717e1199815SMickaël Salaün }, 2718e1199815SMickaël Salaün { 2719e1199815SMickaël Salaün .path = merge_do1_fu3, 2720e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2721e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2722e1199815SMickaël Salaün }, 2723135464f9SMickaël Salaün {}, 2724e1199815SMickaël Salaün }; 2725e1199815SMickaël Salaün const struct rule layer5_merge_only[] = { 2726e1199815SMickaël Salaün { 2727e1199815SMickaël Salaün .path = MERGE_DATA, 2728e1199815SMickaël Salaün .access = LANDLOCK_ACCESS_FS_READ_FILE | 2729e1199815SMickaël Salaün LANDLOCK_ACCESS_FS_WRITE_FILE, 2730e1199815SMickaël Salaün }, 2731135464f9SMickaël Salaün {}, 2732e1199815SMickaël Salaün }; 2733e1199815SMickaël Salaün int ruleset_fd; 2734e1199815SMickaël Salaün size_t i; 2735e1199815SMickaël Salaün const char *path_entry; 2736e1199815SMickaël Salaün 2737e1199815SMickaël Salaün /* Sets rules on base directories (i.e. outside overlay scope). */ 2738e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer1_base); 2739e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2740e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2741e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2742e1199815SMickaël Salaün 2743e1199815SMickaël Salaün /* Checks lower layer. */ 2744e1199815SMickaël Salaün for_each_path(lower_base_files, path_entry, i) { 2745e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 2746e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 2747e1199815SMickaël Salaün } 2748e1199815SMickaël Salaün for_each_path(lower_base_directories, path_entry, i) { 2749371183faSMickaël Salaün ASSERT_EQ(EACCES, 2750371183faSMickaël Salaün test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2751e1199815SMickaël Salaün } 2752e1199815SMickaël Salaün for_each_path(lower_sub_files, path_entry, i) { 2753e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 2754e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 2755e1199815SMickaël Salaün } 2756e1199815SMickaël Salaün /* Checks upper layer. */ 2757e1199815SMickaël Salaün for_each_path(upper_base_files, path_entry, i) { 2758e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 2759e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 2760e1199815SMickaël Salaün } 2761e1199815SMickaël Salaün for_each_path(upper_base_directories, path_entry, i) { 2762371183faSMickaël Salaün ASSERT_EQ(EACCES, 2763371183faSMickaël Salaün test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2764e1199815SMickaël Salaün } 2765e1199815SMickaël Salaün for_each_path(upper_sub_files, path_entry, i) { 2766e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 2767e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 2768e1199815SMickaël Salaün } 2769e1199815SMickaël Salaün /* 2770e1199815SMickaël Salaün * Checks that access rights are independent from the lower and upper 2771e1199815SMickaël Salaün * layers: write access to upper files viewed through the merge point 2772e1199815SMickaël Salaün * is still allowed, and write access to lower file viewed (and copied) 2773e1199815SMickaël Salaün * through the merge point is still allowed. 2774e1199815SMickaël Salaün */ 2775e1199815SMickaël Salaün for_each_path(merge_base_files, path_entry, i) { 2776e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2777e1199815SMickaël Salaün } 2778e1199815SMickaël Salaün for_each_path(merge_base_directories, path_entry, i) { 2779e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2780e1199815SMickaël Salaün } 2781e1199815SMickaël Salaün for_each_path(merge_sub_files, path_entry, i) { 2782e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2783e1199815SMickaël Salaün } 2784e1199815SMickaël Salaün 2785e1199815SMickaël Salaün /* Sets rules on data directories (i.e. inside overlay scope). */ 2786e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer2_data); 2787e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2788e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2789e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2790e1199815SMickaël Salaün 2791e1199815SMickaël Salaün /* Checks merge. */ 2792e1199815SMickaël Salaün for_each_path(merge_base_files, path_entry, i) { 2793e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2794e1199815SMickaël Salaün } 2795e1199815SMickaël Salaün for_each_path(merge_base_directories, path_entry, i) { 2796e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2797e1199815SMickaël Salaün } 2798e1199815SMickaël Salaün for_each_path(merge_sub_files, path_entry, i) { 2799e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2800e1199815SMickaël Salaün } 2801e1199815SMickaël Salaün 2802e1199815SMickaël Salaün /* Same checks with tighter rules. */ 2803e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer3_subdirs); 2804e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2805e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2806e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2807e1199815SMickaël Salaün 2808e1199815SMickaël Salaün /* Checks changes for lower layer. */ 2809e1199815SMickaël Salaün for_each_path(lower_base_files, path_entry, i) { 2810e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 2811e1199815SMickaël Salaün } 2812e1199815SMickaël Salaün /* Checks changes for upper layer. */ 2813e1199815SMickaël Salaün for_each_path(upper_base_files, path_entry, i) { 2814e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 2815e1199815SMickaël Salaün } 2816e1199815SMickaël Salaün /* Checks all merge accesses. */ 2817e1199815SMickaël Salaün for_each_path(merge_base_files, path_entry, i) { 2818e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR)); 2819e1199815SMickaël Salaün } 2820e1199815SMickaël Salaün for_each_path(merge_base_directories, path_entry, i) { 2821e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2822e1199815SMickaël Salaün } 2823e1199815SMickaël Salaün for_each_path(merge_sub_files, path_entry, i) { 2824e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2825e1199815SMickaël Salaün } 2826e1199815SMickaël Salaün 2827e1199815SMickaël Salaün /* Sets rules directly on overlayed files. */ 2828e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer4_files); 2829e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2830e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2831e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2832e1199815SMickaël Salaün 2833e1199815SMickaël Salaün /* Checks unchanged accesses on lower layer. */ 2834e1199815SMickaël Salaün for_each_path(lower_sub_files, path_entry, i) { 2835e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 2836e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 2837e1199815SMickaël Salaün } 2838e1199815SMickaël Salaün /* Checks unchanged accesses on upper layer. */ 2839e1199815SMickaël Salaün for_each_path(upper_sub_files, path_entry, i) { 2840e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDONLY)); 2841e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_WRONLY)); 2842e1199815SMickaël Salaün } 2843e1199815SMickaël Salaün /* Checks all merge accesses. */ 2844e1199815SMickaël Salaün for_each_path(merge_base_files, path_entry, i) { 2845e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR)); 2846e1199815SMickaël Salaün } 2847e1199815SMickaël Salaün for_each_path(merge_base_directories, path_entry, i) { 2848371183faSMickaël Salaün ASSERT_EQ(EACCES, 2849371183faSMickaël Salaün test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2850e1199815SMickaël Salaün } 2851e1199815SMickaël Salaün for_each_path(merge_sub_files, path_entry, i) { 2852e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2853e1199815SMickaël Salaün } 2854e1199815SMickaël Salaün 2855e1199815SMickaël Salaün /* Only allowes access to the merge hierarchy. */ 2856e1199815SMickaël Salaün ruleset_fd = create_ruleset(_metadata, ACCESS_RW, layer5_merge_only); 2857e1199815SMickaël Salaün ASSERT_LE(0, ruleset_fd); 2858e1199815SMickaël Salaün enforce_ruleset(_metadata, ruleset_fd); 2859e1199815SMickaël Salaün ASSERT_EQ(0, close(ruleset_fd)); 2860e1199815SMickaël Salaün 2861e1199815SMickaël Salaün /* Checks new accesses on lower layer. */ 2862e1199815SMickaël Salaün for_each_path(lower_sub_files, path_entry, i) { 2863e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 2864e1199815SMickaël Salaün } 2865e1199815SMickaël Salaün /* Checks new accesses on upper layer. */ 2866e1199815SMickaël Salaün for_each_path(upper_sub_files, path_entry, i) { 2867e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDONLY)); 2868e1199815SMickaël Salaün } 2869e1199815SMickaël Salaün /* Checks all merge accesses. */ 2870e1199815SMickaël Salaün for_each_path(merge_base_files, path_entry, i) { 2871e1199815SMickaël Salaün ASSERT_EQ(EACCES, test_open(path_entry, O_RDWR)); 2872e1199815SMickaël Salaün } 2873e1199815SMickaël Salaün for_each_path(merge_base_directories, path_entry, i) { 2874371183faSMickaël Salaün ASSERT_EQ(EACCES, 2875371183faSMickaël Salaün test_open(path_entry, O_RDONLY | O_DIRECTORY)); 2876e1199815SMickaël Salaün } 2877e1199815SMickaël Salaün for_each_path(merge_sub_files, path_entry, i) { 2878e1199815SMickaël Salaün ASSERT_EQ(0, test_open(path_entry, O_RDWR)); 2879e1199815SMickaël Salaün } 2880e1199815SMickaël Salaün } 2881e1199815SMickaël Salaün 2882e1199815SMickaël Salaün TEST_HARNESS_MAIN 2883