Skip to content

Commit 399b263

Browse files
committed
tests: verify the auxstr of clone3(2) when --namespace=switch is given
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent dd7968a commit 399b263

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

tests/clone3-report-ns-id.c

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Check decoding of clone flags.
3+
*
4+
* Copyright (c) 2024 The strace developers.
5+
* All rights reserved.
6+
*
7+
* SPDX-License-Identifier: GPL-2.0-or-later
8+
*/
9+
10+
#include "tests.h"
11+
#include "xmalloc.h"
12+
13+
#include <limits.h>
14+
#include <linux/sched.h>
15+
#include <stdio.h>
16+
#include <sys/socket.h>
17+
#include <sys/syscall.h>
18+
#include <unistd.h>
19+
20+
static void
21+
retrieve_userns(int pid, char *userns_buf, size_t userns_buflen, int fd)
22+
{
23+
char c = 0;
24+
if (read(fd, &c, 1) != 1)
25+
perror_msg_and_fail("read from the child");
26+
27+
char *fname = xasprintf("/proc/%d/ns/user", pid);
28+
int rc = readlink(fname, userns_buf, userns_buflen - 1);
29+
if ((unsigned int) rc >= userns_buflen)
30+
perror_msg_and_fail("readlink");
31+
userns_buf[rc] = '\0';
32+
33+
if (write(fd, &c, 1) != 1)
34+
perror_msg_and_fail("write to the child");
35+
}
36+
37+
static void
38+
child_pause(int fd)
39+
{
40+
char c = 0;
41+
if (write(fd, &c, 1) != 1)
42+
return;
43+
if (read(fd, &c, 1) != 1)
44+
return;
45+
}
46+
47+
int
48+
main(void)
49+
{
50+
static int child_sockpair[2];
51+
if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_sockpair) < 0)
52+
perror_msg_and_fail("socketpair");
53+
54+
struct clone_args arg = { .flags = CLONE_NEWUSER };
55+
int pid = syscall(__NR_clone3, &arg, sizeof(arg));
56+
if (pid < 0)
57+
perror_msg_and_fail("clone3");
58+
if (pid > 0) {
59+
printf("clone3({flags=CLONE_NEWUSER, exit_signal=0, stack=NULL, stack_size=0}"
60+
", %zu) = %s ", sizeof(arg), sprintrc(pid));
61+
62+
char userns[PATH_MAX];
63+
retrieve_userns(pid, userns, sizeof(userns), child_sockpair[0]);
64+
printf("(%s)\n", userns);
65+
puts("+++ exited with 0 +++");
66+
} else
67+
child_pause(child_sockpair[1]);
68+
return 0;
69+
}

tests/gen_tests.in

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ clone3-success-Xabbrev -einject=clone3:retval=42 -etrace=clone3 -a16 -Xabbrev
6666
clone3-success-Xraw -einject=clone3:retval=42 -etrace=clone3 -a16 -Xraw
6767
clone3-success-Xverbose -einject=clone3:retval=42 -etrace=clone3 -a16 -Xverbose
6868
clone_parent +clone_ptrace.test
69+
clone3-report-ns-id -etrace=clone3 -a35 -enamespace=switch
6970
clone_parent--quiet-exit +clone_ptrace.test --quiet=exit,personality
7071
clone_parent-q +clone_ptrace.test -q
7172
clone_parent-qq +clone_ptrace.test -qq

tests/pure_executables.list

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ clone3
4444
clone3-Xabbrev
4545
clone3-Xraw
4646
clone3-Xverbose
47+
clone3-report-ns-id
4748
copy_file_range
4849
creat
4950
delete_module

0 commit comments

Comments
 (0)