| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/mount.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
|
| 6 |
#include "capability-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "fs-util.h" |
| 10 |
#include "mountpoint-util.h" |
| 11 |
#include "mstack.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "process-util.h" |
| 14 |
#include "rm-rf.h" |
| 15 |
#include "tests.h" |
| 16 |
#include "tmpfile-util.h" |
| 17 |
#include "virt.h" |
| 18 |
|
| 19 |
static bool overlayfs_set_fd_lowerdir_plus_supported(void) { |
| 20 |
int r; |
| 21 |
|
| 22 |
_cleanup_close_ int sb_fd = fsopen("overlay", FSOPEN_CLOEXEC); |
| 23 |
if (sb_fd < 0 && (ERRNO_IS_NOT_SUPPORTED(errno) || errno == ENODEV)) |
| 24 |
return false; |
| 25 |
ASSERT_OK_ERRNO(sb_fd); |
| 26 |
|
| 27 |
_cleanup_close_ int layer_fd = open("/", O_DIRECTORY|O_CLOEXEC); |
| 28 |
ASSERT_OK_ERRNO(layer_fd); |
| 29 |
|
| 30 |
r = RET_NERRNO(fsconfig(sb_fd, FSCONFIG_SET_FD, "lowerdir+", /* value= */ NULL, layer_fd)); |
| 31 |
if (r < 0 && (ERRNO_IS_NEG_NOT_SUPPORTED(r) || r == -EINVAL)) |
| 32 |
return false; |
| 33 |
|
| 34 |
ASSERT_OK_ERRNO(r); |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
TEST(mstack) { |
| 39 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 40 |
_cleanup_close_ int tfd = -EBADF; |
| 41 |
int r; |
| 42 |
|
| 43 |
tfd = mkdtemp_open("/tmp/mstack-what-XXXXXX", O_PATH, &t); |
| 44 |
ASSERT_OK(tfd); |
| 45 |
|
| 46 |
ASSERT_OK_ERRNO(mkdirat(tfd, "rw", 0755)); |
| 47 |
ASSERT_OK_ERRNO(mkdirat(tfd, "rw/data", 0755)); |
| 48 |
ASSERT_OK_ERRNO(mkdirat(tfd, "rw/data/check1", 0755)); |
| 49 |
ASSERT_OK_ERRNO(mkdirat(tfd, "layer@0", 0755)); |
| 50 |
ASSERT_OK_ERRNO(mkdirat(tfd, "layer@0/check2", 0755)); |
| 51 |
ASSERT_OK_ERRNO(mkdirat(tfd, "layer@0/zzz", 0755)); |
| 52 |
ASSERT_OK_ERRNO(mkdirat(tfd, "layer@1", 0755)); |
| 53 |
ASSERT_OK_ERRNO(mkdirat(tfd, "layer@1/check3", 0755)); |
| 54 |
ASSERT_OK_ERRNO(mkdirat(tfd, "layer@0/yyy", 0755)); |
| 55 |
ASSERT_OK_ERRNO(mkdirat(tfd, "bind@zzz", 0755)); |
| 56 |
ASSERT_OK_ERRNO(mkdirat(tfd, "bind@zzz/check4", 0755)); |
| 57 |
ASSERT_OK_ERRNO(mkdirat(tfd, "robind@yyy", 0755)); |
| 58 |
ASSERT_OK_ERRNO(mkdirat(tfd, "robind@yyy/check5", 0755)); |
| 59 |
|
| 60 |
_cleanup_(mstack_freep) MStack *mstack = NULL; |
| 61 |
ASSERT_OK(mstack_load(t, tfd, &mstack)); |
| 62 |
|
| 63 |
ASSERT_OK_ZERO(mstack_is_read_only(mstack)); |
| 64 |
ASSERT_OK_ZERO(mstack_is_foreign_uid_owned(mstack)); |
| 65 |
|
| 66 |
if (!have_effective_cap(CAP_SYS_ADMIN)) |
| 67 |
return (void) log_tests_skipped("not attaching mstack, lacking privs"); |
| 68 |
if (!mount_new_api_supported()) |
| 69 |
return (void) log_tests_skipped("kernel does not support new mount API, skipping mstack attachment test."); |
| 70 |
if (!overlayfs_set_fd_lowerdir_plus_supported()) |
| 71 |
return (void) log_tests_skipped("overlayfs does not support FSCONFIG_SET_FD with lowerdir+, skipping mstack attachment test."); |
| 72 |
if (running_in_chroot() > 0) /* we cannot disable mount prop if we are in a chroot without the root inode being a proper mount point */ |
| 73 |
return (void) log_tests_skipped("running in chroot(), skipping mstack attachment test."); |
| 74 |
|
| 75 |
mstack = mstack_free(mstack); |
| 76 |
|
| 77 |
/* For with a new mountns */ |
| 78 |
r = pidref_safe_fork("(mstack-test", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, /* ret= */ NULL); |
| 79 |
ASSERT_OK(r); |
| 80 |
|
| 81 |
if (r == 0) { |
| 82 |
MStackFlags flags = 0; |
| 83 |
|
| 84 |
/* Close the original temporary fd, it still points to an inode of the original mountns, |
| 85 |
* which we cannot use to generate mounts from */ |
| 86 |
tfd = safe_close(tfd); |
| 87 |
|
| 88 |
{ |
| 89 |
ASSERT_OK(mstack_load(t, -EBADF, &mstack)); |
| 90 |
|
| 91 |
ASSERT_OK(mstack_open_images( |
| 92 |
mstack, |
| 93 |
/* mountfsd_link= */ NULL, |
| 94 |
/* userns_fd= */ -EBADF, |
| 95 |
/* image_policy= */ NULL, |
| 96 |
/* image_filter= */ NULL, |
| 97 |
flags)); |
| 98 |
|
| 99 |
_cleanup_(rmdir_and_freep) char *m = NULL; |
| 100 |
ASSERT_OK(mkdtemp_malloc("/tmp/mstack-temporary-XXXXXX", &m)); |
| 101 |
|
| 102 |
ASSERT_OK(mstack_make_mounts(mstack, m, flags)); |
| 103 |
|
| 104 |
_cleanup_(rmdir_and_freep) char *w = NULL; |
| 105 |
ASSERT_OK(mkdtemp_malloc("/tmp/mstack-where-XXXXXX", &w)); |
| 106 |
|
| 107 |
_cleanup_close_ int rfd = -EBADF; |
| 108 |
ASSERT_OK(mstack_bind_mounts(mstack, w, /* where_fd= */ -EBADF, flags, &rfd)); |
| 109 |
|
| 110 |
_cleanup_close_ int ofd = open(w, O_PATH|O_CLOEXEC); |
| 111 |
ASSERT_OK_ERRNO(ofd); |
| 112 |
|
| 113 |
ASSERT_OK_ERRNO(faccessat(ofd, "check1", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 114 |
ASSERT_OK_ERRNO(faccessat(ofd, "check2/", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 115 |
ASSERT_OK_ERRNO(faccessat(ofd, "check3/", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 116 |
ASSERT_OK_ERRNO(faccessat(ofd, "zzz/check4/", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 117 |
ASSERT_OK_ERRNO(faccessat(ofd, "yyy/check5/", F_OK, AT_SYMLINK_NOFOLLOW)); |
| 118 |
|
| 119 |
_cleanup_free_ char *j = ASSERT_PTR(path_join(w, "zzz")); |
| 120 |
ASSERT_OK_ERRNO(umount2(j, MNT_DETACH)); |
| 121 |
_cleanup_free_ char *jj = ASSERT_PTR(path_join(w, "yyy")); |
| 122 |
ASSERT_OK_ERRNO(umount2(jj, MNT_DETACH)); |
| 123 |
ASSERT_OK_ERRNO(umount2(w, MNT_DETACH)); |
| 124 |
} |
| 125 |
|
| 126 |
mstack = mstack_free(mstack); |
| 127 |
|
| 128 |
_exit(EXIT_SUCCESS); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
DEFINE_TEST_MAIN(LOG_INFO); |