Skip to content

Commit 811808d

Browse files
Nirjhar Royakpm00
authored andcommitted
mm/kfence: add a new kunit test test_use_after_free_read_nofault()
Faults from copy_from_kernel_nofault() need to be handled by fixup table and should not be handled by kfence. Otherwise while reading /proc/kcore which uses copy_from_kernel_nofault(), kfence can generate false negatives. This can happen when /proc/kcore ends up reading an unmapped address from kfence pool. Let's add a testcase to cover this case. Link: https://lkml.kernel.org/r/210e561f7845697a32de44b643393890f180069f.1729272697.git.ritesh.list@gmail.com Signed-off-by: Nirjhar Roy <nirjhar@linux.ibm.com> Co-developed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Tested-by: Marco Elver <elver@google.com> Reviewed-by: Marco Elver <elver@google.com> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f364cde commit 811808d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

mm/kfence/kfence_test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ static void test_use_after_free_read(struct kunit *test)
383383
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
384384
}
385385

386+
static void test_use_after_free_read_nofault(struct kunit *test)
387+
{
388+
const size_t size = 32;
389+
char *addr;
390+
char dst;
391+
int ret;
392+
393+
setup_test_cache(test, size, 0, NULL);
394+
addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY);
395+
test_free(addr);
396+
/* Use after free with *_nofault() */
397+
ret = copy_from_kernel_nofault(&dst, addr, 1);
398+
KUNIT_EXPECT_EQ(test, ret, -EFAULT);
399+
KUNIT_EXPECT_FALSE(test, report_available());
400+
}
401+
386402
static void test_double_free(struct kunit *test)
387403
{
388404
const size_t size = 32;
@@ -780,6 +796,7 @@ static struct kunit_case kfence_test_cases[] = {
780796
KFENCE_KUNIT_CASE(test_out_of_bounds_read),
781797
KFENCE_KUNIT_CASE(test_out_of_bounds_write),
782798
KFENCE_KUNIT_CASE(test_use_after_free_read),
799+
KFENCE_KUNIT_CASE(test_use_after_free_read_nofault),
783800
KFENCE_KUNIT_CASE(test_double_free),
784801
KFENCE_KUNIT_CASE(test_invalid_addr_free),
785802
KFENCE_KUNIT_CASE(test_corruption),

0 commit comments

Comments
 (0)