xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/capset/capset01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
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  *
6*49cdfc7eSAndroid Build Coastguard Worker  * 2005/01/01: add an hint to a possible solution when test fails
7*49cdfc7eSAndroid Build Coastguard Worker  * Ricky Ng-Adam <[email protected]>
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2003-2023
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker /*\
13*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * Test capset() with with LINUX_CAPABILITY_VERSION_{1,2,3}.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include <linux/capability.h>
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid;
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 static struct tcase {
28*49cdfc7eSAndroid Build Coastguard Worker 	int version;
29*49cdfc7eSAndroid Build Coastguard Worker 	char *message;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{0x19980330, "LINUX_CAPABILITY_VERSION_1"},
32*49cdfc7eSAndroid Build Coastguard Worker 	{0x20071026, "LINUX_CAPABILITY_VERSION_2"},
33*49cdfc7eSAndroid Build Coastguard Worker 	{0x20080522, "LINUX_CAPABILITY_VERSION_3"},
34*49cdfc7eSAndroid Build Coastguard Worker };
35*49cdfc7eSAndroid Build Coastguard Worker 
verify_capset(unsigned int n)36*49cdfc7eSAndroid Build Coastguard Worker static void verify_capset(unsigned int n)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	header->version = tc->version;
41*49cdfc7eSAndroid Build Coastguard Worker 	header->pid = pid;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_capget, header, data));
44*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
45*49cdfc7eSAndroid Build Coastguard Worker 	      tst_brk(TFAIL | TTERRNO, "capget() failed");
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(tst_syscall(__NR_capset, header, data),
48*49cdfc7eSAndroid Build Coastguard Worker 	             "capset() with %s", tc->message);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker 	pid = getpid();
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
57*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
58*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
59*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_capset,
60*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
61*49cdfc7eSAndroid Build Coastguard Worker 		{&header, .size = sizeof(*header)},
62*49cdfc7eSAndroid Build Coastguard Worker 		{&data, .size = 2 * sizeof(*data)},
63*49cdfc7eSAndroid Build Coastguard Worker 		{},
64*49cdfc7eSAndroid Build Coastguard Worker 	}
65*49cdfc7eSAndroid Build Coastguard Worker };
66