1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com
5 *
6 * capset() fails with errno set or EPERM if the new_Inheritable is
7 * not a subset of old_Inheritable and old_Permitted without CAP_SETPCAP.
8 */
9 #include <stdlib.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include "tst_test.h"
13 #include "lapi/syscalls.h"
14 #include <linux/capability.h>
15
16 #define CAP1 (1 << CAP_KILL)
17 #define CAP2 (CAP1 | 1 << CAP_NET_RAW)
18
19 static struct __user_cap_header_struct *header;
20 static struct __user_cap_data_struct *data;
21
verify_capset(void)22 static void verify_capset(void)
23 {
24 tst_res(TINFO, "Test bad value data(when pI is not old pP or old pI without CAP_SETPCAP)");
25 data[0].inheritable = CAP2;
26 TEST(tst_syscall(__NR_capset, header, data));
27 if (TST_RET == 0) {
28 tst_res(TFAIL, "capset succeed unexpectedly");
29 return;
30 }
31 if (TST_ERR == EPERM)
32 tst_res(TPASS | TTERRNO, "capset() failed as expected");
33 else
34 tst_res(TFAIL | TTERRNO, "capset expected EPERM, bug got");
35 }
36
setup(void)37 static void setup(void)
38 {
39 header->version = 0x20080522;
40
41 data[0].effective = CAP1;
42 data[0].permitted = CAP1;
43 data[0].inheritable = CAP1;
44
45 TEST(tst_syscall(__NR_capset, header, data));
46 if (TST_RET == -1)
47 tst_brk(TBROK | TTERRNO, "capset data failed");
48 }
49
50 static struct tst_test test = {
51 .setup = setup,
52 .test_all = verify_capset,
53 .needs_root = 1,
54 .bufs = (struct tst_buffers []) {
55 {&header, .size = sizeof(*header)},
56 {&data, .size = 2 * sizeof(*data)},
57 {},
58 }
59 };
60