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 * Tests basic error handling of the capget syscall.
7f08c3bdfSopenharmony_ci * 1) capget() fails with errno set to EFAULT if an invalid address
8f08c3bdfSopenharmony_ci * is given for header.
9f08c3bdfSopenharmony_ci * 2) capget() fails with errno set to EFAULT if an invalid address
10f08c3bdfSopenharmony_ci * is given for data
11f08c3bdfSopenharmony_ci * 3) capget() fails with errno set to EINVAL if an invalid value
12f08c3bdfSopenharmony_ci * is given for header->version
13f08c3bdfSopenharmony_ci * 4) capget() fails with errno set to EINVAL if header->pid < 0
14f08c3bdfSopenharmony_ci * 5) capget() fails with errno set to ESRCH if the process with
15f08c3bdfSopenharmony_ci *  pid, header->pid does not exist.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <sys/types.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
21f08c3bdfSopenharmony_ci#include <linux/capability.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic pid_t unused_pid;
24f08c3bdfSopenharmony_cistatic struct __user_cap_header_struct *header;
25f08c3bdfSopenharmony_cistatic struct __user_cap_data_struct *data, *bad_data;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic struct tcase {
28f08c3bdfSopenharmony_ci	int version;
29f08c3bdfSopenharmony_ci	int pid;
30f08c3bdfSopenharmony_ci	int exp_err;
31f08c3bdfSopenharmony_ci	int flag;
32f08c3bdfSopenharmony_ci	char *message;
33f08c3bdfSopenharmony_ci} tcases[] = {
34f08c3bdfSopenharmony_ci	{0x20080522, 0, EFAULT, 1, "bad address header"},
35f08c3bdfSopenharmony_ci	{0x20080522, 0, EFAULT, 2, "bad address data"},
36f08c3bdfSopenharmony_ci	{0, 0, EINVAL, 0, "bad version"},
37f08c3bdfSopenharmony_ci	{0x20080522, -1, EINVAL, 0, "bad pid"},
38f08c3bdfSopenharmony_ci	{0x20080522, 1, ESRCH, 0, "unused pid"},
39f08c3bdfSopenharmony_ci};
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void verify_capget(unsigned int n)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	header->version = tc->version;
46f08c3bdfSopenharmony_ci	if (tc->pid == 1)
47f08c3bdfSopenharmony_ci		header->pid = unused_pid;
48f08c3bdfSopenharmony_ci	else
49f08c3bdfSopenharmony_ci		header->pid = tc->pid;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	/*
52f08c3bdfSopenharmony_ci	 * header must not be NULL. data may be NULL only when the user is
53f08c3bdfSopenharmony_ci	 * trying to determine the preferred capability version format
54f08c3bdfSopenharmony_ci	 * supported by the kernel. So use tst_get_bad_addr() to get
55f08c3bdfSopenharmony_ci	 * this error.
56f08c3bdfSopenharmony_ci	 */
57f08c3bdfSopenharmony_ci	TST_EXP_FAIL(tst_syscall(__NR_capget, tc->flag - 1 ? header : NULL,
58f08c3bdfSopenharmony_ci	                         tc->flag - 2 ? data : bad_data),
59f08c3bdfSopenharmony_ci		     tc->exp_err, "capget() with %s", tc->message);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	/*
62f08c3bdfSopenharmony_ci	 * When an unsupported version value is specified, it will
63f08c3bdfSopenharmony_ci	 * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
64f08c3bdfSopenharmony_ci	 * Since linux 2.6.26, version 3 is default. We use it.
65f08c3bdfSopenharmony_ci	 */
66f08c3bdfSopenharmony_ci	if (header->version != 0x20080522)
67f08c3bdfSopenharmony_ci		tst_res(TFAIL, "kernel doesn't return preferred linux"
68f08c3bdfSopenharmony_ci			" capability version when using bad version");
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic void setup(void)
72f08c3bdfSopenharmony_ci{
73f08c3bdfSopenharmony_ci	unused_pid = tst_get_unused_pid();
74f08c3bdfSopenharmony_ci	bad_data = tst_get_bad_addr(NULL);
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistatic struct tst_test test = {
78f08c3bdfSopenharmony_ci	.setup = setup,
79f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
80f08c3bdfSopenharmony_ci	.test = verify_capget,
81f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
82f08c3bdfSopenharmony_ci		{&header, .size = sizeof(*header)},
83f08c3bdfSopenharmony_ci		{&data, .size = 2 * sizeof(*data)},
84f08c3bdfSopenharmony_ci		{&bad_data, .size = 2 * sizeof(*data)},
85f08c3bdfSopenharmony_ci		{},
86f08c3bdfSopenharmony_ci	}
87f08c3bdfSopenharmony_ci};
88