aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/sysctl/sysctl04.c
blob: 36b25cff0fe020b7b08ec3da1bd658c2ef7e692c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) International Business Machines  Corp., 200i1
 * Copyright (c) 2018 Xiao Yang <yangx.jy@cn.fujitsu.com>
 *
 * This program is free software;  you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY;  without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 */

/*
 * DESCRIPTION
 * 1) Call sysctl(2) with nlen set to 0, and expect ENOTDIR.
 * 2) Call sysctl(2) with nlen greater than CTL_MAXNAME, and expect ENOTDIR.
 * 3) Call sysctl(2) with the address of oldname outside the address space of
 *    the process, and expect EFAULT.
 * 4) Call sysctl(2) with the address of soldval outside the address space of
 *    the process, and expect EFAULT.
 */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <linux/sysctl.h>

#include "tst_test.h"
#include "lapi/syscalls.h"

static char osname[BUFSIZ];
static size_t length = BUFSIZ;

static struct tcase {
	int name[2];
	int nlen;
	void *oldval;
	size_t *oldlen;
	int exp_err;
} tcases[] = {
	{{CTL_KERN, KERN_OSREV}, 0, osname, &length, ENOTDIR},
	{{CTL_KERN, KERN_OSREV}, CTL_MAXNAME + 1, osname, &length, ENOTDIR},
	{{CTL_KERN, KERN_OSRELEASE}, 2, (void *) -1, &length, EFAULT},
	{{CTL_KERN, KERN_VERSION}, 2, osname, (void *) -1, EFAULT},
};

static void verify_sysctl(unsigned int n)
{
	struct tcase *tc = &tcases[n];
	struct __sysctl_args args = {
		.name = tc->name,
		.nlen = tc->nlen,
		.oldval = tc->oldval,
		.oldlenp = tc->oldlen,
	};

	TEST(tst_syscall(__NR__sysctl, &args));
	if (TEST_RETURN != -1) {
		tst_res(TFAIL, "sysctl(2) succeeded unexpectedly");
		return;
	}

	if (TEST_ERRNO == tc->exp_err) {
		tst_res(TPASS | TTERRNO, "Got expected error");
	} else {
		tst_res(TFAIL | TTERRNO, "Got unexpected error, expected %s",
			tst_strerrno(tc->exp_err));
	}
}

static struct tst_test test = {
	.tcnt = ARRAY_SIZE(tcases),
	.test = verify_sysctl,
};