Skip to content

Commit

Permalink
tests: verify the auxstr of clone3(2) when --namespace=switchTo is given
Browse files Browse the repository at this point in the history
* tests/clone3-report-ns-id.c: New file.
* tests/pure_executables.list: Add clone3-report-ns-id.
* tests/.gitignore: Likewise.
* tests/gen_tests.in: New test.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Jun 13, 2024
1 parent 14c76ff commit 4f5ecae
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ clone3
clone3-Xabbrev
clone3-Xraw
clone3-Xverbose
clone3-report-ns-id
clone3-success
clone3-success-Xabbrev
clone3-success-Xraw
Expand Down
71 changes: 71 additions & 0 deletions tests/clone3-report-ns-id.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Check appending auxstr of clone3(2) when --namespace=switchTo is given
*
* Copyright (c) 2024 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "tests.h"
#include "xmalloc.h"

#include <limits.h>
#include <linux/sched.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>

#include "scno.h"

static void
retrieve_userns(int pid, char *userns_buf, size_t userns_buflen, int fd)
{
char c = 0;
if (read(fd, &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(fd, &c, 1) != 1)
perror_msg_and_fail("write to the child");
}

static void
child_pause(int fd)
{
char c = 0;
if (write(fd, &c, 1) != 1)
return;
if (read(fd, &c, 1) != 1)
return;
}

int
main(void)
{
static int child_sockpair[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_sockpair) < 0)
perror_msg_and_fail("socketpair");

struct clone_args arg = { .flags = CLONE_NEWUSER };
int pid = syscall(__NR_clone3, &arg, sizeof(arg));
if (pid < 0)
perror_msg_and_fail("clone3");
if (pid > 0) {
printf("clone3({flags=CLONE_NEWUSER, exit_signal=0, stack=NULL, stack_size=0}"
", %zu) = %s ", sizeof(arg), sprintrc(pid));

char userns[PATH_MAX];
retrieve_userns(pid, userns, sizeof(userns), child_sockpair[0]);
printf("(%s)\n", userns);
puts("+++ exited with 0 +++");
} else
child_pause(child_sockpair[1]);
return 0;
}
1 change: 1 addition & 0 deletions tests/gen_tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ clone3-success-Xabbrev -einject=clone3:retval=42 -etrace=clone3 -a16 -Xabbrev
clone3-success-Xraw -einject=clone3:retval=42 -etrace=clone3 -a16 -Xraw
clone3-success-Xverbose -einject=clone3:retval=42 -etrace=clone3 -a16 -Xverbose
clone_parent +clone_ptrace.test
clone3-report-ns-id -etrace=clone3 -a35 -enamespace=switchTo
clone_parent--quiet-exit +clone_ptrace.test --quiet=exit,personality
clone_parent-q +clone_ptrace.test -q
clone_parent-qq +clone_ptrace.test -qq
Expand Down
1 change: 1 addition & 0 deletions tests/pure_executables.list
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ clone3
clone3-Xabbrev
clone3-Xraw
clone3-Xverbose
clone3-report-ns-id
copy_file_range
creat
delete_module
Expand Down

0 comments on commit 4f5ecae

Please sign in to comment.