Skip to content

Commit 41eefc4

Browse files
committed
string: Convert strscpy() self-test to KUnit
Convert the strscpy() self-test to a KUnit test. Cc: David Gow <[email protected]> Cc: Tobin C. Harding <[email protected]> Tested-by: Nathan Chancellor <[email protected]> Link: https://lore.kernel.org/lkml/Y072ZMk/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 9e4a617 commit 41eefc4

File tree

5 files changed

+136
-154
lines changed

5 files changed

+136
-154
lines changed

MAINTAINERS

+1
Original file line numberDiff line numberDiff line change
@@ -8045,6 +8045,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/har
80458045
F: include/linux/fortify-string.h
80468046
F: lib/fortify_kunit.c
80478047
F: lib/memcpy_kunit.c
8048+
F: lib/strscpy_kunit.c
80488049
F: lib/test_fortify/*
80498050
F: scripts/test_fortify.sh
80508051
K: \b__NO_FORTIFY\b

lib/Kconfig.debug

+5-3
Original file line numberDiff line numberDiff line change
@@ -2215,9 +2215,6 @@ config STRING_SELFTEST
22152215
config TEST_STRING_HELPERS
22162216
tristate "Test functions located in the string_helpers module at runtime"
22172217

2218-
config TEST_STRSCPY
2219-
tristate "Test strscpy*() family of functions at runtime"
2220-
22212218
config TEST_KSTRTOX
22222219
tristate "Test kstrto*() family of functions at runtime"
22232220

@@ -2583,6 +2580,11 @@ config HW_BREAKPOINT_KUNIT_TEST
25832580

25842581
If unsure, say N.
25852582

2583+
config STRSCPY_KUNIT_TEST
2584+
tristate "Test strscpy*() family of functions at runtime" if !KUNIT_ALL_TESTS
2585+
depends on KUNIT
2586+
default KUNIT_ALL_TESTS
2587+
25862588
config TEST_UDELAY
25872589
tristate "udelay test driver"
25882590
help

lib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o
8282
obj-$(CONFIG_TEST_PRINTF) += test_printf.o
8383
obj-$(CONFIG_TEST_SCANF) += test_scanf.o
8484
obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o
85-
obj-$(CONFIG_TEST_STRSCPY) += test_strscpy.o
8685
obj-$(CONFIG_TEST_UUID) += test_uuid.o
8786
obj-$(CONFIG_TEST_XARRAY) += test_xarray.o
8887
obj-$(CONFIG_TEST_PARMAN) += test_parman.o
@@ -380,6 +379,7 @@ obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
380379
CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
381380
obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
382381
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
382+
obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
383383

384384
obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
385385

lib/strscpy_kunit.c

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Kernel module for testing 'strscpy' family of functions.
4+
*/
5+
6+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7+
8+
#include <kunit/test.h>
9+
#include <linux/string.h>
10+
11+
/*
12+
* tc() - Run a specific test case.
13+
* @src: Source string, argument to strscpy_pad()
14+
* @count: Size of destination buffer, argument to strscpy_pad()
15+
* @expected: Expected return value from call to strscpy_pad()
16+
* @terminator: 1 if there should be a terminating null byte 0 otherwise.
17+
* @chars: Number of characters from the src string expected to be
18+
* written to the dst buffer.
19+
* @pad: Number of pad characters expected (in the tail of dst buffer).
20+
* (@pad does not include the null terminator byte.)
21+
*
22+
* Calls strscpy_pad() and verifies the return value and state of the
23+
* destination buffer after the call returns.
24+
*/
25+
static void tc(struct kunit *test, char *src, int count, int expected,
26+
int chars, int terminator, int pad)
27+
{
28+
int nr_bytes_poison;
29+
int max_expected;
30+
int max_count;
31+
int written;
32+
char buf[6];
33+
int index, i;
34+
const char POISON = 'z';
35+
36+
KUNIT_ASSERT_TRUE_MSG(test, src != NULL,
37+
"null source string not supported");
38+
39+
memset(buf, POISON, sizeof(buf));
40+
/* Future proofing test suite, validate args */
41+
max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
42+
max_expected = count - 1; /* Space for the null */
43+
44+
KUNIT_ASSERT_LE_MSG(test, count, max_count,
45+
"count (%d) is too big (%d) ... aborting", count, max_count);
46+
KUNIT_EXPECT_LE_MSG(test, expected, max_expected,
47+
"expected (%d) is bigger than can possibly be returned (%d)",
48+
expected, max_expected);
49+
50+
written = strscpy_pad(buf, src, count);
51+
KUNIT_ASSERT_EQ(test, written, expected);
52+
53+
if (count && written == -E2BIG) {
54+
KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1),
55+
"buffer state invalid for -E2BIG");
56+
KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
57+
"too big string is not null terminated correctly");
58+
}
59+
60+
for (i = 0; i < chars; i++)
61+
KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i],
62+
"buf[i]==%c != src[i]==%c", buf[i], src[i]);
63+
64+
if (terminator)
65+
KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
66+
"string is not null terminated correctly");
67+
68+
for (i = 0; i < pad; i++) {
69+
index = chars + terminator + i;
70+
KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0',
71+
"padding missing at index: %d", i);
72+
}
73+
74+
nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
75+
for (i = 0; i < nr_bytes_poison; i++) {
76+
index = sizeof(buf) - 1 - i; /* Check from the end back */
77+
KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON,
78+
"poison value missing at index: %d", i);
79+
}
80+
}
81+
82+
static void strscpy_test(struct kunit *test)
83+
{
84+
/*
85+
* tc() uses a destination buffer of size 6 and needs at
86+
* least 2 characters spare (one for null and one to check for
87+
* overflow). This means we should only call tc() with
88+
* strings up to a maximum of 4 characters long and 'count'
89+
* should not exceed 4. To test with longer strings increase
90+
* the buffer size in tc().
91+
*/
92+
93+
/* tc(test, src, count, expected, chars, terminator, pad) */
94+
tc(test, "a", 0, -E2BIG, 0, 0, 0);
95+
tc(test, "", 0, -E2BIG, 0, 0, 0);
96+
97+
tc(test, "a", 1, -E2BIG, 0, 1, 0);
98+
tc(test, "", 1, 0, 0, 1, 0);
99+
100+
tc(test, "ab", 2, -E2BIG, 1, 1, 0);
101+
tc(test, "a", 2, 1, 1, 1, 0);
102+
tc(test, "", 2, 0, 0, 1, 1);
103+
104+
tc(test, "abc", 3, -E2BIG, 2, 1, 0);
105+
tc(test, "ab", 3, 2, 2, 1, 0);
106+
tc(test, "a", 3, 1, 1, 1, 1);
107+
tc(test, "", 3, 0, 0, 1, 2);
108+
109+
tc(test, "abcd", 4, -E2BIG, 3, 1, 0);
110+
tc(test, "abc", 4, 3, 3, 1, 0);
111+
tc(test, "ab", 4, 2, 2, 1, 1);
112+
tc(test, "a", 4, 1, 1, 1, 2);
113+
tc(test, "", 4, 0, 0, 1, 3);
114+
}
115+
116+
static struct kunit_case strscpy_test_cases[] = {
117+
KUNIT_CASE(strscpy_test),
118+
{}
119+
};
120+
121+
static struct kunit_suite strscpy_test_suite = {
122+
.name = "strscpy",
123+
.test_cases = strscpy_test_cases,
124+
};
125+
126+
kunit_test_suite(strscpy_test_suite);
127+
128+
MODULE_AUTHOR("Tobin C. Harding <[email protected]>");
129+
MODULE_LICENSE("GPL");

lib/test_strscpy.c

-150
This file was deleted.

0 commit comments

Comments
 (0)