[PATCH] mm/madvise: avoid any chance of uninitialised pointer deref

From: Lorenzo Stoakes
Date: Tue Jun 17 2025 - 09:35:13 EST


If we were to extend madvise() to support more operations under VMA lock,
we could potentially dereference prev to uninitialised state in
madvise_update_vma().

Avoid this by explicitly setting prev to vma before invoking the visit()
function.

This has no impact on behaviour, as all visitors compatible with a VMA lock
do not require prev to be set to the previous VMA and at any rate we only
examine a single VMA in VMA lock mode.

Reported-by: Lance Yang <ioworker0@xxxxxxxxx>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>
---
mm/madvise.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/mm/madvise.c b/mm/madvise.c
index efe5d64e1175..0970623a0e98 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1333,6 +1333,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma,
return madvise_guard_remove(vma, prev, start, end);
}

+ /* We cannot provide prev in this lock mode. */
+ VM_WARN_ON_ONCE(arg->lock_mode == MADVISE_VMA_READ_LOCK);
anon_name = anon_vma_name(vma);
anon_vma_name_get(anon_name);
error = madvise_update_vma(vma, prev, start, end, new_flags,
@@ -1549,6 +1551,7 @@ int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
if (madv_behavior && madv_behavior->lock_mode == MADVISE_VMA_READ_LOCK) {
vma = try_vma_read_lock(mm, madv_behavior, start, end);
if (vma) {
+ prev = vma;
error = visit(vma, &prev, start, end, arg);
vma_end_read(vma);
return error;
--
2.49.0