mm/gup: fix NULL pointer dereference in fixup_user_fault()
Subsystems (2)
PATCH 1/1 mm/gup: fix NULL pointer dereference in fixup_user_fault()
In fixup_user_fault(), the 'unlocked' parameter is checked for NULL early on, allowing callers to pass NULL if they do not track whether the mmap lock was dropped.
However, if handle_mm_fault() returns VM_FAULT_COMPLETED, line 1597 dereferences 'unlocked' directly (*unlocked = true) without checking if it is NULL. Callers like s390's pci_mmio.c pass NULL for 'unlocked', leading to a kernel NULL pointer dereference when VM_FAULT_COMPLETED occurs.
Fix this by checking if 'unlocked' is non-NULL before assigning to it.
--- mm/gup.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/gup.c +4 -2
@@ -1594,7 +1594,8 @@ int fixup_user_fault(struct mm_struct *mm,* could tell the callers so they do not need to unlock. */ mmap_read_lock (mm); - *unlocked = true; +if (unlocked) + *unlocked = true;return 0 ; }
@@ -1608,7 +1609,8 @@ int fixup_user_fault(struct mm_struct *mm,if (ret &VM_FAULT_RETRY ) {mmap_read_lock (mm); - *unlocked = true; +if (unlocked) + *unlocked = true; fault_flags |=FAULT_FLAG_TRIED ; goto retry; }
Signature · 1 line
2.55.0
Andrew Morton
maintainerQuoted 11 lines · On Sun, 4 Oct 2026 02:48:46 +0700 Nguyen Duy Nhat Anh <[email protected]>…
In
fixup_user_fault(), the 'unlocked' parameter is checked for NULL early on, allowing callers to pass NULL if they do not track whether the mmap lock was dropped.However, if
handle_mm_fault()returnsVM_FAULT_COMPLETED, line 1597 dereferences 'unlocked' directly (*unlocked = true) without checking if it is NULL. Callers like s390'spci_mmio.cpass NULL for 'unlocked', leading to a kernel NULL pointer dereference whenVM_FAULT_COMPLETEDoccurs.Fix this by checking if 'unlocked' is non-NULL before assigning to it.
This code is too subtle so you aren't the first to attempt to "fix" it.
The key hint is in the kerneldoc:
* @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller
* does not allow retry. If NULL, the caller must guarantee
* that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY.
Trace through the FAULT_FLAG_ALLOW_RETRY/VM_FAULT_COMPLETED/VM_FAULT_RETRY logic to confirm that the null deref is a cant-happen.
It would be great if you could pick through all this and propose addition of a comment which will make this code less confusing for others.
Thanks.
Quoted 18 lines · Andrew Morton
Quoted 11 lines · On Sun, 4 Oct 2026 02:48:46 +0700 Nguyen Duy Nhat Anh <[email protected]>…
In
fixup_user_fault(), the 'unlocked' parameter is checked for NULL early on, allowing callers to pass NULL if they do not track whether the mmap lock was dropped.However, if
handle_mm_fault()returnsVM_FAULT_COMPLETED, line 1597 dereferences 'unlocked' directly (*unlocked = true) without checking if it is NULL. Callers like s390'spci_mmio.cpass NULL for 'unlocked', leading to a kernel NULL pointer dereference whenVM_FAULT_COMPLETEDoccurs.Fix this by checking if 'unlocked' is non-NULL before assigning to it.
This code is too subtle so you aren't the first to attempt to "fix" it.
The key hint is in the kerneldoc:
Quoted 7 lines
* @unlocked: did we unlock the
mmap_lockwhile retrying, maybe NULL if caller
* does not allow retry. If NULL, the caller must guarantee
* thatfault_flagsdoes not containFAULT_FLAG_ALLOW_RETRY.Trace through the FAULT_FLAG_ALLOW_RETRY/VM_FAULT_COMPLETED/VM_FAULT_RETRY logic to confirm that the null deref is a cant-happen.
IIUC, that's indeed a can't-happen :)
PATCH v2 1/1 mm/gup: document unlocked invariant in fixup_user_fault
In reply to Andrew MortonStatic analysis tools flag potential NULL pointer dereferences of 'unlocked' in fixup_user_fault() when handling VM_FAULT_COMPLETED or VM_FAULT_RETRY.
These warnings are false positives. 'unlocked' is only dereferenced when handle_mm_fault() returns VM_FAULT_COMPLETED or VM_FAULT_RETRY. Both of these return codes require FAULT_FLAG_ALLOW_RETRY to be set in fault_flags, which fixup_user_fault() only sets if 'unlocked' is non-NULL upon entry. Therefore, if 'unlocked' is NULL, the control flow branches that dereference 'unlocked' are unreachable.
However, this part of the code is subtle and can trip up contributors or automated tools. Document this invariant with a comment above 'if (unlocked)' where fault_flags is constructed, explaining why omitting FAULT_FLAG_ALLOW_RETRY guarantees 'unlocked' will not be dereferenced later in the fault recovery loop.
---
v1: https://lore.kernel.org/linux-mm/[email protected]/
v2 changes:
- Instead of adding runtime NULL checks at dereference sites, document the FAULT_FLAG_ALLOW_RETRY invariant above if (unlocked).
--- mm/gup.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/gup.c +6 -0
@@ -1570,6 +1570,12 @@ int fixup_user_fault(struct mm_struct *mm, address =untagged_addr_remote (mm, address); +/* +* If the caller passes 'unlocked' as NULL, FAULT_FLAG_ALLOW_RETRY is omitted. +* This guarantees handle_mm_fault() will never drop the lock or +* return VM_FAULT_COMPLETED / VM_FAULT_RETRY, making subsequent +* dereferences of 'unlocked' unreachable when NULL. +*/ if (unlocked) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;
Signature · 1 line
2.55.0
David Hildenbrand (Arm)
maintainerQuoted 24 lines · Lance Yang
Quoted 22 lines · Andrew Morton
Quoted 11 lines · On Sun, 4 Oct 2026 02:48:46 +0700 Nguyen Duy Nhat Anh <[email protected]>…
In
fixup_user_fault(), the 'unlocked' parameter is checked for NULL early on, allowing callers to pass NULL if they do not track whether the mmap lock was dropped.However, if
handle_mm_fault()returnsVM_FAULT_COMPLETED, line 1597 dereferences 'unlocked' directly (*unlocked = true) without checking if it is NULL. Callers like s390'spci_mmio.cpass NULL for 'unlocked', leading to a kernel NULL pointer dereference whenVM_FAULT_COMPLETEDoccurs.Fix this by checking if 'unlocked' is non-NULL before assigning to it.
This code is too subtle so you aren't the first to attempt to "fix" it.
The key hint is in the kerneldoc:
* @unlocked: did we unlock the
mmap_lockwhile retrying, maybe NULL if caller
* does not allow retry. If NULL, the caller must guarantee
* thatfault_flagsdoes not containFAULT_FLAG_ALLOW_RETRY.
Quoted 5 lines
Quoted 3 lines
Trace through the FAULT_FLAG_ALLOW_RETRY/VM_FAULT_COMPLETED/VM_FAULT_RETRY logic to confirm that the null deref is a cant-happen.
IIUC, that's indeed a can't-happen :)
https://lore.kernel.org/linux-mm/[email protected]/
People should stop trusting tool output and use their brain ;)
If you did mmap_read_lock() and wouldn't have a way to indicate that to the user something would be seriously messed up.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v2] mm/gup: document unlocked invariant in fixup_user_fault
In reply to Nguyen Duy Nhat AnhQuoted 36 lines · Nguyen Duy Nhat Anh
Static analysis tools flag potential NULL pointer dereferences of 'unlocked' in
fixup_user_fault()when handlingVM_FAULT_COMPLETEDorVM_FAULT_RETRY.These warnings are false positives. 'unlocked' is only dereferenced when
handle_mm_fault()returnsVM_FAULT_COMPLETEDorVM_FAULT_RETRY. Both of these return codes requireFAULT_FLAG_ALLOW_RETRYto be set infault_flags, whichfixup_user_fault()only sets if 'unlocked' is non-NULL upon entry. Therefore, if 'unlocked' is NULL, the control flow branches that dereference 'unlocked' are unreachable.However, this part of the code is subtle and can trip up contributors or automated tools. Document this invariant with a comment above 'if (unlocked)' where
fault_flagsis constructed, explaining why omittingFAULT_FLAG_ALLOW_RETRYguarantees 'unlocked' will not be dereferenced later in the fault recovery loop.Suggested-by Andrew Morton <[email protected]>Signed-off-by Nguyen Duy Nhat Anh <[email protected]>---
v1: https://lore.kernel.org/linux-mm/[email protected]/v2 changes:
- Instead of adding runtime NULL checks at dereference sites, document theFAULT_FLAG_ALLOW_RETRYinvariant above if (unlocked).--- mm/gup.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/gup.c +0
-0@@ -1570,6 +1570,12 @@ int fixup_user_fault(struct mm_struct *mm, address =untagged_addr_remote (mm, address);
mm/gup.c int fixup_user_fault(struct mm_struct *mm, · line 1581
Quoted 10 lines
mm/gup.c +6
-0+/* +* If the caller passes 'unlocked' as NULL, FAULT_FLAG_ALLOW_RETRY is omitted. +* This guarantees handle_mm_fault() will never drop the lock or +* return VM_FAULT_COMPLETED / VM_FAULT_RETRY, making subsequent +* dereferences of 'unlocked' unreachable when NULL. +*/ if (unlocked) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;
For which audience is the comment targeted? For people that know what they are doing? No.
For tools that report such problems? No.
So I don't think this (4 lines of comment) is the right way to improve this.
Signature · 3 lines
Cheers, David
Re: [PATCH v2] mm/gup: document unlocked invariant in fixup_user_fault
Hi David,
Thanks for taking the time to review.
My motivation came from running Smatch over fixup_user_fault(), which flagged the dereference as a potential NULL pointer issue. Because the relationship between FAULT_FLAG_ALLOW_RETRY and the unlocked pointer is subtle when first reading through the path, I initially misread it as an unguarded dereference in v1.
When Andrew pointed out the invariant, I thought documenting it might help future readers or newer contributors who hit the same static analysis warning.
If adding a comment isn't the right approach here, would you prefer leaving the implicit contract as-is, or is an explicit runtime check (such as VM_WARN_ON_ONCE) something more fitting?
Thanks,
Anh
Quoted 60 lines · David Hildenbrand (Arm)
Quoted 36 lines · Nguyen Duy Nhat Anh
Static analysis tools flag potential NULL pointer dereferences of 'unlocked' in
fixup_user_fault()when handlingVM_FAULT_COMPLETEDorVM_FAULT_RETRY.These warnings are false positives. 'unlocked' is only dereferenced when
handle_mm_fault()returnsVM_FAULT_COMPLETEDorVM_FAULT_RETRY. Both of these return codes requireFAULT_FLAG_ALLOW_RETRYto be set infault_flags, whichfixup_user_fault()only sets if 'unlocked' is non-NULL upon entry. Therefore, if 'unlocked' is NULL, the control flow branches that dereference 'unlocked' are unreachable.However, this part of the code is subtle and can trip up contributors or automated tools. Document this invariant with a comment above 'if (unlocked)' where
fault_flagsis constructed, explaining why omittingFAULT_FLAG_ALLOW_RETRYguarantees 'unlocked' will not be dereferenced later in the fault recovery loop.Suggested-by Andrew Morton <[email protected]>Signed-off-by Nguyen Duy Nhat Anh <[email protected]>---
v1: https://lore.kernel.org/linux-mm/[email protected]/v2 changes:
- Instead of adding runtime NULL checks at dereference sites, document theFAULT_FLAG_ALLOW_RETRYinvariant above if (unlocked).--- mm/gup.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/gup.c +0
-0@@ -1570,6 +1570,12 @@ int fixup_user_fault(struct mm_struct *mm, address =untagged_addr_remote (mm, address);mm/gup.c int fixup_user_fault(struct mm_struct *mm, · line 1581
Quoted 10 lines
mm/gup.c +6
-0+/* +* If the caller passes 'unlocked' as NULL, FAULT_FLAG_ALLOW_RETRY is omitted. +* This guarantees handle_mm_fault() will never drop the lock or +* return VM_FAULT_COMPLETED / VM_FAULT_RETRY, making subsequent +* dereferences of 'unlocked' unreachable when NULL. +*/ if (unlocked) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;For which audience is the comment targeted? For people that know what they are doing? No.
For tools that report such problems? No.
So I don't think this (4 lines of comment) is the right way to improve this.
--
Cheers,David
David Hildenbrand (Arm)
maintainerRe: [PATCH v2] mm/gup: document unlocked invariant in fixup_user_fault
Quoted 2 lines · Nhật Anh Nguyễn Duy
Hi David,
Hi,
Quoted 3 lines
Thanks for taking the time to review.
My motivation came from running Smatch over
fixup_user_fault(), which flagged
just like the (at least) 2 previous reports. One from them from 2021 IIRC where Linus even rejected a code change.
Quoted 11 lines
the dereference as a potential NULL pointer issue. Because the relationship between
FAULT_FLAG_ALLOW_RETRYand the unlocked pointer is subtle when first reading through the path, I initially misread it as an unguarded dereference in v1.When Andrew pointed out the invariant, I thought documenting it might help future readers or newer contributors who hit the same static analysis warning.
If adding a comment isn't the right approach here, would you prefer leaving the implicit contract as-is, or is an explicit runtime check (such as
VM_WARN_ON_ONCE) something more fitting?
We want to silence smatch. Either fix smatch or find a reasonable way to stop it from reporting things that are simply not true.
Signature · 3 lines
Cheers, David