Skip to contentSkip to conversation

Implement access checks in iommu page fault paths

PATCH In review Joerg Roedel 6 participants 11 messages Nov 17, 2015 – Dec 14, 2015 1 message not in the archive IOMMU

lore
Share & tools
lore

Joerg Roedel

author maintainer
#0 lore raw

v2 cover Implement access checks in iommu page fault paths

Hi,

here is the second version of the patch-set to implement proper access checks into the io-page-fault handlers of the AMD IOMMU and Intel VT-d drivers.

Two additional patches clean up the AMD part a bit further. Since I can't test this this code myself due to lack of hardware or software that utilizes it, I'd appreciate some external testing.

It took me a while to get these out, mostly because I tried to setup my own HSA test environment to at least test the AMD changes myself. That failed, so I am sending this out with another request for testing.

Oded, Jesse, would you two please test these patches and report back? Thanks a lot!

Joerg

Changes since v1:

* Updated the access_error functions based on Linus' feedback
* Rebased to v4.4-rc1

Joerg Roedel (4):
iommu/amd: Do proper access checking before calling handle_mm_fault() iommu/amd: Correctly set flags for handle_mm_fault call iommu/amd: Cleanup error handling in do_fault() iommu/vt-d: Do access checks before calling handle_mm_fault()

 drivers/iommu/amd_iommu_v2.c | 54 +++++++++++++++++++++++++++-----------------
 drivers/iommu/intel-svm.c    | 20 ++++++++++++++++
 2 files changed, 53 insertions(+), 21 deletions(-)
Signature · 1 line
1.9.1
author maintainer
#1 lore raw

PATCH v2 1/4 iommu/amd: Do proper access checking before calling handle_mm_fault()

From: Joerg Roedel <[email protected]>

The handle_mm_fault function expects the caller to do the access checks. Not doing so and calling the function with wrong permissions is a bug (catched by a BUG_ON). So fix this bug by adding proper access checking to the io page-fault code in the AMD IOMMUv2 driver.

---
 drivers/iommu/amd_iommu_v2.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/iommu/amd_iommu_v2.c +18 -2
index d21d4ed..7caf2fa 100644
@@ -494,6 +494,22 @@ static void handle_fault_error(struct fault *fault)
 	}
 }
 
+static bool access_error(struct vm_area_struct *vma, struct fault *fault)
+{
+	unsigned long requested = 0;
+
+	if (fault->flags & PPR_FAULT_EXEC)
+		requested |= VM_EXEC;
+
+	if (fault->flags & PPR_FAULT_READ)
+		requested |= VM_READ;
+
+	if (fault->flags & PPR_FAULT_WRITE)
+		requested |= VM_WRITE;
+
+	return (requested & ~vma->vm_flags) != 0;
+}
+
 static void do_fault(struct work_struct *work)
 {
 	struct fault *fault = container_of(work, struct fault, work);
@@ -516,8 +532,8 @@ static void do_fault(struct work_struct *work)
 		goto out;
 	}
 
-	if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) {
-		/* handle_mm_fault would BUG_ON() */
+	/* Check if we have the right permissions on the vma */
+	if (access_error(vma, fault)) {
 		up_read(&mm->mmap_sem);
 		handle_fault_error(fault);
 		goto out;
Signature · 1 line
1.9.1
author maintainer
#2 lore raw

PATCH v2 2/4 iommu/amd: Correctly set flags for handle_mm_fault call

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Instead of just checking for a write access, calculate the flags that are passed to handle_mm_fault() more precisly and use the pre-defined macros.

---
 drivers/iommu/amd_iommu_v2.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/iommu/amd_iommu_v2.c +9 -5
index 7caf2fa..a7edbd6 100644
@@ -513,16 +513,20 @@ static bool access_error(struct vm_area_struct *vma, struct fault *fault)
 static void do_fault(struct work_struct *work)
 {
 	struct fault *fault = container_of(work, struct fault, work);
-	struct mm_struct *mm;
 	struct vm_area_struct *vma;
+	unsigned int flags = 0;
+	struct mm_struct *mm;
 	u64 address;
-	int ret, write;
-
-	write = !!(fault->flags & PPR_FAULT_WRITE);
+	int ret;
 
 	mm = fault->state->mm;
 	address = fault->address;
 
+	if (fault->flags & PPR_FAULT_USER)
+		flags |= FAULT_FLAG_USER;
+	if (fault->flags & PPR_FAULT_WRITE)
+		flags |= FAULT_FLAG_WRITE;
+
 	down_read(&mm->mmap_sem);
 	vma = find_extend_vma(mm, address);
 	if (!vma || address < vma->vm_start) {
@@ -539,7 +543,7 @@ static void do_fault(struct work_struct *work)
 		goto out;
 	}
 
-	ret = handle_mm_fault(mm, vma, address, write);
+	ret = handle_mm_fault(mm, vma, address, flags);
 	if (ret & VM_FAULT_ERROR) {
 		/* failed to service fault */
 		up_read(&mm->mmap_sem);
Signature · 1 line
1.9.1
author maintainer
#3 lore raw

PATCH v2 3/4 iommu/amd: Cleanup error handling in do_fault()

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Get rid of the three error paths that look the same and move error handling to a single place.

---
 drivers/iommu/amd_iommu_v2.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
drivers/iommu/amd_iommu_v2.c +8 -16
index a7edbd6..6a28b74 100644
@@ -514,10 +514,10 @@ static void do_fault(struct work_struct *work)
 {
 	struct fault *fault = container_of(work, struct fault, work);
 	struct vm_area_struct *vma;
+	int ret = VM_FAULT_ERROR;
 	unsigned int flags = 0;
 	struct mm_struct *mm;
 	u64 address;
-	int ret;
 
 	mm = fault->state->mm;
 	address = fault->address;
@@ -529,31 +529,23 @@ static void do_fault(struct work_struct *work)
 
 	down_read(&mm->mmap_sem);
 	vma = find_extend_vma(mm, address);
-	if (!vma || address < vma->vm_start) {
+	if (!vma || address < vma->vm_start)
 		/* failed to get a vma in the right range */
-		up_read(&mm->mmap_sem);
-		handle_fault_error(fault);
 		goto out;
-	}
 
 	/* Check if we have the right permissions on the vma */
-	if (access_error(vma, fault)) {
-		up_read(&mm->mmap_sem);
-		handle_fault_error(fault);
+	if (access_error(vma, fault))
 		goto out;
-	}
 
 	ret = handle_mm_fault(mm, vma, address, flags);
-	if (ret & VM_FAULT_ERROR) {
-		/* failed to service fault */
-		up_read(&mm->mmap_sem);
-		handle_fault_error(fault);
-		goto out;
-	}
 
+out:
 	up_read(&mm->mmap_sem);
 
-out:
+	if (ret & VM_FAULT_ERROR)
+		/* failed to service fault */
+		handle_fault_error(fault);
+
 	finish_pri_tag(fault->dev_state, fault->state, fault->tag);
 
 	put_pasid_state(fault->state);
Signature · 1 line
1.9.1
author maintainer
#4 lore raw

PATCH v2 4/4 iommu/vt-d: Do access checks before calling handle_mm_fault()

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Not doing so is a bug and might trigger a BUG_ON in handle_mm_fault(). So add the proper permission checks before calling into mm code.

---
 drivers/iommu/intel-svm.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/iommu/intel-svm.c +20 -0
index c69e3f9..5046483 100644
@@ -484,6 +484,23 @@ struct page_req_dsc {
 };
 
 #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10)
+
+static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
+{
+	unsigned long requested = 0;
+
+	if (req->exe_req)
+		requested |= VM_EXEC;
+
+	if (req->rd_req)
+		requested |= VM_READ;
+
+	if (req->wr_req)
+		requested |= VM_WRITE;
+
+	return (requested & ~vma->vm_flags) != 0;
+}
+
 static irqreturn_t prq_event_thread(int irq, void *d)
 {
 	struct intel_iommu *iommu = d;
@@ -539,6 +556,9 @@ static irqreturn_t prq_event_thread(int irq, void *d)
 		if (!vma || address < vma->vm_start)
 			goto invalid;
 
+		if (access_error(vma, req))
+			goto invalid;
+
 		ret = handle_mm_fault(svm->mm, vma, address,
 				      req->wr_req ? FAULT_FLAG_WRITE : 0);
 		if (ret & VM_FAULT_ERROR)
Signature · 1 line
1.9.1
Acked-by on v2 4/4
#5 lore raw

Re: [PATCH 4/4] iommu/vt-d: Do access checks before calling handle_mm_fault()

Quoted 7 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

Not doing so is a bug and might trigger a BUG_ON in handle_mm_fault(). So add the proper permission checks before calling into mm code.

As noted, I can't test it right now until I've replaced my test hardware. Certainly looks better than the last version though :)

Thanks.

Signature · 2 lines
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation
#6 lore raw

Re: [PATCH 4/4] iommu/vt-d: Do access checks before calling handle_mm_fault()

Quoted 4 lines · David Woodhouse

As noted, I can't test it right now until I've replaced my test hardware. Certainly looks better than the last version though :)

Okay, thanks. I applied the series to the iommu tree. Patch 1 and 4 as fixes, the others the the x86/amd branch.

#7 lore raw
In reply to Joerg Roedel
Quoted 34 lines · Joerg Roedel

Hi,

here is the second version of the patch-set to implement proper access checks into the io-page-fault handlers of the AMD IOMMU and Intel VT-d drivers.

Two additional patches clean up the AMD part a bit further. Since I can't test this this code myself due to lack of hardware or software that utilizes it, I'd appreciate some external testing.

It took me a while to get these out, mostly because I tried to setup my own HSA test environment to at least test the AMD changes myself. That failed, so I am sending this out with another request for testing.

Oded, Jesse, would you two please test these patches and report back? Thanks a lot!

Joerg

Changes since v1:

* Updated the access_error functions based on Linus' feedback
* Rebased to v4.4-rc1

Joerg Roedel (4):
iommu/amd: Do proper access checking before calling handle_mm_fault() iommu/amd: Correctly set flags for handle_mm_fault call iommu/amd: Cleanup error handling in do_fault() iommu/vt-d: Do access checks before calling handle_mm_fault()

Quoted 7 lines
 drivers/iommu/amd_iommu_v2.c | 54 +++++++++++++++++++++++++++-----------------
 drivers/iommu/intel-svm.c    | 20 ++++++++++++++++
 2 files changed, 53 insertions(+), 21 deletions(-)

--
1.9.1

Hi Joerg,

I have very limited testing capabilities since I left AMD - I only have Kaveri (which is kind of obsolete) and I only have a very simple test utility.

Quoted 1 line

From what I could check, I didn't see any problems, but this is really

such a very basic check that I don't even want to presume to give a Tested-by tag. Sorry.

I suggest you talk to AMD people, as they have access to Carrizo H/W and S/W testing framework.

Oded

#8 lore raw
In reply to Joerg Roedel
Quoted 11 lines · Joerg Roedel

Hi,

here is the second version of the patch-set to implement proper access checks into the io-page-fault handlers of the AMD IOMMU and Intel VT-d drivers.

Two additional patches clean up the AMD part a bit further. Since I can't test this this code myself due to lack of hardware or software that utilizes it, I'd appreciate some external testing.

Quoted 7 lines

It took me a while to get these out, mostly because I tried to setup my own HSA test environment to at least test the AMD changes myself. That failed, so I am sending this out with another request for testing.

Oded, Jesse, would you two please test these patches and report back? Thanks a lot!

I might be able to find time later this week or next; I have to update my tree to David's latest bits and rebase my i915 code on top along with these patches. Then I can run some of my tests and expand them to try r/w tests w/o mapping the buffer with the right access mode.

Jesse

author maintainer
#9 lore raw

Hey Jesse,

Quoted 5 lines · Jesse Barnes

I might be able to find time later this week or next; I have to update my tree to David's latest bits and rebase my i915 code on top along with these patches. Then I can run some of my tests and expand them to try r/w tests w/o mapping the buffer with the right access mode.

Have you found time to run some small tests on this?

Thanks,

Joerg

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw

No sorry, and now it looks like it'll be another couple of weeks. I'd say go ahead and land them though. You can add my r-b and I'll test them next time I rebase my i915 patches.

Thanks,
Jesse

---- Joerg Roedel wrote ----

Quoted 17 lines

Hey Jesse,

Quoted 5 lines · Jesse Barnes

I might be able to find time later this week or next; I have to update my tree to David's latest bits and rebase my i915 code on top along with these patches. Then I can run some of my tests and expand them to try r/w tests w/o mapping the buffer with the right access mode.

Have you found time to run some small tests on this?

Thanks,

Joerg