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