xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/capset/capset04.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) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2020-2023
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Saji Kumar.V.R <[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  * Test whether capset() can be used to modify the capabilities of a thread
12*49cdfc7eSAndroid Build Coastguard Worker  * other than itself. Now, most linux distributions with kernel supporting
13*49cdfc7eSAndroid Build Coastguard Worker  * VFS capabilities, this should be never permitted.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include <linux/capability.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static struct __user_cap_header_struct *header;
24*49cdfc7eSAndroid Build Coastguard Worker static struct __user_cap_data_struct *data;
25*49cdfc7eSAndroid Build Coastguard Worker static pid_t child_pid;
26*49cdfc7eSAndroid Build Coastguard Worker 
verify_capset(void)27*49cdfc7eSAndroid Build Coastguard Worker static void verify_capset(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	child_pid = SAFE_FORK();
30*49cdfc7eSAndroid Build Coastguard Worker 	if (!child_pid)
31*49cdfc7eSAndroid Build Coastguard Worker 		pause();
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test capset() for a different process");
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	header->pid = child_pid;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(tst_syscall(__NR_capset, header, data), EPERM, "capset()");
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_KILL(child_pid, SIGTERM);
40*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAIT(NULL);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)43*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	header->version = 0x20080522;
46*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_capget, header, data));
47*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "capget data failed");
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
52*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
53*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_capset,
54*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
55*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
56*49cdfc7eSAndroid Build Coastguard Worker 		{&header, .size = sizeof(*header)},
57*49cdfc7eSAndroid Build Coastguard Worker 		{&data, .size = 2 * sizeof(*data)},
58*49cdfc7eSAndroid Build Coastguard Worker 		{},
59*49cdfc7eSAndroid Build Coastguard Worker 	}
60*49cdfc7eSAndroid Build Coastguard Worker };
61