xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/userns/userns05.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) Huawei Technologies Co., Ltd., 2015
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that if a process created via fork(2) or clone(2) without the
11*49cdfc7eSAndroid Build Coastguard Worker  * CLONE_NEWUSER flag is a member of the same user namespace as its parent.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * When unshare an user namespace, the calling process is moved into a new user
14*49cdfc7eSAndroid Build Coastguard Worker  * namespace which is not shared with any previously existing process.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "common.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
child_fn1(void)24*49cdfc7eSAndroid Build Coastguard Worker static void child_fn1(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
27*49cdfc7eSAndroid Build Coastguard Worker }
28*49cdfc7eSAndroid Build Coastguard Worker 
getusernsidbypid(int pid)29*49cdfc7eSAndroid Build Coastguard Worker static unsigned int getusernsidbypid(int pid)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	char path[BUFSIZ];
32*49cdfc7eSAndroid Build Coastguard Worker 	char userid[BUFSIZ];
33*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int id = 0;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(path, "/proc/%d/ns/user", pid);
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READLINK(path, userid, BUFSIZ);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	if (sscanf(userid, "user:[%u]", &id) < 0)
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "sscanf failure");
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	return id;
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker 
run(void)45*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_clone_args args1 = { .exit_signal = SIGCHLD };
48*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_clone_args args2 = {
49*49cdfc7eSAndroid Build Coastguard Worker 		.flags = CLONE_NEWUSER,
50*49cdfc7eSAndroid Build Coastguard Worker 		.exit_signal = SIGCHLD,
51*49cdfc7eSAndroid Build Coastguard Worker 	};
52*49cdfc7eSAndroid Build Coastguard Worker 	int cpid1, cpid2, cpid3;
53*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int parentuserns, cpid1userns, cpid2userns, newparentuserns;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	parentuserns = getusernsidbypid(getpid());
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	cpid1 = SAFE_CLONE(&args1);
58*49cdfc7eSAndroid Build Coastguard Worker 	if (!cpid1) {
59*49cdfc7eSAndroid Build Coastguard Worker 		child_fn1();
60*49cdfc7eSAndroid Build Coastguard Worker 		return;
61*49cdfc7eSAndroid Build Coastguard Worker 	}
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	cpid1userns = getusernsidbypid(cpid1);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	/* A process created via fork(2) or clone(2) without the
68*49cdfc7eSAndroid Build Coastguard Worker 	 * CLONE_NEWUSER flag is a member of the same user namespace as its
69*49cdfc7eSAndroid Build Coastguard Worker 	 * parent
70*49cdfc7eSAndroid Build Coastguard Worker 	 */
71*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(parentuserns, cpid1userns);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	cpid2 = SAFE_CLONE(&args2);
74*49cdfc7eSAndroid Build Coastguard Worker 	if (!cpid2) {
75*49cdfc7eSAndroid Build Coastguard Worker 		child_fn1();
76*49cdfc7eSAndroid Build Coastguard Worker 		return;
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	cpid2userns = getusernsidbypid(cpid2);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(parentuserns != cpid2userns,
84*49cdfc7eSAndroid Build Coastguard Worker 		"parent namespace != child namespace");
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	cpid3 = SAFE_FORK();
87*49cdfc7eSAndroid Build Coastguard Worker 	if (!cpid3) {
88*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UNSHARE(CLONE_NEWUSER);
89*49cdfc7eSAndroid Build Coastguard Worker 		newparentuserns = getusernsidbypid(getpid());
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 		/* When unshare an user namespace, the calling process
92*49cdfc7eSAndroid Build Coastguard Worker 		 * is moved into a new user namespace which is not shared
93*49cdfc7eSAndroid Build Coastguard Worker 		 * with any previously existing process
94*49cdfc7eSAndroid Build Coastguard Worker 		 */
95*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_EXPR(parentuserns != newparentuserns,
96*49cdfc7eSAndroid Build Coastguard Worker 			"parent namespace != unshared child namespace");
97*49cdfc7eSAndroid Build Coastguard Worker 	}
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
101*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
102*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
103*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
104*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
105*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
106*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_USER_NS",
107*49cdfc7eSAndroid Build Coastguard Worker 		NULL,
108*49cdfc7eSAndroid Build Coastguard Worker 	},
109*49cdfc7eSAndroid Build Coastguard Worker };
110