Skip to contentSkip to conversation

powerpc/book3s64: Fix error handling in mm_iommu_do_alloc()

PATCH In review Jan Kara 2 participants 4 messages Dec 11, 2019 – Dec 23, 2019 PowerPC

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH 1/1 powerpc/book3s64: Fix error handling in mm_iommu_do_alloc()

The last jump to free_exit in mm_iommu_do_alloc() happens after page pointers in struct mm_iommu_table_group_mem_t were already converted to physical addresses. Thus calling put_page() on these physical addresses will likely crash. Convert physical addresses back to page pointers during the error cleanup.

---
 arch/powerpc/mm/book3s64/iommu_api.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Beware, this is completely untested, spotted just by code audit.

arch/powerpc/mm/book3s64/iommu_api.c +4 -1
index 56cc84520577..06c403381c9c 100644
@@ -154,7 +154,7 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 				       (mem2->entries << PAGE_SHIFT)))) {
 			ret = -EINVAL;
 			mutex_unlock(&mem_list_mutex);
-			goto free_exit;
+			goto convert_exit;
 		}
 	}
 
@@ -166,6 +166,9 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 
 	return 0;
 
+convert_exit:
+	for (i = 0; i < pinned; i++)
+		mem->hpages[i] = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
 free_exit:
 	/* free the reference taken */
 	for (i = 0; i < pinned; i++)
Signature · 1 line
2.16.4
#1 lore raw
Quoted 27 lines · Jan Kara

The last jump to free_exit in mm_iommu_do_alloc() happens after page pointers in struct mm_iommu_table_group_mem_t were already converted to physical addresses. Thus calling put_page() on these physical addresses will likely crash. Convert physical addresses back to page pointers during the error cleanup.

---
 arch/powerpc/mm/book3s64/iommu_api.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Beware, this is completely untested, spotted just by code audit.

arch/powerpc/mm/book3s64/iommu_api.c +1 -1
index 56cc84520577..06c403381c9c 100644
@@ -154,7 +154,7 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 				       (mem2->entries << PAGE_SHIFT)))) {
 			ret = -EINVAL;
 			mutex_unlock(&mem_list_mutex);
-			goto free_exit;
+			goto convert_exit;
 		}
 	}
 

arch/powerpc/mm/book3s64/iommu_api.c static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua, · line 171

Quoted 6 lines
arch/powerpc/mm/book3s64/iommu_api.c +3 -0
@@ -166,6 +166,9 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 
 	return 0;
 
+convert_exit:
+	for (i = 0; i < pinned; i++)
+		mem->hpages[i] = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);

Good find. Although doing it where you added "goto convert_exit" seems cleaner imho. Thanks,

Quoted 4 lines
arch/powerpc/mm/book3s64/iommu_api.c +0 -0
 free_exit:
 	/* free the reference taken */
 	for (i = 0; i < pinned; i++)
Signature · 1 line
Alexey
author
#2 lore raw
Quoted 33 lines · Alexey Kardashevskiy
Quoted 30 lines · Jan Kara

The last jump to free_exit in mm_iommu_do_alloc() happens after page pointers in struct mm_iommu_table_group_mem_t were already converted to physical addresses. Thus calling put_page() on these physical addresses will likely crash. Convert physical addresses back to page pointers during the error cleanup.

---
 arch/powerpc/mm/book3s64/iommu_api.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Beware, this is completely untested, spotted just by code audit.

arch/powerpc/mm/book3s64/iommu_api.c +1 -1
index 56cc84520577..06c403381c9c 100644
@@ -154,7 +154,7 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 				       (mem2->entries << PAGE_SHIFT)))) {
 			ret = -EINVAL;
 			mutex_unlock(&mem_list_mutex);
-			goto free_exit;
+			goto convert_exit;
 		}
 	}
 
@@ -166,6 +166,9 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 
 	return 0;
 
Quoted 7 lines

arch/powerpc/mm/book3s64/iommu_api.c static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua, · line 171

Quoted 3 lines
arch/powerpc/mm/book3s64/iommu_api.c +3 -0
+convert_exit:
+	for (i = 0; i < pinned; i++)
+		mem->hpages[i] = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);

Good find. Although doing it where you added "goto convert_exit" seems cleaner imho. Thanks,

I don't really care :). V2 attached.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

From 947c7a893c282b829a8623da73276a2fe56fdcd3 Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Wed, 11 Dec 2019 11:36:54 +0100
Subject: [PATCH v2] powerpc/book3s64: Fix error handling in mm_iommu_do_alloc()

The last jump to free_exit in mm_iommu_do_alloc() happens after page pointers in struct mm_iommu_table_group_mem_t were already converted to physical addresses. Thus calling put_page() on these physical addresses will likely crash. Convert physical addresses back to page pointers during the error cleanup.

---
 arch/powerpc/mm/book3s64/iommu_api.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/powerpc/mm/book3s64/iommu_api.c +5 -0
index 56cc84520577..1aa06584d783 100644
@@ -154,6 +154,11 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 				       (mem2->entries << PAGE_SHIFT)))) {
 			ret = -EINVAL;
 			mutex_unlock(&mem_list_mutex);
+			/* Convert back to page pointers */
+			for (i = 0; i < pinned; i++) {
+				mem->hpages[i] =
+					pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
+			}
 			goto free_exit;
 		}
 	}
Signature · 1 line
2.16.4
Tested-by Reviewed-by
#3 lore raw
Quoted 42 lines · Jan Kara
Quoted 33 lines · Alexey Kardashevskiy
Quoted 30 lines · Jan Kara

The last jump to free_exit in mm_iommu_do_alloc() happens after page pointers in struct mm_iommu_table_group_mem_t were already converted to physical addresses. Thus calling put_page() on these physical addresses will likely crash. Convert physical addresses back to page pointers during the error cleanup.

---
 arch/powerpc/mm/book3s64/iommu_api.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Beware, this is completely untested, spotted just by code audit.

arch/powerpc/mm/book3s64/iommu_api.c +1 -1
index 56cc84520577..06c403381c9c 100644
@@ -154,7 +154,7 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 				       (mem2->entries << PAGE_SHIFT)))) {
 			ret = -EINVAL;
 			mutex_unlock(&mem_list_mutex);
-			goto free_exit;
+			goto convert_exit;
 		}
 	}
 
@@ -166,6 +166,9 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
 
 	return 0;
 
Quoted 7 lines

arch/powerpc/mm/book3s64/iommu_api.c static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua, · line 171

Quoted 3 lines
arch/powerpc/mm/book3s64/iommu_api.c +3 -0
+convert_exit:
+	for (i = 0; i < pinned; i++)
+		mem->hpages[i] = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);

Good find. Although doing it where you added "goto convert_exit" seems cleaner imho. Thanks,

Quoted 1 line

I don't really care :). V2 attached.

Usually patches are posted using git send-mail, not as attachment. A
nit: v2 has now curly braces which v1 did not.

Either way, v2 is correct.

Signature · 1 line
Alexey