Skip to content

Commit cf2f06f

Browse files
committed
lkdtm/fortify: Drop unneeded FORTIFY_STR_OBJECT test
The str* family of fortified functions all use member-sized limits for a while now, so the FORTIFY_STR_OBJECT test is redundant to FORTIFY_STR_MEMBER. While here, replace the strncpy() use with strscpy(), as strncpy() is being removed. Link: https://patch.msgid.link/20260324020726.work.624-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 00247cb commit cf2f06f

2 files changed

Lines changed: 6 additions & 31 deletions

File tree

drivers/misc/lkdtm/fortify.c

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,6 @@
1010

1111
static volatile int fortify_scratch_space;
1212

13-
static void lkdtm_FORTIFY_STR_OBJECT(void)
14-
{
15-
struct target {
16-
char a[10];
17-
int foo;
18-
} target[3] = {};
19-
/*
20-
* Using volatile prevents the compiler from determining the value of
21-
* 'size' at compile time. Without that, we would get a compile error
22-
* rather than a runtime error.
23-
*/
24-
volatile int size = 20;
25-
26-
pr_info("trying to strcmp() past the end of a struct\n");
27-
28-
strncpy(target[0].a, target[1].a, size);
29-
30-
/* Store result to global to prevent the code from being eliminated */
31-
fortify_scratch_space = target[0].a[3];
32-
33-
pr_err("FAIL: fortify did not block a strncpy() object write overflow!\n");
34-
pr_expected_config(CONFIG_FORTIFY_SOURCE);
35-
}
36-
3713
static void lkdtm_FORTIFY_STR_MEMBER(void)
3814
{
3915
struct target {
@@ -47,22 +23,23 @@ static void lkdtm_FORTIFY_STR_MEMBER(void)
4723
if (!src)
4824
return;
4925

26+
/* 15 bytes: past end of a[] but not target. */
5027
strscpy(src, "over ten bytes", size);
5128
size = strlen(src) + 1;
5229

53-
pr_info("trying to strncpy() past the end of a struct member...\n");
30+
pr_info("trying to strscpy() past the end of a struct member...\n");
5431

5532
/*
56-
* strncpy(target.a, src, 20); will hit a compile error because the
57-
* compiler knows at build time that target.a < 20 bytes. Use a
33+
* strscpy(target.a, src, 15); will hit a compile error because the
34+
* compiler knows at build time that target.a < 15 bytes. Use a
5835
* volatile to force a runtime error.
5936
*/
60-
strncpy(target.a, src, size);
37+
strscpy(target.a, src, size);
6138

6239
/* Store result to global to prevent the code from being eliminated */
6340
fortify_scratch_space = target.a[3];
6441

65-
pr_err("FAIL: fortify did not block a strncpy() struct member write overflow!\n");
42+
pr_err("FAIL: fortify did not block a strscpy() struct member write overflow!\n");
6643
pr_expected_config(CONFIG_FORTIFY_SOURCE);
6744

6845
kfree(src);
@@ -210,7 +187,6 @@ static void lkdtm_FORTIFY_STRSCPY(void)
210187
}
211188

212189
static struct crashtype crashtypes[] = {
213-
CRASHTYPE(FORTIFY_STR_OBJECT),
214190
CRASHTYPE(FORTIFY_STR_MEMBER),
215191
CRASHTYPE(FORTIFY_MEM_OBJECT),
216192
CRASHTYPE(FORTIFY_MEM_MEMBER),

tools/testing/selftests/lkdtm/tests.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ STACKLEAK_ERASING OK: the rest of the thread stack is properly erased
8282
CFI_FORWARD_PROTO
8383
CFI_BACKWARD call trace:|ok: control flow unchanged
8484
FORTIFY_STRSCPY detected buffer overflow
85-
FORTIFY_STR_OBJECT detected buffer overflow
8685
FORTIFY_STR_MEMBER detected buffer overflow
8786
FORTIFY_MEM_OBJECT detected buffer overflow
8887
FORTIFY_MEM_MEMBER detected field-spanning write

0 commit comments

Comments
 (0)