1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4 * Author: Saji Kumar.V.R <saji.kumar@wipro.com>
5 * Copyright (c) Linux Test Project, 2003-2023
6 */
7
8/*\
9 * [Description]
10 * Tests basic error handling of the capset syscall.
11 *
12 * 1. capset() fails with errno set to EFAULT if an invalid address
13 * is given for header.
14 * 2. capset() fails with errno set to EFAULT if an invalid address
15 * is given for data.
16 * 3. capset() fails with errno set to EINVAL if an invalid value
17 * is given for header->version.
18 * 4. capset() fails with errno set to EPERM if the new_Effective is
19 * not a subset of the new_Permitted.
20 * 5. capset() fails with errno set to EPERM if the new_Permitted is
21 * not a subset of the old_Permitted.
22 * 6. capset() fails with errno set ot EPERM if the new_Inheritable is
23 * not a subset of  the old_Inheritable and bounding set.
24 */
25
26#include <stdlib.h>
27#include <sys/types.h>
28#include <unistd.h>
29#include <sys/prctl.h>
30#include "tst_test.h"
31#include "lapi/syscalls.h"
32#include <linux/capability.h>
33
34#define CAP1 (1 << CAP_NET_RAW | 1 << CAP_CHOWN  | 1 << CAP_SETPCAP)
35#define CAP2 (CAP1 | 1 << CAP_KILL)
36
37static struct __user_cap_header_struct *header;
38static struct __user_cap_data_struct *data;
39
40static void *bad_addr;
41
42static struct tcase {
43	int version;
44	int pid;
45	int effective;
46	int permitted;
47	int inheritable;
48	int exp_err;
49	int flag;
50	char *message;
51} tcases[] = {
52	{0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 1, "bad address header"},
53	{0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 2, "bad address data"},
54	{0, 0, CAP1, CAP1, CAP1, EINVAL, 0, "bad version"},
55	{0x20080522, 0, CAP2, CAP1, CAP1, EPERM, 0, "bad value data(when pE is not in pP)"},
56	{0x20080522, 0, CAP1, CAP2, CAP1, EPERM, 0, "bad value data(when pP is not in old pP)"},
57	{0x20080522, 0, CAP1, CAP1, CAP2, EPERM, 0, "bad value data(when pI is not in bounding set or old pI)"},
58};
59
60static void verify_capset(unsigned int n)
61{
62	struct tcase *tc = &tcases[n];
63
64	header->version = tc->version;
65	header->pid = tc->pid;
66
67	data->effective = tc->effective;
68	data->permitted = tc->permitted;
69	data->inheritable = tc->inheritable;
70
71	TST_EXP_FAIL(tst_syscall(__NR_capset, tc->flag - 1 ? header : bad_addr,
72	                         tc->flag - 2 ? data : bad_addr),
73	             tc->exp_err, "capset() with %s", tc->message);
74	/*
75	 * When an unsupported version value is specified, it will
76	 * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
77	 * Since linux 2.6.26, version 3 is default. We use it.
78	 */
79	if (header->version != 0x20080522)
80		tst_res(TFAIL, "kernel doesn't return preferred linux"
81			" capability version when using bad version");
82}
83
84static void setup(void)
85{
86	header->version = 0x20080522;
87	data->effective = CAP1;
88	data->permitted = CAP1;
89	data->inheritable = CAP1;
90
91	TEST(tst_syscall(__NR_capset, header, data));
92	if (TST_RET == -1)
93		tst_brk(TBROK | TTERRNO, "capset data failed");
94
95	TEST(prctl(PR_CAPBSET_DROP, CAP_KILL));
96	if (TST_RET == -1)
97		tst_brk(TBROK | TTERRNO, "drop CAP_KILL failed");
98
99	bad_addr = tst_get_bad_addr(NULL);
100}
101
102static struct tst_test test = {
103	.setup = setup,
104	.tcnt = ARRAY_SIZE(tcases),
105	.test = verify_capset,
106	.needs_root = 1,
107	.bufs = (struct tst_buffers []) {
108		{&header, .size = sizeof(*header)},
109		{&data, .size = 2 * sizeof(*data)},
110		{},
111	}
112};
113