Skip to content

Commit 9308f2f

Browse files
mcgroftorvalds
authored andcommitted
test_sysctl: add dedicated proc sysctl test driver
The existing tools/testing/selftests/sysctl/ tests include two test cases, but these use existing production kernel sysctl interfaces. We want to expand test coverage but we can't just be looking for random safe production values to poke at, that's just insane! Instead just dedicate a test driver for debugging purposes and port the existing scripts to use it. This will make it easier for further tests to be added. Subsequent patches will extend our test coverage for sysctl. The stress test driver uses a new license (GPL on Linux, copyleft-next outside of Linux). Linus was fine with this [0] and later due to Ted's and Alans's request ironed out an "or" language clause to use [1] which is already present upstream. [0] https://lkml.kernel.org/r/CA+55aFyhxcvD+q7tp+-yrSFDKfR0mOHgyEAe=f_94aKLsOu0Og@mail.gmail.com [1] https://lkml.kernel.org/r/1495234558.7848.122.camel@linux.intel.com Link: http://lkml.kernel.org/r/20170630224431.17374-2-mcgrof@kernel.org Signed-off-by: Luis R. Rodriguez <mcgrof@kernel.org> Acked-by: Kees Cook <keescook@chromium.org> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 61d9b56 commit 9308f2f

6 files changed

Lines changed: 130 additions & 4 deletions

File tree

‎lib/Kconfig.debug‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,17 @@ config TEST_FIRMWARE
17851785

17861786
If unsure, say N.
17871787

1788+
config TEST_SYSCTL
1789+
tristate "sysctl test driver"
1790+
default n
1791+
depends on PROC_SYSCTL
1792+
help
1793+
This builds the "test_sysctl" module. This driver enables to test the
1794+
proc sysctl interfaces available to drivers safely without affecting
1795+
production knobs which might alter system functionality.
1796+
1797+
If unsure, say N.
1798+
17881799
config TEST_UDELAY
17891800
tristate "udelay test driver"
17901801
default n

‎lib/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
4646
obj-y += kstrtox.o
4747
obj-$(CONFIG_TEST_BPF) += test_bpf.o
4848
obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
49+
obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
4950
obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o
5051
obj-$(CONFIG_TEST_KASAN) += test_kasan.o
5152
obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o

‎lib/test_sysctl.c‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* proc sysctl test driver
3+
*
4+
* Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org>
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the Free
8+
* Software Foundation; either version 2 of the License, or at your option any
9+
* later version; or, when distributed separately from the Linux kernel or
10+
* when incorporated into other software packages, subject to the following
11+
* license:
12+
*
13+
* This program is free software; you can redistribute it and/or modify it
14+
* under the terms of copyleft-next (version 0.3.1 or later) as published
15+
* at http://copyleft-next.org/.
16+
*/
17+
18+
/*
19+
* This module provides an interface to the the proc sysctl interfaces. This
20+
* driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the
21+
* system unless explicitly requested by name. You can also build this driver
22+
* into your kernel.
23+
*/
24+
25+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26+
27+
#include <linux/init.h>
28+
#include <linux/list.h>
29+
#include <linux/module.h>
30+
#include <linux/printk.h>
31+
#include <linux/fs.h>
32+
#include <linux/miscdevice.h>
33+
#include <linux/slab.h>
34+
#include <linux/uaccess.h>
35+
#include <linux/async.h>
36+
#include <linux/delay.h>
37+
#include <linux/vmalloc.h>
38+
39+
static int i_zero;
40+
static int i_one_hundred = 100;
41+
42+
struct test_sysctl_data {
43+
int int_0001;
44+
char string_0001[65];
45+
};
46+
47+
static struct test_sysctl_data test_data = {
48+
.int_0001 = 60,
49+
.string_0001 = "(none)",
50+
};
51+
52+
/* These are all under /proc/sys/debug/test_sysctl/ */
53+
static struct ctl_table test_table[] = {
54+
{
55+
.procname = "int_0001",
56+
.data = &test_data.int_0001,
57+
.maxlen = sizeof(int),
58+
.mode = 0644,
59+
.proc_handler = proc_dointvec_minmax,
60+
.extra1 = &i_zero,
61+
.extra2 = &i_one_hundred,
62+
},
63+
{
64+
.procname = "string_0001",
65+
.data = &test_data.string_0001,
66+
.maxlen = sizeof(test_data.string_0001),
67+
.mode = 0644,
68+
.proc_handler = proc_dostring,
69+
},
70+
{ }
71+
};
72+
73+
static struct ctl_table test_sysctl_table[] = {
74+
{
75+
.procname = "test_sysctl",
76+
.maxlen = 0,
77+
.mode = 0555,
78+
.child = test_table,
79+
},
80+
{ }
81+
};
82+
83+
static struct ctl_table test_sysctl_root_table[] = {
84+
{
85+
.procname = "debug",
86+
.maxlen = 0,
87+
.mode = 0555,
88+
.child = test_sysctl_table,
89+
},
90+
{ }
91+
};
92+
93+
static struct ctl_table_header *test_sysctl_header;
94+
95+
static int __init test_sysctl_init(void)
96+
{
97+
test_sysctl_header = register_sysctl_table(test_sysctl_root_table);
98+
if (!test_sysctl_header)
99+
return -ENOMEM;
100+
return 0;
101+
}
102+
late_initcall(test_sysctl_init);
103+
104+
static void __exit test_sysctl_exit(void)
105+
{
106+
if (test_sysctl_header)
107+
unregister_sysctl_table(test_sysctl_header);
108+
}
109+
110+
module_exit(test_sysctl_exit);
111+
112+
MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>");
113+
MODULE_LICENSE("GPL");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_TEST_SYSCTL=y

‎tools/testing/selftests/sysctl/run_numerictests‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

3-
SYSCTL="/proc/sys"
4-
TARGET="${SYSCTL}/vm/swappiness"
3+
SYSCTL="/proc/sys/debug/test_sysctl/"
4+
TARGET="${SYSCTL}/int_0001"
55
ORIG=$(cat "${TARGET}")
66
TEST_STR=$(( $ORIG + 1 ))
77

‎tools/testing/selftests/sysctl/run_stringtests‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22

3-
SYSCTL="/proc/sys"
4-
TARGET="${SYSCTL}/kernel/domainname"
3+
SYSCTL="/proc/sys/debug/test_sysctl/"
4+
TARGET="${SYSCTL}/string_0001"
55
ORIG=$(cat "${TARGET}")
66
TEST_STR="Testing sysctl"
77

0 commit comments

Comments
 (0)