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