1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test of proc sysctl.
6 #include <kunit/test.h>
7 #include <linux/sysctl.h>
9 #define KUNIT_PROC_READ 0
10 #define KUNIT_PROC_WRITE 1
13 * Test that proc_dointvec will not try to use a NULL .data field even when the
16 static void sysctl_test_api_dointvec_null_tbl_data(struct kunit *test)
18 struct ctl_table null_data_table = {
21 * Here we are testing that proc_dointvec behaves correctly when
22 * we give it a NULL .data field. Normally this would point to a
23 * piece of memory where the value would be stored.
26 .maxlen = sizeof(int),
28 .proc_handler = proc_dointvec,
29 .extra1 = SYSCTL_ZERO,
30 .extra2 = SYSCTL_ONE_HUNDRED,
33 * proc_dointvec expects a buffer in user space, so we allocate one. We
34 * also need to cast it to __user so sparse doesn't get mad.
36 void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
42 * We don't care what the starting length is since proc_dointvec should
43 * not try to read because .data is NULL.
46 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&null_data_table,
47 KUNIT_PROC_READ, buffer, &len,
49 KUNIT_EXPECT_EQ(test, 0, len);
55 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&null_data_table,
56 KUNIT_PROC_WRITE, buffer, &len,
58 KUNIT_EXPECT_EQ(test, 0, len);
62 * Similar to the previous test, we create a struct ctrl_table that has a .data
63 * field that proc_dointvec cannot do anything with; however, this time it is
64 * because we tell proc_dointvec that the size is 0.
66 static void sysctl_test_api_dointvec_table_maxlen_unset(struct kunit *test)
69 struct ctl_table data_maxlen_unset_table = {
73 * So .data is no longer NULL, but we tell proc_dointvec its
74 * length is 0, so it still shouldn't try to use it.
78 .proc_handler = proc_dointvec,
79 .extra1 = SYSCTL_ZERO,
80 .extra2 = SYSCTL_ONE_HUNDRED,
82 void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
88 * As before, we don't care what buffer length is because proc_dointvec
89 * cannot do anything because its internal .data buffer has zero length.
92 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&data_maxlen_unset_table,
93 KUNIT_PROC_READ, buffer, &len,
95 KUNIT_EXPECT_EQ(test, 0, len);
98 * See previous comment.
101 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&data_maxlen_unset_table,
102 KUNIT_PROC_WRITE, buffer, &len,
104 KUNIT_EXPECT_EQ(test, 0, len);
108 * Here we provide a valid struct ctl_table, but we try to read and write from
109 * it using a buffer of zero length, so it should still fail in a similar way as
112 static void sysctl_test_api_dointvec_table_len_is_zero(struct kunit *test)
116 struct ctl_table table = {
119 .maxlen = sizeof(int),
121 .proc_handler = proc_dointvec,
122 .extra1 = SYSCTL_ZERO,
123 .extra2 = SYSCTL_ONE_HUNDRED,
125 void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
128 * However, now our read/write buffer has zero length.
133 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ, buffer,
135 KUNIT_EXPECT_EQ(test, 0, len);
137 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_WRITE, buffer,
139 KUNIT_EXPECT_EQ(test, 0, len);
143 * Test that proc_dointvec refuses to read when the file position is non-zero.
145 static void sysctl_test_api_dointvec_table_read_but_position_set(
150 struct ctl_table table = {
153 .maxlen = sizeof(int),
155 .proc_handler = proc_dointvec,
156 .extra1 = SYSCTL_ZERO,
157 .extra2 = SYSCTL_ONE_HUNDRED,
159 void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
162 * We don't care about our buffer length because we start off with a
163 * non-zero file position.
167 * proc_dointvec should refuse to read into the buffer since the file
172 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ, buffer,
174 KUNIT_EXPECT_EQ(test, 0, len);
178 * Test that we can read a two digit number in a sufficiently size buffer.
181 static void sysctl_test_dointvec_read_happy_single_positive(struct kunit *test)
185 struct ctl_table table = {
188 .maxlen = sizeof(int),
190 .proc_handler = proc_dointvec,
191 .extra1 = SYSCTL_ZERO,
192 .extra2 = SYSCTL_ONE_HUNDRED,
196 char *buffer = kunit_kzalloc(test, len, GFP_USER);
197 char __user *user_buffer = (char __user *)buffer;
198 /* Store 13 in the data field. */
199 *((int *)table.data) = 13;
201 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ,
202 user_buffer, &len, &pos));
203 KUNIT_ASSERT_EQ(test, 3, len);
205 /* And we read 13 back out. */
206 KUNIT_EXPECT_STREQ(test, "13\n", buffer);
210 * Same as previous test, just now with negative numbers.
212 static void sysctl_test_dointvec_read_happy_single_negative(struct kunit *test)
216 struct ctl_table table = {
219 .maxlen = sizeof(int),
221 .proc_handler = proc_dointvec,
222 .extra1 = SYSCTL_ZERO,
223 .extra2 = SYSCTL_ONE_HUNDRED,
227 char *buffer = kunit_kzalloc(test, len, GFP_USER);
228 char __user *user_buffer = (char __user *)buffer;
229 *((int *)table.data) = -16;
231 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ,
232 user_buffer, &len, &pos));
233 KUNIT_ASSERT_EQ(test, 4, len);
235 KUNIT_EXPECT_STREQ(test, "-16\n", buffer);
239 * Test that a simple positive write works.
241 static void sysctl_test_dointvec_write_happy_single_positive(struct kunit *test)
245 struct ctl_table table = {
248 .maxlen = sizeof(int),
250 .proc_handler = proc_dointvec,
251 .extra1 = SYSCTL_ZERO,
252 .extra2 = SYSCTL_ONE_HUNDRED,
255 size_t len = sizeof(input) - 1;
257 char *buffer = kunit_kzalloc(test, len, GFP_USER);
258 char __user *user_buffer = (char __user *)buffer;
260 memcpy(buffer, input, len);
262 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_WRITE,
263 user_buffer, &len, &pos));
264 KUNIT_EXPECT_EQ(test, sizeof(input) - 1, len);
265 KUNIT_EXPECT_EQ(test, sizeof(input) - 1, pos);
266 KUNIT_EXPECT_EQ(test, 9, *((int *)table.data));
270 * Same as previous test, but now with negative numbers.
272 static void sysctl_test_dointvec_write_happy_single_negative(struct kunit *test)
275 struct ctl_table table = {
278 .maxlen = sizeof(int),
280 .proc_handler = proc_dointvec,
281 .extra1 = SYSCTL_ZERO,
282 .extra2 = SYSCTL_ONE_HUNDRED,
285 size_t len = sizeof(input) - 1;
287 char *buffer = kunit_kzalloc(test, len, GFP_USER);
288 char __user *user_buffer = (char __user *)buffer;
290 memcpy(buffer, input, len);
292 KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_WRITE,
293 user_buffer, &len, &pos));
294 KUNIT_EXPECT_EQ(test, sizeof(input) - 1, len);
295 KUNIT_EXPECT_EQ(test, sizeof(input) - 1, pos);
296 KUNIT_EXPECT_EQ(test, -9, *((int *)table.data));
300 * Test that writing a value smaller than the minimum possible value is not
303 static void sysctl_test_api_dointvec_write_single_less_int_min(
307 struct ctl_table table = {
310 .maxlen = sizeof(int),
312 .proc_handler = proc_dointvec,
313 .extra1 = SYSCTL_ZERO,
314 .extra2 = SYSCTL_ONE_HUNDRED,
316 size_t max_len = 32, len = max_len;
318 char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
319 char __user *user_buffer = (char __user *)buffer;
320 unsigned long abs_of_less_than_min = (unsigned long)INT_MAX
321 - (INT_MAX + INT_MIN) + 1;
324 * We use this rigmarole to create a string that contains a value one
325 * less than the minimum accepted value.
327 KUNIT_ASSERT_LT(test,
328 (size_t)snprintf(buffer, max_len, "-%lu",
329 abs_of_less_than_min),
332 KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec(&table, KUNIT_PROC_WRITE,
333 user_buffer, &len, &pos));
334 KUNIT_EXPECT_EQ(test, max_len, len);
335 KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
339 * Test that writing the maximum possible value works.
341 static void sysctl_test_api_dointvec_write_single_greater_int_max(
345 struct ctl_table table = {
348 .maxlen = sizeof(int),
350 .proc_handler = proc_dointvec,
351 .extra1 = SYSCTL_ZERO,
352 .extra2 = SYSCTL_ONE_HUNDRED,
354 size_t max_len = 32, len = max_len;
356 char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
357 char __user *user_buffer = (char __user *)buffer;
358 unsigned long greater_than_max = (unsigned long)INT_MAX + 1;
360 KUNIT_ASSERT_GT(test, greater_than_max, (unsigned long)INT_MAX);
361 KUNIT_ASSERT_LT(test, (size_t)snprintf(buffer, max_len, "%lu",
364 KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec(&table, KUNIT_PROC_WRITE,
365 user_buffer, &len, &pos));
366 KUNIT_ASSERT_EQ(test, max_len, len);
367 KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
370 static struct kunit_case sysctl_test_cases[] = {
371 KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data),
372 KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset),
373 KUNIT_CASE(sysctl_test_api_dointvec_table_len_is_zero),
374 KUNIT_CASE(sysctl_test_api_dointvec_table_read_but_position_set),
375 KUNIT_CASE(sysctl_test_dointvec_read_happy_single_positive),
376 KUNIT_CASE(sysctl_test_dointvec_read_happy_single_negative),
377 KUNIT_CASE(sysctl_test_dointvec_write_happy_single_positive),
378 KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative),
379 KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min),
380 KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max),
384 static struct kunit_suite sysctl_test_suite = {
385 .name = "sysctl_test",
386 .test_cases = sysctl_test_cases,
389 kunit_test_suites(&sysctl_test_suite);
391 MODULE_LICENSE("GPL v2");