Skip to content

Commit 3bf301e

Browse files
committed
string: Add Kunit tests for strcat() family
Add tests to make sure the strcat() family of functions behave correctly. Signed-off-by: Kees Cook <[email protected]>
1 parent a9dc8d0 commit 3bf301e

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8061,6 +8061,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/har
80618061
F: include/linux/fortify-string.h
80628062
F: lib/fortify_kunit.c
80638063
F: lib/memcpy_kunit.c
8064+
F: lib/strcat_kunit.c
80648065
F: lib/strscpy_kunit.c
80658066
F: lib/test_fortify/*
80668067
F: scripts/test_fortify.sh

lib/Kconfig.debug

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,11 @@ config HW_BREAKPOINT_KUNIT_TEST
26622662

26632663
If unsure, say N.
26642664

2665+
config STRCAT_KUNIT_TEST
2666+
tristate "Test strcat() family of functions at runtime" if !KUNIT_ALL_TESTS
2667+
depends on KUNIT
2668+
default KUNIT_ALL_TESTS
2669+
26652670
config STRSCPY_KUNIT_TEST
26662671
tristate "Test strscpy*() family of functions at runtime" if !KUNIT_ALL_TESTS
26672672
depends on KUNIT

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
392392
CFLAGS_fortify_kunit.o += $(call cc-disable-warning, unsequenced)
393393
CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)
394394
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
395+
obj-$(CONFIG_STRCAT_KUNIT_TEST) += strcat_kunit.o
395396
obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o
396397
obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o
397398

lib/strcat_kunit.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Kernel module for testing 'strcat' 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+
static volatile int unconst;
12+
13+
static void strcat_test(struct kunit *test)
14+
{
15+
char dest[8];
16+
17+
/* Destination is terminated. */
18+
memset(dest, 0, sizeof(dest));
19+
KUNIT_EXPECT_EQ(test, strlen(dest), 0);
20+
/* Empty copy does nothing. */
21+
KUNIT_EXPECT_TRUE(test, strcat(dest, "") == dest);
22+
KUNIT_EXPECT_STREQ(test, dest, "");
23+
/* 4 characters copied in, stops at %NUL. */
24+
KUNIT_EXPECT_TRUE(test, strcat(dest, "four\000123") == dest);
25+
KUNIT_EXPECT_STREQ(test, dest, "four");
26+
KUNIT_EXPECT_EQ(test, dest[5], '\0');
27+
/* 2 more characters copied in okay. */
28+
KUNIT_EXPECT_TRUE(test, strcat(dest, "AB") == dest);
29+
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
30+
}
31+
32+
static void strncat_test(struct kunit *test)
33+
{
34+
char dest[8];
35+
36+
/* Destination is terminated. */
37+
memset(dest, 0, sizeof(dest));
38+
KUNIT_EXPECT_EQ(test, strlen(dest), 0);
39+
/* Empty copy of size 0 does nothing. */
40+
KUNIT_EXPECT_TRUE(test, strncat(dest, "", 0 + unconst) == dest);
41+
KUNIT_EXPECT_STREQ(test, dest, "");
42+
/* Empty copy of size 1 does nothing too. */
43+
KUNIT_EXPECT_TRUE(test, strncat(dest, "", 1 + unconst) == dest);
44+
KUNIT_EXPECT_STREQ(test, dest, "");
45+
/* Copy of max 0 characters should do nothing. */
46+
KUNIT_EXPECT_TRUE(test, strncat(dest, "asdf", 0 + unconst) == dest);
47+
KUNIT_EXPECT_STREQ(test, dest, "");
48+
49+
/* 4 characters copied in, even if max is 8. */
50+
KUNIT_EXPECT_TRUE(test, strncat(dest, "four\000123", 8 + unconst) == dest);
51+
KUNIT_EXPECT_STREQ(test, dest, "four");
52+
KUNIT_EXPECT_EQ(test, dest[5], '\0');
53+
KUNIT_EXPECT_EQ(test, dest[6], '\0');
54+
/* 2 characters copied in okay, 2 ignored. */
55+
KUNIT_EXPECT_TRUE(test, strncat(dest, "ABCD", 2 + unconst) == dest);
56+
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
57+
}
58+
59+
static void strlcat_test(struct kunit *test)
60+
{
61+
char dest[8] = "";
62+
int len = sizeof(dest) + unconst;
63+
64+
/* Destination is terminated. */
65+
KUNIT_EXPECT_EQ(test, strlen(dest), 0);
66+
/* Empty copy is size 0. */
67+
KUNIT_EXPECT_EQ(test, strlcat(dest, "", len), 0);
68+
KUNIT_EXPECT_STREQ(test, dest, "");
69+
/* Size 1 should keep buffer terminated, report size of source only. */
70+
KUNIT_EXPECT_EQ(test, strlcat(dest, "four", 1 + unconst), 4);
71+
KUNIT_EXPECT_STREQ(test, dest, "");
72+
73+
/* 4 characters copied in. */
74+
KUNIT_EXPECT_EQ(test, strlcat(dest, "four", len), 4);
75+
KUNIT_EXPECT_STREQ(test, dest, "four");
76+
/* 2 characters copied in okay, gets to 6 total. */
77+
KUNIT_EXPECT_EQ(test, strlcat(dest, "AB", len), 6);
78+
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
79+
/* 2 characters ignored if max size (7) reached. */
80+
KUNIT_EXPECT_EQ(test, strlcat(dest, "CD", 7 + unconst), 8);
81+
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
82+
/* 1 of 2 characters skipped, now at true max size. */
83+
KUNIT_EXPECT_EQ(test, strlcat(dest, "EFG", len), 9);
84+
KUNIT_EXPECT_STREQ(test, dest, "fourABE");
85+
/* Everything else ignored, now at full size. */
86+
KUNIT_EXPECT_EQ(test, strlcat(dest, "1234", len), 11);
87+
KUNIT_EXPECT_STREQ(test, dest, "fourABE");
88+
}
89+
90+
static struct kunit_case strcat_test_cases[] = {
91+
KUNIT_CASE(strcat_test),
92+
KUNIT_CASE(strncat_test),
93+
KUNIT_CASE(strlcat_test),
94+
{}
95+
};
96+
97+
static struct kunit_suite strcat_test_suite = {
98+
.name = "strcat",
99+
.test_cases = strcat_test_cases,
100+
};
101+
102+
kunit_test_suite(strcat_test_suite);
103+
104+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)