Skip to contentSkip to conversation

dma-iommu: Add a check to avoid dereference null pointer in function iommu_dma_map_sg()

PATCH In review chenxiang 2 participants 3 messages May 21, 2021

Subsystems (2)
lore
Share & tools
lore

chenxiang

author
#0 lore raw

PATCH 1/1 dma-iommu: Add a check to avoid dereference null pointer in function iommu_dma_map_sg()

From: Xiang Chen <[email protected]>

The issue is reported by tool TscanCode, and it is possible to deference null pointer when prev is NULL which is the initial value.

---
 drivers/iommu/dma-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +1 -1
index 4cb63b2..88a4f34 100644
@@ -1042,7 +1042,7 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
 		 *   iova_len == 0, thus we cannot dereference prev the first
 		 *   time through here (i.e. before it has a meaningful value).
 		 */
-		if (pad_len && pad_len < s_length - 1) {
+		if (prev && pad_len && pad_len < s_length - 1) {
 			prev->length += pad_len;
 			iova_len += pad_len;
 		}
Signature · 6 lines
2.8.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#1 lore raw
Quoted 4 lines · chenxiang

From: Xiang Chen <[email protected]>

The issue is reported by tool TscanCode, and it is possible to deference null pointer when prev is NULL which is the initial value.

No it isn't. This is literally explained in the comment visible in the diff context below...

Robin.

Quoted 11 lines
---
  drivers/iommu/dma-iommu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +0 -0
index 4cb63b2..88a4f34 100644

drivers/iommu/dma-iommu.c static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, · line 1048

Quoted 8 lines
drivers/iommu/dma-iommu.c +1 -1
@@ -1042,7 +1042,7 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
  		 *   iova_len == 0, thus we cannot dereference prev the first
  		 *   time through here (i.e. before it has a meaningful value).
  		 */
-		if (pad_len && pad_len < s_length - 1) {
+		if (prev && pad_len && pad_len < s_length - 1) {
  			prev->length += pad_len;
  			iova_len += pad_len;
  		}

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#2 lore raw
Quoted 9 lines · Robin Murphy
Quoted 4 lines · chenxiang

From: Xiang Chen <[email protected]>

The issue is reported by tool TscanCode, and it is possible to deference null pointer when prev is NULL which is the initial value.

No it isn't. This is literally explained in the comment visible in the diff context below...

Robin.

ok, thanks

Quoted 16 lines
Quoted 16 lines
---
  drivers/iommu/dma-iommu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/dma-iommu.c +0 -0
index 4cb63b2..88a4f34 100644
@@ -1042,7 +1042,7 @@ static int iommu_dma_map_sg(struct device *dev, 
 struct scatterlist *sg,
           *   iova_len == 0, thus we cannot dereference prev the first
           *   time through here (i.e. before it has a meaningful 

value).

           */
Quoted 9 lines

drivers/iommu/dma-iommu.c static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, · line 1048

Quoted 6 lines
drivers/iommu/dma-iommu.c +1 -1
-        if (pad_len && pad_len < s_length - 1) {
+        if (prev && pad_len && pad_len < s_length - 1) {
              prev->length += pad_len;
              iova_len += pad_len;
          }

.

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu