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