Patch "kunit/fortify: Fix replaced failure path to unbreak __alloc_size" has been added to the 6.9-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    kunit/fortify: Fix replaced failure path to unbreak __alloc_size

to the 6.9-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     kunit-fortify-fix-replaced-failure-path-to-unbreak-_.patch
and it can be found in the queue-6.9 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit 2bb605b67ffdb84694c6a7b39647fd0925dcbfcc
Author: Kees Cook <keescook@xxxxxxxxxxxx>
Date:   Wed May 1 16:29:48 2024 -0700

    kunit/fortify: Fix replaced failure path to unbreak __alloc_size
    
    [ Upstream commit 74df22453c51392476117d7330bf02cee6e987cf ]
    
    The __alloc_size annotation for kmemdup() was getting disabled under
    KUnit testing because the replaced fortify_panic macro implementation
    was using "return NULL" as a way to survive the sanity checking. But
    having the chance to return NULL invalidated __alloc_size, so kmemdup
    was not passing the __builtin_dynamic_object_size() tests any more:
    
    [23:26:18] [PASSED] fortify_test_alloc_size_kmalloc_const
    [23:26:19]     # fortify_test_alloc_size_kmalloc_dynamic: EXPECTATION FAILED at lib/fortify_kunit.c:265
    [23:26:19]     Expected __builtin_dynamic_object_size(p, 1) == expected, but
    [23:26:19]         __builtin_dynamic_object_size(p, 1) == -1 (0xffffffffffffffff)
    [23:26:19]         expected == 11 (0xb)
    [23:26:19] __alloc_size() not working with __bdos on kmemdup("hello there", len, gfp)
    [23:26:19] [FAILED] fortify_test_alloc_size_kmalloc_dynamic
    
    Normal builds were not affected: __alloc_size continued to work there.
    
    Use a zero-sized allocation instead, which allows __alloc_size to
    behave.
    
    Fixes: 4ce615e798a7 ("fortify: Provide KUnit counters for failure testing")
    Fixes: fa4a3f86d498 ("fortify: Add KUnit tests for runtime overflows")
    Link: https://lore.kernel.org/r/20240501232937.work.532-kees@xxxxxxxxxx
    Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
index 6aeebe0a67770..6eaa190d0083c 100644
--- a/include/linux/fortify-string.h
+++ b/include/linux/fortify-string.h
@@ -734,7 +734,8 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
 	if (__compiletime_lessthan(p_size, size))
 		__read_overflow();
 	if (p_size < size)
-		fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size, NULL);
+		fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size,
+			      __real_kmemdup(p, 0, gfp));
 	return __real_kmemdup(p, size, gfp);
 }
 
diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c
index 86c1b1a6e2c89..fdba0eaf19a59 100644
--- a/lib/fortify_kunit.c
+++ b/lib/fortify_kunit.c
@@ -917,19 +917,19 @@ static void kmemdup_test(struct kunit *test)
 
 	/* Out of bounds by 1 byte. */
 	copy = kmemdup(src, len + 1, GFP_KERNEL);
-	KUNIT_EXPECT_NULL(test, copy);
+	KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
 	KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
 	kfree(copy);
 
 	/* Way out of bounds. */
 	copy = kmemdup(src, len * 2, GFP_KERNEL);
-	KUNIT_EXPECT_NULL(test, copy);
+	KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
 	KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
 	kfree(copy);
 
 	/* Starting offset causing out of bounds. */
 	copy = kmemdup(src + 1, len, GFP_KERNEL);
-	KUNIT_EXPECT_NULL(test, copy);
+	KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
 	KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3);
 	kfree(copy);
 }




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux