Extend idmapped mount testsuite
v2 cover Extend idmapped mount testsuite
From: Christian Brauner <[email protected]>
Hey,
This adds two new tests:
- a regression test for vfs capabilities
- a new test with nested idmapping layouts
In addition, I've added a new test for btrfs which is on-track to support idmapped mounts in v5.15. I leave it to Eryu whether he wants to merge the btrfs test already or not. I think there's no harm in doing so but I'm also happy to wait.
Thanks!
Christian
Christian Brauner (6):
idmapped-mounts: prepare for additional tests generic/640: add fscaps regression test
idmapped-mounts: refactor helpers
idmapped-mounts: add nested userns creation helpers generic/641: add nested user namespace tests btrfs/244: introduce btrfs specific idmapped mounts tests
src/idmapped-mounts/idmapped-mounts.c | 4583 ++++++++++++++++++++++++- src/idmapped-mounts/mount-idmapped.c | 229 +- src/idmapped-mounts/utils.c | 359 +- src/idmapped-mounts/utils.h | 102 +- tests/btrfs/244 | 34 + tests/btrfs/244.out | 2 + tests/generic/633 | 3 +- tests/generic/640 | 28 + tests/generic/640.out | 2 + tests/generic/641 | 28 + tests/generic/641.out | 2 + 11 files changed, 5054 insertions(+), 318 deletions(-) create mode 100755 tests/btrfs/244 create mode 100644 tests/btrfs/244.out create mode 100755 tests/generic/640 create mode 100644 tests/generic/640.out create mode 100755 tests/generic/641 create mode 100644 tests/generic/641.out
base-commit: dad0c0a852d1b10e7da285f29e99397dec0efec1
Signature · 1 line
2.30.2
PATCH v2 1/6 idmapped-mounts: prepare for additional tests
From: Christian Brauner <[email protected]>
- Use sequential option numbering for option parsing since we're only using longopts anyway and to easier correlate test parsing in the switch with the options specified in longopts.
- Introduce an explicit command line switch to runs the basic test suite. This prepares for the introduction of additional command line switches to run tests.
- Update help output.
- Use die() when logging test failure.
---
/* v2 */
new patch
--- src/idmapped-mounts/idmapped-mounts.c | 44 +++++++++++++++------------ tests/generic/633 | 3 +- 2 files changed, 27 insertions(+), 20 deletions(-)
src/idmapped-mounts/idmapped-mounts.c +25 -19
@@ -8717,19 +8717,24 @@ static void usage(void)fprintf (stderr," Run idmapped mount tests\n\n" );fprintf (stderr,"Arguments:\n" ); -fprintf (stderr,"-d --device Device used in the tests\n" ); -fprintf (stderr,"-m --mountpoint Mountpoint of device\n" ); +fprintf (stderr,"--device Device used in the tests\n" ); +fprintf (stderr,"--fstype Filesystem type used in the tests\n" ); +fprintf (stderr,"--help Print help\n" ); +fprintf (stderr,"--mountpoint Mountpoint of device\n" ); +fprintf (stderr,"--supported Test whether idmapped mounts are supported on this filesystem\n" ); +fprintf (stderr,"--test-core Run core idmapped mount testsuite\n" );_exit (EXIT_SUCCESS ); }static const struct option longopts[] = { - {"device" , required_argument,0 ,'d' }, - {"fstype" , required_argument,0 ,'f' }, - {"mountpoint" , required_argument,0 ,'m' }, - {"supported" , no_argument,0 ,'s' }, - {"help" , no_argument,0 ,'h' }, - {NULL ,0 ,0 ,0 }, + {"device" , required_argument,0 ,1 }, + {"fstype" , required_argument,0 ,2 }, + {"mountpoint" , required_argument,0 ,3 }, + {"supported" , no_argument,0 ,4 }, + {"help" , no_argument,0 ,5 }, + {"test-core" , no_argument,0 ,6 }, + {NULL ,0 ,0 ,0 }, };struct t_idmapped_mounts {
@@ -8804,10 +8809,8 @@ static bool run_test(struct t_idmapped_mounts suite[], size_t suite_size)if (pid ==0 ) { ret = t->test (); -if (ret) { -fprintf (stderr,"failure: %s\n" , t->description); -exit (EXIT_FAILURE ); - } +if (ret) +die ("failure: %s" , t->description);exit (EXIT_SUCCESS ); }
@@ -8826,23 +8829,26 @@ int main(int argc, char *argv[]) {int fret, ret;int index =0 ; -bool supported = false; +bool supported = false, test_core = false;while ((ret =getopt_long (argc, argv,"" , longopts, &index)) !=-1 ) {switch (ret) { -case 'd' : +case 1 : t_device = optarg;break ; -case 'f' : +case 2 : t_fstype = optarg;break ; -case 'm' : +case 3 : t_mountpoint = optarg;break ; -case 's' : +case 4 : supported = true;break ; -case 'h' : +case 6 : + test_core = true; +break ; +case 5 :/* fallthrough */ default :usage ();
@@ -8911,7 +8917,7 @@ int main(int argc, char *argv[]) fret =EXIT_FAILURE ; -if (!run_test (basic_suite,ARRAY_SIZE (basic_suite))) +if (test_core && !run_test (basic_suite,ARRAY_SIZE (basic_suite))) goto out; fret =EXIT_SUCCESS ;
tests/generic/633 +2 -1
@@ -20,7 +20,8 @@ _require_test echo "Silence is golden" -$here/src/idmapped-mounts/idmapped-mounts --device "$TEST_DEV" --mount "$TEST_DIR" --fstype "$FSTYP" +$here/src/idmapped-mounts/idmapped-mounts --test-core --device "$TEST_DEV" \ + --mount "$TEST_DIR" --fstype "$FSTYP" status=$? exit
Signature · 1 line
2.30.2
PATCH v2 2/6 generic/640: add fscaps regression test
In reply to Christian BraunerFrom: Christian Brauner <[email protected]>
Add a test to verify that setting a v3 fscap from an idmapped mount works as expected. This and other related use-cases were regressed by commit [1] which was reverted in [2] and the proper fix merged right before v5.12 was released in [3].
[1]: commit 3b0c2d3eaa83 ("Revert 95ebabde382c ("capabilities: Don't allow writing ambiguous v3 file capabilities")") [2]: commit 95ebabde382c ("capabilities: Don't allow writing ambiguous v3 file capabilities")
[3]: commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")
---
/* v2 */
- Eryu Guan <[email protected]>:
- Don't create symlinks for each test. Instead, use the same binary and introduce new options to specify which tests to run.
--- src/idmapped-mounts/idmapped-mounts.c | 159 +++++++++++++++++++++++--- tests/generic/640 | 28 +++++ tests/generic/640.out | 2 + 3 files changed, 174 insertions(+), 15 deletions(-) create mode 100755 tests/generic/640 create mode 100644 tests/generic/640.out
src/idmapped-mounts/idmapped-mounts.c +144 -15
@@ -3199,6 +3199,121 @@ out:return fret; } +static int fscaps_idmapped_mounts_in_userns_valid_in_ancestor_userns (void ) +{ +int fret =-1 ; +int file1_fd = -EBADF , file1_fd2 = -EBADF , open_tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + + file1_fd =openat (t_dir1_fd,FILE1 ,O_CREAT |O_EXCL |O_CLOEXEC ,0644 ); +if (file1_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* Skip if vfs caps are unsupported. */ +if (set_dummy_vfs_caps (file1_fd,0 ,1000 )) +return 0 ; + +if (fremovexattr (file1_fd,"security.capability" )) { +log_stderr ("failure: fremovexattr" ); + goto out; + } +if (expected_dummy_vfs_caps_uid (file1_fd,-1 )) { +log_stderr ("failure: expected_dummy_vfs_caps_uid" ); + goto out; + } +if (errno !=ENODATA ) { +log_stderr ("failure: errno" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + + file1_fd2 =openat (open_tree_fd,FILE1 ,O_RDWR |O_CLOEXEC ,0 ); +if (file1_fd2 <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* +* Verify we can set an v3 fscap for real root this was regressed at +* some point. Make sure this doesn't happen again! +*/ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +if (expected_dummy_vfs_caps_uid (file1_fd2,-1 )) +die ("failure: expected_dummy_vfs_caps_uid" ); +if (errno !=ENODATA ) +die ("failure: errno" ); + +if (set_dummy_vfs_caps (file1_fd2,0 ,0 )) +die ("failure: set_dummy_vfs_caps" ); + +if (!expected_dummy_vfs_caps_uid (file1_fd2,0 )) +die ("failure: expected_dummy_vfs_caps_uid" ); + +if (!expected_dummy_vfs_caps_uid (file1_fd,0 ) && errno !=EOVERFLOW ) +die ("failure: expected_dummy_vfs_caps_uid" ); + +exit (EXIT_SUCCESS ); + } + +if (wait_for_pid (pid)) + goto out; + +if (!expected_dummy_vfs_caps_uid (file1_fd2,10000 )) { +log_stderr ("failure: expected_dummy_vfs_caps_uid" ); + goto out; + } + +if (!expected_dummy_vfs_caps_uid (file1_fd,0 )) { +log_stderr ("failure: expected_dummy_vfs_caps_uid" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (file1_fd); +safe_close (file1_fd2); +safe_close (open_tree_fd); + +return fret; +} +static int fscaps_idmapped_mounts_in_userns_separate_userns (void ) {int fret =-1 ;
@@ -8717,24 +8832,26 @@ static void usage(void)fprintf (stderr," Run idmapped mount tests\n\n" );fprintf (stderr,"Arguments:\n" ); -fprintf (stderr,"--device Device used in the tests\n" ); -fprintf (stderr,"--fstype Filesystem type used in the tests\n" ); -fprintf (stderr,"--help Print help\n" ); -fprintf (stderr,"--mountpoint Mountpoint of device\n" ); -fprintf (stderr,"--supported Test whether idmapped mounts are supported on this filesystem\n" ); -fprintf (stderr,"--test-core Run core idmapped mount testsuite\n" ); +fprintf (stderr,"--device Device used in the tests\n" ); +fprintf (stderr,"--fstype Filesystem type used in the tests\n" ); +fprintf (stderr,"--help Print help\n" ); +fprintf (stderr,"--mountpoint Mountpoint of device\n" ); +fprintf (stderr,"--supported Test whether idmapped mounts are supported on this filesystem\n" ); +fprintf (stderr,"--test-core Run core idmapped mount testsuite\n" ); +fprintf (stderr,"--test-fscaps-regression Run fscap regression tests\n" );_exit (EXIT_SUCCESS ); }static const struct option longopts[] = { - {"device" , required_argument,0 ,1 }, - {"fstype" , required_argument,0 ,2 }, - {"mountpoint" , required_argument,0 ,3 }, - {"supported" , no_argument,0 ,4 }, - {"help" , no_argument,0 ,5 }, - {"test-core" , no_argument,0 ,6 }, - {NULL ,0 ,0 ,0 }, + {"device" , required_argument,0 ,1 }, + {"fstype" , required_argument,0 ,2 }, + {"mountpoint" , required_argument,0 ,3 }, + {"supported" , no_argument,0 ,4 }, + {"help" , no_argument,0 ,5 }, + {"test-core" , no_argument,0 ,6 }, + {"test-fscaps-regression" , no_argument,0 ,7 }, + {NULL ,0 ,0 ,0 }, };struct t_idmapped_mounts {
@@ -8748,7 +8865,7 @@ struct t_idmapped_mounts { { fscaps, "fscaps on regular mounts", }, { fscaps_idmapped_mounts, "fscaps on idmapped mounts", }, { fscaps_idmapped_mounts_in_userns, "fscaps on idmapped mounts in user namespace", }, - { fscaps_idmapped_mounts_in_userns_separate_userns, "fscaps on idmapped mounts in user namespace with different id mappings ", }, + { fscaps_idmapped_mounts_in_userns_separate_userns, "fscaps on idmapped mounts in user namespace with different id mappings", }, { fsids_mapped, "mapped fsids", }, { fsids_unmapped, "unmapped fsids", }, { hardlink_crossing_mounts, "cross mount hardlink", },
@@ -8792,6 +8909,10 @@ struct t_idmapped_mounts { { threaded_idmapped_mount_interactions, "threaded operations on idmapped mounts", }, }; +struct t_idmapped_mounts fscaps_in_ancestor_userns[] = { + { fscaps_idmapped_mounts_in_userns_valid_in_ancestor_userns,"fscaps on idmapped mounts in user namespace writing fscap valid in ancestor userns" , }, +}; +static bool run_test (struct t_idmapped_mounts suite[],size_t suite_size) {int i;
@@ -8829,7 +8950,7 @@ int main(int argc, char *argv[]) {int fret, ret;int index =0 ; -bool supported = false, test_core = false; +bool supported = false, test_core = false, test_fscaps_regression = false;while ((ret =getopt_long (argc, argv,"" , longopts, &index)) !=-1 ) {switch (ret) {
@@ -8848,6 +8969,9 @@ int main(int argc, char *argv[])case 6 : test_core = true;break ; +case 7 : + test_fscaps_regression = true; +break ;case 5 :/* fallthrough */ default :
@@ -8920,6 +9044,11 @@ int main(int argc, char *argv[])if (test_core && !run_test (basic_suite,ARRAY_SIZE (basic_suite))) goto out; +if (test_fscaps_regression && + !run_test (fscaps_in_ancestor_userns, +ARRAY_SIZE (fscaps_in_ancestor_userns))) + goto out; + fret =EXIT_SUCCESS ; out:
tests/generic/640 new file +28 -0
@@ -0,0 +1,28 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2021 Christian Brauner. All Rights Reserved. +# +# FS QA Test 640 +# +# Test that fscaps on idmapped mounts behave correctly. +# +. ./common/preamble +_begin_fstest auto quick cap idmapped mount + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter + +# real QA test starts here + +_supported_fs generic +_require_idmapped_mounts +_require_test + +echo "Silence is golden" + +$here/src/idmapped-mounts/idmapped-mounts --test-fscaps-regression \ + --device "$TEST_DEV" --mount "$TEST_DIR" --fstype "$FSTYP" + +status=$? +exit
tests/generic/640.out new file +2 -0
@@ -0,0 +1,2 @@ +QA output created by 640 +Silence is golden
Signature · 1 line
2.30.2
PATCH v2 3/6 idmapped-mounts: refactor helpers
In reply to Christian BraunerFrom: Christian Brauner <[email protected]>
Make all userns creation helpers share a commond codebase and move a bunch of code into utils.{c,h}. This simplifies a bunch of things and makes it easier to create nested user namespaces in follow up patches.
---
/* v2 */
unchanged
--- src/idmapped-mounts/mount-idmapped.c | 197 ------------------------- src/idmapped-mounts/utils.c | 209 +++++++++++++++++++-------- src/idmapped-mounts/utils.h | 73 +++++++++- 3 files changed, 223 insertions(+), 256 deletions(-)
src/idmapped-mounts/mount-idmapped.c +0 -197
@@ -27,77 +27,6 @@#include "missing.h" #include "utils.h" -/* A few helpful macros. */ -#define STRLITERALLEN (x) (sizeof(""x"") - 1) - -#define INTTYPE_TO_STRLEN (type) \ - (2 + (sizeof(type) <= 1 \ - ? 3 \ - : sizeof(type) <= 2 \ - ? 5 \ - : sizeof(type) <= 4 \ - ? 10 \ - : sizeof(type) <= 8 ? 20 : sizeof(int[-2 * (sizeof(type) > 8)]))) - -#define syserror (format, ...) \ - ({ \ - fprintf(stderr, format, ##__VA_ARGS__); \ - (-errno); \ - }) - -#define syserror_set (__ret__, format, ...) \ - ({ \ - typeof(__ret__) __internal_ret__ = (__ret__); \ - errno = labs(__ret__); \ - fprintf(stderr, format, ##__VA_ARGS__); \ - __internal_ret__; \ - }) - -struct list { -void *elem; -struct list *next; -struct list *prev; -}; - -#define list_for_each (__iterator, __list) \ - for (__iterator = (__list)->next; __iterator != __list; __iterator = __iterator->next) - -static inline void list_init (struct list *list) -{ - list->elem =NULL ; - list->next = list->prev = list; -} - -static inline int list_empty (const struct list *list) -{ -return list == list->next; -} - -static inline void __list_add (struct list *new,struct list *prev,struct list *next) -{ - next->prev = new; - new->next = next; - new->prev = prev; - prev->next = new; -} - -static inline void list_add_tail (struct list *head,struct list *list) -{ -__list_add (list, head->prev, head); -} - -typedef enum idmap_type_t { -ID_TYPE_UID , -ID_TYPE_GID -}idmap_type_t ; - -struct id_map { -idmap_type_t map_type; -__u32 nsid; -__u32 hostid; -__u32 range; -}; -static struct list active_map; static int add_map_entry(__u32 id_host,
@@ -166,132 +95,6 @@ static int parse_map(char *map)return 0 ; } -static int write_id_mapping (idmap_type_t map_type,pid_t pid,const char *buf,size_t buf_size) -{ -int fd = -EBADF , setgroups_fd = -EBADF ; -int fret =-1 ; -int ret; -char path[STRLITERALLEN ("/proc/" ) +INTTYPE_TO_STRLEN (pid_t) + -STRLITERALLEN ("/setgroups" ) +1 ]; - -if (geteuid () !=0 && map_type ==ID_TYPE_GID ) { - ret =snprintf (path,sizeof (path),"/proc/%d/setgroups" , pid); -if (ret <0 || ret >=sizeof (path)) - goto out; - - setgroups_fd =open (path,O_WRONLY |O_CLOEXEC ); -if (setgroups_fd <0 && errno !=ENOENT ) { -syserror ("Failed to open \"%s\"" , path); - goto out; - } - -if (setgroups_fd >=0 ) { - ret =write_nointr (setgroups_fd,"deny\n" ,STRLITERALLEN ("deny\n" )); -if (ret !=STRLITERALLEN ("deny\n" )) { -syserror ("Failed to write \"deny\" to \"/proc/%d/setgroups\"" , pid); - goto out; - } - } - } - - ret =snprintf (path,sizeof (path),"/proc/%d/%cid_map" , pid, map_type ==ID_TYPE_UID ?'u' :'g' ); -if (ret <0 || ret >=sizeof (path)) - goto out; - - fd =open (path,O_WRONLY |O_CLOEXEC ); -if (fd <0 ) { -syserror ("Failed to open \"%s\"" , path); - goto out; - } - - ret =write_nointr (fd, buf, buf_size); -if (ret != buf_size) { -syserror ("Failed to write %cid mapping to \"%s\"" , - map_type ==ID_TYPE_UID ?'u' :'g' , path); - goto out; - } - - fret =0 ; -out: -if (fd >=0 ) -close (fd); -if (setgroups_fd >=0 ) -close (setgroups_fd); - -return fret; -} - -static int map_ids_from_idmap (struct list *idmap,pid_t pid) -{ -int fill, left; -char mapbuf[4096 ] = {}; -bool had_entry = false; -idmap_type_t map_type, u_or_g; - -for (map_type =ID_TYPE_UID , u_or_g ='u' ; - map_type <=ID_TYPE_GID ; map_type++, u_or_g ='g' ) { -char *pos = mapbuf; -int ret; -struct list *iterator; - - -list_for_each (iterator, idmap) { -struct id_map *map = iterator->elem; -if (map->map_type != map_type) -continue ; - - had_entry = true; - - left =4096 - (pos - mapbuf); - fill =snprintf (pos, left,"%u %u %u\n" , map->nsid, map->hostid, map->range); -/* -* The kernel only takes <= 4k for writes to -* /proc/<pid>/{g,u}id_map -*/ -if (fill <=0 || fill >= left) -return syserror_set (-E2BIG ,"Too many %cid mappings defined" , u_or_g); - - pos += fill; - } -if (!had_entry) -continue ; - - ret =write_id_mapping (map_type, pid, mapbuf, pos - mapbuf); -if (ret <0 ) -return syserror ("Failed to write mapping: %s" , mapbuf); - -memset (mapbuf,0 ,sizeof (mapbuf)); - } - -return 0 ; -} - -static int get_userns_fd_from_idmap (struct list *idmap) -{ -int ret; -pid_t pid; -char path_ns[STRLITERALLEN ("/proc/" ) +INTTYPE_TO_STRLEN (pid_t) + -STRLITERALLEN ("/ns/user" ) +1 ]; - - pid =do_clone (get_userns_fd_cb,NULL ,CLONE_NEWUSER |CLONE_NEWNS ); -if (pid <0 ) -return -errno; - - ret =map_ids_from_idmap (idmap, pid); -if (ret <0 ) -return ret; - - ret =snprintf (path_ns,sizeof (path_ns),"/proc/%d/ns/user" , pid); -if (ret <0 || (size_t )ret >=sizeof (path_ns)) - ret = -EIO ; -else - ret =open (path_ns,O_RDONLY |O_CLOEXEC |O_NOCTTY ); - - (void )kill (pid,SIGKILL ); - (void )wait_for_pid (pid); -return ret; -} -static inline bool strnequal (const char *str,const char *eq,size_t len) {return strncmp (str, eq, len) ==0 ;
src/idmapped-mounts/utils.c +151 -58
250 lines not on this page · raw message
src/idmapped-mounts/utils.h +72 -1
82 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 4/6 idmapped-mounts: add nested userns creation helpers
In reply to Christian BraunerFrom: Christian Brauner <[email protected]>
Add a helper to create a nested userns hierarchy. This will be used in follow-up tests.
--- src/idmapped-mounts/idmapped-mounts.c | 14 --- src/idmapped-mounts/mount-idmapped.c | 32 +----- src/idmapped-mounts/utils.c | 160 +++++++++++++++++++++++++- src/idmapped-mounts/utils.h | 25 ++++ 4 files changed, 185 insertions(+), 46 deletions(-)
src/idmapped-mounts/idmapped-mounts.c +0 -14
@@ -388,20 +388,6 @@ static inline bool switch_fsids(uid_t fsuid, gid_t fsgid)return true; } -static inline bool switch_ids (uid_t uid,gid_t gid) -{ -if (setgroups (0 ,NULL )) -return log_errno (false,"failure: setgroups" ); - -if (setresgid (gid, gid, gid)) -return log_errno (false,"failure: setresgid" ); - -if (setresuid (uid, uid, uid)) -return log_errno (false,"failure: setresuid" ); - -return true; -} -static inline bool switch_userns (int fd,uid_t uid,gid_t gid,bool drop_caps) {if (setns (fd,CLONE_NEWUSER ))
src/idmapped-mounts/mount-idmapped.c +1 -31
@@ -29,36 +29,6 @@static struct list active_map; -static int add_map_entry (__u32 id_host, -__u32 id_ns, -__u32 range, -idmap_type_t map_type) -{ -struct list *new_list =NULL ; -struct id_map *newmap =NULL ; - - newmap =malloc (sizeof (*newmap)); -if (!newmap) -return -ENOMEM ; - - new_list =malloc (sizeof (struct list )); -if (!new_list) { -free (newmap); -return -ENOMEM ; - } - - *newmap = (struct id_map ){ - .hostid = id_host, - .nsid = id_ns, - .range = range, - .map_type = map_type, - }; - - new_list->elem = newmap; -list_add_tail (&active_map, new_list); -return 0 ; -} -static int parse_map (char *map) {char types[2 ] = {'u' ,'g' };
@@ -87,7 +57,7 @@ static int parse_map(char *map)else map_type =ID_TYPE_GID ; - ret =add_map_entry (id_host, id_ns, range, map_type); + ret =add_map_entry (&active_map, id_host, id_ns, range, map_type);if (ret <0 )return ret; }
src/idmapped-mounts/utils.c +159 -1
179 lines not on this page · raw message
src/idmapped-mounts/utils.h +25 -0
@@ -10,6 +10,7 @@#include <linux/types.h> #include <sched.h> #include <signal.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
@@ -19,6 +20,9 @@#include "missing.h" +/* Maximum number of nested user namespaces in the kernel. */ +#define MAX_USERNS_LEVEL 32 +/* A few helpful macros. */ #define STRLITERALLEN (x) (sizeof(""x"") - 1)
@@ -63,6 +67,13 @@ struct list {struct list *prev; }; +struct userns_hierarchy { +int fd_userns; +int fd_event; +unsigned int level; +struct list id_map; +}; +#define list_for_each (__iterator, __list) \ for (__iterator = (__list)->next; __iterator != __list; __iterator = __iterator->next)
@@ -90,6 +101,16 @@ static inline void list_add_tail(struct list *head, struct list *list)__list_add (list, head->prev, head); } +static inline void list_del (struct list *list) +{ +struct list *next, *prev; + + next = list->next; + prev = list->prev; + next->prev = prev; + prev->next = next; +} +extern pid_t do_clone (int (*fn)(void *),void *arg,int flags);extern int get_userns_fd (unsigned long nsid,unsigned long hostid,unsigned long range);
@@ -97,5 +118,9 @@ extern int get_userns_fd_from_idmap(struct list *idmap);extern ssize_t read_nointr (int fd,void *buf,size_t count);extern int wait_for_pid (pid_t pid);extern ssize_t write_nointr (int fd,const void *buf,size_t count); +extern bool switch_ids (uid_t uid,gid_t gid); +extern int create_userns_hierarchy (struct userns_hierarchy *h); +extern int add_map_entry (struct list *head,__u32 id_host,__u32 id_ns, +__u32 range,idmap_type_t map_type);#endif /* __IDMAP_UTILS_H */
Signature · 1 line
2.30.2
PATCH v2 5/6 generic/641: add nested user namespace tests
In reply to Christian BraunerFrom: Christian Brauner <[email protected]>
Test ownership and ownership changes in a complex user namespace hierarchy.
---
/* v2 */
- Eryu Guan <[email protected]>:
- Don't create symlinks for each test. Instead, use the same binary and introduce new options to specify which tests to run.
--- src/idmapped-mounts/idmapped-mounts.c | 723 +++++++++++++++++++++++++- src/idmapped-mounts/utils.h | 4 + tests/generic/641 | 28 + tests/generic/641.out | 2 + 4 files changed, 756 insertions(+), 1 deletion(-) create mode 100755 tests/generic/641 create mode 100644 tests/generic/641.out
src/idmapped-mounts/idmapped-mounts.c +722 -1
@@ -8812,6 +8812,714 @@ out:return fret; } +static int nested_userns (void ) +{ +int fret =-1 ; +int ret; +pid_t pid; +struct list *it, *next; +struct userns_hierarchy hierarchy[] = { + { .level =1 , .fd_userns = -EBADF , }, + { .level =2 , .fd_userns = -EBADF , }, + { .level =3 , .fd_userns = -EBADF , }, + { .level =4 , .fd_userns = -EBADF , }, +/* Dummy entry that marks the end. */ + { .level =MAX_USERNS_LEVEL , .fd_userns = -EBADF , }, + }; +struct mount_attr attr_level1 = { + .attr_set =MOUNT_ATTR_IDMAP , + .userns_fd = -EBADF , + }; +struct mount_attr attr_level2 = { + .attr_set =MOUNT_ATTR_IDMAP , + .userns_fd = -EBADF , + }; +struct mount_attr attr_level3 = { + .attr_set =MOUNT_ATTR_IDMAP , + .userns_fd = -EBADF , + }; +struct mount_attr attr_level4 = { + .attr_set =MOUNT_ATTR_IDMAP , + .userns_fd = -EBADF , + }; +int fd_dir1 = -EBADF , + fd_open_tree_level1 = -EBADF , + fd_open_tree_level2 = -EBADF , + fd_open_tree_level3 = -EBADF , + fd_open_tree_level4 = -EBADF ; +const unsigned int id_file_range =10000 ; + +list_init (&hierarchy[0 ].id_map); +list_init (&hierarchy[1 ].id_map); +list_init (&hierarchy[2 ].id_map); +list_init (&hierarchy[3 ].id_map); + +/* +* Give a large map to the outermost user namespace so we can create +* comfortable nested maps. +*/ + ret =add_map_entry (&hierarchy[0 ].id_map,1000000 ,0 ,1000000000 ,ID_TYPE_UID ); +if (ret) { +log_stderr ("failure: adding uidmap for userns at level 1" ); + goto out; + } + + ret =add_map_entry (&hierarchy[0 ].id_map,1000000 ,0 ,1000000000 ,ID_TYPE_GID ); +if (ret) { +log_stderr ("failure: adding gidmap for userns at level 1" ); + goto out; + } + +/* This is uid:0->2000000:100000000 in init userns. */ + ret =add_map_entry (&hierarchy[1 ].id_map,1000000 ,0 ,100000000 ,ID_TYPE_UID ); +if (ret) { +log_stderr ("failure: adding uidmap for userns at level 2" ); + goto out; + } + +/* This is gid:0->2000000:100000000 in init userns. */ + ret =add_map_entry (&hierarchy[1 ].id_map,1000000 ,0 ,100000000 ,ID_TYPE_GID ); +if (ret) { +log_stderr ("failure: adding gidmap for userns at level 2" ); + goto out; + } + +/* This is uid:0->3000000:999 in init userns. */ + ret =add_map_entry (&hierarchy[2 ].id_map,1000000 ,0 ,999 ,ID_TYPE_UID ); +if (ret) { +log_stderr ("failure: adding uidmap for userns at level 3" ); + goto out; + } + +/* This is gid:0->3000000:999 in the init userns. */ + ret =add_map_entry (&hierarchy[2 ].id_map,1000000 ,0 ,999 ,ID_TYPE_GID ); +if (ret) { +log_stderr ("failure: adding gidmap for userns at level 3" ); + goto out; + } + +/* id 999 will remain unmapped. */ + +/* This is uid:1000->2001000:1 in init userns. */ + ret =add_map_entry (&hierarchy[2 ].id_map,1000 ,1000 ,1 ,ID_TYPE_UID ); +if (ret) { +log_stderr ("failure: adding uidmap for userns at level 3" ); + goto out; + } + +/* This is gid:1000->2001000:1 in init userns. */ + ret =add_map_entry (&hierarchy[2 ].id_map,1000 ,1000 ,1 ,ID_TYPE_GID ); +if (ret) { +log_stderr ("failure: adding gidmap for userns at level 3" ); + goto out; + } + +/* This is uid:1001->3001001:10000 in init userns. */ + ret =add_map_entry (&hierarchy[2 ].id_map,1001001 ,1001 ,10000000 ,ID_TYPE_UID ); +if (ret) { +log_stderr ("failure: adding uidmap for userns at level 3" ); + goto out; + } + +/* This is gid:1001->3001001:10000 in init userns. */ + ret =add_map_entry (&hierarchy[2 ].id_map,1001001 ,1001 ,10000000 ,ID_TYPE_GID ); +if (ret) { +log_stderr ("failure: adding gidmap for userns at level 3" ); + goto out; + } + +/* Don't write a mapping in the 4th userns. */ +list_empty (&hierarchy[4 ].id_map); + +/* Create the actual userns hierarchy. */ + ret =create_userns_hierarchy (hierarchy); +if (ret) { +log_stderr ("failure: create userns hierarchy" ); + goto out; + } + + attr_level1.userns_fd = hierarchy[0 ].fd_userns; + attr_level2.userns_fd = hierarchy[1 ].fd_userns; + attr_level3.userns_fd = hierarchy[2 ].fd_userns; + attr_level4.userns_fd = hierarchy[3 ].fd_userns; + +/* +* Create one directory where we create files for each uid/gid within +* the first userns. +*/ +if (mkdirat (t_dir1_fd,DIR1 ,0777 )) { +log_stderr ("failure: mkdirat" ); + goto out; + } + + fd_dir1 =openat (t_dir1_fd,DIR1 ,O_DIRECTORY |O_CLOEXEC ); +if (fd_dir1 <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +for (unsigned int id =0 ; id <= id_file_range; id++) { +char file[256 ]; + +snprintf (file,sizeof (file),DIR1 "/" FILE1 "_%u" , id); + +if (mknodat (t_dir1_fd, file,S_IFREG |0644 ,0 )) { +log_stderr ("failure: create %s" , file); + goto out; + } + +if (fchownat (t_dir1_fd, file, id, id,AT_SYMLINK_NOFOLLOW )) { +log_stderr ("failure: fchownat %s" , file); + goto out; + } + +if (!expected_uid_gid (t_dir1_fd, file,0 , id, id)) { +log_stderr ("failure: check ownership %s" , file); + goto out; + } + } + +/* Create detached mounts for all the user namespaces. */ + fd_open_tree_level1 =sys_open_tree (t_dir1_fd,DIR1 , +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (fd_open_tree_level1 <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + + fd_open_tree_level2 =sys_open_tree (t_dir1_fd,DIR1 , +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (fd_open_tree_level2 <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + + fd_open_tree_level3 =sys_open_tree (t_dir1_fd,DIR1 , +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (fd_open_tree_level3 <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + + fd_open_tree_level4 =sys_open_tree (t_dir1_fd,DIR1 , +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (fd_open_tree_level4 <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +/* Turn detached mounts into detached idmapped mounts. */ +if (sys_mount_setattr (fd_open_tree_level1,"" ,AT_EMPTY_PATH , + &attr_level1,sizeof (attr_level1))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +if (sys_mount_setattr (fd_open_tree_level2,"" ,AT_EMPTY_PATH , + &attr_level2,sizeof (attr_level2))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +if (sys_mount_setattr (fd_open_tree_level3,"" ,AT_EMPTY_PATH , + &attr_level3,sizeof (attr_level3))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +if (sys_mount_setattr (fd_open_tree_level4,"" ,AT_EMPTY_PATH , + &attr_level4,sizeof (attr_level4))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* Verify that ownership looks correct for callers in the init userns. */ +for (unsigned int id =0 ; id <= id_file_range; id++) { +bool bret; +unsigned int id_level1, id_level2, id_level3; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + + id_level1 = id +1000000 ; +if (!expected_uid_gid (fd_open_tree_level1, file,0 , id_level1, id_level1)) { +log_stderr ("failure: check ownership %s" , file); + goto out; + } + + id_level2 = id +2000000 ; +if (!expected_uid_gid (fd_open_tree_level2, file,0 , id_level2, id_level2)) { +log_stderr ("failure: check ownership %s" , file); + goto out; + } + +if (id ==999 ) { +/* This id is unmapped. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid); + }else if (id ==1000 ) { + id_level3 = id +2000000 ;/* We punched a hole in the map at 1000. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + }else { + id_level3 = id +3000000 ;/* Rest is business as usual. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + } +if (!bret) { +log_stderr ("failure: check ownership %s" , file); + goto out; + } + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) { +log_stderr ("failure: check ownership %s" , file); + goto out; + } + } + +/* Verify that ownership looks correct for callers in the first userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr_level1.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +bool bret; +unsigned int id_level1, id_level2, id_level3; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + + id_level1 = id; +if (!expected_uid_gid (fd_open_tree_level1, file,0 , id_level1, id_level1)) +die ("failure: check ownership %s" , file); + + id_level2 = id +1000000 ; +if (!expected_uid_gid (fd_open_tree_level2, file,0 , id_level2, id_level2)) +die ("failure: check ownership %s" , file); + +if (id ==999 ) { +/* This id is unmapped. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid); + }else if (id ==1000 ) { + id_level3 = id +1000000 ;/* We punched a hole in the map at 1000. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + }else { + id_level3 = id +2000000 ;/* Rest is business as usual. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + } +if (!bret) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that ownership looks correct for callers in the second userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr_level2.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +bool bret; +unsigned int id_level2, id_level3; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + +if (!expected_uid_gid (fd_open_tree_level1, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + + id_level2 = id; +if (!expected_uid_gid (fd_open_tree_level2, file,0 , id_level2, id_level2)) +die ("failure: check ownership %s" , file); + +if (id ==999 ) { +/* This id is unmapped. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid); + }else if (id ==1000 ) { + id_level3 = id;/* We punched a hole in the map at 1000. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + }else { + id_level3 = id +1000000 ;/* Rest is business as usual. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + } +if (!bret) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that ownership looks correct for callers in the third userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr_level3.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +bool bret; +unsigned int id_level2, id_level3; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + +if (!expected_uid_gid (fd_open_tree_level1, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (id ==1000 ) { +/* +* The idmapping of the third userns has a hole +* at uid/gid 1000. That means: +* - 1000->userns_0(2000000) // init userns +* - 1000->userns_1(2000000) // level 1 +* - 1000->userns_2(1000000) // level 2 +* - 1000->userns_3(1000) // level 3 (because level 3 has a hole) +*/ + id_level2 = id; + bret =expected_uid_gid (fd_open_tree_level2, file,0 , id_level2, id_level2); + }else { + bret =expected_uid_gid (fd_open_tree_level2, file,0 , t_overflowuid, t_overflowgid); + } +if (!bret) +die ("failure: check ownership %s" , file); + + +if (id ==999 ) { +/* This id is unmapped. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid); + }else { + id_level3 = id;/* Rest is business as usual. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + } +if (!bret) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that ownership looks correct for callers in the fourth userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (setns (attr_level4.userns_fd,CLONE_NEWUSER )) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + +if (!expected_uid_gid (fd_open_tree_level1, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level2, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that chown works correctly for callers in the first userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr_level1.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +bool bret; +unsigned int id_level1, id_level2, id_level3, id_new; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + + id_new = id +1 ; +if (fchownat (fd_open_tree_level1, file, id_new, id_new,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + + id_level1 = id_new; +if (!expected_uid_gid (fd_open_tree_level1, file,0 , id_level1, id_level1)) +die ("failure: check ownership %s" , file); + + id_level2 = id_new +1000000 ; +if (!expected_uid_gid (fd_open_tree_level2, file,0 , id_level2, id_level2)) +die ("failure: check ownership %s" , file); + +if (id_new ==999 ) { +/* This id is unmapped. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid); + }else if (id_new ==1000 ) { + id_level3 = id_new +1000000 ;/* We punched a hole in the map at 1000. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + }else { + id_level3 = id_new +2000000 ;/* Rest is business as usual. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + } +if (!bret) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +/* Revert ownership. */ +if (fchownat (fd_open_tree_level1, file, id, id,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that chown works correctly for callers in the second userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr_level2.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +bool bret; +unsigned int id_level2, id_level3, id_new; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + + id_new = id +1 ; +if (fchownat (fd_open_tree_level2, file, id_new, id_new,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + +if (!expected_uid_gid (fd_open_tree_level1, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + + id_level2 = id_new; +if (!expected_uid_gid (fd_open_tree_level2, file,0 , id_level2, id_level2)) +die ("failure: check ownership %s" , file); + +if (id_new ==999 ) { +/* This id is unmapped. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid); + }else if (id_new ==1000 ) { + id_level3 = id_new;/* We punched a hole in the map at 1000. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + }else { + id_level3 = id_new +1000000 ;/* Rest is business as usual. */ + bret =expected_uid_gid (fd_open_tree_level3, file,0 , id_level3, id_level3); + } +if (!bret) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +/* Revert ownership. */ +if (fchownat (fd_open_tree_level2, file, id, id,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that chown works correctly for callers in the third userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr_level3.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +unsigned int id_new; +char file[256 ]; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + + id_new = id +1 ; +if (id_new ==999 || id_new ==1000 ) { +/* +* We can't change ownership as we can't +* chown from or to an unmapped id. +*/ +if (!fchownat (fd_open_tree_level3, file, id_new, id_new,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + }else { +if (fchownat (fd_open_tree_level3, file, id_new, id_new,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + } + +if (!expected_uid_gid (fd_open_tree_level1, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +/* There's no id 1000 anymore as we changed ownership for id 1000 to 1001 above. */ +if (!expected_uid_gid (fd_open_tree_level2, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (id_new ==999 ) { +/* +* We did not change ownership as we can't +* chown to an unmapped id. +*/ +if (!expected_uid_gid (fd_open_tree_level3, file,0 , id, id)) +die ("failure: check ownership %s" , file); + }else if (id_new ==1000 ) { +/* +* We did not change ownership as we can't +* chown from an unmapped id. +*/ +if (!expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + }else { +if (!expected_uid_gid (fd_open_tree_level3, file,0 , id_new, id_new)) +die ("failure: check ownership %s" , file); + } + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +/* Revert ownership. */ +if (id_new !=999 && id_new !=1000 ) { +if (fchownat (fd_open_tree_level3, file, id, id,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + } + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* Verify that chown works correctly for callers in the fourth userns. */ + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (setns (attr_level4.userns_fd,CLONE_NEWUSER )) +die ("failure: switch_userns" ); + +for (unsigned int id =0 ; id <= id_file_range; id++) { +char file[256 ]; +unsigned long id_new; + +snprintf (file,sizeof (file),FILE1 "_%u" , id); + + id_new = id +1 ; +if (!fchownat (fd_open_tree_level4, file, id_new, id_new,AT_SYMLINK_NOFOLLOW )) +die ("failure: fchownat %s" , file); + +if (!expected_uid_gid (fd_open_tree_level1, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level2, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level3, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + +if (!expected_uid_gid (fd_open_tree_level4, file,0 , t_overflowuid, t_overflowgid)) +die ("failure: check ownership %s" , file); + + } + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); + +out: +list_for_each_safe (it, &hierarchy[0 ].id_map, next) { +list_del (it); +free (it->elem); +free (it); + } + +list_for_each_safe (it, &hierarchy[1 ].id_map, next) { +list_del (it); +free (it->elem); +free (it); + } + +list_for_each_safe (it, &hierarchy[2 ].id_map, next) { +list_del (it); +free (it->elem); +free (it); + } + +safe_close (hierarchy[0 ].fd_userns); +safe_close (hierarchy[1 ].fd_userns); +safe_close (hierarchy[2 ].fd_userns); +safe_close (fd_dir1); +safe_close (fd_open_tree_level1); +safe_close (fd_open_tree_level2); +safe_close (fd_open_tree_level3); +safe_close (fd_open_tree_level4); +return fret; +} +static void usage (void ) {fprintf (stderr,"Description:\n" );
@@ -8837,6 +9545,7 @@ static const struct option longopts[] = { {"help", no_argument, 0, 5}, {"test-core", no_argument, 0, 6}, {"test-fscaps-regression", no_argument, 0, 7}, + {"test-nested-userns", no_argument, 0, 8}, {NULL, 0, 0, 0}, };
@@ -8899,6 +9608,10 @@ struct t_idmapped_mounts fscaps_in_ancestor_userns[] = { { fscaps_idmapped_mounts_in_userns_valid_in_ancestor_userns, "fscaps on idmapped mounts in user namespace writing fscap valid in ancestor userns", }, }; +struct t_idmapped_mounts t_nested_userns[] = { + { nested_userns,"test that nested user namespaces behave correctly when attached to idmapped mounts" , }, +}; +static bool run_test (struct t_idmapped_mounts suite[],size_t suite_size) {int i;
@@ -8936,7 +9649,8 @@ int main(int argc, char *argv[]) {int fret, ret;int index =0 ; -bool supported = false, test_core = false, test_fscaps_regression = false; +bool supported = false, test_core = false, + test_fscaps_regression = false, test_nested_userns = false;while ((ret =getopt_long (argc, argv,"" , longopts, &index)) !=-1 ) {switch (ret) {
@@ -8958,6 +9672,9 @@ int main(int argc, char *argv[])case 7 : test_fscaps_regression = true;break ; +case 8 : + test_nested_userns = true; +break ;case 5 :/* fallthrough */ default :
@@ -9035,6 +9752,10 @@ int main(int argc, char *argv[]) ARRAY_SIZE(fscaps_in_ancestor_userns))) goto out; +if (test_nested_userns && + !run_test (t_nested_userns,ARRAY_SIZE (t_nested_userns))) + goto out; + fret =EXIT_SUCCESS ; out:
src/idmapped-mounts/utils.h +4 -0
@@ -77,6 +77,10 @@ struct userns_hierarchy {#define list_for_each (__iterator, __list) \ for (__iterator = (__list)->next; __iterator != __list; __iterator = __iterator->next) +#define list_for_each_safe (__iterator, __list, __next) \ + for (__iterator = (__list)->next, __next = __iterator->next; \ + __iterator != __list; __iterator = __next, __next = __next->next) +static inline void list_init (struct list *list) { list->elem =NULL ;
tests/generic/641 new file +28 -0
@@ -0,0 +1,28 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2021 Christian Brauner. All Rights Reserved. +# +# FS QA Test 641 +# +# Test that idmapped mounts behave correctly with complex user namespaces. +# +. ./common/preamble +_begin_fstest auto quick idmapped mount + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter + +# real QA test starts here + +_supported_fs generic +_require_idmapped_mounts +_require_test + +echo "Silence is golden" + +$here/src/idmapped-mounts/idmapped-mounts --test-nested-userns \ + --device "$TEST_DEV" --mount "$TEST_DIR" --fstype "$FSTYP" + +status=$? +exit
tests/generic/641.out new file +2 -0
@@ -0,0 +1,2 @@ +QA output created by 641 +Silence is golden
Signature · 1 line
2.30.2
PATCH v2 6/6 btrfs/244: introduce btrfs specific idmapped mounts tests
In reply to Christian BraunerFrom: Christian Brauner <[email protected]>
While core vfs functionality that btrfs implements is completely covered by the generic test-suite the btrfs specific ioctls are not. This patch expands the test-suite to cover btrfs specific ioctls that required changes to work on idmapped mounts. We deliberately don't use the libbtrfsutil library as we need to know exactly what ioctl's are issued and we need to be in control of all privileges at all times. This test-suite currently tests:
- BTRFS_IOC_{SNAP,SUBVOL}_CREATE_V2
- subvolume creation on idmapped mounts where the fsids do have a mapping in the superblock
- snapshot creation on idmapped mounts where the fsids do have a mapping in the superblock
- subvolume creation on idmapped mounts where the fsids do not have a mapping in the superblock
- snapshot creation on idmapped mounts where the fsids do not have a mapping in the superblock
- subvolume creation on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- snapshot creation on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- subvolume creation on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
- snapshot creation on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
- BTRFS_IOC_SNAP_DESTROY_V2
- subvolume deletion on idmapped mounts where the fsids do have a mapping in the superblock
- snapshot deletion on idmapped mounts where the fsids do have a mapping in the superblock
- subvolume deletion on idmapped mounts where the fsids do not have a mapping in the superblock
- snapshot deletion on idmapped mounts where the fsids do not have a mapping in the superblock
- subvolume deletion on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- snapshot deletion on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- subvolume deletion on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
- snapshot deletion on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
- unprivileged subvolume deletion on idmapped mounts where the fsids do have a mapping in the superblock and the filesystem is mounted with "user_subvol_rm_allowed"
- unprivileged snapshot deletion on idmapped mounts where the fsids do have a mapping in the superblock and the filesystem is mounted with "user_subvol_rm_allowed"
- subvolume deletion on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock and the filesystem is mounted with "user_subvol_rm_allowed"
- snapshot deletion on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock and the filesystem is mounted with "user_subvol_rm_allowed"
- BTRFS_IOC_SUBVOL_SETFLAGS
- subvolume flags on idmapped mounts where the fsids do have a mapping in the superblock
- snapshot flags on idmapped mounts where the fsids do have a mapping in the superblock
- subvolume flags on idmapped mounts where the fsids do not have a mapping in the superblock
- snapshot flags on idmapped mounts where the fsids do not have a mapping in the superblock
- subvolume flags on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- snapshot flags on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- subvolume flags on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
- snapshot flags on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
- BTRFS_IOC_INO_LOOKUP_USER
- subvolume lookup on idmapped mounts where the fsids do have a mapping in the superblock
- subvolume lookup on idmapped mounts where the fsids do not have a mapping in the superblock
- subvolume lookup on idmapped mounts where the caller is located in a user namespace and the fsids do have a mapping in the superblock
- subvolume lookup on idmapped mounts where the caller is located in a user namespace and the fsids do not have a mapping in the superblock
---
/* v2 */
new patch
--- src/idmapped-mounts/idmapped-mounts.c | 3693 ++++++++++++++++++++++++- tests/btrfs/244 | 34 + tests/btrfs/244.out | 2 + 3 files changed, 3719 insertions(+), 10 deletions(-) create mode 100755 tests/btrfs/244 create mode 100644 tests/btrfs/244.out
src/idmapped-mounts/idmapped-mounts.c +3683 -10
@@ -11,6 +11,8 @@#include <getopt.h> #include <grp.h> #include <limits.h> +#include <linux/btrfs.h> +#include <linux/btrfs_tree.h> #include <linux/limits.h> #include <linux/types.h> #include <pthread.h>
@@ -91,12 +93,21 @@ const char *t_fstype;/* path of the test device */ const char *t_device; +/* path of the test scratch device */ +const char *t_device_scratch; +/* mountpoint of the test device */ const char *t_mountpoint; +/* mountpoint of the test device */ +const char *t_mountpoint_scratch; +/* fd for @t_mountpoint */ int t_mnt_fd; +/* fd for @t_mountpoint_scratch */ +int t_mnt_scratch_fd; +/* fd for @T_DIR1 */ int t_dir1_fd;
@@ -9520,6 +9531,3622 @@ out:return fret; } +static int btrfs_delete_subvolume (int parent_fd,const char *name) +{ +struct btrfs_ioctl_vol_args args = {}; +size_t len; +int ret; + + len =strlen (name); +if (len >=sizeof (args.name)) +return -ENAMETOOLONG ; + +memcpy (args.name, name, len); + args.name[len] ='\0' ; + + ret =ioctl (parent_fd,BTRFS_IOC_SNAP_DESTROY , &args); +if (ret <0 ) +return -1 ; + +return 0 ; +} + +static int btrfs_delete_subvolume_id (int parent_fd,uint64_t subvolid) +{ +struct btrfs_ioctl_vol_args_v2 args = {}; +int ret; + + args.flags =BTRFS_SUBVOL_SPEC_BY_ID ; + args.subvolid = subvolid; + + ret =ioctl (parent_fd,BTRFS_IOC_SNAP_DESTROY_V2 , &args); +if (ret <0 ) +return -1 ; + +return 0 ; +} + +static int btrfs_create_subvolume (int parent_fd,const char *name) +{ +struct btrfs_ioctl_vol_args_v2 args = {}; +size_t len; +int ret; + + len =strlen (name); +if (len >=sizeof (args.name)) +return -ENAMETOOLONG ; + +memcpy (args.name, name, len); + args.name[len] ='\0' ; + + ret =ioctl (parent_fd,BTRFS_IOC_SUBVOL_CREATE_V2 , &args); +if (ret <0 ) +return -1 ; + +return 0 ; +} + +static int btrfs_create_snapshot (int fd,int parent_fd,const char *name, +int flags) +{ +struct btrfs_ioctl_vol_args_v2 args = { + .fd = fd, + }; +size_t len; +int ret; + +if (flags & ~BTRFS_SUBVOL_RDONLY ) +return -EINVAL ; + + len =strlen (name); +if (len >=sizeof (args.name)) +return -ENAMETOOLONG ; +memcpy (args.name, name, len); + args.name[len] ='\0' ; + +if (flags &BTRFS_SUBVOL_RDONLY ) + args.flags |=BTRFS_SUBVOL_RDONLY ; + ret =ioctl (parent_fd,BTRFS_IOC_SNAP_CREATE_V2 , &args); +if (ret <0 ) +return -1 ; + +return 0 ; +} + +static int btrfs_get_subvolume_ro (int fd,bool *read_only_ret) +{ +uint64_t flags; +int ret; + + ret =ioctl (fd,BTRFS_IOC_SUBVOL_GETFLAGS , &flags); +if (ret <0 ) +return -1 ; + + *read_only_ret = flags &BTRFS_SUBVOL_RDONLY ; +return 0 ; +} + +static int btrfs_set_subvolume_ro (int fd,bool read_only) +{ +uint64_t flags; +int ret; + + ret =ioctl (fd,BTRFS_IOC_SUBVOL_GETFLAGS , &flags); +if (ret <0 ) +return -1 ; + +if (read_only) + flags |=BTRFS_SUBVOL_RDONLY ; +else + flags &= ~BTRFS_SUBVOL_RDONLY ; + + ret =ioctl (fd,BTRFS_IOC_SUBVOL_SETFLAGS , &flags); +if (ret <0 ) +return -1 ; + +return 0 ; +} + +static int btrfs_get_subvolume_id (int fd,uint64_t *id_ret) +{ +struct btrfs_ioctl_ino_lookup_args args = { + .treeid =0 , + .objectid =BTRFS_FIRST_FREE_OBJECTID , + }; +int ret; + + ret =ioctl (fd,BTRFS_IOC_INO_LOOKUP , &args); +if (ret <0 ) +return -1 ; + + *id_ret = args.treeid; + +return 0 ; +} + +/* +* The following helpers are adapted from the btrfsutils library. We can't use +* the library directly since we need full control over how the subvolume +* iteration happens. We need to be able to check whether unprivileged +* subvolume iteration is possible, i.e. whether BTRFS_IOC_INO_LOOKUP_USER is +* available and also ensure that it is actually used when looking up paths. +*/ +struct btrfs_stack { +uint64_t tree_id; +struct btrfs_ioctl_get_subvol_rootref_args rootref_args; +size_t items_pos; +size_t path_len; +}; + +struct btrfs_iter { +int fd; +int cur_fd; + +struct btrfs_stack *search_stack; +size_t stack_len; +size_t stack_capacity; + +char *cur_path; +size_t cur_path_capacity; +}; + +static struct btrfs_stack *top_stack_entry (struct btrfs_iter *iter) +{ +return &iter->search_stack[iter->stack_len -1 ]; +} + +static int pop_stack (struct btrfs_iter *iter) +{ +struct btrfs_stack *top, *parent; +int fd, parent_fd; +size_t i; + +if (iter->stack_len ==1 ) { + iter->stack_len--; +return 0 ; + } + + top =top_stack_entry (iter); + iter->stack_len--; + parent =top_stack_entry (iter); + + fd = iter->cur_fd; +for (i = parent->path_len; i < top->path_len; i++) { +if (i ==0 || iter->cur_path[i] =='/' ) { + parent_fd =openat (fd,".." ,O_RDONLY ); +if (fd != iter->cur_fd) +close (fd); +if (parent_fd ==-1 ) +return -1 ; + fd = parent_fd; + } + } +if (iter->cur_fd != iter->fd) +close (iter->cur_fd); + iter->cur_fd = fd; + +return 0 ; +} + +static int append_stack (struct btrfs_iter *iter,uint64_t tree_id,size_t path_len) +{ +struct btrfs_stack *entry; + +if (iter->stack_len >= iter->stack_capacity) { +size_t new_capacity = iter->stack_capacity *2 ; +struct btrfs_stack *new_search_stack; + + new_search_stack =reallocarray (iter->search_stack, new_capacity, +sizeof (*iter->search_stack)); +if (!new_search_stack) +return -ENOMEM ; + + iter->stack_capacity = new_capacity; + iter->search_stack = new_search_stack; + } + + entry = &iter->search_stack[iter->stack_len]; + +memset (entry,0 ,sizeof (*entry)); + entry->path_len = path_len; + entry->tree_id = tree_id; + +if (iter->stack_len) { +struct btrfs_stack *top; +char *path; +int fd; + + top =top_stack_entry (iter); + path = &iter->cur_path[top->path_len]; +if (*path =='/' ) + path++; + fd =openat (iter->cur_fd, path,O_RDONLY ); +if (fd ==-1 ) +return -errno; + +close (iter->cur_fd); + iter->cur_fd = fd; + } + + iter->stack_len++; + +return 0 ; +} + +static int btrfs_iterator_start (int fd,uint64_t top,struct btrfs_iter **ret) +{ +struct btrfs_iter *iter; +int err; + + iter =malloc (sizeof (*iter)); +if (!iter) +return -ENOMEM ; + + iter->fd = fd; + iter->cur_fd = fd; + + iter->stack_len =0 ; + iter->stack_capacity =4 ; + iter->search_stack =malloc (sizeof (*iter->search_stack) * + iter->stack_capacity); +if (!iter->search_stack) { + err = -ENOMEM ; + goto out_iter; + } + + iter->cur_path_capacity =256 ; + iter->cur_path =malloc (iter->cur_path_capacity); +if (!iter->cur_path) { + err = -ENOMEM ; + goto out_search_stack; + } + + err =append_stack (iter, top,0 ); +if (err) + goto out_cur_path; + + *ret = iter; + +return 0 ; + +out_cur_path: +free (iter->cur_path); +out_search_stack: +free (iter->search_stack); +out_iter: +free (iter); +return err; +} + +static void btrfs_iterator_end (struct btrfs_iter *iter) +{ +if (iter) { +free (iter->cur_path); +free (iter->search_stack); +if (iter->cur_fd != iter->fd) +close (iter->cur_fd); +close (iter->fd); +free (iter); + } +} + +static int __append_path (struct btrfs_iter *iter,const char *name, +size_t name_len,const char *dir,size_t dir_len, +size_t *path_len_ret) +{ +struct btrfs_stack *top =top_stack_entry (iter); +size_t path_len; +char *p; + + path_len = top->path_len; +/* +* We need a joining slash if we have a current path and a subdirectory. +*/ +if (top->path_len && dir_len) + path_len++; + path_len += dir_len; +/* +* We need another joining slash if we have a current path and a name, +* but not if we have a subdirectory, because the lookup ioctl includes +* a trailing slash. +*/ +if (top->path_len && !dir_len && name_len) + path_len++; + path_len += name_len; + +/* We need one extra character for the NUL terminator. */ +if (path_len +1 > iter->cur_path_capacity) { +char *tmp =realloc (iter->cur_path, path_len +1 ); + +if (!tmp) +return -ENOMEM ; + iter->cur_path = tmp; + iter->cur_path_capacity = path_len +1 ; + } + + p = iter->cur_path + top->path_len; +if (top->path_len && dir_len) + *p++ ='/' ; +memcpy (p, dir, dir_len); + p += dir_len; +if (top->path_len && !dir_len && name_len) + *p++ ='/' ; +memcpy (p, name, name_len); + p += name_len; + *p ='\0' ; + + *path_len_ret = path_len; + +return 0 ; +} + +static int get_subvolume_path (struct btrfs_iter *iter,uint64_t treeid, +uint64_t dirid,size_t *path_len_ret) +{ +struct btrfs_ioctl_ino_lookup_user_args args = { + .treeid = treeid, + .dirid = dirid, + }; +int ret; + + ret =ioctl (iter->cur_fd,BTRFS_IOC_INO_LOOKUP_USER , &args); +if (ret ==-1 ) +return -1 ; + +return __append_path (iter, args.name,strlen (args.name), args.path, +strlen (args.path), path_len_ret); +} + +static int btrfs_iterator_next (struct btrfs_iter *iter,char **path_ret, +uint64_t *id_ret) +{ +struct btrfs_stack *top; +uint64_t treeid, dirid; +size_t path_len; +int ret, err; + +for (;;) { +for (;;) { +if (iter->stack_len ==0 ) +return 1 ; + + top =top_stack_entry (iter); +if (top->items_pos < top->rootref_args.num_items) { +break ; + }else { + ret =ioctl (iter->cur_fd, +BTRFS_IOC_GET_SUBVOL_ROOTREF , + &top->rootref_args); +if (ret ==-1 && errno !=EOVERFLOW ) +return -1 ; + top->items_pos =0 ; + +if (top->rootref_args.num_items ==0 ) { + err =pop_stack (iter); +if (err) +return err; + } + } + } + + treeid = top->rootref_args.rootref[top->items_pos].treeid; + dirid = top->rootref_args.rootref[top->items_pos].dirid; + top->items_pos++; + err =get_subvolume_path (iter, treeid, dirid, &path_len); +if (err) { +/* Skip the subvolume if we can't access it. */ +if (errno ==EACCES ) +continue ; +return err; + } + + err =append_stack (iter, treeid, path_len); +if (err) { +/* +* Skip the subvolume if it does not exist (which can +* happen if there is another filesystem mounted over a +* parent directory) or we don't have permission to +* access it. +*/ +if (errno ==ENOENT || errno ==EACCES ) +continue ; +return err; + } + + top =top_stack_entry (iter); + goto out; + } + +out: +if (path_ret) { + *path_ret =malloc (top->path_len +1 ); +if (!*path_ret) +return -ENOMEM ; +memcpy (*path_ret, iter->cur_path, top->path_len); + (*path_ret)[top->path_len] ='\0' ; + } +if (id_ret) + *id_ret = top->tree_id; +return 0 ; +} + +#define BTRFS_SUBVOLUME1 "subvol1" +#define BTRFS_SUBVOLUME1_SNAPSHOT1 "subvol1_snapshot1" +#define BTRFS_SUBVOLUME1_SNAPSHOT1_RO "subvol1_snapshot1_ro" +#define BTRFS_SUBVOLUME1_RENAME "subvol1_rename" +#define BTRFS_SUBVOLUME2 "subvol2" + +static int btrfs_subvolumes_fsids_mapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_fsids (10000 ,10000 )) +die ("failure: switch fsids" ); + +if (!caps_up ()) +die ("failure: raise caps" ); + +/* +* The caller's fsids now have mappings in the idmapped mount so +* any file creation must succeed. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: check ownership" ); + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: check ownership" ); + +if (!caps_down ()) +die ("failure: lower caps" ); + +/* +* The filesystem is not mounted with user_subvol_rm_allowed so +* subvolume deletion must fail. +*/ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: check ownership" ); + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_fsids_mapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +/* The caller's fsids now have mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) +die ("failure: check ownership" ); + +/* remove subvolume */ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_fsids_unmapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; + +/* create directory for rename test */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +/* change ownership of all files to uid 0 */ +if (fchownat (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: fchownat" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +if (!switch_fsids (0 ,0 )) { +log_stderr ("failure: switch_fsids" ); + goto out; + } + +/* +* The caller's fsids don't have a mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* create subvolume */ +if (!btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME2 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } +if (errno !=EOVERFLOW ) { +log_stderr ("failure: errno" ); + goto out; + } + +/* try to rename a subvolume */ +if (!renameat2 (open_tree_fd,BTRFS_SUBVOLUME1 , open_tree_fd, +BTRFS_SUBVOLUME1_RENAME ,0 )) { +log_stderr ("failure: renameat2" ); + goto out; + } +if (errno !=EOVERFLOW ) { +log_stderr ("failure: errno" ); + goto out; + } + +/* The caller is privileged over the inode so file deletion must work. */ + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_fsids_unmapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF , userns_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +/* create directory for rename test */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +/* change ownership of all files to uid 0 */ +if (fchownat (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: fchownat" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + userns_fd =get_userns_fd (0 ,30000 ,10000 ); +if (userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 , + t_overflowuid, t_overflowgid)) +die ("failure: expected_uid_gid" ); + +if (!expected_uid_gid (open_tree_fd,BTRFS_SUBVOLUME1 ,0 , + t_overflowuid, t_overflowgid)) +die ("failure: expected_uid_gid" ); + +/* +* The caller's fsids don't have a mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* create subvolume */ +if (!btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME2 )) +die ("failure: btrfs_create_subvolume" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* try to rename a subvolume */ +if (!renameat2 (open_tree_fd,BTRFS_SUBVOLUME1 , open_tree_fd, +BTRFS_SUBVOLUME1_RENAME ,0 )) +die ("failure: renameat2" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* +* The caller is not privileged over the inode so subvolume +* deletion must fail. +*/ + +/* remove subvolume */ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); +safe_close (userns_fd); + +return fret; +} + +static int btrfs_snapshots_fsids_mapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; + +if (!switch_fsids (10000 ,10000 )) +die ("failure: switch fsids" ); + +if (!caps_up ()) +die ("failure: raise caps" ); + +/* The caller's fsids now have mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +/* create read-only snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +safe_close (subvolume_fd); + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +/* remove read-write snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) +die ("failure: btrfs_delete_subvolume" ); + +/* remove read-only snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) +die ("failure: btrfs_delete_subvolume" ); + +/* create directory */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +/* create read-only snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + +/* remove read-write snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + +/* remove read-only snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_fsids_mapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; + +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + +/* create read-only snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +/* remove read-write snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +/* remove read-only snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_fsids_unmapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* create directory for rename test */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +/* change ownership of all files to uid 0 */ +if (fchownat (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: fchownat" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr, +sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; + +if (!switch_fsids (0 ,0 )) { +log_stderr ("failure: switch_fsids" ); + goto out; + } + +/* +* The caller's fsids don't have a mappings in the idmapped +* mount so any file creation must fail. +*/ + +/* +* The open_tree() syscall returns an O_PATH file descriptor +* which we can't use with ioctl(). So let's reopen it as a +* proper file descriptor. +*/ + tree_fd =openat (open_tree_fd,"." , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) +die ("failure: openat" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create directory */ +if (!btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME2 )) +die ("failure: btrfs_create_subvolume" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* create read-write snapshot */ +if (!btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* create read-only snapshot */ +if (!btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* try to rename a directory */ +if (!renameat2 (open_tree_fd,BTRFS_SUBVOLUME1 , open_tree_fd, +BTRFS_SUBVOLUME1_RENAME ,0 )) +die ("failure: renameat2" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +if (!caps_down ()) +die ("failure: caps_down" ); + +/* create read-write snapshot */ +if (!btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +/* create read-only snapshot */ +if (!btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +/* +* The caller is not privileged over the inode so subvolume +* deletion must fail. +*/ + +/* remove directory */ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +if (!caps_up ()) +die ("failure: caps_down" ); + +/* +* The caller is privileged over the inode so subvolume +* deletion must work. +*/ + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_fsids_unmapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , subvolume_fd = -EBADF , tree_fd = -EBADF , + userns_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* create directory for rename test */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +/* change ownership of all files to uid 0 */ +if (fchownat (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: fchownat" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + userns_fd =get_userns_fd (0 ,30000 ,10000 ); +if (userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr, +sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor +* which we can't use with ioctl(). So let's reopen it as a +* proper file descriptor. +*/ + tree_fd =openat (open_tree_fd,"." , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 , + t_overflowuid, t_overflowgid)) +die ("failure: expected_uid_gid" ); + +if (!expected_uid_gid (open_tree_fd,BTRFS_SUBVOLUME1 ,0 , + t_overflowuid, t_overflowgid)) +die ("failure: expected_uid_gid" ); + +/* +* The caller's fsids don't have a mappings in the idmapped +* mount so any file creation must fail. +*/ + +/* create directory */ +if (!btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME2 )) +die ("failure: btrfs_create_subvolume" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* create read-write snapshot */ +if (!btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +/* create read-only snapshot */ +if (!btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +/* try to rename a directory */ +if (!renameat2 (open_tree_fd,BTRFS_SUBVOLUME1 , open_tree_fd, +BTRFS_SUBVOLUME1_RENAME ,0 )) +die ("failure: renameat2" ); +if (errno !=EOVERFLOW ) +die ("failure: errno" ); + +/* +* The caller is not privileged over the inode so subvolume +* deletion must fail. +*/ + +/* remove directory */ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (subvolume_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_fsids_mapped_user_subvol_rm_allowed (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_mnt_scratch_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_fsids (10000 ,10000 )) +die ("failure: switch fsids" ); + +if (!caps_down ()) +die ("failure: raise caps" ); + +/* +* The caller's fsids now have mappings in the idmapped mount so +* any file creation must succedd. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: check ownership" ); + +/* +* The scratch device is mounted with user_subvol_rm_allowed so +* subvolume deletion must succeed. +*/ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_fsids_mapped_userns_user_subvol_rm_allowed (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_mnt_scratch_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +/* The caller's fsids now have mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) +die ("failure: check ownership" ); + +/* +* The scratch device is mounted with user_subvol_rm_allowed so +* subvolume deletion must succeed. +*/ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_fsids_mapped_user_subvol_rm_allowed (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_mnt_scratch_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; + +if (!switch_fsids (10000 ,10000 )) +die ("failure: switch fsids" ); + +if (!caps_down ()) +die ("failure: raise caps" ); + +/* +* The caller's fsids now have mappings in the idmapped mount so +* any file creation must succeed. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +/* create read-only snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + +safe_close (subvolume_fd); + +/* remove subvolume */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +/* remove read-write snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) +die ("failure: btrfs_delete_subvolume" ); + +/* remove read-only snapshot */ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) +die ("failure: btrfs_delete_subvolume" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +if (btrfs_set_subvolume_ro (subvolume_fd, false)) +die ("failure: btrfs_set_subvolume_ro" ); + +safe_close (subvolume_fd); + +/* remove read-only snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_fsids_mapped_userns_user_subvol_rm_allowed (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_mnt_scratch_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; + +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + +/* create read-only snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +BTRFS_SUBVOL_RDONLY )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_delete_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + +/* remove read-write snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) +die ("failure: btrfs_delete_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + +/* remove read-only snapshot */ +if (!btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) +die ("failure: btrfs_delete_subvolume" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +if (btrfs_set_subvolume_ro (subvolume_fd, false)) +die ("failure: btrfs_set_subvolume_ro" ); + +safe_close (subvolume_fd); + +/* remove read-only snapshot */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1_RO )) +die ("failure: btrfs_delete_subvolume" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_delete_by_spec_id (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , subvolume_fd = -EBADF , tree_fd = -EBADF ; +uint64_t subvolume_id1 = -EINVAL , subvolume_id2 = -EINVAL ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + +/* create subvolume */ +if (btrfs_create_subvolume (t_mnt_scratch_fd,"A" )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +/* create subvolume */ +if (btrfs_create_subvolume (t_mnt_scratch_fd,"B" )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + + subvolume_fd =openat (t_mnt_scratch_fd,"B" ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* create subvolume */ +if (btrfs_create_subvolume (subvolume_fd,"C" )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +safe_close (subvolume_fd); + + subvolume_fd =openat (t_mnt_scratch_fd,"A" ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +if (btrfs_get_subvolume_id (subvolume_fd, &subvolume_id1)) { +log_stderr ("failure: btrfs_get_subvolume_id" ); + goto out; + } + + subvolume_fd =openat (t_mnt_scratch_fd,"B/C" ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +if (btrfs_get_subvolume_id (subvolume_fd, &subvolume_id2)) { +log_stderr ("failure: btrfs_get_subvolume_id" ); + goto out; + } + +if (sys_mount (t_device_scratch, t_mountpoint,"btrfs" ,0 ,"subvol=B/C" )) { +log_stderr ("failure: mount" ); + goto out; + } + + open_tree_fd =sys_open_tree (-EBADF , t_mountpoint, +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +/* +* The subvolume isn't exposed in the idmapped mount so +* delation via spec id must fail. +*/ +if (!btrfs_delete_subvolume_id (tree_fd, subvolume_id1)) +die ("failure: btrfs_delete_subvolume_id" ); +if (errno !=EOPNOTSUPP ) +die ("failure: errno" ); + +if (btrfs_delete_subvolume_id (t_mnt_scratch_fd, subvolume_id1)) +die ("failure: btrfs_delete_subvolume_id" ); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); +sys_umount2 (t_mountpoint,MNT_DETACH ); +btrfs_delete_subvolume_id (t_mnt_scratch_fd, subvolume_id2); +btrfs_delete_subvolume (t_mnt_scratch_fd,"B" ); + +return fret; +} + +static int btrfs_subvolumes_setflags_fsids_mapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; +bool read_only = false; + +if (!switch_fsids (10000 ,10000 )) +die ("failure: switch fsids" ); + +if (!caps_down ()) +die ("failure: raise caps" ); + +/* The caller's fsids now have mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +if (btrfs_set_subvolume_ro (subvolume_fd, true)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (!read_only) +die ("failure: not read_only" ); + +if (btrfs_set_subvolume_ro (subvolume_fd, false)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_setflags_fsids_mapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; +bool read_only = false; + +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +/* The caller's fsids now have mappings in the idmapped mount so +* any file creation must fail. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +if (btrfs_set_subvolume_ro (subvolume_fd, true)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (!read_only) +die ("failure: not read_only" ); + +if (btrfs_set_subvolume_ro (subvolume_fd, false)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_setflags_fsids_unmapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* create subvolume */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; +bool read_only = false; + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +if (!switch_fsids (0 ,0 )) +die ("failure: switch fsids" ); + +if (!caps_down ()) +die ("failure: raise caps" ); + +/* +* The caller's fsids don't have mappings in the idmapped mount +* so any file creation must fail. +*/ + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +if (!btrfs_set_subvolume_ro (subvolume_fd, true)) +die ("failure: btrfs_set_subvolume_ro" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_subvolumes_setflags_fsids_unmapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF , userns_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + +/* Changing mount properties on a detached mount. */ + userns_fd =get_userns_fd (0 ,30000 ,10000 ); +if (userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* create subvolume */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int subvolume_fd = -EBADF ; +bool read_only = false; + +/* +* The caller's fsids don't have mappings in the idmapped mount +* so any file creation must fail. +*/ + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +if (!switch_userns (userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 , + t_overflowuid, t_overflowgid)) +die ("failure: expected_uid_gid" ); + +if (!expected_uid_gid (open_tree_fd,BTRFS_SUBVOLUME1 ,0 , + t_overflowuid, t_overflowgid)) +die ("failure: expected_uid_gid" ); + +if (btrfs_get_subvolume_ro (subvolume_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +if (!btrfs_set_subvolume_ro (subvolume_fd, true)) +die ("failure: btrfs_set_subvolume_ro" ); +if (errno !=EPERM ) +die ("failure: errno" ); + +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); +safe_close (userns_fd); + +return fret; +} + +static int btrfs_snapshots_setflags_fsids_mapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int snapshot_fd = -EBADF , subvolume_fd = -EBADF ; +bool read_only = false; + +if (!switch_fsids (10000 ,10000 )) +die ("failure: switch fsids" ); + +if (!caps_down ()) +die ("failure: raise caps" ); + +/* +* The caller's fsids now have mappings in the idmapped mount +* so any file creation must succeed. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,10000 ,10000 )) +die ("failure: expected_uid_gid" ); + + snapshot_fd =openat (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (snapshot_fd <0 ) +die ("failure: openat" ); + +if (btrfs_get_subvolume_ro (snapshot_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +if (btrfs_set_subvolume_ro (snapshot_fd, true)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (snapshot_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (!read_only) +die ("failure: not read_only" ); + +if (btrfs_set_subvolume_ro (snapshot_fd, false)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (snapshot_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +safe_close (snapshot_fd); +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_setflags_fsids_mapped_userns (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int snapshot_fd = -EBADF , subvolume_fd = -EBADF ; +bool read_only = false; + +if (!switch_userns (attr.userns_fd,0 ,0 , false)) +die ("failure: switch_userns" ); + +/* +* The caller's fsids now have mappings in the idmapped mount so +* any file creation must succeed. +*/ + +/* create subvolume */ +if (btrfs_create_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) +die ("failure: btrfs_create_subvolume" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + + subvolume_fd =openat (tree_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) +die ("failure: openat" ); + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, tree_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) +die ("failure: btrfs_create_snapshot" ); + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,0 ,0 )) +die ("failure: expected_uid_gid" ); + + snapshot_fd =openat (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (snapshot_fd <0 ) +die ("failure: openat" ); + +if (btrfs_get_subvolume_ro (snapshot_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +if (btrfs_set_subvolume_ro (snapshot_fd, true)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (snapshot_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (!read_only) +die ("failure: not read_only" ); + +if (btrfs_set_subvolume_ro (snapshot_fd, false)) +die ("failure: btrfs_set_subvolume_ro" ); + +if (btrfs_get_subvolume_ro (snapshot_fd, &read_only)) +die ("failure: btrfs_get_subvolume_ro" ); + +if (read_only) +die ("failure: read_only" ); + +safe_close (snapshot_fd); +safe_close (subvolume_fd); + +exit (EXIT_SUCCESS ); + } +if (wait_for_pid (pid)) + goto out; + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + +/* remove directory */ +if (btrfs_delete_subvolume (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 )) { +log_stderr ("failure: btrfs_delete_subvolume" ); + goto out; + } + + fret =0 ; +log_debug ("Ran test" ); +out: +safe_close (attr.userns_fd); +safe_close (open_tree_fd); +safe_close (tree_fd); + +return fret; +} + +static int btrfs_snapshots_setflags_fsids_unmapped (void ) +{ +int fret =-1 ; +int open_tree_fd = -EBADF , subvolume_fd = -EBADF , tree_fd = -EBADF ; +struct mount_attr attr = { + .attr_set =MOUNT_ATTR_IDMAP , + }; +pid_t pid; + +if (!caps_supported ()) +return 0 ; + +/* Changing mount properties on a detached mount. */ + attr.userns_fd =get_userns_fd (0 ,10000 ,10000 ); +if (attr.userns_fd <0 ) { +log_stderr ("failure: get_userns_fd" ); + goto out; + } + + open_tree_fd =sys_open_tree (t_dir1_fd,"" , +AT_EMPTY_PATH | +AT_NO_AUTOMOUNT | +AT_SYMLINK_NOFOLLOW | +OPEN_TREE_CLOEXEC | +OPEN_TREE_CLONE ); +if (open_tree_fd <0 ) { +log_stderr ("failure: sys_open_tree" ); + goto out; + } + +if (sys_mount_setattr (open_tree_fd,"" ,AT_EMPTY_PATH , &attr,sizeof (attr))) { +log_stderr ("failure: sys_mount_setattr" ); + goto out; + } + +/* +* The open_tree() syscall returns an O_PATH file descriptor which we +* can't use with ioctl(). So let's reopen it as a proper file +* descriptor. +*/ + tree_fd =openat (open_tree_fd,"." ,O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (tree_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* create subvolume */ +if (btrfs_create_subvolume (t_dir1_fd,BTRFS_SUBVOLUME1 )) { +log_stderr ("failure: btrfs_create_subvolume" ); + goto out; + } + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1 ,0 ,0 ,0 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1 ,0 ,10000 ,10000 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + + subvolume_fd =openat (t_dir1_fd,BTRFS_SUBVOLUME1 , +O_RDONLY |O_CLOEXEC |O_DIRECTORY ); +if (subvolume_fd <0 ) { +log_stderr ("failure: openat" ); + goto out; + } + +/* create read-write snapshot */ +if (btrfs_create_snapshot (subvolume_fd, t_dir1_fd, +BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 )) { +log_stderr ("failure: btrfs_create_snapshot" ); + goto out; + } + +if (!expected_uid_gid (t_dir1_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,0 ,0 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + +if (!expected_uid_gid (tree_fd,BTRFS_SUBVOLUME1_SNAPSHOT1 ,0 ,10000 ,10000 )) { +log_stderr ("failure: expected_uid_gid" ); + goto out; + } + + pid =fork (); +if (pid <0 ) { +log_stderr ("failure: fork" ); + goto out; + } +if (pid ==0 ) { +int snapshot_fd = -EBADF ;
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.30.2
Part of the diff is not shown here. View the raw message.
Quoted 4 lines · Christian Brauner
From: Christian Brauner <[email protected]>
Hey,
Quoted 8 lines
This adds two new tests:
- a regression test for vfs capabilities
- a new test with nested idmapping layoutsIn addition, I've added a new test for btrfs which is on-track to support idmapped mounts in v5.15. I leave it to Eryu whether he wants to merge the btrfs test already or not. I think there's no harm in doing so but I'm also happy to wait.
The last patch hasn't showed up on the list. We had this problem before and the cause is likely that the patch is too large. The whole series including the 6th patch can be pulled from:
[email protected]:pub/scm/linux/kernel/git/brauner/xfstests-dev fs.idmapped.nested_userns [email protected]:brauner/xfstests.git fs.idmapped.nested_userns [email protected]:brauner/xfstests.git fs.idmapped.nested_userns
Thanks!
Christian
Re: [PATCH v2 1/6] idmapped-mounts: prepare for additional tests
In reply to Christian BraunerQuoted 5 lines · Christian Brauner
From: Christian Brauner <[email protected]>
- Use sequential option numbering for option parsing since we're only using longopts anyway and to easier correlate test parsing in the switch with the options specified in longopts.
These sequential options are rally weird. Why not use getopt_long_only?
Quoted 6 lines
- Introduce an explicit command line switch to runs the basic test suite. This prepares for the introduction of additional command line switches to run tests.
- Update help output.
- Usedie()when logging test failure.
Each of these changes really should be a separate patch.
Re: [PATCH v2 2/6] generic/640: add fscaps regression test
In reply to Christian BraunerLooks good,
Re: [PATCH v2 3/6] idmapped-mounts: refactor helpers
In reply to Christian BraunerLooks good,
Re: [PATCH v2 4/6] idmapped-mounts: add nested userns creation helpers
In reply to Christian BraunerLooks good,
Re: [PATCH v2 5/6] generic/641: add nested user namespace tests
In reply to Christian BraunerLooks good,
Re: [PATCH v2 1/6] idmapped-mounts: prepare for additional tests
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
Quoted 5 lines · Christian Brauner
From: Christian Brauner <[email protected]>
- Use sequential option numbering for option parsing since we're only using longopts anyway and to easier correlate test parsing in the switch with the options specified in longopts.
These sequential options are rally weird. Why not use
getopt_long_only?
Good point! Let me see if I can make that work.
Quoted 9 lines
Quoted 7 lines
- Introduce an explicit command line switch to runs the basic test suite. This prepares for the introduction of additional command line switches to run tests.
- Update help output.
- Usedie()when logging test failure.Each of these changes really should be a separate patch.
Ok, will split this up.
Thanks for the reviews, much appreciated!
Christian