xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/capget/capget01.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  *  Author: Saji Kumar.V.R <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * Description:
7*49cdfc7eSAndroid Build Coastguard Worker  * This case tests capget() syscall whether works well on three versions.
8*49cdfc7eSAndroid Build Coastguard Worker  * Also, it checks the results buffer.
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
13*49cdfc7eSAndroid Build Coastguard Worker #include <linux/capability.h>
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid;
16*49cdfc7eSAndroid Build Coastguard Worker static struct __user_cap_header_struct *hdr;
17*49cdfc7eSAndroid Build Coastguard Worker static struct __user_cap_data_struct *data;
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
20*49cdfc7eSAndroid Build Coastguard Worker 	int version;
21*49cdfc7eSAndroid Build Coastguard Worker 	char *message;
22*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
23*49cdfc7eSAndroid Build Coastguard Worker 	{0x19980330, "LINUX_CAPABILITY_VERSION_1"},
24*49cdfc7eSAndroid Build Coastguard Worker 	{0x20071026, "LINUX_CAPABILITY_VERSION_2"},
25*49cdfc7eSAndroid Build Coastguard Worker 	{0x20080522, "LINUX_CAPABILITY_VERSION_3"},
26*49cdfc7eSAndroid Build Coastguard Worker };
27*49cdfc7eSAndroid Build Coastguard Worker 
verify_capget(unsigned int n)28*49cdfc7eSAndroid Build Coastguard Worker static void verify_capget(unsigned int n)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	hdr->version = tc->version;
33*49cdfc7eSAndroid Build Coastguard Worker 	hdr->pid = pid;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(tst_syscall(__NR_capget, hdr, data),
36*49cdfc7eSAndroid Build Coastguard Worker 	             "capget() with %s", tc->message);
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	if (data[0].effective & 1 << CAP_NET_RAW)
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "capget() gets CAP_NET_RAW unexpectedly in pE");
40*49cdfc7eSAndroid Build Coastguard Worker 	else
41*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "capget() doesn't get CAP_NET_RAW as expected in PE");
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	pid = getpid();
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
50*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
51*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
52*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_capget,
53*49cdfc7eSAndroid Build Coastguard Worker 	.caps = (struct tst_cap []) {
54*49cdfc7eSAndroid Build Coastguard Worker 		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
55*49cdfc7eSAndroid Build Coastguard Worker 		{}
56*49cdfc7eSAndroid Build Coastguard Worker 	},
57*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
58*49cdfc7eSAndroid Build Coastguard Worker 		{&hdr, .size = sizeof(*hdr)},
59*49cdfc7eSAndroid Build Coastguard Worker 		{&data, .size = 2 * sizeof(*data)},
60*49cdfc7eSAndroid Build Coastguard Worker 		{},
61*49cdfc7eSAndroid Build Coastguard Worker 	}
62*49cdfc7eSAndroid Build Coastguard Worker };
63