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/20240806212808.1885309-1-pedro.falcato@gmail.com...
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(-)
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.
Cc: stable@vger.kernel.org Fixes: 8be7258aad44 ("mseal: add mseal syscall") Signed-off-by: Pedro Falcato pedro.falcato@gmail.com --- mm/mseal.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/mm/mseal.c b/mm/mseal.c index bf783bba8ed..15bba28acc0 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -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)
On Wed, 7 Aug 2024 18:33:35 +0100 Pedro Falcato pedro.falcato@gmail.com wrote:
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?
On Wed, Aug 7, 2024 at 7:58 PM Andrew Morton akpm@linux-foundation.org wrote:
On Wed, 7 Aug 2024 18:33:35 +0100 Pedro Falcato pedro.falcato@gmail.com wrote:
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?
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
On Wed, 7 Aug 2024 20:25:45 +0100 Pedro Falcato pedro.falcato@gmail.com wrote:
On Wed, Aug 7, 2024 at 7:58 PM Andrew Morton akpm@linux-foundation.org wrote:
On Wed, 7 Aug 2024 18:33:35 +0100 Pedro Falcato pedro.falcato@gmail.com wrote:
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?
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.
Add an mseal test for madvise() operations that aren't considered "discard" (e.g purely advisory ops such as MADV_RANDOM).
Signed-off-by: Pedro Falcato pedro.falcato@gmail.com --- tools/testing/selftests/mm/mseal_test.c | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index a818f010de4..2dcda7440eb 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -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);
Adjust the mseal test's plan.
Signed-off-by: Pedro Falcato pedro.falcato@gmail.com
---
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(-)
diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index 2dcda7440eb..7eec3f0152e 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -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();
linux-kselftest-mirror@lists.linaro.org