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