xref: /linux-6.15/lib/test_sysctl.c (revision 77774077)
1 // SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
2 /*
3  * proc sysctl test driver
4  *
5  * Copyright (C) 2017 Luis R. Rodriguez <[email protected]>
6  */
7 
8 /*
9  * This module provides an interface to the proc sysctl interfaces.  This
10  * driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the
11  * system unless explicitly requested by name. You can also build this driver
12  * into your kernel.
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/printk.h>
21 #include <linux/fs.h>
22 #include <linux/miscdevice.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/async.h>
26 #include <linux/delay.h>
27 #include <linux/vmalloc.h>
28 
29 static int i_zero;
30 static int i_one_hundred = 100;
31 static int match_int_ok = 1;
32 
33 
34 static struct {
35 	struct ctl_table_header *test_h_setup_node;
36 	struct ctl_table_header *test_h_mnt;
37 	struct ctl_table_header *test_h_mnterror;
38 	struct ctl_table_header *empty_add;
39 	struct ctl_table_header *empty;
40 } sysctl_test_headers;
41 
42 struct test_sysctl_data {
43 	int int_0001;
44 	int int_0002;
45 	int int_0003[4];
46 
47 	int boot_int;
48 
49 	unsigned int uint_0001;
50 
51 	char string_0001[65];
52 
53 #define SYSCTL_TEST_BITMAP_SIZE	65536
54 	unsigned long *bitmap_0001;
55 };
56 
57 static struct test_sysctl_data test_data = {
58 	.int_0001 = 60,
59 	.int_0002 = 1,
60 
61 	.int_0003[0] = 0,
62 	.int_0003[1] = 1,
63 	.int_0003[2] = 2,
64 	.int_0003[3] = 3,
65 
66 	.boot_int = 0,
67 
68 	.uint_0001 = 314,
69 
70 	.string_0001 = "(none)",
71 };
72 
73 /* These are all under /proc/sys/debug/test_sysctl/ */
74 static struct ctl_table test_table[] = {
75 	{
76 		.procname	= "int_0001",
77 		.data		= &test_data.int_0001,
78 		.maxlen		= sizeof(int),
79 		.mode		= 0644,
80 		.proc_handler	= proc_dointvec_minmax,
81 		.extra1		= &i_zero,
82 		.extra2         = &i_one_hundred,
83 	},
84 	{
85 		.procname	= "int_0002",
86 		.data		= &test_data.int_0002,
87 		.maxlen		= sizeof(int),
88 		.mode		= 0644,
89 		.proc_handler	= proc_dointvec,
90 	},
91 	{
92 		.procname	= "int_0003",
93 		.data		= &test_data.int_0003,
94 		.maxlen		= sizeof(test_data.int_0003),
95 		.mode		= 0644,
96 		.proc_handler	= proc_dointvec,
97 	},
98 	{
99 		.procname	= "match_int",
100 		.data		= &match_int_ok,
101 		.maxlen		= sizeof(match_int_ok),
102 		.mode		= 0444,
103 		.proc_handler	= proc_dointvec,
104 	},
105 	{
106 		.procname	= "boot_int",
107 		.data		= &test_data.boot_int,
108 		.maxlen		= sizeof(test_data.boot_int),
109 		.mode		= 0644,
110 		.proc_handler	= proc_dointvec,
111 		.extra1		= SYSCTL_ZERO,
112 		.extra2         = SYSCTL_ONE,
113 	},
114 	{
115 		.procname	= "uint_0001",
116 		.data		= &test_data.uint_0001,
117 		.maxlen		= sizeof(unsigned int),
118 		.mode		= 0644,
119 		.proc_handler	= proc_douintvec,
120 	},
121 	{
122 		.procname	= "string_0001",
123 		.data		= &test_data.string_0001,
124 		.maxlen		= sizeof(test_data.string_0001),
125 		.mode		= 0644,
126 		.proc_handler	= proc_dostring,
127 	},
128 	{
129 		.procname	= "bitmap_0001",
130 		.data		= &test_data.bitmap_0001,
131 		.maxlen		= SYSCTL_TEST_BITMAP_SIZE,
132 		.mode		= 0644,
133 		.proc_handler	= proc_do_large_bitmap,
134 	},
135 	{ }
136 };
137 
138 static void test_sysctl_calc_match_int_ok(void)
139 {
140 	int i;
141 
142 	struct {
143 		int defined;
144 		int wanted;
145 	} match_int[] = {
146 		{.defined = *(int *)SYSCTL_ZERO,	.wanted = 0},
147 		{.defined = *(int *)SYSCTL_ONE,		.wanted = 1},
148 		{.defined = *(int *)SYSCTL_TWO,		.wanted = 2},
149 		{.defined = *(int *)SYSCTL_THREE,	.wanted = 3},
150 		{.defined = *(int *)SYSCTL_FOUR,	.wanted = 4},
151 		{.defined = *(int *)SYSCTL_ONE_HUNDRED, .wanted = 100},
152 		{.defined = *(int *)SYSCTL_TWO_HUNDRED,	.wanted = 200},
153 		{.defined = *(int *)SYSCTL_ONE_THOUSAND, .wanted = 1000},
154 		{.defined = *(int *)SYSCTL_THREE_THOUSAND, .wanted = 3000},
155 		{.defined = *(int *)SYSCTL_INT_MAX,	.wanted = INT_MAX},
156 		{.defined = *(int *)SYSCTL_MAXOLDUID,	.wanted = 65535},
157 		{.defined = *(int *)SYSCTL_NEG_ONE,	.wanted = -1},
158 	};
159 
160 	for (i = 0; i < ARRAY_SIZE(match_int); i++)
161 		if (match_int[i].defined != match_int[i].wanted)
162 			match_int_ok = 0;
163 }
164 
165 static int test_sysctl_setup_node_tests(void)
166 {
167 	test_sysctl_calc_match_int_ok();
168 	test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL);
169 	if (!test_data.bitmap_0001)
170 		return -ENOMEM;
171 	sysctl_test_headers.test_h_setup_node = register_sysctl("debug/test_sysctl", test_table);
172 	if (!sysctl_test_headers.test_h_setup_node) {
173 		kfree(test_data.bitmap_0001);
174 		return -ENOMEM;
175 	}
176 
177 	return 0;
178 }
179 
180 /* Used to test that unregister actually removes the directory */
181 static struct ctl_table test_table_unregister[] = {
182 	{
183 		.procname	= "unregister_error",
184 		.data		= &test_data.int_0001,
185 		.maxlen		= sizeof(int),
186 		.mode		= 0644,
187 		.proc_handler	= proc_dointvec_minmax,
188 	},
189 	{}
190 };
191 
192 static int test_sysctl_run_unregister_nested(void)
193 {
194 	struct ctl_table_header *unregister;
195 
196 	unregister = register_sysctl("debug/test_sysctl/unregister_error",
197 				   test_table_unregister);
198 	if (!unregister)
199 		return -ENOMEM;
200 
201 	unregister_sysctl_table(unregister);
202 	return 0;
203 }
204 
205 static int test_sysctl_run_register_mount_point(void)
206 {
207 	sysctl_test_headers.test_h_mnt
208 		= register_sysctl_mount_point("debug/test_sysctl/mnt");
209 	if (!sysctl_test_headers.test_h_mnt)
210 		return -ENOMEM;
211 
212 	sysctl_test_headers.test_h_mnterror
213 		= register_sysctl("debug/test_sysctl/mnt/mnt_error",
214 				  test_table_unregister);
215 	/*
216 	 * Don't check the result.:
217 	 * If it fails (expected behavior), return 0.
218 	 * If successful (missbehavior of register mount point), we want to see
219 	 * mnt_error when we run the sysctl test script
220 	 */
221 
222 	return 0;
223 }
224 
225 static struct ctl_table test_table_empty[] = { };
226 
227 static int test_sysctl_run_register_empty(void)
228 {
229 	/* Tets that an empty dir can be created */
230 	sysctl_test_headers.empty_add
231 		= register_sysctl("debug/test_sysctl/empty_add", test_table_empty);
232 	if (!sysctl_test_headers.empty_add)
233 		return -ENOMEM;
234 
235 	/* Test that register on top of an empty dir works */
236 	sysctl_test_headers.empty
237 		= register_sysctl("debug/test_sysctl/empty_add/empty", test_table_empty);
238 	if (!sysctl_test_headers.empty)
239 		return -ENOMEM;
240 
241 	return 0;
242 }
243 
244 static int __init test_sysctl_init(void)
245 {
246 	int err;
247 
248 	err = test_sysctl_setup_node_tests();
249 	if (err)
250 		goto out;
251 
252 	err = test_sysctl_run_unregister_nested();
253 	if (err)
254 		goto out;
255 
256 	err = test_sysctl_run_register_mount_point();
257 	if (err)
258 		goto out;
259 
260 	err = test_sysctl_run_register_empty();
261 
262 out:
263 	return err;
264 }
265 module_init(test_sysctl_init);
266 
267 static void __exit test_sysctl_exit(void)
268 {
269 	kfree(test_data.bitmap_0001);
270 	if (sysctl_test_headers.test_h_setup_node)
271 		unregister_sysctl_table(sysctl_test_headers.test_h_setup_node);
272 	if (sysctl_test_headers.test_h_mnt)
273 		unregister_sysctl_table(sysctl_test_headers.test_h_mnt);
274 	if (sysctl_test_headers.test_h_mnterror)
275 		unregister_sysctl_table(sysctl_test_headers.test_h_mnterror);
276 	if (sysctl_test_headers.empty)
277 		unregister_sysctl_table(sysctl_test_headers.empty);
278 	if (sysctl_test_headers.empty_add)
279 		unregister_sysctl_table(sysctl_test_headers.empty_add);
280 }
281 
282 module_exit(test_sysctl_exit);
283 
284 MODULE_AUTHOR("Luis R. Rodriguez <[email protected]>");
285 MODULE_LICENSE("GPL");
286