Skip to contentSkip to conversation

mseal: Fix is_madv_discard()

PATCH Applied Pedro Falcato 2 participants 7 messages Aug 7, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover mseal: Fix is_madv_discard()

This small series fixes is_madv_discard() and adds a small sanity check test to selftests/mm/mseal_test. Without this patch, is_madv_discard() erroneously thinks innocent ops like MADV_RANDOM are discard operations (which they are not, and are supposed to be allowed, per the overall design).

Based on Linus's tree and taken from my mseal depessimization series[1]. [1]: https://lore.kernel.org/all/[email protected]/

Pedro Falcato (2):
mseal: Fix is_madv_discard()
selftests/mm: Add mseal test for no-discard madvise

 mm/mseal.c                              | 14 +++++++---
 tools/testing/selftests/mm/mseal_test.c | 34 +++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/2 mseal: Fix is_madv_discard()

is_madv_discard did its check wrong. MADV_ flags are not bitwise, they're normal sequential numbers. So, for instance:

	behavior & (/* ... */ | MADV_REMOVE)

tagged both MADV_REMOVE and MADV_RANDOM (bit 0 set) as discard operations. This is obviously incorrect, so use a switch statement instead.

Fixes 8be7258aad44 ("mseal: add mseal syscall")
---
 mm/mseal.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
mm/mseal.c +11 -3
index bf783bba8ed..15bba28acc0 100644
@@ -40,9 +40,17 @@ static bool can_modify_vma(struct vm_area_struct *vma)
 
 static bool is_madv_discard(int behavior)
 {
-	return	behavior &
-		(MADV_FREE | MADV_DONTNEED | MADV_DONTNEED_LOCKED |
-		 MADV_REMOVE | MADV_DONTFORK | MADV_WIPEONFORK);
+	switch (behavior) {
+	case MADV_FREE:
+	case MADV_DONTNEED:
+	case MADV_DONTNEED_LOCKED:
+	case MADV_REMOVE:
+	case MADV_DONTFORK:
+	case MADV_WIPEONFORK:
+		return true;
+	}
+
+	return false;
 }
 
 static bool is_ro_anon(struct vm_area_struct *vma)
Signature · 1 line
2.46.0
maintainer
#2 lore raw
Quoted 7 lines · On Wed, 7 Aug 2024 18:33:35 +0100 Pedro Falcato <[email protected]>…

is_madv_discard did its check wrong. MADV_ flags are not bitwise, they're normal sequential numbers. So, for instance:

	behavior & (/* ... */ | MADV_REMOVE)

tagged both MADV_REMOVE and MADV_RANDOM (bit 0 set) as discard operations. This is obviously incorrect, so use a switch statement instead.

Please describe the userspace-visible runtime effects of this bug?

#3 lore raw
Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Wed, 7 Aug 2024 18:33:35 +0100 Pedro Falcato <[email protected]>…

is_madv_discard did its check wrong. MADV_ flags are not bitwise, they're normal sequential numbers. So, for instance:

      behavior & (/* ... */ | MADV_REMOVE)
Quoted 5 lines
Quoted 3 lines

tagged both MADV_REMOVE and MADV_RANDOM (bit 0 set) as discard operations. This is obviously incorrect, so use a switch statement instead.

Please describe the userspace-visible runtime effects of this bug?

The kernel could erroneously block certain madvises (e.g MADV_RANDOM or MADV_HUGEPAGE) on sealed VMAs due to them sharing bits with blocked MADV operations (e.g REMOVE or WIPEONFORK).

Thanks,
Pedro

maintainer
#4 lore raw
Quoted 12 lines · On Wed, 7 Aug 2024 20:25:45 +0100 Pedro Falcato <[email protected]> wrote:
Quoted 11 lines · Andrew Morton
Quoted 7 lines · On Wed, 7 Aug 2024 18:33:35 +0100 Pedro Falcato <[email protected]>…

is_madv_discard did its check wrong. MADV_ flags are not bitwise, they're normal sequential numbers. So, for instance:

      behavior & (/* ... */ | MADV_REMOVE)

tagged both MADV_REMOVE and MADV_RANDOM (bit 0 set) as discard operations. This is obviously incorrect, so use a switch statement instead.

Quoted 5 lines
Quoted 1 line

Please describe the userspace-visible runtime effects of this bug?

The kernel could erroneously block certain madvises (e.g MADV_RANDOM or MADV_HUGEPAGE) on sealed VMAs due to them sharing bits with blocked MADV operations (e.g REMOVE or WIPEONFORK).

Thanks, I updated the changelog.

#5 lore raw

PATCH 2/2 selftests/mm: Add mseal test for no-discard madvise

In reply to Pedro Falcato

Add an mseal test for madvise() operations that aren't considered "discard" (e.g purely advisory ops such as MADV_RANDOM).

---
 tools/testing/selftests/mm/mseal_test.c | 34 +++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/testing/selftests/mm/mseal_test.c +34 -0
index a818f010de4..2dcda7440eb 100644
@@ -1731,6 +1731,38 @@ static void test_seal_discard_ro_anon(bool seal)
 	REPORT_TEST_PASS();
 }
 
+static void test_seal_madvise_nodiscard(bool seal)
+{
+	void *ptr;
+	unsigned long page_size = getpagesize();
+	unsigned long size = 4 * page_size;
+	int ret;
+
+	setup_single_address(size, &ptr);
+	FAIL_TEST_IF_FALSE(ptr != (void *)-1);
+
+	if (seal) {
+		ret = seal_single_address(ptr, size);
+		FAIL_TEST_IF_FALSE(!ret);
+	}
+
+	/*
+	 * Test a random madvise flag like MADV_RANDOM that does not touch page
+	 * contents (and thus should work for msealed VMAs). RANDOM also happens to
+	 * share bits with other discard-ish flags like REMOVE.
+	 */
+	ret = sys_madvise(ptr, size, MADV_RANDOM);
+	FAIL_TEST_IF_FALSE(!ret);
+
+	ret = sys_munmap(ptr, size);
+	if (seal)
+		FAIL_TEST_IF_FALSE(ret < 0);
+	else
+		FAIL_TEST_IF_FALSE(!ret);
+
+	REPORT_TEST_PASS();
+}
+
 int main(int argc, char **argv)
 {
 	bool test_seal = seal_support();
@@ -1822,6 +1854,8 @@ int main(int argc, char **argv)
 	test_seal_mremap_move_fixed_zero(true);
 	test_seal_mremap_move_dontunmap_anyaddr(false);
 	test_seal_mremap_move_dontunmap_anyaddr(true);
+	test_seal_madvise_nodiscard(false);
+	test_seal_madvise_nodiscard(true);
 	test_seal_discard_ro_anon(false);
 	test_seal_discard_ro_anon(true);
 	test_seal_discard_ro_anon_on_rw(false);
Signature · 1 line
2.46.0
#6 lore raw

PATCH 1/1 fixup! selftests/mm: Add mseal test for no-discard madvise

Adjust the mseal test's plan.

---

Andrew, please squash this small fix into "selftests/mm: Add mseal test for no-discard madvise". Thank you!

 tools/testing/selftests/mm/mseal_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/mseal_test.c +1 -1
index 2dcda7440eb..7eec3f0152e 100644
@@ -1775,7 +1775,7 @@ int main(int argc, char **argv)
 	if (!pkey_supported())
 		ksft_print_msg("PKEY not supported\n");
 
-	ksft_set_plan(80);
+	ksft_set_plan(82);
 
 	test_seal_addseal();
 	test_seal_unmapped_start();
Signature · 1 line
2.46.0