aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2026-08-20 15:30:32 +1000
committerStefano Brivio <sbrivio@redhat.com>2026-09-08 16:00:22 +0200
commita8aedb8cd6283229da72ccb0d062f6e609bdba0c (patch)
treefe07221e2811056c86e63d979fe7a1ea89a289fd
parent9587a355ff6ed05a92ba226cc2a37e113c1d106c (diff)
downloadpasst-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.tar
passt-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.tar.gz
passt-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.tar.bz2
passt-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.tar.lz
passt-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.tar.xz
passt-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.tar.zst
passt-a8aedb8cd6283229da72ccb0d062f6e609bdba0c.zip
isolation: Create helper function to enter user namespace
Currently, isolate_user() open codes the steps needed to open and join an existing user namespace. Upcoming changes are going to want to re-use these steps, so move this logic into a helper function, Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--isolation.c30
1 files changed, 19 insertions, 11 deletions
diff --git a/isolation.c b/isolation.c
index a30b329..024c26a 100644
--- a/isolation.c
+++ b/isolation.c
@@ -293,6 +293,24 @@ int isolate_fds(int argc, char **argv)
}
/**
+ * enter_userns() - Enter a named user namespace
+ * @userns: userns path to enter
+ */
+static void enter_userns(const char *userns)
+{
+ int ufd;
+
+ ufd = open(userns, O_RDONLY | O_CLOEXEC);
+ if (ufd < 0)
+ die_perror("Couldn't open user namespace %s", userns);
+
+ if (setns(ufd, CLONE_NEWUSER) != 0)
+ die_perror("Couldn't enter user namespace %s", userns);
+
+ close(ufd);
+}
+
+/**
* isolate_user() - Switch to final UID/GID and move into userns
* @c: Execution context
* @uid: User ID to run as (in original userns)
@@ -325,17 +343,7 @@ void isolate_user(const struct ctx *c, uid_t uid, gid_t gid, bool use_userns,
die_perror("Can't set UID to %u", uid);
if (*userns) { /* If given a userns, join it */
- int ufd;
-
- ufd = open(userns, O_RDONLY | O_CLOEXEC);
- if (ufd < 0)
- die_perror("Couldn't open user namespace %s", userns);
-
- if (setns(ufd, CLONE_NEWUSER) != 0)
- die_perror("Couldn't enter user namespace %s", userns);
-
- close(ufd);
-
+ enter_userns(userns);
} else if (use_userns) { /* Create and join a new userns */
if (unshare(CLONE_NEWUSER) != 0)
die_perror("Couldn't create user namespace");