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