Skip to content

Commit 6229be8

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

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

tests/clone3-report-ns-id.c

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
#include "scno.h"
21+
22+
static void
23+
retrieve_userns(int pid, char *userns_buf, size_t userns_buflen, int fd)
24+
{
25+
char c = 0;
26+
if (read(fd, &c, 1) != 1)
27+
perror_msg_and_fail("read from the child");
28+
29+
char *fname = xasprintf("/proc/%d/ns/user", pid);
30+
int rc = readlink(fname, userns_buf, userns_buflen - 1);
31+
if ((unsigned int) rc >= userns_buflen)
32+
perror_msg_and_fail("readlink");
33+
userns_buf[rc] = '\0';
34+
35+
if (write(fd, &c, 1) != 1)
36+
perror_msg_and_fail("write to the child");
37+
}
38+
39+
static void
40+
child_pause(int fd)
41+
{
42+
char c = 0;
43+
if (write(fd, &c, 1) != 1)
44+
return;
45+
if (read(fd, &c, 1) != 1)
46+
return;
47+
}
48+
49+
int
50+
main(void)
51+
{
52+
static int child_sockpair[2];
53+
if (socketpair(AF_UNIX, SOCK_STREAM, 0, child_sockpair) < 0)
54+
perror_msg_and_fail("socketpair");
55+
56+
struct clone_args arg = { .flags = CLONE_NEWUSER };
57+
int pid = syscall(__NR_clone3, &arg, sizeof(arg));
58+
if (pid < 0)
59+
perror_msg_and_fail("clone3");
60+
if (pid > 0) {
61+
printf("clone3({flags=CLONE_NEWUSER, exit_signal=0, stack=NULL, stack_size=0}"
62+
", %zu) = %s ", sizeof(arg), sprintrc(pid));
63+
64+
char userns[PATH_MAX];
65+
retrieve_userns(pid, userns, sizeof(userns), child_sockpair[0]);
66+
printf("(%s)\n", userns);
67+
puts("+++ exited with 0 +++");
68+
} else
69+
child_pause(child_sockpair[1]);
70+
return 0;
71+
}

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)