xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setns/setns02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2014-2020
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * functional test for setns(2) - reassociate thread with a namespace
6*49cdfc7eSAndroid Build Coastguard Worker  * 1. create child with CLONE_NEWUTS, set different hostname in child,
7*49cdfc7eSAndroid Build Coastguard Worker  *    set namespace back to parent ns and check that hostname has changed
8*49cdfc7eSAndroid Build Coastguard Worker  * 2. create child with CLONE_NEWIPC, set up shared memory in parent
9*49cdfc7eSAndroid Build Coastguard Worker  *    and verify that child can't shmat it, then set namespace
10*49cdfc7eSAndroid Build Coastguard Worker  *    back to parent one and verify that child is able to do shmat
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/shm.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/utsname.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_sysv_ipc.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "setns.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #define CHILD_STACK_SIZE (1024*1024)
30*49cdfc7eSAndroid Build Coastguard Worker #define CP "(child) "
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static char *dummy_hostname = "setns_dummy_uts";
33*49cdfc7eSAndroid Build Coastguard Worker static int ns_ipc_fd;
34*49cdfc7eSAndroid Build Coastguard Worker static int ns_uts_fd;
35*49cdfc7eSAndroid Build Coastguard Worker static key_t ipc_key;
36*49cdfc7eSAndroid Build Coastguard Worker static int shmid;
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
39*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
40*49cdfc7eSAndroid Build Coastguard Worker 
do_child_newuts(void * arg)41*49cdfc7eSAndroid Build Coastguard Worker static int do_child_newuts(void *arg)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	struct utsname uts, uts_parent;
44*49cdfc7eSAndroid Build Coastguard Worker 	int ns_flag = *(int *)arg;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (uname(&uts_parent) == -1)
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"uname");
48*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, CP"hostname (inherited from parent): %s",
49*49cdfc7eSAndroid Build Coastguard Worker 		uts_parent.nodename);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	if (sethostname(dummy_hostname, strlen(dummy_hostname)) == -1)
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"sethostname");
53*49cdfc7eSAndroid Build Coastguard Worker 	if (uname(&uts) == -1)
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"uname");
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, CP"hostname changed to: %s", uts.nodename);
57*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(uts_parent.nodename, uts.nodename) == 0) {
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, CP"expected hostname to be different");
59*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
60*49cdfc7eSAndroid Build Coastguard Worker 	} else {
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, CP"hostname is different in parent/child");
62*49cdfc7eSAndroid Build Coastguard Worker 	}
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, CP"attempting to switch ns back to parent ns");
65*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_syscall(__NR_setns, ns_uts_fd, ns_flag) == -1) {
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"setns");
67*49cdfc7eSAndroid Build Coastguard Worker 		return 2;
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker 	if (uname(&uts) == -1)
70*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"uname");
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, CP"hostname: %s", uts.nodename);
73*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(uts_parent.nodename, uts.nodename) != 0) {
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, CP"expected hostname to match parent");
75*49cdfc7eSAndroid Build Coastguard Worker 		return 3;
76*49cdfc7eSAndroid Build Coastguard Worker 	} else {
77*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, CP"hostname now as expected");
78*49cdfc7eSAndroid Build Coastguard Worker 	}
79*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
do_child_newipc(void * arg)82*49cdfc7eSAndroid Build Coastguard Worker static int do_child_newipc(void *arg)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	void *p;
85*49cdfc7eSAndroid Build Coastguard Worker 	int ns_flag = *(int *)arg;
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	p = shmat(shmid, NULL, 0);
88*49cdfc7eSAndroid Build Coastguard Worker 	if (p == (void *) -1) {
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS|TERRNO, CP"shmat failed as expected");
90*49cdfc7eSAndroid Build Coastguard Worker 	} else {
91*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, CP"shmat unexpectedly suceeded");
92*49cdfc7eSAndroid Build Coastguard Worker 		shmdt(p);
93*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
94*49cdfc7eSAndroid Build Coastguard Worker 	}
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, CP"attempting to switch ns back to parent ns");
97*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_syscall(__NR_setns, ns_ipc_fd, ns_flag) == -1) {
98*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"setns");
99*49cdfc7eSAndroid Build Coastguard Worker 		return 2;
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	p = shmat(shmid, NULL, 0);
103*49cdfc7eSAndroid Build Coastguard Worker 	if (p == (void *) -1) {
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, CP"shmat failed after setns");
105*49cdfc7eSAndroid Build Coastguard Worker 		return 3;
106*49cdfc7eSAndroid Build Coastguard Worker 	} else {
107*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, CP"shmat suceeded");
108*49cdfc7eSAndroid Build Coastguard Worker 		shmdt(p);
109*49cdfc7eSAndroid Build Coastguard Worker 	}
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker 
test_flag(int clone_flag,int ns_flag,int (* fn)(void * arg))114*49cdfc7eSAndroid Build Coastguard Worker static void test_flag(int clone_flag, int ns_flag, int (*fn) (void *arg))
115*49cdfc7eSAndroid Build Coastguard Worker {
116*49cdfc7eSAndroid Build Coastguard Worker 	void *child_stack;
117*49cdfc7eSAndroid Build Coastguard Worker 	int ret, status;
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	child_stack = SAFE_MALLOC(CHILD_STACK_SIZE);
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "creating child with clone_flag=0x%x, ns_flag=0x%x",
122*49cdfc7eSAndroid Build Coastguard Worker 		clone_flag, ns_flag);
123*49cdfc7eSAndroid Build Coastguard Worker 	ret = ltp_clone(SIGCHLD|clone_flag, fn, &ns_flag,
124*49cdfc7eSAndroid Build Coastguard Worker 		CHILD_STACK_SIZE, child_stack);
125*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1)
126*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TERRNO, "ltp_clone");
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(ret, &status, 0);
129*49cdfc7eSAndroid Build Coastguard Worker 	free(child_stack);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker 
test_all(void)132*49cdfc7eSAndroid Build Coastguard Worker void test_all(void)
133*49cdfc7eSAndroid Build Coastguard Worker {
134*49cdfc7eSAndroid Build Coastguard Worker 	if (ns_uts_fd != -1) {
135*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "test_newuts");
136*49cdfc7eSAndroid Build Coastguard Worker 		test_flag(CLONE_NEWUTS, CLONE_NEWUTS, do_child_newuts);
137*49cdfc7eSAndroid Build Coastguard Worker 		test_flag(CLONE_NEWUTS, 0, do_child_newuts);
138*49cdfc7eSAndroid Build Coastguard Worker 	} else
139*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "CLONE_NEWUTS is not supported");
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	if (ns_ipc_fd != -1) {
142*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "test_newipc");
143*49cdfc7eSAndroid Build Coastguard Worker 		test_flag(CLONE_NEWIPC, CLONE_NEWIPC, do_child_newipc);
144*49cdfc7eSAndroid Build Coastguard Worker 		test_flag(CLONE_NEWIPC, 0, do_child_newipc);
145*49cdfc7eSAndroid Build Coastguard Worker 	} else
146*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "CLONE_NEWIPC is not supported");
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)149*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
150*49cdfc7eSAndroid Build Coastguard Worker {
151*49cdfc7eSAndroid Build Coastguard Worker 	char tmp[PATH_MAX];
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 	/* runtime check if syscall is supported */
154*49cdfc7eSAndroid Build Coastguard Worker 	tst_syscall(__NR_setns, -1, 0);
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker 	/* check if kernel has CONFIG_*_NS set and exports /proc entries */
157*49cdfc7eSAndroid Build Coastguard Worker 	ns_ipc_fd = get_ns_fd(getpid(), "ipc");
158*49cdfc7eSAndroid Build Coastguard Worker 	ns_uts_fd = get_ns_fd(getpid(), "uts");
159*49cdfc7eSAndroid Build Coastguard Worker 	if (ns_ipc_fd == -1 && ns_uts_fd == -1)
160*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "your kernel has CONFIG_IPC_NS, "
161*49cdfc7eSAndroid Build Coastguard Worker 			"CONFIG_UTS_NS or CONFIG_PROC disabled");
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETCWD(tmp, PATH_MAX);
164*49cdfc7eSAndroid Build Coastguard Worker 	ipc_key = ftok(tmp, 65);
165*49cdfc7eSAndroid Build Coastguard Worker 	shmid = SAFE_SHMGET(ipc_key, getpagesize(), IPC_CREAT | 0666);
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)168*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
169*49cdfc7eSAndroid Build Coastguard Worker {
170*49cdfc7eSAndroid Build Coastguard Worker 	if (ns_ipc_fd != -1)
171*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(ns_ipc_fd);
172*49cdfc7eSAndroid Build Coastguard Worker 	if (ns_uts_fd != -1)
173*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(ns_uts_fd);
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SHMCTL(shmid, IPC_RMID, NULL);
176*49cdfc7eSAndroid Build Coastguard Worker }
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
179*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_all,
180*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
181*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
182*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
183*49cdfc7eSAndroid Build Coastguard Worker };
184