• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4  *
5  * 1) prctl() fails with EINVAL when an invalid value is given for option
6  * 2) prctl() fails with EINVAL when option is PR_SET_PDEATHSIG & arg2 is
7  * not zero or a valid signal number.
8  * 3) prctl() fails with EINVAL when option is PR_SET_DUMPABLE & arg2 is
9  * neither SUID_DUMP_DISABLE nor SUID_DUMP_USER.
10  * 4) prctl() fails with EFAULT when arg2 is an invalid address.
11  * 5) prctl() fails with EFAULT when option is PR_SET_SECCOMP & arg2 is
12  * SECCOMP_MODE_FILTER & arg3 is an invalid address.
13  * 6) prctl() fails with EACCES when option is PR_SET_SECCOMP & arg2 is
14  * SECCOMP_MODE_FILTER & the process does not have the CAP_SYS_ADMIN
15  * capability.
16  * 7) prctl() fails with EINVAL when option is PR_SET_TIMING & arg2 is not
17  * not PR_TIMING_STATISTICAL.
18  * 8,9) prctl() fails with EINVAL when option is PR_SET_NO_NEW_PRIVS & arg2
19  * is not equal to 1 or arg3 is nonzero.
20  * 10) prctl() fails with EINVAL when options is PR_GET_NO_NEW_PRIVS & arg2,
21  * arg3, arg4, or arg5 is nonzero.
22  * 11) prctl() fails with EINVAL when options is PR_SET_THP_DISABLE & arg3,
23  * arg4, arg5 is non-zero.
24  * 12) prctl() fails with EINVAL when options is PR_GET_THP_DISABLE & arg2,
25  * arg3, arg4, or arg5 is nonzero.
26  * 13) prctl() fails with EINVAL when options is PR_CAP_AMBIENT & an unused
27  * argument such as arg4 is nonzero.
28  * 14) prctl() fails with EINVAL when option is PR_GET_SPECULATION_CTRL and
29  * unused arguments is nonzero.
30  * 15) prctl() fails with EPERM when option is PR_SET_SECUREBITS and the
31  * caller does not have the CAP_SETPCAP capability.
32  * 16) prctl() fails with EPERM when option is PR_CAPBSET_DROP and the caller
33  * does not have the CAP_SETPCAP capability.
34  */
35 
36 #include <errno.h>
37 #include <signal.h>
38 #include <sys/prctl.h>
39 #include <linux/filter.h>
40 #include <linux/capability.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <stddef.h>
44 #include "config.h"
45 #include "lapi/prctl.h"
46 #include "lapi/seccomp.h"
47 #include "lapi/syscalls.h"
48 #include "tst_test.h"
49 #include "tst_capability.h"
50 
51 #define OPTION_INVALID 999
52 #define unsup_string "prctl() doesn't support this option, skip it"
53 static const struct sock_filter  strict_filter[] = {
54 	BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
55 };
56 
57 static const struct sock_fprog strict = {
58 	.len = (unsigned short)ARRAY_SIZE(strict_filter),
59 	.filter = (struct sock_filter *)strict_filter
60 };
61 
62 static unsigned long strict_addr = (unsigned long)&strict;
63 
64 static unsigned long bad_addr;
65 static unsigned long num_0;
66 static unsigned long num_1 = 1;
67 static unsigned long num_2 = 2;
68 static unsigned long num_invalid = 999;
69 static int seccomp_nsup;
70 static int nonewprivs_nsup;
71 static int thpdisable_nsup;
72 static int capambient_nsup;
73 static int speculationctrl_nsup;
74 
75 static struct tcase {
76 	int option;
77 	unsigned long *arg2;
78 	unsigned long *arg3;
79 	int exp_errno;
80 	char *tname;
81 } tcases[] = {
82 	{OPTION_INVALID, &num_1, &num_0, EINVAL, "invalid option"},
83 	{PR_SET_PDEATHSIG, &num_invalid, &num_0, EINVAL, "PR_SET_PDEATHSIG"},
84 	{PR_SET_DUMPABLE, &num_2, &num_0, EINVAL, "PR_SET_DUMPABLE"},
85 	{PR_SET_NAME, &bad_addr, &num_0, EFAULT, "PR_SET_NAME"},
86 	{PR_SET_SECCOMP, &num_2, &bad_addr, EFAULT, "PR_SET_SECCOMP"},
87 	{PR_SET_SECCOMP, &num_2, &strict_addr, EACCES, "PR_SET_SECCOMP"},
88 	{PR_SET_TIMING, &num_1, &num_0, EINVAL, "PR_SET_TIMING"},
89 	{PR_SET_NO_NEW_PRIVS, &num_0, &num_0, EINVAL, "PR_SET_NO_NEW_PRIVS"},
90 	{PR_SET_NO_NEW_PRIVS, &num_1, &num_0, EINVAL, "PR_SET_NO_NEW_PRIVS"},
91 	{PR_GET_NO_NEW_PRIVS, &num_1, &num_0, EINVAL, "PR_GET_NO_NEW_PRIVS"},
92 	{PR_SET_THP_DISABLE, &num_0, &num_1, EINVAL, "PR_SET_THP_DISABLE"},
93 	{PR_GET_THP_DISABLE, &num_1, &num_1, EINVAL, "PR_GET_THP_DISABLE"},
94 	{PR_CAP_AMBIENT, &num_2, &num_1, EINVAL, "PR_CAP_AMBIENT"},
95 	{PR_GET_SPECULATION_CTRL, &num_1, &num_0, EINVAL, "PR_GET_SPECULATION_CTRL"},
96 	{PR_SET_SECUREBITS, &num_0, &num_0, EPERM, "PR_SET_SECUREBITS"},
97 	{PR_CAPBSET_DROP, &num_1, &num_0, EPERM, "PR_CAPBSET_DROP"},
98 };
99 
verify_prctl(unsigned int n)100 static void verify_prctl(unsigned int n)
101 {
102 	struct tcase *tc = &tcases[n];
103 
104 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
105 
106 	switch (tc->option) {
107 	case PR_SET_SECCOMP:
108 		if (seccomp_nsup) {
109 			tst_res(TCONF, "%s", unsup_string);
110 			return;
111 		}
112 	break;
113 	case PR_GET_NO_NEW_PRIVS:
114 	case PR_SET_NO_NEW_PRIVS:
115 		if (nonewprivs_nsup) {
116 			tst_res(TCONF, "%s", unsup_string);
117 			return;
118 		}
119 	break;
120 	case PR_SET_THP_DISABLE:
121 	case PR_GET_THP_DISABLE:
122 		if (thpdisable_nsup) {
123 			tst_res(TCONF, "%s", unsup_string);
124 			return;
125 		}
126 	break;
127 	case PR_CAP_AMBIENT:
128 		if (capambient_nsup) {
129 			tst_res(TCONF, "%s", unsup_string);
130 			return;
131 		}
132 	break;
133 	case PR_GET_SPECULATION_CTRL:
134 		if (speculationctrl_nsup) {
135 			tst_res(TCONF, "%s", unsup_string);
136 			return;
137 		}
138 	break;
139 	default:
140 	break;
141 	}
142 
143 	TEST(prctl(tc->option, *tc->arg2, *tc->arg3));
144 	if (TST_RET == 0) {
145 		tst_res(TFAIL, "prctl() succeeded unexpectedly");
146 		return;
147 	}
148 
149 	if (tc->exp_errno == TST_ERR) {
150 		tst_res(TPASS | TTERRNO, "prctl() failed as expected");
151 	} else {
152 		if (tc->option == PR_SET_SECCOMP && TST_ERR == EINVAL)
153 			tst_res(TCONF, "current system was not built with CONFIG_SECCOMP_FILTER.");
154 		else
155 			tst_res(TFAIL | TTERRNO, "prctl() failed unexpectedly, expected %s",
156 				tst_strerrno(tc->exp_errno));
157 	}
158 }
159 
setup(void)160 static void setup(void)
161 {
162 	bad_addr = (unsigned long)tst_get_bad_addr(NULL);
163 
164 	TEST(prctl(PR_GET_SECCOMP));
165 	if (TST_ERR == EINVAL)
166 		seccomp_nsup = 1;
167 
168 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
169 	if (TST_ERR == EINVAL)
170 		nonewprivs_nsup = 1;
171 
172 	TEST(prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0));
173 	if (TST_ERR == EINVAL)
174 		thpdisable_nsup = 1;
175 
176 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0));
177 	if (TST_ERR == EINVAL)
178 		capambient_nsup = 1;
179 
180 	TEST(prctl(PR_GET_SPECULATION_CTRL, 0, 0, 0, 0));
181 	if (TST_ERR == EINVAL)
182 		speculationctrl_nsup = 1;
183 }
184 
185 static struct tst_test test = {
186 	.setup = setup,
187 	.tcnt = ARRAY_SIZE(tcases),
188 	.test = verify_prctl,
189 	.caps = (struct tst_cap []) {
190 		TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN),
191 		TST_CAP(TST_CAP_DROP, CAP_SETPCAP),
192 		{}
193 	},
194 };
195