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