* [PATCH 0/2] mseal: Fix is_madv_discard()
@ 2024-08-07 17:33 Pedro Falcato
2024-08-07 17:33 ` [PATCH 1/2] " Pedro Falcato
2024-08-07 17:33 ` [PATCH 2/2] selftests/mm: Add mseal test for no-discard madvise Pedro Falcato
0 siblings, 2 replies; 7+ messages in thread
From: Pedro Falcato @ 2024-08-07 17:33 UTC (permalink / raw)
To: Andrew Morton, Shuah Khan, Liam R. Howlett, Jeff Xu, Kees Cook,
linux-mm
Cc: linux-kernel, linux-kselftest, Pedro Falcato
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(-)
--
2.46.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] mseal: Fix is_madv_discard()
2024-08-07 17:33 [PATCH 0/2] mseal: Fix is_madv_discard() Pedro Falcato
@ 2024-08-07 17:33 ` Pedro Falcato
2024-08-07 18:58 ` Andrew Morton
2024-08-07 17:33 ` [PATCH 2/2] selftests/mm: Add mseal test for no-discard madvise Pedro Falcato
1 sibling, 1 reply; 7+ messages in thread
From: Pedro Falcato @ 2024-08-07 17:33 UTC (permalink / raw)
To: Andrew Morton, Shuah Khan, Liam R. Howlett, Jeff Xu, Kees Cook,
linux-mm
Cc: linux-kernel, linux-kselftest, Pedro Falcato, stable
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)
--
2.46.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/2] selftests/mm: Add mseal test for no-discard madvise
2024-08-07 17:33 [PATCH 0/2] mseal: Fix is_madv_discard() Pedro Falcato
2024-08-07 17:33 ` [PATCH 1/2] " Pedro Falcato
@ 2024-08-07 17:33 ` Pedro Falcato
2024-08-07 20:37 ` [PATCH] fixup! " Pedro Falcato
1 sibling, 1 reply; 7+ messages in thread
From: Pedro Falcato @ 2024-08-07 17:33 UTC (permalink / raw)
To: Andrew Morton, Shuah Khan, Liam R. Howlett, Jeff Xu, Kees Cook,
linux-mm
Cc: linux-kernel, linux-kselftest, Pedro Falcato
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);
--
2.46.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] mseal: Fix is_madv_discard()
2024-08-07 17:33 ` [PATCH 1/2] " Pedro Falcato
@ 2024-08-07 18:58 ` Andrew Morton
2024-08-07 19:25 ` Pedro Falcato
0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2024-08-07 18:58 UTC (permalink / raw)
To: Pedro Falcato
Cc: Shuah Khan, Liam R. Howlett, Jeff Xu, Kees Cook, linux-mm,
linux-kernel, linux-kselftest, stable
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?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] mseal: Fix is_madv_discard()
2024-08-07 18:58 ` Andrew Morton
@ 2024-08-07 19:25 ` Pedro Falcato
2024-08-07 19:31 ` Andrew Morton
0 siblings, 1 reply; 7+ messages in thread
From: Pedro Falcato @ 2024-08-07 19:25 UTC (permalink / raw)
To: Andrew Morton
Cc: Shuah Khan, Liam R. Howlett, Jeff Xu, Kees Cook, linux-mm,
linux-kernel, linux-kselftest, stable
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] mseal: Fix is_madv_discard()
2024-08-07 19:25 ` Pedro Falcato
@ 2024-08-07 19:31 ` Andrew Morton
0 siblings, 0 replies; 7+ messages in thread
From: Andrew Morton @ 2024-08-07 19:31 UTC (permalink / raw)
To: Pedro Falcato
Cc: Shuah Khan, Liam R. Howlett, Jeff Xu, Kees Cook, linux-mm,
linux-kernel, linux-kselftest, stable
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] fixup! selftests/mm: Add mseal test for no-discard madvise
2024-08-07 17:33 ` [PATCH 2/2] selftests/mm: Add mseal test for no-discard madvise Pedro Falcato
@ 2024-08-07 20:37 ` Pedro Falcato
0 siblings, 0 replies; 7+ messages in thread
From: Pedro Falcato @ 2024-08-07 20:37 UTC (permalink / raw)
To: pedro.falcato, akpm
Cc: Liam.Howlett, jeffxu, kees, linux-kernel, linux-kselftest,
linux-mm, shuah
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();
--
2.46.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-08-07 20:37 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-07 17:33 [PATCH 0/2] mseal: Fix is_madv_discard() Pedro Falcato
2024-08-07 17:33 ` [PATCH 1/2] " Pedro Falcato
2024-08-07 18:58 ` Andrew Morton
2024-08-07 19:25 ` Pedro Falcato
2024-08-07 19:31 ` Andrew Morton
2024-08-07 17:33 ` [PATCH 2/2] selftests/mm: Add mseal test for no-discard madvise Pedro Falcato
2024-08-07 20:37 ` [PATCH] fixup! " Pedro Falcato
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox