1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines Corp., 200i1
4 * Copyright (c) 2018 Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6
7/*
8 * DESCRIPTION
9 * 1) Call sysctl(2) with nlen set to 0, and expect ENOTDIR.
10 * 2) Call sysctl(2) with nlen greater than CTL_MAXNAME, and expect ENOTDIR.
11 * 3) Call sysctl(2) with the address of oldname outside the address space of
12 *    the process, and expect EFAULT.
13 * 4) Call sysctl(2) with the address of soldval outside the address space of
14 *    the process, and expect EFAULT.
15 */
16
17#include <stdio.h>
18#include <errno.h>
19#include <unistd.h>
20#include <linux/unistd.h>
21#include <linux/sysctl.h>
22
23#include "tst_test.h"
24#include "lapi/syscalls.h"
25
26static char osname[BUFSIZ];
27static size_t length = BUFSIZ;
28
29static struct tcase {
30	int name[2];
31	int nlen;
32	void *oldval;
33	size_t *oldlen;
34	int exp_err;
35} tcases[] = {
36	{{CTL_KERN, KERN_OSREV}, 0, osname, &length, ENOTDIR},
37	{{CTL_KERN, KERN_OSREV}, CTL_MAXNAME + 1, osname, &length, ENOTDIR},
38	{{CTL_KERN, KERN_OSRELEASE}, 2, (void *) -1, &length, EFAULT},
39	{{CTL_KERN, KERN_VERSION}, 2, osname, (void *) -1, EFAULT},
40};
41
42static void verify_sysctl(unsigned int n)
43{
44	struct tcase *tc = &tcases[n];
45	struct __sysctl_args args = {
46		.name = tc->name,
47		.nlen = tc->nlen,
48		.oldval = tc->oldval,
49		.oldlenp = tc->oldlen,
50	};
51
52	TEST(tst_syscall(__NR__sysctl, &args));
53	if (TST_RET != -1) {
54		tst_res(TFAIL, "sysctl(2) succeeded unexpectedly");
55		return;
56	}
57
58	if (TST_ERR == tc->exp_err) {
59		tst_res(TPASS | TTERRNO, "Got expected error");
60	} else {
61		tst_res(TFAIL | TTERRNO, "Got unexpected error, expected %s",
62			tst_strerrno(tc->exp_err));
63	}
64}
65
66static struct tst_test test = {
67	.tcnt = ARRAY_SIZE(tcases),
68	.test = verify_sysctl,
69};
70