Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 2003e48

Browse files
committed
fortify: Do not special-case 0-sized destinations
All fake flexible arrays should have been removed now, so remove the special casing that was avoiding checking them. If a destination claims to be 0 sized, believe it. This is especially important for cases where __counted_by is in use and may have a 0 element count. Link: https://lore.kernel.org/r/20240619203105.work.747-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent d6f635b commit 2003e48

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

include/linux/fortify-string.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,19 +601,15 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
601601
/*
602602
* Warn when writing beyond destination field size.
603603
*
604-
* We must ignore p_size_field == 0 for existing 0-element
605-
* fake flexible arrays, until they are all converted to
606-
* proper flexible arrays.
607-
*
608-
* The implementation of __builtin_*object_size() behaves
604+
* Note the implementation of __builtin_*object_size() behaves
609605
* like sizeof() when not directly referencing a flexible
610606
* array member, which means there will be many bounds checks
611607
* that will appear at run-time, without a way for them to be
612608
* detected at compile-time (as can be done when the destination
613609
* is specifically the flexible array member).
614610
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832
615611
*/
616-
if (p_size_field != 0 && p_size_field != SIZE_MAX &&
612+
if (p_size_field != SIZE_MAX &&
617613
p_size != p_size_field && p_size_field < size)
618614
return true;
619615

lib/fortify_kunit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,10 +910,9 @@ static void fortify_test_##memfunc(struct kunit *test) \
910910
memfunc(zero.buf, srcB, 0 + unconst); \
911911
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
912912
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
913-
/* We currently explicitly ignore zero-sized dests. */ \
914913
memfunc(zero.buf, srcB, 1 + unconst); \
915914
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); \
916-
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 0); \
915+
KUNIT_EXPECT_EQ(test, fortify_write_overflows, 1); \
917916
}
918917
__fortify_test(memcpy)
919918
__fortify_test(memmove)

0 commit comments

Comments
 (0)