Skip to contentSkip to conversation

iommu/riscv: prevent NULL deref in iova_to_phys

PATCH Applied XianLiang Huang 4 participants 9 messages Aug 20, 2025 – Oct 9, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 iommu/riscv: prevent NULL deref in iova_to_phys

The riscv_iommu_pte_fetch() function returns either NULL for unmapped/never-mapped iova, or a valid leaf pte pointer that requires no further validation.

riscv_iommu_iova_to_phys() failed to handle NULL returns. Prevent null pointer dereference in riscv_iommu_iova_to_phys(), and remove the pte validation.

Fixes 488ffbf18171 ("iommu/riscv: Paging domain support")
Cc Tomasz Jeznach <[email protected]>

---
Changes
v4:
- Change the summary as Markus recommends

v3:
- Remove redundant pte validation in riscv_iommu_iova_to_phys
- Improve subject line to emphasize prevention

v2:
- Refine problem description
- Add "Fixes" tag

---
 drivers/iommu/riscv/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/riscv/iommu.c +1 -1
index 2d0d31ba2886..0eae2f4bdc5e 100644
@@ -1283,7 +1283,7 @@ static phys_addr_t riscv_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
 	unsigned long *ptr;
 
 	ptr = riscv_iommu_pte_fetch(domain, iova, &pte_size);
-	if (_io_pte_none(*ptr) || !_io_pte_present(*ptr))
+	if (!ptr)
 		return 0;
 
 	return pfn_to_phys(__page_val_to_pfn(*ptr)) | (iova & (pte_size - 1));
Signature · 1 line
2.34.1
#2 lore raw

https://lore.kernel.org/lkml/[email protected]/

I updated the patch in v3, and updated change description accordingly:

In v1

Review

Quoted 2 lines
(quoted diff) +1 -1
-	if (_io_pte_none(*ptr) || !_io_pte_present(*ptr))
+	if (!ptr || _io_pte_none(*ptr) || !_io_pte_present(*ptr))

In v3

drivers/iommu/riscv/iommu.c static phys_addr_t riscv_iommu_iova_to_phys(struct iommu_domain *iommu_domain, · line 1286

Quoted 2 lines
drivers/iommu/riscv/iommu.c +1 -1
-	if (_io_pte_none(*ptr) || !_io_pte_present(*ptr))
+	if (!ptr)

but put your proposal at the patch Subject...
v1: check pte null pointer before use
v3: prevent NULL deref in iova_to_phys

Quoted 6 lines

…> ---

Quoted 5 lines

Changes
v4:
- Change the summary as Markus recommends

v3:

sorry for making this messy.

Regards,
Xianliang

#3 lore raw

…> but put your proposal at the patch Subject... …
Does it trigger undesirable communication difficulties?

Regards,
Markus

#4 lore raw
Quoted 1 line

Does it trigger undesirable communication difficulties?

No, and I agree that both subject and summary phrase can be improved

And now I do learn from this, thanks.

Quoted 4 lines

Your patch will almost certainly get comments from reviewers on ways in which the patch can be improved, in the form of a reply to your email. You must respond to those comments; ignoring reviewers is a good way to get ignored in return. You can simply reply to their emails to answer their comments. Review

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst#n310

Regards,
Xianliang

#6 lore raw
Quoted 1 line

I suggest to take another look for the better distinction of mentioned key words.

I misunderstood this

Quoted 3 lines
Quoted 1 line

…> Check the pointer before using it to avoid the bug. …

Would a summary phrase like “Prevent null pointer dereference in riscv_iommu_iova_to_phys()” be also helpful?

https://lore.kernel.org/lkml/[email protected]/

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

Changes
v4:
- Change the summary as Markus recommends

v3:
- Remove redundant pte validation in riscv_iommu_iova_to_phys
- Improve subject line to emphasize prevention

Does anything hinder you to integrate repeated patch review ideas better into your contributions?

Do you suggest merge the repeated patch changlogs?
may a RESEND v3:
- Remove redundant pte validation in riscv_iommu_iova_to_phys
- Improve summary and description to emphasize prevention as Markus recommends

Regards,
Xianliang

#8 lore raw
In reply to XianLiang Huang
This patch was applied to riscv/linux.git (for-next) by Joerg Roedel <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Joerg Roedel <[email protected]>:

Quoted 8 lines · On Wed, 20 Aug 2025 15:22:48 +0800 you wrote:

The riscv_iommu_pte_fetch() function returns either NULL for unmapped/never-mapped iova, or a valid leaf pte pointer that requires no further validation.

riscv_iommu_iova_to_phys() failed to handle NULL returns. Prevent null pointer dereference in riscv_iommu_iova_to_phys(), and remove the pte validation.

[...]

Here is the summary with links:
- [v4] iommu/riscv: prevent NULL deref in iova_to_phys

    https://git.kernel.org/riscv/c/99d4d1a07087

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html