summaryrefslogtreecommitdiff
path: root/lib/test_sysctl.c
diff options
context:
space:
mode:
authorJoel Granados <joel.granados@kernel.org>2025-03-13 22:35:25 +0100
committerJoel Granados <joel.granados@kernel.org>2025-04-14 14:13:41 +0200
commit138303ec6ccbe38611931eeb955a722c6f78ec25 (patch)
tree478c53bd05ccbd294b28f1d2c271aac7b71abfa5 /lib/test_sysctl.c
parentbc4f328ff516f6ea53c3c0d385a84ea7ae423c20 (diff)
downloadlinux-138303ec6ccbe38611931eeb955a722c6f78ec25.tar.gz
linux-138303ec6ccbe38611931eeb955a722c6f78ec25.tar.bz2
linux-138303ec6ccbe38611931eeb955a722c6f78ec25.zip
sysctl: move u8 register test to lib/test_sysctl.c
If the test added in commit b5ffbd139688 ("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: b5ffbd139688 ("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>
Diffstat (limited to 'lib/test_sysctl.c')
-rw-r--r--lib/test_sysctl.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c
index 4249e0cc8aaf..54a22e4b1346 100644
--- a/lib/test_sysctl.c
+++ b/lib/test_sysctl.c
@@ -37,6 +37,7 @@ static struct {
struct ctl_table_header *test_h_mnterror;
struct ctl_table_header *empty_add;
struct ctl_table_header *empty;
+ struct ctl_table_header *test_u8;
} sysctl_test_headers;
struct test_sysctl_data {
@@ -239,6 +240,65 @@ static int test_sysctl_run_register_empty(void)
return 0;
}
+static const struct ctl_table table_u8_over[] = {
+ {
+ .procname = "u8_over",
+ .data = &test_data.uint_0001,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_FOUR,
+ .extra2 = SYSCTL_ONE_THOUSAND,
+ },
+};
+
+static const struct ctl_table table_u8_under[] = {
+ {
+ .procname = "u8_under",
+ .data = &test_data.uint_0001,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_NEG_ONE,
+ .extra2 = SYSCTL_ONE_HUNDRED,
+ },
+};
+
+static const struct ctl_table table_u8_valid[] = {
+ {
+ .procname = "u8_valid",
+ .data = &test_data.uint_0001,
+ .maxlen = sizeof(u8),
+ .mode = 0644,
+ .proc_handler = proc_dou8vec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_TWO_HUNDRED,
+ },
+};
+
+static int test_sysctl_register_u8_extra(void)
+{
+ /* should fail because it's over */
+ sysctl_test_headers.test_u8
+ = register_sysctl("debug/test_sysctl", table_u8_over);
+ if (sysctl_test_headers.test_u8)
+ return -ENOMEM;
+
+ /* should fail because it's under */
+ sysctl_test_headers.test_u8
+ = register_sysctl("debug/test_sysctl", table_u8_under);
+ if (sysctl_test_headers.test_u8)
+ return -ENOMEM;
+
+ /* should not fail because it's valid */
+ sysctl_test_headers.test_u8
+ = register_sysctl("debug/test_sysctl", table_u8_valid);
+ if (!sysctl_test_headers.test_u8)
+ return -ENOMEM;
+
+ return 0;
+}
+
static int __init test_sysctl_init(void)
{
int err;
@@ -256,6 +316,10 @@ static int __init test_sysctl_init(void)
goto out;
err = test_sysctl_run_register_empty();
+ if (err)
+ goto out;
+
+ err = test_sysctl_register_u8_extra();
out:
return err;
@@ -275,6 +339,8 @@ static void __exit test_sysctl_exit(void)
unregister_sysctl_table(sysctl_test_headers.empty);
if (sysctl_test_headers.empty_add)
unregister_sysctl_table(sysctl_test_headers.empty_add);
+ if (sysctl_test_headers.test_u8)
+ unregister_sysctl_table(sysctl_test_headers.test_u8);
}
module_exit(test_sysctl_exit);