1 /*
2 * Copyright (c) International Business Machines Corp., 200i1
3 * Copyright (c) 2018 Xiao Yang <yangx.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 */
15
16 /*
17 * DESCRIPTION
18 * 1) Call sysctl(2) with nlen set to 0, and expect ENOTDIR.
19 * 2) Call sysctl(2) with nlen greater than CTL_MAXNAME, and expect ENOTDIR.
20 * 3) Call sysctl(2) with the address of oldname outside the address space of
21 * the process, and expect EFAULT.
22 * 4) Call sysctl(2) with the address of soldval outside the address space of
23 * the process, and expect EFAULT.
24 */
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <linux/unistd.h>
30 #include <linux/sysctl.h>
31
32 #include "tst_test.h"
33 #include "lapi/syscalls.h"
34
35 static char osname[BUFSIZ];
36 static size_t length = BUFSIZ;
37
38 static struct tcase {
39 int name[2];
40 int nlen;
41 void *oldval;
42 size_t *oldlen;
43 int exp_err;
44 } tcases[] = {
45 {{CTL_KERN, KERN_OSREV}, 0, osname, &length, ENOTDIR},
46 {{CTL_KERN, KERN_OSREV}, CTL_MAXNAME + 1, osname, &length, ENOTDIR},
47 {{CTL_KERN, KERN_OSRELEASE}, 2, (void *) -1, &length, EFAULT},
48 {{CTL_KERN, KERN_VERSION}, 2, osname, (void *) -1, EFAULT},
49 };
50
verify_sysctl(unsigned int n)51 static void verify_sysctl(unsigned int n)
52 {
53 struct tcase *tc = &tcases[n];
54 struct __sysctl_args args = {
55 .name = tc->name,
56 .nlen = tc->nlen,
57 .oldval = tc->oldval,
58 .oldlenp = tc->oldlen,
59 };
60
61 TEST(tst_syscall(__NR__sysctl, &args));
62 if (TST_RET != -1) {
63 tst_res(TFAIL, "sysctl(2) succeeded unexpectedly");
64 return;
65 }
66
67 if (TST_ERR == tc->exp_err) {
68 tst_res(TPASS | TTERRNO, "Got expected error");
69 } else {
70 tst_res(TFAIL | TTERRNO, "Got unexpected error, expected %s",
71 tst_strerrno(tc->exp_err));
72 }
73 }
74
75 static struct tst_test test = {
76 .tcnt = ARRAY_SIZE(tcases),
77 .test = verify_sysctl,
78 };
79