Skip to contentSkip to conversation

Implement access checks in iommu page fault paths

PATCH Superseded Joerg Roedel 4 participants 9 messages Nov 10, 2015 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Joerg Roedel

author maintainer
#0 lore raw

cover Implement access checks in iommu page fault paths

Hi,

here is a patch-set that 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.

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

Joerg

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    | 11 +++++++++
 2 files changed, 44 insertions(+), 21 deletions(-)
Signature · 1 line
1.9.1
author maintainer
#1 lore raw

PATCH 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..612f7c8 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 int allowed = 0;
+
+	if (fault->flags & PPR_FAULT_EXEC)
+		allowed = vma->vm_flags & VM_EXEC;
+
+	if (fault->flags & PPR_FAULT_READ)
+		allowed = vma->vm_flags & VM_READ;
+
+	if (fault->flags & PPR_FAULT_WRITE)
+		allowed = vma->vm_flags & VM_WRITE;
+
+	return allowed ? false : true;
+}
+
 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 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 612f7c8..afa8f9c 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 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 afa8f9c..cd1a222 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 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 | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/iommu/intel-svm.c +11 -0
index c69e3f9..4ebf097 100644
@@ -484,6 +484,14 @@ 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)
+{
+	return !((req->rd_req  && (vma->vm_flags & VM_READ))  ||
+		 (req->wr_req  && (vma->vm_flags & VM_WRITE)) ||
+		 (req->exe_req && (vma->vm_flags & VM_EXEC)));
+}
+
 static irqreturn_t prq_event_thread(int irq, void *d)
 {
 	struct intel_iommu *iommu = d;
@@ -539,6 +547,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
#5 lore raw

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

Quoted 11 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.

+static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)

drivers/iommu/intel-svm.c struct page_req_dsc { · line 494

Quoted 6 lines
drivers/iommu/intel-svm.c +6 -0
+{
+	return !((req->rd_req  && (vma->vm_flags & VM_READ))  ||
+		 (req->wr_req  && (vma->vm_flags & VM_WRITE)) ||
+		 (req->exe_req && (vma->vm_flags & VM_EXEC)));
+}
+

This is a TLB fill request from the device — can it not be asking for *all* of read, write and exec privs? And you allow it to succeed if any *one* of the permissions that it asks for is available?

Even if we don't see read+write in the same request, the VT-d spec does seem quite clear that we *will* see read+exec (§7.5.1.1 p7-17 of v2.3:

• Execute Requested: If the PASID Present, Read Requested and Execute Requested fields are all 1, the request-with-PASID that encountered the recoverable fault that resulted in this page request, requires execute access to the page.

Also, I'm afraid my Skylake box blew up the day I sent the pull request to Linus so I'm unable to test until I've sorted out a replacement. Jesse should be able to though...

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 14 lines · David Woodhouse

drivers/iommu/intel-svm.c struct page_req_dsc { · line 494

Quoted 8 lines · Joerg Roedel

+static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)

drivers/iommu/intel-svm.c +6 -0
+{
+	return !((req->rd_req  && (vma->vm_flags & VM_READ))  ||
+		 (req->wr_req  && (vma->vm_flags & VM_WRITE)) ||
+		 (req->exe_req && (vma->vm_flags & VM_EXEC)));
+}
+

This is a TLB fill request from the device — can it not be asking for *all* of read, write and exec privs? And you allow it to succeed if any *one* of the permissions that it asks for is available?

Quoted 7 lines

Even if we don't see read+write in the same request, the VT-d spec does seem quite clear that we *will* see read+exec (§7.5.1.1 p7-17 of v2.3:

• Execute Requested: If the PASID Present, Read Requested and Execute Requested fields are all 1, the request-with-PASID that encountered the recoverable fault that resulted in this page request, requires execute access to the page.

Okay, thanks for the clarification. The code above assumes that only one of RWX is set. I'll update the patch to correctly check when multiple bits are set.

Joerg

#7 lore raw

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

In reply to Joerg Roedel

drivers/iommu/intel-svm.c struct page_req_dsc { · line 493

Quoted 7 lines · Joerg Roedel
drivers/iommu/intel-svm.c +7 -0
+
+static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
+{
+       return !((req->rd_req  && (vma->vm_flags & VM_READ))  ||
+                (req->wr_req  && (vma->vm_flags & VM_WRITE)) ||
+                (req->exe_req && (vma->vm_flags & VM_EXEC)));
+}

This seems odd.

Shouldn't it be

    return ((req->rd_req  && !(vma->vm_flags & VM_READ))  ||
            (req->wr_req  && !(vma->vm_flags & VM_WRITE)) ||
            (req->exe_req && !(vma->vm_flags & VM_EXEC)));

instead?

Of course, if you just used the VM_xyz flags internally itself, this would all be easier, and you'd end up with something like

    /* Do we have requested bits that aren't in the allowed set? */
    return (requested & ~vma->vm_flags) != 0;

instead..

Linus

author maintainer
#8 lore raw

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

Quoted 11 lines · Linus Torvalds

drivers/iommu/intel-svm.c struct page_req_dsc { · line 493

Quoted 7 lines · Joerg Roedel
drivers/iommu/intel-svm.c +7 -0
+
+static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
+{
+       return !((req->rd_req  && (vma->vm_flags & VM_READ))  ||
+                (req->wr_req  && (vma->vm_flags & VM_WRITE)) ||
+                (req->exe_req && (vma->vm_flags & VM_EXEC)));
+}

This seems odd.

Quoted 7 lines

Shouldn't it be

    return ((req->rd_req  && !(vma->vm_flags & VM_READ))  ||
            (req->wr_req  && !(vma->vm_flags & VM_WRITE)) ||
            (req->exe_req && !(vma->vm_flags & VM_EXEC)));

instead?

Yes, thats better, it solves the multiple-bits-set problem too, which David mentioned.

Quoted 7 lines

Of course, if you just used the VM_xyz flags internally itself, this would all be easier, and you'd end up with something like

    /* Do we have requested bits that aren't in the allowed set? */
    return (requested & ~vma->vm_flags) != 0;

instead..

But this is probably the best solution, some architecture page-fault handlers do something similar. I'll update the patch and use this for the AMD part too.

Thanks,

Joerg