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