Skip to content

Commit

Permalink
tests: verify the auxstr of clone(2) when --namespace=switchTo is given
Browse files Browse the repository at this point in the history
* tests/clone-flags.c: Include <sys/socket.h>.
(child_sockpair[2]): New file static variable.
(retrieve_userns): New function.
(wait_cloned): Extend parameters to pass a string representing
userns to caller.
(do_clone_newns): New macro.
(do_clone): Redefine using do_clone_newns.
(child_with_pause): New function.
(main): Do clone with CLONE_NEWUSER and print the new user
namespace.
* tests/clone-flags.test: Pass -e namespace=switchTo
option to strace.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Jun 26, 2024
1 parent 590f33c commit ff908da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
53 changes: 50 additions & 3 deletions tests/clone-flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,36 @@
#include <sys/wait.h>
#include <unistd.h>
#include <linux/sched.h>
#include <sys/socket.h>

static const int child_exit_status = 42;
static pid_t pid;
static int child_sockpair[2];

static void
retrieve_userns(pid_t pid, char *userns_buf, size_t userns_buflen)
{
char c = 0;
if (read(child_sockpair[0], &c, 1) != 1)
perror_msg_and_fail("read from the child");

char *fname = xasprintf("/proc/%d/ns/user", pid);
int rc = readlink(fname, userns_buf, userns_buflen - 1);
if ((unsigned int) rc >= userns_buflen)
perror_msg_and_fail("readlink");
userns_buf[rc] = '\0';

if (write(child_sockpair[0], &c, 1) != 1)
perror_msg_and_fail("write to the child");
}

static pid_t
wait_cloned(pid_t pid, int sig, const char *text)
wait_cloned(pid_t pid, int sig, const char *text, char *userns_buf, size_t userns_buflen)
{
if (pid < 0)
perror_msg_and_fail("clone %s", text);
if (userns_buf)
retrieve_userns(pid, userns_buf, userns_buflen);
int status;
pid_t rc = wait(&status);
if (sig) {
Expand All @@ -50,14 +71,20 @@ wait_cloned(pid_t pid, int sig, const char *text)
extern int __clone2(int (*)(void *), void *, size_t, int, void *, ...);
# define do_clone(fn_, stack_, size_, flags_, arg_, ...) \
wait_cloned(__clone2((fn_), (stack_), (size_), (flags_), (arg_), \
## __VA_ARGS__), (flags_) & 0xff, #flags_)
## __VA_ARGS__), (flags_) & 0xff, #flags_, NULL, 0)
# define do_clone_newns(userns_buf_, fn_, stack_, size_, flags_, arg_, ...) \
wait_cloned(__clone2((fn_), (stack_), (size_), (flags_), (arg_), \
## __VA_ARGS__), (flags_) & 0xff, #flags_, userns_buf_, sizeof(userns_buf_))
# define SYSCALL_NAME "clone2"
# define STACK_SIZE_FMT ", stack_size=%#lx"
# define STACK_SIZE_ARG child_stack_size,
#else
# define do_clone(fn_, stack_, size_, flags_, arg_, ...) \
wait_cloned(clone((fn_), (stack_), (flags_), (arg_), \
## __VA_ARGS__), (flags_) & 0xff, #flags_)
## __VA_ARGS__), (flags_) & 0xff, #flags_, NULL, 0)
# define do_clone_newns(userns_buf_, fn_, stack_, size_, flags_, arg_, ...) \
wait_cloned(clone((fn_), (stack_), (flags_), (arg_), \
## __VA_ARGS__), (flags_) & 0xff, #flags_, userns_buf_, sizeof(userns_buf_))
# define SYSCALL_NAME "clone"
# define STACK_SIZE_FMT ""
# define STACK_SIZE_ARG
Expand All @@ -69,6 +96,17 @@ child(void *const arg)
return child_exit_status;
}

static int
child_with_pause(void *const arg)
{
char c = 0;
if (write(child_sockpair[1], &c, 1) != 1)
return 1;
if (read(child_sockpair[1], &c, 1) != 1)
return 1;
return child_exit_status;
}

int
main(void)
{
Expand Down Expand Up @@ -143,5 +181,14 @@ main(void)
"CLONE_PIDFD|SIGCHLD", *ptid, buf, pid);
}

char userns[PATH_MAX];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_sockpair) < 0)
perror_msg_and_fail("socketpair");
pid = do_clone_newns(userns, child_with_pause, child_stack, child_stack_size,
CLONE_NEWUSER, 0);
printf("%s(child_stack=%#lx" STACK_SIZE_FMT ", flags=%s) = %d (%s)\n",
SYSCALL_NAME, child_stack_printed, STACK_SIZE_ARG
"CLONE_NEWUSER", pid, userns);

return 0;
}
2 changes: 1 addition & 1 deletion tests/clone-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ CHILD_STACK_REPORTED="$(sed -n 's/^clone[^(]*(child_stack=\(0x[[:xdigit:]]\+\),.
export CHILD_STACK_EXPECTED CHILD_STACK_REPORTED

# Use child stack addresses to check decoding.
run_strace_match_diff -a35 --silence=exits,personality -y -e signal=none -e trace=$syscall
run_strace_match_diff -a35 --silence=exits,personality -y -e signal=none -e trace=$syscall -e namespace=switchTo

0 comments on commit ff908da

Please sign in to comment.