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