selftests/open_tree: add OPEN_TREE_NAMESPACE tests

Add tests for OPEN_TREE_NAMESPACE.

Link: https://patch.msgid.link/[email protected]
Tested-by: Jeff Layton <[email protected]>
Reviewed-by: Jeff Layton <[email protected]>
Signed-off-by: Christian Brauner <[email protected]>
This commit is contained in:
Christian Brauner committed 2026-01-16 19:21:40 +01:00
1 parent 9b8a0ba682
commit b8f7622aa6
5 files changed
+1068

No files matched your search

@@ -0,0 +1 @@
open_tree_ns_test
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-2.0
TEST_GEN_PROGS := open_tree_ns_test
CFLAGS := -Wall -Werror -g $(KHDR_INCLUDES)
LDLIBS := -lcap
include ../../lib.mk
$(OUTPUT)/open_tree_ns_test: open_tree_ns_test.c ../utils.c
$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)
File diff suppressed because it is too large. Load diff
@@ -515,6 +515,32 @@ int setup_userns(void)
return 0;
}
int enter_userns(void)
{
int ret;
char buf[32];
uid_t uid = getuid();
gid_t gid = getgid();
ret = unshare(CLONE_NEWUSER);
if (ret)
return ret;
sprintf(buf, "0 %d 1", uid);
ret = write_file("/proc/self/uid_map", buf);
if (ret)
return ret;
ret = write_file("/proc/self/setgroups", "deny");
if (ret)
return ret;
sprintf(buf, "0 %d 1", gid);
ret = write_file("/proc/self/gid_map", buf);
if (ret)
return ret;
return 0;
}
/* caps_down - lower all effective caps */
int caps_down(void)
{
@@ -28,6 +28,7 @@ extern int cap_down(cap_value_t down);
extern bool switch_ids(uid_t uid, gid_t gid);
extern int setup_userns(void);
extern int enter_userns(void);
static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
{