Skip to contentSkip to conversation

idmapped mounts: extend testsuite and fixes

PATCH In review Christian Brauner 2 participants 10 messages May 7, 2021 – May 23, 2021

lore
Share & tools
lore
#0 lore raw

cover idmapped mounts: extend testsuite and fixes

From: Christian Brauner <[email protected]>

Hey,

This introduces two new idmapped mount tests.
The first test extends the v3 fscaps tests for idmapped mounts. The second test verified that idmapped mounts behave correctly when nested user namespaces are used. In essence it creates a fairly complex nested user namespace hierarchy and then tests whether file ownership changes are correctly reflected in all idmapped mounts as seen from all those user namespaces.
In addition this fixes a couple of minor things and shares more code between the mount-idmapped and idmapped-mounts binaries.

Thanks!
Christian

Christian Brauner (7):
idmapped-mounts: remove unused set_cloexec() helper
idmapped-mounts: add missing newline to print_r()
idmapped-mounts: split out run_test() function generic/637: add fscaps regression test
idmapped-mounts: refactor helpers
idmapped-mounts: add nested userns creation helpers generic/638: add nested user namespace tests

 .gitignore                            |   2 +
 src/idmapped-mounts/Makefile          |  16 +-
 src/idmapped-mounts/idmapped-mounts.c | 931 ++++++++++++++++++++++++--
 src/idmapped-mounts/mount-idmapped.c  | 228 +------
 src/idmapped-mounts/utils.c           | 359 ++++++++--
 src/idmapped-mounts/utils.h           | 102 ++-
 tests/generic/637                     |  42 ++
 tests/generic/637.out                 |   2 +
 tests/generic/638                     |  42 ++
 tests/generic/638.out                 |   2 +
 tests/generic/group                   |   2 +
 11 files changed, 1397 insertions(+), 331 deletions(-)
 create mode 100755 tests/generic/637
 create mode 100644 tests/generic/637.out
 create mode 100755 tests/generic/638
 create mode 100644 tests/generic/638.out

base-commit: 40818883aecd19581a71cc096d07eb9106c11b10

Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/7 idmapped-mounts: remove unused set_cloexec() helper

From: Christian Brauner <[email protected]>

This function has never been used so remove it.

---
 src/idmapped-mounts/idmapped-mounts.c | 5 -----
 1 file changed, 5 deletions(-)
src/idmapped-mounts/idmapped-mounts.c +0 -5
index 870a8fe7..66bdd817 100644
@@ -371,11 +371,6 @@ static bool is_sticky(int dfd, const char *path, int flags)
 	return (st.st_mode & S_ISVTX) > 0;
 }
 
-static inline int set_cloexec(int fd)
-{
-	return fcntl(fd, F_SETFD, FD_CLOEXEC);
-}
-
 static inline bool switch_fsids(uid_t fsuid, gid_t fsgid)
 {
 	if (setfsgid(fsgid))
Signature · 1 line
2.27.0
#2 lore raw

PATCH 2/7 idmapped-mounts: add missing newline to print_r()

In reply to Christian Brauner

From: Christian Brauner <[email protected]>

The function missed to print a newline making the output difficult to read when running with DEBUG_TRACE.

---
 src/idmapped-mounts/idmapped-mounts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/idmapped-mounts/idmapped-mounts.c +1 -1
index 66bdd817..59a3daa8 100644
@@ -620,7 +620,7 @@ __attribute__((unused)) static int print_r(int fd, const char *path)
 		ret = fstatat(fd, path, &st,
 			      AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW);
 	if (!ret)
-		fprintf(stderr, "mode(%o):uid(%d):gid(%d) -> %s",
+		fprintf(stderr, "mode(%o):uid(%d):gid(%d) -> %s\n",
 			(st.st_mode & ~S_IFMT), st.st_uid, st.st_gid,
 			(path && *path) ? path : "(null)");
 
Signature · 1 line
2.27.0
#3 lore raw

PATCH 3/7 idmapped-mounts: split out run_test() function

In reply to Christian Brauner

From: Christian Brauner <[email protected]>

to make it easier to run subsets of tests.

---
 src/idmapped-mounts/idmapped-mounts.c | 70 ++++++++++++++++-----------
 1 file changed, 41 insertions(+), 29 deletions(-)
src/idmapped-mounts/idmapped-mounts.c +41 -29
index 59a3daa8..4d93b721 100644
@@ -82,6 +82,8 @@
 
 #define die(format, ...) die_errno(errno, format, ##__VA_ARGS__)
 
+#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
+
 uid_t t_overflowuid = 65534;
 gid_t t_overflowgid = 65534;
 
@@ -8735,7 +8737,7 @@ static const struct option longopts[] = {
 struct t_idmapped_mounts {
 	int (*test)(void);
 	const char *description;
-} t_idmapped_mounts[] = {
+} basic_suite[] = {
 	{ acls,								"posix acls on regular mounts",									},
 	{ create_in_userns,						"create operations in user namespace",								},
 	{ device_node_in_userns,					"device node in user namespace",								},
@@ -8787,9 +8789,44 @@ struct t_idmapped_mounts {
 	{ threaded_idmapped_mount_interactions,				"threaded operations on idmapped mounts",							},
 };
 
+static bool run_test(struct t_idmapped_mounts suite[], size_t suite_size)
+{
+	int i;
+
+	for (i = 0; i < suite_size; i++) {
+		struct t_idmapped_mounts *t = &suite[i];
+		int ret;
+		pid_t pid;
+
+		test_setup();
+
+		pid = fork();
+		if (pid < 0)
+			return false;
+
+		if (pid == 0) {
+			ret = t->test();
+			if (ret) {
+				fprintf(stderr, "failure: %s\n", t->description);
+				exit(EXIT_FAILURE);
+			}
+
+			exit(EXIT_SUCCESS);
+		}
+
+		ret = wait_for_pid(pid);
+		test_cleanup();
+
+		if (ret)
+			return false;
+	}
+
+	return true;
+}
+
 int main(int argc, char *argv[])
 {
-	int i, fret, ret;
+	int fret, ret;
 	int index = 0;
 	bool supported = false;
 
@@ -8876,33 +8913,8 @@ int main(int argc, char *argv[])
 
 	fret = EXIT_FAILURE;
 
-	/* Proper test suite run. */
-	for (i = 0; i < (sizeof(t_idmapped_mounts) / sizeof(t_idmapped_mounts[0])); i++) {
-		struct t_idmapped_mounts *t = &t_idmapped_mounts[i];
-		pid_t pid;
-
-		test_setup();
-
-		pid = fork();
-		if (pid < 0)
-			goto out;
-
-		if (pid == 0) {
-			ret = t->test();
-			if (ret) {
-				fprintf(stderr, "failure: %s\n", t->description);
-				exit(EXIT_FAILURE);
-			}
-
-			exit(EXIT_SUCCESS);
-		}
-
-		ret = wait_for_pid(pid);
-		test_cleanup();
-
-		if (ret)
-			goto out;
-	}
+	if (!run_test(basic_suite, ARRAY_SIZE(basic_suite)))
+		goto out;
 
 	fret = EXIT_SUCCESS;
 
Signature · 1 line
2.27.0
#4 lore raw

PATCH 4/7 generic/637: add fscaps regression test

In reply to Christian Brauner

From: 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")

---
 .gitignore                            |   1 +
 src/idmapped-mounts/Makefile          |  12 ++-
 src/idmapped-mounts/idmapped-mounts.c | 135 +++++++++++++++++++++++++-
 tests/generic/637                     |  42 ++++++++
 tests/generic/637.out                 |   2 +
 tests/generic/group                   |   1 +
 6 files changed, 188 insertions(+), 5 deletions(-)
 create mode 100755 tests/generic/637
 create mode 100644 tests/generic/637.out
.gitignore +1 -0
index 4cc9c807..da48e6f8 100644
@@ -180,6 +180,7 @@
 /src/aio-dio-regress/aiodio_sparse2
 /src/idmapped-mounts/idmapped-mounts
 /src/idmapped-mounts/mount-idmapped
+/src/idmapped-mounts/fscaps-in-ancestor-userns
 /src/log-writes/replay-log
 /src/perf/*.pyc
 
src/idmapped-mounts/Makefile +10 -2
index ad4ddc99..1bab6471 100644
@@ -3,7 +3,9 @@
 TOPDIR = ../..
 include $(TOPDIR)/include/builddefs
 
-TARGETS = idmapped-mounts mount-idmapped
+BINS = idmapped-mounts mount-idmapped
+LINKS = fscaps-in-ancestor-userns
+TARGETS = $(BINS) $(LINKS)
 CFILES_IDMAPPED_MOUNTS = idmapped-mounts.c utils.c
 CFILES_MOUNT_IDMAPPED = mount-idmapped.c utils.c
 
@@ -29,12 +31,18 @@ idmapped-mounts: $(CFILES_IDMAPPED_MOUNTS)
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) $(CFILES_IDMAPPED_MOUNTS) -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
+fscaps-in-ancestor-userns:
+	ln -sf idmapped-mounts fscaps-in-ancestor-userns
+
 mount-idmapped: $(CFILES_MOUNT_IDMAPPED)
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) $(CFILES_MOUNT_IDMAPPED) -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
 install:
 	$(INSTALL) -m 755 -d $(PKG_LIB_DIR)/src/idmapped-mounts
-	$(INSTALL) -m 755 $(TARGETS) $(PKG_LIB_DIR)/src/idmapped-mounts
+	$(INSTALL) -m 755 $(BINS) $(PKG_LIB_DIR)/src/idmapped-mounts
+	cd $(PKG_LIB_DIR)/src/idmapped-mounts && \
+		rm -f $(LINKS) && \
+		$(LN_S) idmapped-mounts fscaps-in-ancestor-userns
 
 -include .dep
src/idmapped-mounts/idmapped-mounts.c +132 -3

165 lines not on this page · raw message

tests/generic/637 new file +42 -0

42 lines not on this page · raw message

tests/generic/637.out new file +2 -0
new file mode 100644
index 00000000..55a3d825
@@ -0,0 +1,2 @@
+QA output created by 637
+Silence is golden
tests/generic/group +1 -0
index 105763c4..9dfefdf4 100644
@@ -639,3 +639,4 @@
 634 auto quick atime bigtime
 635 auto quick atime bigtime shutdown
 636 auto quick swap
+637 auto quick idmapped
Signature · 1 line
2.27.0
#5 lore raw

Re: [PATCH 4/7] generic/637: add fscaps regression test

Quoted 7 lines · Christian Brauner

From: 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].

Quoted 6 lines

[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")

Sorry for the late review..

Quoted 213 lines
---
 .gitignore                            |   1 +
 src/idmapped-mounts/Makefile          |  12 ++-
 src/idmapped-mounts/idmapped-mounts.c | 135 +++++++++++++++++++++++++-
 tests/generic/637                     |  42 ++++++++
 tests/generic/637.out                 |   2 +
 tests/generic/group                   |   1 +
 6 files changed, 188 insertions(+), 5 deletions(-)
 create mode 100755 tests/generic/637
 create mode 100644 tests/generic/637.out
.gitignore +1 -0
index 4cc9c807..da48e6f8 100644
@@ -180,6 +180,7 @@
 /src/aio-dio-regress/aiodio_sparse2
 /src/idmapped-mounts/idmapped-mounts
 /src/idmapped-mounts/mount-idmapped
+/src/idmapped-mounts/fscaps-in-ancestor-userns
 /src/log-writes/replay-log
 /src/perf/*.pyc
 
src/idmapped-mounts/Makefile +10 -2
index ad4ddc99..1bab6471 100644
@@ -3,7 +3,9 @@
 TOPDIR = ../..
 include $(TOPDIR)/include/builddefs
 
-TARGETS = idmapped-mounts mount-idmapped
+BINS = idmapped-mounts mount-idmapped
+LINKS = fscaps-in-ancestor-userns
+TARGETS = $(BINS) $(LINKS)
 CFILES_IDMAPPED_MOUNTS = idmapped-mounts.c utils.c
 CFILES_MOUNT_IDMAPPED = mount-idmapped.c utils.c
 
@@ -29,12 +31,18 @@ idmapped-mounts: $(CFILES_IDMAPPED_MOUNTS)
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) $(CFILES_IDMAPPED_MOUNTS) -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
+fscaps-in-ancestor-userns:
+	ln -sf idmapped-mounts fscaps-in-ancestor-userns
+
 mount-idmapped: $(CFILES_MOUNT_IDMAPPED)
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) $(CFILES_MOUNT_IDMAPPED) -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
 install:
 	$(INSTALL) -m 755 -d $(PKG_LIB_DIR)/src/idmapped-mounts
-	$(INSTALL) -m 755 $(TARGETS) $(PKG_LIB_DIR)/src/idmapped-mounts
+	$(INSTALL) -m 755 $(BINS) $(PKG_LIB_DIR)/src/idmapped-mounts
+	cd $(PKG_LIB_DIR)/src/idmapped-mounts && \
+		rm -f $(LINKS) && \
+		$(LN_S) idmapped-mounts fscaps-in-ancestor-userns
 
 -include .dep
src/idmapped-mounts/idmapped-mounts.c +121 -1
index 4d93b721..94b83c01 100644
@@ -3201,6 +3201,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;
@@ -8745,7 +8860,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",										},
@@ -8789,6 +8904,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;
@@ -8826,6 +8945,7 @@ static bool run_test(struct t_idmapped_mounts suite[], size_t suite_size)
 
 int main(int argc, char *argv[])
 {
+	const char *invocation_name;
 	int fret, ret;
 	int index = 0;
 	bool supported = false;

src/idmapped-mounts/idmapped-mounts.c int main(int argc, char *argv[]) · line 9040

Quoted 10 lines
src/idmapped-mounts/idmapped-mounts.c +5 -2
@@ -8913,8 +9033,17 @@ int main(int argc, char *argv[])
 
 	fret = EXIT_FAILURE;
 
-	if (!run_test(basic_suite, ARRAY_SIZE(basic_suite)))
-		goto out;
+	invocation_name = basename(argv[0]);
+	if (strcmp(invocation_name, "idmapped-mounts") == 0) {
+		if (!run_test(basic_suite, ARRAY_SIZE(basic_suite)))
+			goto out;
+	} else if (strcmp(invocation_name, "fscaps-in-ancestor-userns") == 0) {

I think there's no need to create symlink for each new test and use different invocation name to run the new test. How about always using the idmapped-mounts binary, just introduce a new option to specify which test group to run? e.g. introduce a "-t" option and run

./idmapped-mounts -t basic_suite # for existing tests, and ./idmapped-mounts -t fscaps_in_ancester_userns # for this new test

Thanks,
Eryu

Quoted 76 lines
src/idmapped-mounts/idmapped-mounts.c +6 -0
+		if (!run_test(fscaps_in_ancestor_userns,
+			      ARRAY_SIZE(fscaps_in_ancestor_userns)))
+			goto out;
+	} else {
+		die("idmapped mount test suite \"%s\" unknown", invocation_name);
+	}
 
 	fret = EXIT_SUCCESS;
 
tests/generic/637 new file +42 -0
new file mode 100755
index 00000000..25c601b5
@@ -0,0 +1,42 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2021 Christian Brauner.  All Rights Reserved.
+#
+# FS QA Test 637
+#
+# Test that fscaps on idmapped mounts behave correctly.
+#
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+# real QA test starts here
+
+_supported_fs generic
+_require_idmapped_mounts
+_require_test
+
+echo "Silence is golden"
+
+$here/src/idmapped-mounts/fscaps-in-ancestor-userns --device "$TEST_DEV" --mount "$TEST_DIR" --fstype "$FSTYP"
+
+status=$?
+exit
tests/generic/637.out new file +2 -0
new file mode 100644
index 00000000..55a3d825
@@ -0,0 +1,2 @@
+QA output created by 637
+Silence is golden
tests/generic/group +1 -0
index 105763c4..9dfefdf4 100644
@@ -639,3 +639,4 @@
 634 auto quick atime bigtime
 635 auto quick atime bigtime shutdown
 636 auto quick swap
+637 auto quick idmapped
Signature · 1 line
2.27.0
#6 lore raw

PATCH 5/7 idmapped-mounts: refactor helpers

In reply to Christian Brauner

From: 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.

---
 src/idmapped-mounts/mount-idmapped.c | 196 -------------------------
 src/idmapped-mounts/utils.c          | 209 +++++++++++++++++++--------
 src/idmapped-mounts/utils.h          |  73 +++++++++-
 3 files changed, 223 insertions(+), 255 deletions(-)
src/idmapped-mounts/mount-idmapped.c +0 -196
index 5f5ba5d2..ee3b4f05 100644
@@ -31,77 +31,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,
@@ -170,131 +99,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;
-
-	for (idmap_type_t 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.27.0
#7 lore raw

PATCH 6/7 idmapped-mounts: add nested userns creation helpers

In reply to Christian Brauner

From: 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
index 94b83c01..f5a48af7 100644
@@ -390,20 +390,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
index ee3b4f05..46d110fc 100644
@@ -33,36 +33,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'};
@@ -91,7 +61,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
index 4f976f9f..9694980e 100644
@@ -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.27.0
#8 lore raw

PATCH 7/7 generic/638: add nested user namespace tests

In reply to Christian Brauner

From: Christian Brauner <[email protected]>

Test ownership and ownership changes in a complex user namespace hierarchy.

---
 .gitignore                            |   1 +
 src/idmapped-mounts/Makefile          |   8 +-
 src/idmapped-mounts/idmapped-mounts.c | 715 ++++++++++++++++++++++++++
 src/idmapped-mounts/utils.h           |   4 +
 tests/generic/638                     |  42 ++
 tests/generic/638.out                 |   2 +
 tests/generic/group                   |   1 +
 7 files changed, 771 insertions(+), 2 deletions(-)
 create mode 100755 tests/generic/638
 create mode 100644 tests/generic/638.out
.gitignore +1 -0
index da48e6f8..713e4886 100644
@@ -181,6 +181,7 @@
 /src/idmapped-mounts/idmapped-mounts
 /src/idmapped-mounts/mount-idmapped
 /src/idmapped-mounts/fscaps-in-ancestor-userns
+/src/idmapped-mounts/nested-userns
 /src/log-writes/replay-log
 /src/perf/*.pyc
 
src/idmapped-mounts/Makefile +6 -2
index 1bab6471..a205c830 100644
@@ -4,7 +4,7 @@ TOPDIR = ../..
 include $(TOPDIR)/include/builddefs
 
 BINS = idmapped-mounts mount-idmapped
-LINKS = fscaps-in-ancestor-userns
+LINKS = fscaps-in-ancestor-userns nested-userns
 TARGETS = $(BINS) $(LINKS)
 CFILES_IDMAPPED_MOUNTS = idmapped-mounts.c utils.c
 CFILES_MOUNT_IDMAPPED = mount-idmapped.c utils.c
@@ -34,6 +34,9 @@ idmapped-mounts: $(CFILES_IDMAPPED_MOUNTS)
 fscaps-in-ancestor-userns:
 	ln -sf idmapped-mounts fscaps-in-ancestor-userns
 
+nested-userns:
+	ln -sf idmapped-mounts nested-userns
+
 mount-idmapped: $(CFILES_MOUNT_IDMAPPED)
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) $(CFILES_MOUNT_IDMAPPED) -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
@@ -43,6 +46,7 @@ install:
 	$(INSTALL) -m 755 $(BINS) $(PKG_LIB_DIR)/src/idmapped-mounts
 	cd $(PKG_LIB_DIR)/src/idmapped-mounts && \
 		rm -f $(LINKS) && \
-		$(LN_S) idmapped-mounts fscaps-in-ancestor-userns
+		$(LN_S) idmapped-mounts fscaps-in-ancestor-userns && \
+		$(LN_S) idmapped-mounts nested-userns
 
 -include .dep
src/idmapped-mounts/idmapped-mounts.c +715 -0

733 lines not on this page · raw message

src/idmapped-mounts/utils.h +4 -0
index 9694980e..afb3c228 100644
@@ -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/638 new file +42 -0

42 lines not on this page · raw message

tests/generic/638.out new file +2 -0
new file mode 100644
index 00000000..3113b1e3
@@ -0,0 +1,2 @@
+QA output created by 638
+Silence is golden
tests/generic/group +1 -0
index 9dfefdf4..63bae9cc 100644
@@ -640,3 +640,4 @@
 635 auto quick atime bigtime shutdown
 636 auto quick swap
 637 auto quick idmapped
+638 auto quick idmapped
Signature · 1 line
2.27.0
#9 lore raw
In reply to Christian Brauner
Quoted 4 lines · Christian Brauner

From: Christian Brauner <[email protected]>

Hey,

Quoted 9 lines

This introduces two new idmapped mount tests.
The first test extends the v3 fscaps tests for idmapped mounts. The second test verified that idmapped mounts behave correctly when nested user namespaces are used. In essence it creates a fairly complex nested user namespace hierarchy and then tests whether file ownership changes are correctly reflected in all idmapped mounts as seen from all those user namespaces.
In addition this fixes a couple of minor things and shares more code between the mount-idmapped and idmapped-mounts binaries.

I applied the first 3 patches for this update. And I think patch 4 and 7 may need some rework. Patch 5 and 6 just didn't apply without patch 4.

Thanks,
Eryu

Quoted 34 lines

Thanks!
Christian

Christian Brauner (7):
idmapped-mounts: remove unused set_cloexec() helper
idmapped-mounts: add missing newline to print_r()
idmapped-mounts: split out run_test() function generic/637: add fscaps regression test
idmapped-mounts: refactor helpers
idmapped-mounts: add nested userns creation helpers generic/638: add nested user namespace tests

 .gitignore                            |   2 +
 src/idmapped-mounts/Makefile          |  16 +-
 src/idmapped-mounts/idmapped-mounts.c | 931 ++++++++++++++++++++++++--
 src/idmapped-mounts/mount-idmapped.c  | 228 +------
 src/idmapped-mounts/utils.c           | 359 ++++++++--
 src/idmapped-mounts/utils.h           | 102 ++-
 tests/generic/637                     |  42 ++
 tests/generic/637.out                 |   2 +
 tests/generic/638                     |  42 ++
 tests/generic/638.out                 |   2 +
 tests/generic/group                   |   2 +
 11 files changed, 1397 insertions(+), 331 deletions(-)
 create mode 100755 tests/generic/637
 create mode 100644 tests/generic/637.out
 create mode 100755 tests/generic/638
 create mode 100644 tests/generic/638.out

base-commit: 40818883aecd19581a71cc096d07eb9106c11b10

Signature · 1 line
2.27.0