From patchwork Fri Mar 21 12:47:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Granados X-Patchwork-Id: 14025395 From: Joel Granados Date: Fri, 21 Mar 2025 13:47:24 +0100 Subject: [PATCH 1/4] sysctl: move u8 register test to lib/test_sysctl.c MIME-Version: 1.0 Message-Id: <20250321-jag-test_extra_val-v1-1-a01b3b17dc66@kernel.org> References: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> In-Reply-To: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> To: Kees Cook , Andrew Morton , Shuah Khan , John Sperbeck Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6197; i=joel.granados@kernel.org; h=from:subject:message-id; bh=ulWpz4Aai0sdDwCgU5sVlfrcZ/wicABXdjo+xHGDW0Q=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGfdYAeU3Wbm3ojUm2x3JxVF90KSyxlJec4Bk VpCsM2UJnu5D4kBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJn3WAHAAoJELqXzVK3 lkFPiTQL+gL5vXJ4CNm6vVjLXeVPTKZmkqHccUg2DOlTDHbwEw1fGCwMBkrIxAxsmwTMBve4XbL O81M0V7eJJzKICmKoozJqGidacgfYe3FzF1RzcP/4meWgpKUQaBxd7PCpgQE/BVAwfASjssiCA2 iRj+r6cac3b0WWbaUhh1M9oAM5HIFLrIy0RQuquADP5Ggr3Ff2kxoA3FD8Ho98+8/hSzJf5DaeR cxqiMmm7kc1yhznqsxrU8vQdx0iN1FiZKByoyOvSU/lOkcv46Xy6dh+XesaNciAye/gO+84wl5i iJWwR4NfXtWulPyUmqSaJcNXQ+HR0HJTOc6Omp6tYT4axJJn/JqR9ip0+VFtUaQJiXa2JizLsfC 7Wpuy3etqcW93xn27nXfd1Iwbyh4j9S6zBsoeb0SQPwhMiNKc3rsvGMSgHIVD4bWelSZh4+XP00 5+ZikEWHBfyifCg0YJ8Zs2+nzbtGJpWOhIXRdVzlux1YS1KFuXu+ATh72qzC3hvikn7ZSEWBT26 ls= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 List-Id: B4 Relay Submissions 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: 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")' Signed-off-by: Joel Granados --- kernel/sysctl-test.c | 49 -------------------------------------- lib/test_sysctl.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 49 deletions(-) diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c index eb2842bd055771ee8a72040f96260bdb0e754ad5..92f94ea28957f48893b0d0d947d73001428fecc7 100644 --- a/kernel/sysctl-test.c +++ b/kernel/sysctl-test.c @@ -367,54 +367,6 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max( KUNIT_EXPECT_EQ(test, 0, *((int *)table.data)); } -/* - * Test that registering an invalid extra value is not allowed. - */ -static void sysctl_test_register_sysctl_sz_invalid_extra_value( - struct kunit *test) -{ - unsigned char data = 0; - const struct ctl_table table_foo[] = { - { - .procname = "foo", - .data = &data, - .maxlen = sizeof(u8), - .mode = 0644, - .proc_handler = proc_dou8vec_minmax, - .extra1 = SYSCTL_FOUR, - .extra2 = SYSCTL_ONE_THOUSAND, - }, - }; - - const struct ctl_table table_bar[] = { - { - .procname = "bar", - .data = &data, - .maxlen = sizeof(u8), - .mode = 0644, - .proc_handler = proc_dou8vec_minmax, - .extra1 = SYSCTL_NEG_ONE, - .extra2 = SYSCTL_ONE_HUNDRED, - }, - }; - - const struct ctl_table table_qux[] = { - { - .procname = "qux", - .data = &data, - .maxlen = sizeof(u8), - .mode = 0644, - .proc_handler = proc_dou8vec_minmax, - .extra1 = SYSCTL_ZERO, - .extra2 = SYSCTL_TWO_HUNDRED, - }, - }; - - KUNIT_EXPECT_NULL(test, register_sysctl("foo", table_foo)); - KUNIT_EXPECT_NULL(test, register_sysctl("foo", table_bar)); - KUNIT_EXPECT_NOT_NULL(test, register_sysctl("foo", table_qux)); -} - static struct kunit_case sysctl_test_cases[] = { KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data), KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset), @@ -426,7 +378,6 @@ static struct kunit_case sysctl_test_cases[] = { KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative), KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min), KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max), - KUNIT_CASE(sysctl_test_register_sysctl_sz_invalid_extra_value), {} }; diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c index 4249e0cc8aaff79d7da03aff81b3f9990e9eaac1..54a22e4b134677e022af05df3c75268e7a4a79e7 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); From patchwork Fri Mar 21 12:47:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Granados X-Patchwork-Id: 14025393 From: Joel Granados Date: Fri, 21 Mar 2025 13:47:25 +0100 Subject: [PATCH 2/4] sysctl: Add 0012 to test the u8 range check MIME-Version: 1.0 Message-Id: <20250321-jag-test_extra_val-v1-2-a01b3b17dc66@kernel.org> References: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> In-Reply-To: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> To: Kees Cook , Andrew Morton , Shuah Khan , John Sperbeck Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2105; i=joel.granados@kernel.org; h=from:subject:message-id; bh=++XbEfpnPeqQ4moIezitAVfEJDIk+P74wNcs5SRzozU=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGfdYAj+wL7uNtMJMtN+SaEhFHsQNTYvxgciq qT1UQ+ymTaQb4kBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJn3WAIAAoJELqXzVK3 lkFP8xcL/00KbQvm8zlN6hup0eFPPsksVlQqhtagFA/g4+ODy/KvMy4YItrC1ixaN9bIjyAMSov 2iUFQsO8VB6PaFjajRJyasZ5ntXMAGUgeAGHUBdrt8nTnmsIjM8tgxpbHWUJglbspBbtn3jVzLW XWbNt9UNb0X9QtFAj1H5oT06dlhtNZgFggUZW2AnYBNl5gbYxtW1LIYCXqbZ9hph1P5O7Y+qGIS jpcYS3B6QgdDgj2WCgUPk8i2atXV6p1gR1gDRxT6mQ/2Hi192I45JCF2klRJptybB4D2oKgRQXK gUhlNCdkdjltM4Yk152o8cKH0Tq1/QVLgnLAh8Iq2wh8rzLdUg9n1/x4h6wp1TAPYnV2ue65O7X mh8FGedySrH9q6xqR0wH3eS5nIFbwEEHvPeT4CWuYRml6xD79pB8EVEHBwj8UUlnyhykZhDe01W fX/BxHfIRKD+xnCkKj9+WG0AJDJGB+yl7cvrcKjdxauOb1gVxBne5+Oyzjf/T0t1+r3LT4+unA5 Gs= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 List-Id: B4 Relay Submissions Add a sysctl test that uses the new u8 test ctl files in a created by the sysctl test module. Check that the u8 proc file that is valid is created and that there are two messages in dmesg for the files that were out of range. Signed-off-by: Joel Granados --- tools/testing/selftests/sysctl/sysctl.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/testing/selftests/sysctl/sysctl.sh b/tools/testing/selftests/sysctl/sysctl.sh index 84472b436c07faf43e555999951e554f8e1a60c5..22b53c263dd4525ee8d4655272d6842e5249a7dc 100755 --- a/tools/testing/selftests/sysctl/sysctl.sh +++ b/tools/testing/selftests/sysctl/sysctl.sh @@ -36,6 +36,7 @@ ALL_TESTS="$ALL_TESTS 0008:1:1:match_int:1" ALL_TESTS="$ALL_TESTS 0009:1:1:unregister_error:0" ALL_TESTS="$ALL_TESTS 0010:1:1:mnt/mnt_error:0" ALL_TESTS="$ALL_TESTS 0011:1:1:empty_add:0" +ALL_TESTS="$ALL_TESTS 0012:1:1:u8_valid:0" function allow_user_defaults() { @@ -851,6 +852,34 @@ sysctl_test_0011() return 0 } +sysctl_test_0012() +{ + TARGET="${SYSCTL}/$(get_test_target 0012)" + echo -n "Testing u8 range check in sysctl table check in ${TARGET} ... " + if [ ! -f ${TARGET} ]; then + echo -e "FAIL\nCould not create ${TARGET}" >&2 + rc=1 + test_rc + fi + + local u8over_msg=$(dmesg | grep "u8_over range value" | wc -l) + if [ ! ${u8over_msg} -eq 1 ]; then + echo -e "FAIL\nu8 overflow not detected" >&2 + rc=1 + test_rc + fi + + local u8under_msg=$(dmesg | grep "u8_under range value" | wc -l) + if [ ! ${u8under_msg} -eq 1 ]; then + echo -e "FAIL\nu8 underflow not detected" >&2 + rc=1 + test_rc + fi + + echo "OK" + return 0 +} + list_tests() { echo "Test ID list:" @@ -870,6 +899,7 @@ list_tests() echo "0009 x $(get_test_count 0009) - tests sysct unregister" echo "0010 x $(get_test_count 0010) - tests sysct mount point" echo "0011 x $(get_test_count 0011) - tests empty directories" + echo "0012 x $(get_test_count 0012) - tests range check for u8 proc_handler" } usage() From patchwork Fri Mar 21 12:47:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Granados X-Patchwork-Id: 14025391 From: Joel Granados Date: Fri, 21 Mar 2025 13:47:26 +0100 Subject: [PATCH 3/4] sysctl: call sysctl tests with a for loop MIME-Version: 1.0 Message-Id: <20250321-jag-test_extra_val-v1-3-a01b3b17dc66@kernel.org> References: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> In-Reply-To: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> To: Kees Cook , Andrew Morton , Shuah Khan , John Sperbeck Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1407; i=joel.granados@kernel.org; h=from:subject:message-id; bh=pDm2RKtKMseczShscO8UmtOM+OBDdzP3KgppVzDVCvA=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGfdYAj1VTNzzhCdo4uFhBpVgJh0tmg7KFxvR b25NxXekUiLW4kBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJn3WAIAAoJELqXzVK3 lkFP4RAL/jj/ahISU6Y7eCrqcrae6/t3XdMoNHXWa/1AXf0bWTJCKp+jVm1H3HErHsntU8SZ0Hw 8MBxVCRSsDiX6l3MQyg3S5Q3DrMflT1ESSMvUyzdDLENq5n/yCYZ/1IqVJ60IeQt/ZJ1OjPPVMd AMOVr1iDpO5m+7fghdVqlnup+6izSRaL3dGG9EBDh7zMIm2dOb5RZ4qSZBjcgDoYq5MwuNyrIdo cl88YqbSb8oCH53zCIdq42tzwOl2d+rkb3myetz/oC1Wix+DMCD3fUJNwOs4Yk6rSCR25QeGgEg GIzcTZfEP2at2C4sCj9sO6pOEB4sQHg6J3e7y8fMkr6zKqII2a+3a8OFZf7+kkDytmqOjHPRpvy 8nUI2OLJdikgn8hN6w0/Vf06GS9bAkOqItENFXr2T7GSLOQt7bRlY2EB1a6z15LGLVSVvtrpkRg 768mNcGn33dQR6RqT80A1lbCf5gXqu+UaZ1LFOfqv2pmFW2cn6UZNRHJWMPxnvFGYE3nkKs5rA5 2w= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 List-Id: B4 Relay Submissions As we add more test functions in lib/tests_sysctl the main test function (test_sysctl_init) grows. Condense the logic to make it easier to add/remove tests. Signed-off-by: Joel Granados --- lib/test_sysctl.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c index 54a22e4b134677e022af05df3c75268e7a4a79e7..4b3d56de6269b93220ecbeb3d3d4e42944b0ca78 100644 --- a/lib/test_sysctl.c +++ b/lib/test_sysctl.c @@ -301,27 +301,19 @@ static int test_sysctl_register_u8_extra(void) static int __init test_sysctl_init(void) { - int err; + int err = 0; - err = test_sysctl_setup_node_tests(); - if (err) - goto out; + int (*func_array[])(void) = { + test_sysctl_setup_node_tests, + test_sysctl_run_unregister_nested, + test_sysctl_run_register_mount_point, + test_sysctl_run_register_empty, + test_sysctl_register_u8_extra + }; - err = test_sysctl_run_unregister_nested(); - if (err) - goto out; + for (int i = 0; !err && i < ARRAY_SIZE(func_array); i++) + err = func_array[i](); - err = test_sysctl_run_register_mount_point(); - if (err) - goto out; - - err = test_sysctl_run_register_empty(); - if (err) - goto out; - - err = test_sysctl_register_u8_extra(); - -out: return err; } module_init(test_sysctl_init); From patchwork Fri Mar 21 12:47:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Granados X-Patchwork-Id: 14025394 From: Joel Granados Date: Fri, 21 Mar 2025 13:47:27 +0100 Subject: [PATCH 4/4] sysctl: Close test ctl_headers with a for loop MIME-Version: 1.0 Message-Id: <20250321-jag-test_extra_val-v1-4-a01b3b17dc66@kernel.org> References: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> In-Reply-To: <20250321-jag-test_extra_val-v1-0-a01b3b17dc66@kernel.org> To: Kees Cook , Andrew Morton , Shuah Khan , John Sperbeck Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, Joel Granados X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4809; i=joel.granados@kernel.org; h=from:subject:message-id; bh=tPclanPxc2LJNZ+LffzKz/MsVwoWY65M+jCqO4MmhRM=; b=owJ4nAHtARL+kA0DAAoBupfNUreWQU8ByyZiAGfdYAmcBP428RNEZK9+DxFx/XWo1HWrXCkYx 1mIYMzHtpVKg4kBswQAAQoAHRYhBK5HCVcl5jElzssnkLqXzVK3lkFPBQJn3WAJAAoJELqXzVK3 lkFPldcL/3Q+mTYV2tiKx/akRMoNAII46ZkMXn0pX8wC33X6kFjgRHi2H99XUdtueb4itHmdcQR g0zo+z8PyAqv+1XfJOsCEy4ilUP106towob7bUHVLBVhiPGiw0XCTmEjne92Wfgk8lh2O25n7dX bgIJEKP1pb+aNPkCPJwcx9/Px8W98BcD+Az0yqO52SnPjInsjDJ8xfgS6GpotB4PIRCnLyiB8jW qBBsIjnTs9mPToF8e0qevpvgXGboSlWYmb/ELe7dQz4xlPdra+OI8CH+3aC9UrYyy2mzkOWItnp rV+Bek/HYMFWGXZpgG/ZR74kPDv4avI0Q1xlJl7y8AWeXbaor6BwN0NUBFgxZeqMlb3YLqCF7XX +m4peyWpyZp/NqtKfh9/G0IWxdhdQPh/G6FP8wdTD42t6WSad5o0dC8zoFrp/k1dFAsSlFU8A/H 2qjI62P+1Ef35Esy4U3d5qk0pgr+l/C0E5Sni69ArHnLxPcyEJX53aXB7pQ1HqWupHfZbpSlM38 kE= X-Developer-Key: i=joel.granados@kernel.org; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for joel.granados@kernel.org/default with auth_id=239 List-Id: B4 Relay Submissions As more tests are added, the exit function gets longer than it should be. Condense the un-register calls into a for loop to make it easier to add/remove tests. Signed-off-by: Joel Granados --- lib/test_sysctl.c | 65 +++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c index 4b3d56de6269b93220ecbeb3d3d4e42944b0ca78..c02aa9c868f2117606b24f114326bf1c396cd584 100644 --- a/lib/test_sysctl.c +++ b/lib/test_sysctl.c @@ -30,16 +30,17 @@ static int i_zero; static int i_one_hundred = 100; static int match_int_ok = 1; +enum { + TEST_H_SETUP_NODE, + TEST_H_MNT, + TEST_H_MNTERROR, + TEST_H_EMPTY_ADD, + TEST_H_EMPTY, + TEST_H_U8, + TEST_H_SIZE /* Always at the end */ +}; -static struct { - struct ctl_table_header *test_h_setup_node; - struct ctl_table_header *test_h_mnt; - 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; - +static struct ctl_table_header *ctl_headers[TEST_H_SIZE] = {}; struct test_sysctl_data { int int_0001; int int_0002; @@ -168,8 +169,8 @@ static int test_sysctl_setup_node_tests(void) test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL); if (!test_data.bitmap_0001) return -ENOMEM; - sysctl_test_headers.test_h_setup_node = register_sysctl("debug/test_sysctl", test_table); - if (!sysctl_test_headers.test_h_setup_node) { + ctl_headers[TEST_H_SETUP_NODE] = register_sysctl("debug/test_sysctl", test_table); + if (!ctl_headers[TEST_H_SETUP_NODE]) { kfree(test_data.bitmap_0001); return -ENOMEM; } @@ -203,12 +204,12 @@ static int test_sysctl_run_unregister_nested(void) static int test_sysctl_run_register_mount_point(void) { - sysctl_test_headers.test_h_mnt + ctl_headers[TEST_H_MNT] = register_sysctl_mount_point("debug/test_sysctl/mnt"); - if (!sysctl_test_headers.test_h_mnt) + if (!ctl_headers[TEST_H_MNT]) return -ENOMEM; - sysctl_test_headers.test_h_mnterror + ctl_headers[TEST_H_MNTERROR] = register_sysctl("debug/test_sysctl/mnt/mnt_error", test_table_unregister); /* @@ -226,15 +227,15 @@ static const struct ctl_table test_table_empty[] = { }; static int test_sysctl_run_register_empty(void) { /* Tets that an empty dir can be created */ - sysctl_test_headers.empty_add + ctl_headers[TEST_H_EMPTY_ADD] = register_sysctl("debug/test_sysctl/empty_add", test_table_empty); - if (!sysctl_test_headers.empty_add) + if (!ctl_headers[TEST_H_EMPTY_ADD]) return -ENOMEM; /* Test that register on top of an empty dir works */ - sysctl_test_headers.empty + ctl_headers[TEST_H_EMPTY] = register_sysctl("debug/test_sysctl/empty_add/empty", test_table_empty); - if (!sysctl_test_headers.empty) + if (!ctl_headers[TEST_H_EMPTY]) return -ENOMEM; return 0; @@ -279,21 +280,21 @@ static const struct ctl_table table_u8_valid[] = { static int test_sysctl_register_u8_extra(void) { /* should fail because it's over */ - sysctl_test_headers.test_u8 + ctl_headers[TEST_H_U8] = register_sysctl("debug/test_sysctl", table_u8_over); - if (sysctl_test_headers.test_u8) + if (ctl_headers[TEST_H_U8]) return -ENOMEM; /* should fail because it's under */ - sysctl_test_headers.test_u8 + ctl_headers[TEST_H_U8] = register_sysctl("debug/test_sysctl", table_u8_under); - if (sysctl_test_headers.test_u8) + if (ctl_headers[TEST_H_U8]) return -ENOMEM; /* should not fail because it's valid */ - sysctl_test_headers.test_u8 + ctl_headers[TEST_H_U8] = register_sysctl("debug/test_sysctl", table_u8_valid); - if (!sysctl_test_headers.test_u8) + if (!ctl_headers[TEST_H_U8]) return -ENOMEM; return 0; @@ -321,18 +322,10 @@ module_init(test_sysctl_init); static void __exit test_sysctl_exit(void) { kfree(test_data.bitmap_0001); - if (sysctl_test_headers.test_h_setup_node) - unregister_sysctl_table(sysctl_test_headers.test_h_setup_node); - if (sysctl_test_headers.test_h_mnt) - unregister_sysctl_table(sysctl_test_headers.test_h_mnt); - if (sysctl_test_headers.test_h_mnterror) - unregister_sysctl_table(sysctl_test_headers.test_h_mnterror); - if (sysctl_test_headers.empty) - 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); + for (int i = 0; i < TEST_H_SIZE; i++) { + if (ctl_headers[i]) + unregister_sysctl_table(ctl_headers[i]); + } } module_exit(test_sysctl_exit);