Skip to content

Commit

Permalink
mm/kfence: add a new kunit test test_use_after_free_read_nofault()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Nirjhar Roy authored and Andrew Morton committed Nov 15, 2024
1 parent f364cde commit 811808d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mm/kfence/kfence_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ static void test_use_after_free_read(struct kunit *test)
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

static void test_use_after_free_read_nofault(struct kunit *test)
{
const size_t size = 32;
char *addr;
char dst;
int ret;

setup_test_cache(test, size, 0, NULL);
addr = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY);
test_free(addr);
/* Use after free with *_nofault() */
ret = copy_from_kernel_nofault(&dst, addr, 1);
KUNIT_EXPECT_EQ(test, ret, -EFAULT);
KUNIT_EXPECT_FALSE(test, report_available());
}

static void test_double_free(struct kunit *test)
{
const size_t size = 32;
Expand Down Expand Up @@ -780,6 +796,7 @@ static struct kunit_case kfence_test_cases[] = {
KFENCE_KUNIT_CASE(test_out_of_bounds_read),
KFENCE_KUNIT_CASE(test_out_of_bounds_write),
KFENCE_KUNIT_CASE(test_use_after_free_read),
KFENCE_KUNIT_CASE(test_use_after_free_read_nofault),
KFENCE_KUNIT_CASE(test_double_free),
KFENCE_KUNIT_CASE(test_invalid_addr_free),
KFENCE_KUNIT_CASE(test_corruption),
Expand Down

0 comments on commit 811808d

Please sign in to comment.