• 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 <limits.h>
45 #include "config.h"
46 #include "lapi/prctl.h"
47 #include "lapi/seccomp.h"
48 #include "lapi/syscalls.h"
49 #include "tst_test.h"
50 #include "tst_capability.h"
51 
52 #define OPTION_INVALID 999
53 #define unsup_string "prctl() doesn't support this option, skip it"
54 static const struct sock_filter  strict_filter[] = {
55 	BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
56 };
57 
58 static const struct sock_fprog strict = {
59 	.len = (unsigned short)ARRAY_SIZE(strict_filter),
60 	.filter = (struct sock_filter *)strict_filter
61 };
62 
63 static unsigned long strict_addr = (unsigned long)&strict;
64 
65 static unsigned long bad_addr;
66 static unsigned long num_0;
67 static unsigned long num_1 = 1;
68 static unsigned long num_2 = 2;
69 static unsigned long num_invalid = ULONG_MAX;
70 static int seccomp_nsup;
71 static int nonewprivs_nsup;
72 static int thpdisable_nsup;
73 static int capambient_nsup;
74 static int speculationctrl_nsup;
75 
76 static struct tcase {
77 	int option;
78 	unsigned long *arg2;
79 	unsigned long *arg3;
80 	int exp_errno;
81 	char *tname;
82 } tcases[] = {
83 	{OPTION_INVALID, &num_1, &num_0, EINVAL, "invalid option"},
84 	{PR_SET_PDEATHSIG, &num_invalid, &num_0, EINVAL, "PR_SET_PDEATHSIG"},
85 	{PR_SET_DUMPABLE, &num_2, &num_0, EINVAL, "PR_SET_DUMPABLE"},
86 	{PR_SET_NAME, &bad_addr, &num_0, EFAULT, "PR_SET_NAME"},
87 	{PR_SET_SECCOMP, &num_2, &bad_addr, EFAULT, "PR_SET_SECCOMP"},
88 	{PR_SET_SECCOMP, &num_2, &strict_addr, EACCES, "PR_SET_SECCOMP"},
89 	{PR_SET_TIMING, &num_1, &num_0, EINVAL, "PR_SET_TIMING"},
90 	{PR_SET_NO_NEW_PRIVS, &num_0, &num_0, EINVAL, "PR_SET_NO_NEW_PRIVS"},
91 	{PR_SET_NO_NEW_PRIVS, &num_1, &num_1, EINVAL, "PR_SET_NO_NEW_PRIVS"},
92 	{PR_GET_NO_NEW_PRIVS, &num_1, &num_0, EINVAL, "PR_GET_NO_NEW_PRIVS"},
93 	{PR_SET_THP_DISABLE, &num_0, &num_1, EINVAL, "PR_SET_THP_DISABLE"},
94 	{PR_GET_THP_DISABLE, &num_1, &num_1, EINVAL, "PR_GET_THP_DISABLE"},
95 	{PR_CAP_AMBIENT, &num_invalid, &num_0, EINVAL, "PR_CAP_AMBIENT"},
96 	{PR_GET_SPECULATION_CTRL, &num_0, &num_invalid, EINVAL, "PR_GET_SPECULATION_CTRL"},
97 	{PR_SET_SECUREBITS, &num_0, &num_0, EPERM, "PR_SET_SECUREBITS"},
98 	{PR_CAPBSET_DROP, &num_1, &num_0, EPERM, "PR_CAPBSET_DROP"},
99 };
100 
verify_prctl(unsigned int n)101 static void verify_prctl(unsigned int n)
102 {
103 	struct tcase *tc = &tcases[n];
104 
105 	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
106 
107 	switch (tc->option) {
108 	case PR_SET_SECCOMP:
109 		if (seccomp_nsup) {
110 			tst_res(TCONF, "%s", unsup_string);
111 			return;
112 		}
113 	break;
114 	case PR_GET_NO_NEW_PRIVS:
115 	case PR_SET_NO_NEW_PRIVS:
116 		if (nonewprivs_nsup) {
117 			tst_res(TCONF, "%s", unsup_string);
118 			return;
119 		}
120 	break;
121 	case PR_SET_THP_DISABLE:
122 	case PR_GET_THP_DISABLE:
123 		if (thpdisable_nsup) {
124 			tst_res(TCONF, "%s", unsup_string);
125 			return;
126 		}
127 	break;
128 	case PR_CAP_AMBIENT:
129 		if (capambient_nsup) {
130 			tst_res(TCONF, "%s", unsup_string);
131 			return;
132 		}
133 	break;
134 	case PR_GET_SPECULATION_CTRL:
135 		if (speculationctrl_nsup) {
136 			tst_res(TCONF, "%s", unsup_string);
137 			return;
138 		}
139 	break;
140 	default:
141 	break;
142 	}
143 
144 	TEST(prctl(tc->option, *tc->arg2, *tc->arg3, 0, 0));
145 	if (TST_RET == 0) {
146 		tst_res(TFAIL, "prctl() succeeded unexpectedly");
147 		return;
148 	}
149 
150 	if (tc->exp_errno == TST_ERR) {
151 		tst_res(TPASS | TTERRNO, "prctl() failed as expected");
152 	} else {
153 		if (tc->option == PR_SET_SECCOMP && TST_ERR == EINVAL)
154 			tst_res(TCONF, "current system was not built with CONFIG_SECCOMP_FILTER.");
155 		else
156 			tst_res(TFAIL | TTERRNO, "prctl() failed unexpectedly, expected %s",
157 				tst_strerrno(tc->exp_errno));
158 	}
159 }
160 
setup(void)161 static void setup(void)
162 {
163 	bad_addr = (unsigned long)tst_get_bad_addr(NULL);
164 
165 	TEST(prctl(PR_GET_SECCOMP));
166 	if (TST_ERR == EINVAL)
167 		seccomp_nsup = 1;
168 
169 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
170 	if (TST_ERR == EINVAL)
171 		nonewprivs_nsup = 1;
172 
173 	TEST(prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0));
174 	if (TST_ERR == EINVAL)
175 		thpdisable_nsup = 1;
176 
177 	TEST(prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0));
178 	if (TST_ERR == EINVAL)
179 		capambient_nsup = 1;
180 
181 	TEST(prctl(PR_GET_SPECULATION_CTRL, 0, 0, 0, 0));
182 	if (TST_ERR == EINVAL)
183 		speculationctrl_nsup = 1;
184 }
185 
186 static struct tst_test test = {
187 	.setup = setup,
188 	.tcnt = ARRAY_SIZE(tcases),
189 	.test = verify_prctl,
190 	.caps = (struct tst_cap []) {
191 		TST_CAP(TST_CAP_DROP, CAP_SYS_ADMIN),
192 		TST_CAP(TST_CAP_DROP, CAP_SETPCAP),
193 		{}
194 	},
195 };
196