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, "bad address header"},
35 {0x20080522, 0, EFAULT, 2, "bad address data"},
36 {0, 0, EINVAL, 0, "bad version"},
37 {0x20080522, -1, EINVAL, 0, "bad pid"},
38 {0x20080522, 1, ESRCH, 0, "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 /*
52 * header must not be NULL. data may be NULL only when the user is
53 * trying to determine the preferred capability version format
54 * supported by the kernel. So use tst_get_bad_addr() to get
55 * this error.
56 */
57 TST_EXP_FAIL(tst_syscall(__NR_capget, tc->flag - 1 ? header : NULL,
58 tc->flag - 2 ? data : bad_data),
59 tc->exp_err, "capget() with %s", tc->message);
60
61 /*
62 * When an unsupported version value is specified, it will
63 * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
64 * Since linux 2.6.26, version 3 is default. We use it.
65 */
66 if (header->version != 0x20080522)
67 tst_res(TFAIL, "kernel doesn't return preferred linux"
68 " capability version when using bad version");
69 }
70
setup(void)71 static void setup(void)
72 {
73 unused_pid = tst_get_unused_pid();
74 bad_data = tst_get_bad_addr(NULL);
75 }
76
77 static struct tst_test test = {
78 .setup = setup,
79 .tcnt = ARRAY_SIZE(tcases),
80 .test = verify_capget,
81 .bufs = (struct tst_buffers []) {
82 {&header, .size = sizeof(*header)},
83 {&data, .size = 2 * sizeof(*data)},
84 {&bad_data, .size = 2 * sizeof(*data)},
85 {},
86 }
87 };
88