xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/prctl/prctl06.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) 2019 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[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  * Test PR_GET_NO_NEW_PRIVS and PR_SET_NO_NEW_PRIVS of prctl(2).
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - Return the value of the no_new_privs bit for the calling thread.
13*49cdfc7eSAndroid Build Coastguard Worker  *   A value of 0 indicates the regular execve(2) behavior.  A value of
14*49cdfc7eSAndroid Build Coastguard Worker  *   1 indicates execve(2) will operate in the privilege-restricting mode.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * - With no_new_privs set to 1, diables privilege granting operations
17*49cdfc7eSAndroid Build Coastguard Worker  *   at execve-time. For example, a process will not be able to execute a
18*49cdfc7eSAndroid Build Coastguard Worker  *   setuid binary to change their uid or gid if this bit is set. The same
19*49cdfc7eSAndroid Build Coastguard Worker  *   is true for file capabilities.
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  * - The setting of this bit is inherited by children created by fork(2),
22*49cdfc7eSAndroid Build Coastguard Worker  *   and preserved across execve(2). We also check NoNewPrivs field in
23*49cdfc7eSAndroid Build Coastguard Worker  *   /proc/self/status if it supports.
24*49cdfc7eSAndroid Build Coastguard Worker  */
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "prctl06.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid;
29*49cdfc7eSAndroid Build Coastguard Worker static gid_t nobody_gid;
30*49cdfc7eSAndroid Build Coastguard Worker static int proc_flag = 1;
31*49cdfc7eSAndroid Build Coastguard Worker static char *proc_sup = "Yes";
32*49cdfc7eSAndroid Build Coastguard Worker 
do_prctl(void)33*49cdfc7eSAndroid Build Coastguard Worker static void do_prctl(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	char ipc_env_var[1024];
36*49cdfc7eSAndroid Build Coastguard Worker 	char *const argv[] = {BIN_PATH, "After execve, parent process", proc_sup, NULL};
37*49cdfc7eSAndroid Build Coastguard Worker 	char *const childargv[] = {BIN_PATH, "After execve, child process", proc_sup, NULL};
38*49cdfc7eSAndroid Build Coastguard Worker 	char *const envp[] = {ipc_env_var, NULL };
39*49cdfc7eSAndroid Build Coastguard Worker 	int childpid;
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	check_no_new_privs(0, "parent", proc_flag);
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
44*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "prctl(PR_SET_NO_NEW_PRIVS) failed");
46*49cdfc7eSAndroid Build Coastguard Worker 		return;
47*49cdfc7eSAndroid Build Coastguard Worker 	}
48*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "prctl(PR_SET_NO_NEW_PRIVS) succeeded");
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETGID(nobody_gid);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETUID(nobody_uid);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(ipc_env_var, IPC_ENV_VAR "=%s", getenv(IPC_ENV_VAR));
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	childpid = SAFE_FORK();
56*49cdfc7eSAndroid Build Coastguard Worker 	if (childpid == 0) {
57*49cdfc7eSAndroid Build Coastguard Worker 		check_no_new_privs(1, "After fork, child process", proc_flag);
58*49cdfc7eSAndroid Build Coastguard Worker 		execve(BIN_PATH, childargv, envp);
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TERRNO,
60*49cdfc7eSAndroid Build Coastguard Worker 			"child process failed to execute prctl_execve");
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	} else {
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_reap_children();
64*49cdfc7eSAndroid Build Coastguard Worker 		check_no_new_privs(1, "parent process", proc_flag);
65*49cdfc7eSAndroid Build Coastguard Worker 		execve(BIN_PATH, argv, envp);
66*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TERRNO,
67*49cdfc7eSAndroid Build Coastguard Worker 			"parent process failed to execute prctl_execve");
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
verify_prctl(void)71*49cdfc7eSAndroid Build Coastguard Worker static void verify_prctl(void)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	int pid;
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
76*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
77*49cdfc7eSAndroid Build Coastguard Worker 		do_prctl();
78*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
79*49cdfc7eSAndroid Build Coastguard Worker 	}
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)82*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
85*49cdfc7eSAndroid Build Coastguard Worker 	int field;
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
88*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = pw->pw_uid;
89*49cdfc7eSAndroid Build Coastguard Worker 	nobody_gid = pw->pw_gid;
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CP(TESTBIN, TEST_REL_BIN_DIR);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(BIN_PATH, 0, 0);
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(BIN_PATH, SUID_MODE);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	if (FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field)) {
97*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "%s doesn't support NoNewPrivs field", PROC_STATUS);
98*49cdfc7eSAndroid Build Coastguard Worker 		proc_flag = 0;
99*49cdfc7eSAndroid Build Coastguard Worker 		proc_sup = "No";
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
103*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == 0) {
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "kernel supports PR_GET/SET_NO_NEW_PRIVS");
105*49cdfc7eSAndroid Build Coastguard Worker 		return;
106*49cdfc7eSAndroid Build Coastguard Worker 	}
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EINVAL)
109*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF,
110*49cdfc7eSAndroid Build Coastguard Worker 			"kernel doesn't support PR_GET/SET_NO_NEW_PRIVS");
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	tst_brk(TBROK | TTERRNO,
113*49cdfc7eSAndroid Build Coastguard Worker 		"current environment doesn't permit PR_GET/SET_NO_NEW_PRIVS");
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
117*49cdfc7eSAndroid Build Coastguard Worker 	.resource_files = (const char *const []) {
118*49cdfc7eSAndroid Build Coastguard Worker 		TESTBIN,
119*49cdfc7eSAndroid Build Coastguard Worker 		NULL
120*49cdfc7eSAndroid Build Coastguard Worker 	},
121*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
122*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_prctl,
123*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
124*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
125*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
126*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
127*49cdfc7eSAndroid Build Coastguard Worker 	.child_needs_reinit = 1,
128*49cdfc7eSAndroid Build Coastguard Worker };
129