Skip to content

Commit e46bc2e

Browse files
heatdakpm00
authored andcommitted
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. As a result 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). This is obviously incorrect, so use a switch statement instead. Link: https://lkml.kernel.org/r/20240807173336.2523757-1-pedro.falcato@gmail.com Link: https://lkml.kernel.org/r/20240807173336.2523757-2-pedro.falcato@gmail.com Fixes: 8be7258 ("mseal: add mseal syscall") Signed-off-by: Pedro Falcato <pedro.falcato@gmail.com> Tested-by: Jeff Xu <jeffxu@chromium.org> Reviewed-by: Jeff Xu <jeffxu@chromium.org> Cc: Kees Cook <kees@kernel.org> Cc: Liam R. Howlett <Liam.Howlett@oracle.com> Cc: Shuah Khan <shuah@kernel.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 7c626ce commit e46bc2e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

mm/mseal.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ static bool can_modify_vma(struct vm_area_struct *vma)
4040

4141
static bool is_madv_discard(int behavior)
4242
{
43-
return behavior &
44-
(MADV_FREE | MADV_DONTNEED | MADV_DONTNEED_LOCKED |
45-
MADV_REMOVE | MADV_DONTFORK | MADV_WIPEONFORK);
43+
switch (behavior) {
44+
case MADV_FREE:
45+
case MADV_DONTNEED:
46+
case MADV_DONTNEED_LOCKED:
47+
case MADV_REMOVE:
48+
case MADV_DONTFORK:
49+
case MADV_WIPEONFORK:
50+
return true;
51+
}
52+
53+
return false;
4654
}
4755

4856
static bool is_ro_anon(struct vm_area_struct *vma)

0 commit comments

Comments
 (0)