xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/capset/capset03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2020 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2020-2023
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[email protected]>
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  *
11*49cdfc7eSAndroid Build Coastguard Worker  * capset() fails with errno set or EPERM if the new_Inheritable is
12*49cdfc7eSAndroid Build Coastguard Worker  * not a subset of old_Inheritable and old_Permitted without CAP_SETPCAP.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include <linux/capability.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define CAP1 (1 << CAP_KILL)
23*49cdfc7eSAndroid Build Coastguard Worker #define CAP2 (CAP1 | 1 << CAP_NET_RAW)
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct __user_cap_header_struct *header;
26*49cdfc7eSAndroid Build Coastguard Worker static struct __user_cap_data_struct *data;
27*49cdfc7eSAndroid Build Coastguard Worker 
verify_capset(void)28*49cdfc7eSAndroid Build Coastguard Worker static void verify_capset(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test bad value data(when pI is not old pP or old pI without CAP_SETPCAP)");
31*49cdfc7eSAndroid Build Coastguard Worker 	data[0].inheritable = CAP2;
32*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(tst_syscall(__NR_capset, header, data), EPERM, "capset()");
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	header->version = 0x20080522;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	data[0].effective = CAP1;
40*49cdfc7eSAndroid Build Coastguard Worker 	data[0].permitted = CAP1;
41*49cdfc7eSAndroid Build Coastguard Worker 	data[0].inheritable = CAP1;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_capset, header, data));
44*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "capset data failed");
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
49*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
50*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_capset,
51*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
52*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
53*49cdfc7eSAndroid Build Coastguard Worker 		{&header, .size = sizeof(*header)},
54*49cdfc7eSAndroid Build Coastguard Worker 		{&data, .size = 2 * sizeof(*data)},
55*49cdfc7eSAndroid Build Coastguard Worker 		{},
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker };
58