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