iommu/riscv: prevent NULL deref in iova_to_phys
Subsystems (3)
PATCH v3 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. Fix by adding NULL check before dereferencing and returning 0 for invalid iova.
---
Changes
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
@@ -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
…> riscv_iommu_iova_to_phys() failed to handle NULL returns. Fix by adding NULL
Quoted 1 line
check before dereferencing and returning 0 for invalid iova.
…> ---
Quoted 4 lines
Changes
v3:
- Remove redundant pte validation inriscv_iommu_iova_to_phys
- Improve subject line to emphasize prevention
…
Repetition:
https://lore.kernel.org/lkml/[email protected]/
Would a summary phrase like “Prevent null pointer dereference in riscv_iommu_iova_to_phys()” be nicer anyhow?
Regards,
Markus
Joerg Roedel
maintainerdrivers/iommu/riscv/iommu.c static phys_addr_t riscv_iommu_iova_to_phys(struct iommu_domain *iommu_domain, · line 1287
Quoted 11 lines · XianLiang Huang
drivers/iommu/riscv/iommu.c +1
-1@@ -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 ;
Zero is usually not an invalid physical address, or is it on RISC-V?
-Joerg
Robin Murphy
reviewerQuoted 8 lines · Joerg Roedel
Quoted 7 lines · XianLiang Huang
drivers/iommu/riscv/iommu.c +0
-0@@ -1283,7 +1283,7 @@ static phys_addr_t riscv_iommu_iova_to_phys(struct iommu_domain *iommu_domain,unsigned long *ptr;
Quoted 6 lines
drivers/iommu/riscv/iommu.c static phys_addr_t riscv_iommu_iova_to_phys(struct iommu_domain *iommu_domain, · line 1287
Quoted 4 lines
drivers/iommu/riscv/iommu.c +1
-1ptr =riscv_iommu_pte_fetch (domain, iova, &pte_size); -if (_io_pte_none (*ptr) || !_io_pte_present (*ptr)) +if (!ptr)return 0 ;Zero is usually not an invalid physical address, or is it on RISC-V?
It's a valid PA on many systems of many architectures, but it's also been the "not mapped/error" value for the iova_to_phys operation all the way back to the very very first intel_iommu_iova_to_pfn() nearly 17 years ago, so hey :)
Thanks,
Robin.
Joerg Roedel
maintainerQuoted 4 lines · Robin Murphy
It's a valid PA on many systems of many architectures, but it's also been the "not mapped/error" value for the
iova_to_physoperation all the way back to the very very firstintel_iommu_iova_to_pfn()nearly 17 years ago, so hey :)
Right, the sins of the past finally haunt me ;)