Skip to contentSkip to conversation

mm-kfence-add-a-new-kunit-test-test_use_after_free_read_nofault.patch removed from -mm tree

merged mm-stable Andrew Morton 2 participants 2 messages Nov 15, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: mm/kfence: add a new kunit test test_use_after_free_read_nofault()
has been removed from the -mm tree. Its filename was
mm-kfence-add-a-new-kunit-test-test_use_after_free_read_nofault.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Nirjhar Roy <[email protected]>
Subject: mm/kfence: add a new kunit test test_use_after_free_read_nofault()
Date: Fri, 18 Oct 2024 23:16:01 +0530

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.

---

 mm/kfence/kfence_test.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/kfence/kfence_test.c renamed from mm/kfence/kfence_test.c~mm-kfence-add-a-new-kunit-test-test_use_after_free_read_nofault +17 -0
@@ -383,6 +383,22 @@ static void test_use_after_free_read(str
 	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;
@@ -780,6 +796,7 @@ static struct kunit_case kfence_test_cas
 	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),

_

Patches currently in -mm which might be from [email protected] are

#1 lore raw

Thank you for the update.

Quoted 73 lines · Andrew Morton

The quilt patch titled
Subject: mm/kfence: add a new kunit test test_use_after_free_read_nofault()
has been removed from the -mm tree. Its filename was
mm-kfence-add-a-new-kunit-test-test_use_after_free_read_nofault.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Nirjhar Roy <[email protected]>
Subject: mm/kfence: add a new kunit test test_use_after_free_read_nofault()
Date: Fri, 18 Oct 2024 23:16:01 +0530

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.

---

  mm/kfence/kfence_test.c |   17 +++++++++++++++++
  1 file changed, 17 insertions(+)
mm/kfence/kfence_test.c renamed from mm/kfence/kfence_test.c~mm-kfence-add-a-new-kunit-test-test_use_after_free_read_nofault +17 -0
@@ -383,6 +383,22 @@ static void test_use_after_free_read(str
  	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;
@@ -780,6 +796,7 @@ static struct kunit_case kfence_test_cas
  	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),

_

Patches currently in -mm which might be from [email protected] are

Signature · 4 lines
---
Nirjhar Roy
Linux Kernel Developer
IBM, Bangalore