Skip to content

Commit 138303e

Browse files
committed
sysctl: move u8 register test to lib/test_sysctl.c
If the test added in commit b5ffbd1 ("sysctl: move the extra1/2 boundary check of u8 to sysctl_check_table_array") is run as a module, a lingering reference to the module is left behind, and a 'sysctl -a' leads to a panic. To reproduce CONFIG_KUNIT=y CONFIG_SYSCTL_KUNIT_TEST=m Then run these commands: modprobe sysctl-test rmmod sysctl-test sysctl -a The panic varies but generally looks something like this: BUG: unable to handle page fault for address: ffffa4571c0c7db4 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 100000067 P4D 100000067 PUD 100351067 PMD 114f5e067 PTE 0 Oops: Oops: 0000 [#1] SMP NOPTI ... ... ... RIP: 0010:proc_sys_readdir+0x166/0x2c0 ... ... ... Call Trace: <TASK> iterate_dir+0x6e/0x140 __se_sys_getdents+0x6e/0x100 do_syscall_64+0x70/0x150 entry_SYSCALL_64_after_hwframe+0x76/0x7e Move the test to lib/test_sysctl.c where the registration reference is handled on module exit Fixes: b5ffbd1 ("sysctl: move the extra1/2 boundary check of u8 to sysctl_check_table_array") Reviewed-by: Kees Cook <kees@kernel.org> Signed-off-by: Joel Granados <joel.granados@kernel.org>
1 parent bc4f328 commit 138303e

2 files changed

Lines changed: 66 additions & 49 deletions

File tree

kernel/sysctl-test.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -367,54 +367,6 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max(
367367
KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
368368
}
369369

370-
/*
371-
* Test that registering an invalid extra value is not allowed.
372-
*/
373-
static void sysctl_test_register_sysctl_sz_invalid_extra_value(
374-
struct kunit *test)
375-
{
376-
unsigned char data = 0;
377-
const struct ctl_table table_foo[] = {
378-
{
379-
.procname = "foo",
380-
.data = &data,
381-
.maxlen = sizeof(u8),
382-
.mode = 0644,
383-
.proc_handler = proc_dou8vec_minmax,
384-
.extra1 = SYSCTL_FOUR,
385-
.extra2 = SYSCTL_ONE_THOUSAND,
386-
},
387-
};
388-
389-
const struct ctl_table table_bar[] = {
390-
{
391-
.procname = "bar",
392-
.data = &data,
393-
.maxlen = sizeof(u8),
394-
.mode = 0644,
395-
.proc_handler = proc_dou8vec_minmax,
396-
.extra1 = SYSCTL_NEG_ONE,
397-
.extra2 = SYSCTL_ONE_HUNDRED,
398-
},
399-
};
400-
401-
const struct ctl_table table_qux[] = {
402-
{
403-
.procname = "qux",
404-
.data = &data,
405-
.maxlen = sizeof(u8),
406-
.mode = 0644,
407-
.proc_handler = proc_dou8vec_minmax,
408-
.extra1 = SYSCTL_ZERO,
409-
.extra2 = SYSCTL_TWO_HUNDRED,
410-
},
411-
};
412-
413-
KUNIT_EXPECT_NULL(test, register_sysctl("foo", table_foo));
414-
KUNIT_EXPECT_NULL(test, register_sysctl("foo", table_bar));
415-
KUNIT_EXPECT_NOT_NULL(test, register_sysctl("foo", table_qux));
416-
}
417-
418370
static struct kunit_case sysctl_test_cases[] = {
419371
KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data),
420372
KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset),
@@ -426,7 +378,6 @@ static struct kunit_case sysctl_test_cases[] = {
426378
KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative),
427379
KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min),
428380
KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max),
429-
KUNIT_CASE(sysctl_test_register_sysctl_sz_invalid_extra_value),
430381
{}
431382
};
432383

lib/test_sysctl.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static struct {
3737
struct ctl_table_header *test_h_mnterror;
3838
struct ctl_table_header *empty_add;
3939
struct ctl_table_header *empty;
40+
struct ctl_table_header *test_u8;
4041
} sysctl_test_headers;
4142

4243
struct test_sysctl_data {
@@ -239,6 +240,65 @@ static int test_sysctl_run_register_empty(void)
239240
return 0;
240241
}
241242

243+
static const struct ctl_table table_u8_over[] = {
244+
{
245+
.procname = "u8_over",
246+
.data = &test_data.uint_0001,
247+
.maxlen = sizeof(u8),
248+
.mode = 0644,
249+
.proc_handler = proc_dou8vec_minmax,
250+
.extra1 = SYSCTL_FOUR,
251+
.extra2 = SYSCTL_ONE_THOUSAND,
252+
},
253+
};
254+
255+
static const struct ctl_table table_u8_under[] = {
256+
{
257+
.procname = "u8_under",
258+
.data = &test_data.uint_0001,
259+
.maxlen = sizeof(u8),
260+
.mode = 0644,
261+
.proc_handler = proc_dou8vec_minmax,
262+
.extra1 = SYSCTL_NEG_ONE,
263+
.extra2 = SYSCTL_ONE_HUNDRED,
264+
},
265+
};
266+
267+
static const struct ctl_table table_u8_valid[] = {
268+
{
269+
.procname = "u8_valid",
270+
.data = &test_data.uint_0001,
271+
.maxlen = sizeof(u8),
272+
.mode = 0644,
273+
.proc_handler = proc_dou8vec_minmax,
274+
.extra1 = SYSCTL_ZERO,
275+
.extra2 = SYSCTL_TWO_HUNDRED,
276+
},
277+
};
278+
279+
static int test_sysctl_register_u8_extra(void)
280+
{
281+
/* should fail because it's over */
282+
sysctl_test_headers.test_u8
283+
= register_sysctl("debug/test_sysctl", table_u8_over);
284+
if (sysctl_test_headers.test_u8)
285+
return -ENOMEM;
286+
287+
/* should fail because it's under */
288+
sysctl_test_headers.test_u8
289+
= register_sysctl("debug/test_sysctl", table_u8_under);
290+
if (sysctl_test_headers.test_u8)
291+
return -ENOMEM;
292+
293+
/* should not fail because it's valid */
294+
sysctl_test_headers.test_u8
295+
= register_sysctl("debug/test_sysctl", table_u8_valid);
296+
if (!sysctl_test_headers.test_u8)
297+
return -ENOMEM;
298+
299+
return 0;
300+
}
301+
242302
static int __init test_sysctl_init(void)
243303
{
244304
int err;
@@ -256,6 +316,10 @@ static int __init test_sysctl_init(void)
256316
goto out;
257317

258318
err = test_sysctl_run_register_empty();
319+
if (err)
320+
goto out;
321+
322+
err = test_sysctl_register_u8_extra();
259323

260324
out:
261325
return err;
@@ -275,6 +339,8 @@ static void __exit test_sysctl_exit(void)
275339
unregister_sysctl_table(sysctl_test_headers.empty);
276340
if (sysctl_test_headers.empty_add)
277341
unregister_sysctl_table(sysctl_test_headers.empty_add);
342+
if (sysctl_test_headers.test_u8)
343+
unregister_sysctl_table(sysctl_test_headers.test_u8);
278344
}
279345

280346
module_exit(test_sysctl_exit);

0 commit comments

Comments
 (0)