Commit 41eefc46 authored by Kees Cook's avatar Kees Cook

string: Convert strscpy() self-test to KUnit

Convert the strscpy() self-test to a KUnit test.

Cc: David Gow <davidgow@google.com>
Cc: Tobin C. Harding <tobin@kernel.org>
Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
Link: https://lore.kernel.org/lkml/Y072ZMk/hNkfwqMv@dev-arch.thelio-3990XSigned-off-by: default avatarKees Cook <keescook@chromium.org>
parent 9e4a6177
...@@ -8045,6 +8045,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/har ...@@ -8045,6 +8045,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/har
F: include/linux/fortify-string.h F: include/linux/fortify-string.h
F: lib/fortify_kunit.c F: lib/fortify_kunit.c
F: lib/memcpy_kunit.c F: lib/memcpy_kunit.c
F: lib/strscpy_kunit.c
F: lib/test_fortify/* F: lib/test_fortify/*
F: scripts/test_fortify.sh F: scripts/test_fortify.sh
K: \b__NO_FORTIFY\b K: \b__NO_FORTIFY\b
......
...@@ -2215,9 +2215,6 @@ config STRING_SELFTEST ...@@ -2215,9 +2215,6 @@ config STRING_SELFTEST
config TEST_STRING_HELPERS config TEST_STRING_HELPERS
tristate "Test functions located in the string_helpers module at runtime" tristate "Test functions located in the string_helpers module at runtime"
config TEST_STRSCPY
tristate "Test strscpy*() family of functions at runtime"
config TEST_KSTRTOX config TEST_KSTRTOX
tristate "Test kstrto*() family of functions at runtime" tristate "Test kstrto*() family of functions at runtime"
...@@ -2583,6 +2580,11 @@ config HW_BREAKPOINT_KUNIT_TEST ...@@ -2583,6 +2580,11 @@ config HW_BREAKPOINT_KUNIT_TEST
If unsure, say N. If unsure, say N.
config STRSCPY_KUNIT_TEST
tristate "Test strscpy*() family of functions at runtime" if !KUNIT_ALL_TESTS
depends on KUNIT
default KUNIT_ALL_TESTS
config TEST_UDELAY config TEST_UDELAY
tristate "udelay test driver" tristate "udelay test driver"
help help
......
...@@ -82,7 +82,6 @@ obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o ...@@ -82,7 +82,6 @@ obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o
obj-$(CONFIG_TEST_PRINTF) += test_printf.o obj-$(CONFIG_TEST_PRINTF) += test_printf.o
obj-$(CONFIG_TEST_SCANF) += test_scanf.o obj-$(CONFIG_TEST_SCANF) += test_scanf.o
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
obj-$(CONFIG_TEST_UUID) += test_uuid.o obj-$(CONFIG_TEST_UUID) += test_uuid.o
obj-$(CONFIG_TEST_XARRAY) += test_xarray.o obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
obj-$(CONFIG_TEST_PARMAN) += test_parman.o obj-$(CONFIG_TEST_PARMAN) += test_parman.o
...@@ -380,6 +379,7 @@ obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o ...@@ -380,6 +379,7 @@ obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
......
// SPDX-License-Identifier: GPL-2.0+ // SPDX-License-Identifier: GPL-2.0+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/string.h>
#include "../tools/testing/selftests/kselftest_module.h"
/* /*
* Kernel module for testing 'strscpy' family of functions. * Kernel module for testing 'strscpy' family of functions.
*/ */
KSTM_MODULE_GLOBALS(); #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <kunit/test.h>
#include <linux/string.h>
/* /*
* tc() - Run a specific test case. * tc() - Run a specific test case.
...@@ -26,7 +22,7 @@ KSTM_MODULE_GLOBALS(); ...@@ -26,7 +22,7 @@ KSTM_MODULE_GLOBALS();
* Calls strscpy_pad() and verifies the return value and state of the * Calls strscpy_pad() and verifies the return value and state of the
* destination buffer after the call returns. * destination buffer after the call returns.
*/ */
static int __init tc(char *src, int count, int expected, static void tc(struct kunit *test, char *src, int count, int expected,
int chars, int terminator, int pad) int chars, int terminator, int pad)
{ {
int nr_bytes_poison; int nr_bytes_poison;
...@@ -37,81 +33,53 @@ static int __init tc(char *src, int count, int expected, ...@@ -37,81 +33,53 @@ static int __init tc(char *src, int count, int expected,
int index, i; int index, i;
const char POISON = 'z'; const char POISON = 'z';
total_tests++; KUNIT_ASSERT_TRUE_MSG(test, src != NULL,
"null source string not supported");
if (!src) {
pr_err("null source string not supported\n");
return -1;
}
memset(buf, POISON, sizeof(buf)); memset(buf, POISON, sizeof(buf));
/* Future proofing test suite, validate args */ /* Future proofing test suite, validate args */
max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */ max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
max_expected = count - 1; /* Space for the null */ max_expected = count - 1; /* Space for the null */
if (count > max_count) {
pr_err("count (%d) is too big (%d) ... aborting", count, max_count); KUNIT_ASSERT_LE_MSG(test, count, max_count,
return -1; "count (%d) is too big (%d) ... aborting", count, max_count);
} KUNIT_EXPECT_LE_MSG(test, expected, max_expected,
if (expected > max_expected) { "expected (%d) is bigger than can possibly be returned (%d)",
pr_warn("expected (%d) is bigger than can possibly be returned (%d)",
expected, max_expected); expected, max_expected);
}
written = strscpy_pad(buf, src, count); written = strscpy_pad(buf, src, count);
if ((written) != (expected)) { KUNIT_ASSERT_EQ(test, written, expected);
pr_err("%d != %d (written, expected)\n", written, expected);
goto fail;
}
if (count && written == -E2BIG) { if (count && written == -E2BIG) {
if (strncmp(buf, src, count - 1) != 0) { KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1),
pr_err("buffer state invalid for -E2BIG\n"); "buffer state invalid for -E2BIG");
goto fail; KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
} "too big string is not null terminated correctly");
if (buf[count - 1] != '\0') {
pr_err("too big string is not null terminated correctly\n");
goto fail;
}
} }
for (i = 0; i < chars; i++) { for (i = 0; i < chars; i++)
if (buf[i] != src[i]) { KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i],
pr_err("buf[i]==%c != src[i]==%c\n", buf[i], src[i]); "buf[i]==%c != src[i]==%c", buf[i], src[i]);
goto fail;
}
}
if (terminator) { if (terminator)
if (buf[count - 1] != '\0') { KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
pr_err("string is not null terminated correctly\n"); "string is not null terminated correctly");
goto fail;
}
}
for (i = 0; i < pad; i++) { for (i = 0; i < pad; i++) {
index = chars + terminator + i; index = chars + terminator + i;
if (buf[index] != '\0') { KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0',
pr_err("padding missing at index: %d\n", i); "padding missing at index: %d", i);
goto fail;
}
} }
nr_bytes_poison = sizeof(buf) - chars - terminator - pad; nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
for (i = 0; i < nr_bytes_poison; i++) { for (i = 0; i < nr_bytes_poison; i++) {
index = sizeof(buf) - 1 - i; /* Check from the end back */ index = sizeof(buf) - 1 - i; /* Check from the end back */
if (buf[index] != POISON) { KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON,
pr_err("poison value missing at index: %d\n", i); "poison value missing at index: %d", i);
goto fail;
}
} }
return 0;
fail:
failed_tests++;
return -1;
} }
static void __init selftest(void) static void strscpy_test(struct kunit *test)
{ {
/* /*
* tc() uses a destination buffer of size 6 and needs at * tc() uses a destination buffer of size 6 and needs at
...@@ -122,29 +90,40 @@ static void __init selftest(void) ...@@ -122,29 +90,40 @@ static void __init selftest(void)
* the buffer size in tc(). * the buffer size in tc().
*/ */
/* tc(src, count, expected, chars, terminator, pad) */ /* tc(test, src, count, expected, chars, terminator, pad) */
KSTM_CHECK_ZERO(tc("a", 0, -E2BIG, 0, 0, 0)); tc(test, "a", 0, -E2BIG, 0, 0, 0);
KSTM_CHECK_ZERO(tc("", 0, -E2BIG, 0, 0, 0)); tc(test, "", 0, -E2BIG, 0, 0, 0);
KSTM_CHECK_ZERO(tc("a", 1, -E2BIG, 0, 1, 0)); tc(test, "a", 1, -E2BIG, 0, 1, 0);
KSTM_CHECK_ZERO(tc("", 1, 0, 0, 1, 0)); tc(test, "", 1, 0, 0, 1, 0);
KSTM_CHECK_ZERO(tc("ab", 2, -E2BIG, 1, 1, 0)); tc(test, "ab", 2, -E2BIG, 1, 1, 0);
KSTM_CHECK_ZERO(tc("a", 2, 1, 1, 1, 0)); tc(test, "a", 2, 1, 1, 1, 0);
KSTM_CHECK_ZERO(tc("", 2, 0, 0, 1, 1)); tc(test, "", 2, 0, 0, 1, 1);
KSTM_CHECK_ZERO(tc("abc", 3, -E2BIG, 2, 1, 0)); tc(test, "abc", 3, -E2BIG, 2, 1, 0);
KSTM_CHECK_ZERO(tc("ab", 3, 2, 2, 1, 0)); tc(test, "ab", 3, 2, 2, 1, 0);
KSTM_CHECK_ZERO(tc("a", 3, 1, 1, 1, 1)); tc(test, "a", 3, 1, 1, 1, 1);
KSTM_CHECK_ZERO(tc("", 3, 0, 0, 1, 2)); tc(test, "", 3, 0, 0, 1, 2);
KSTM_CHECK_ZERO(tc("abcd", 4, -E2BIG, 3, 1, 0)); tc(test, "abcd", 4, -E2BIG, 3, 1, 0);
KSTM_CHECK_ZERO(tc("abc", 4, 3, 3, 1, 0)); tc(test, "abc", 4, 3, 3, 1, 0);
KSTM_CHECK_ZERO(tc("ab", 4, 2, 2, 1, 1)); tc(test, "ab", 4, 2, 2, 1, 1);
KSTM_CHECK_ZERO(tc("a", 4, 1, 1, 1, 2)); tc(test, "a", 4, 1, 1, 1, 2);
KSTM_CHECK_ZERO(tc("", 4, 0, 0, 1, 3)); tc(test, "", 4, 0, 0, 1, 3);
} }
KSTM_MODULE_LOADERS(test_strscpy); static struct kunit_case strscpy_test_cases[] = {
KUNIT_CASE(strscpy_test),
{}
};
static struct kunit_suite strscpy_test_suite = {
.name = "strscpy",
.test_cases = strscpy_test_cases,
};
kunit_test_suite(strscpy_test_suite);
MODULE_AUTHOR("Tobin C. Harding <tobin@kernel.org>"); MODULE_AUTHOR("Tobin C. Harding <tobin@kernel.org>");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment