Skip to contentSkip to conversation

arm64: print a fault message when attempting to write RO memory

PATCH Superseded Stephen Boyd 3 participants 7 messages Feb 17, 2017 – Apr 4, 2017 arm64

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm64: print a fault message when attempting to write RO memory

If a page is marked read only we should print out that fact, instead of printing out that there was a page fault. Right now we get a cryptic error message that something went wrong with an unhandled fault, but we don't evaluate the esr to figure out that it was a read/write permission fault.

Instead of seeing:

  Unable to handle kernel paging request at virtual address ffff000008e460d8
  pgd = ffff800003504000
  [ffff000008e460d8] *pgd=0000000083473003, *pud=0000000083503003, *pmd=0000000000000000
  Internal error: Oops: 9600004f [#1] PREEMPT SMP

we'll see:

  Unable to handle kernel write to read-only memory at virtual address ffff000008e760d8
  pgd = ffff80003d3de000
  [ffff000008e760d8] *pgd=0000000083472003, *pud=0000000083435003, *pmd=0000000000000000
  Internal error: Oops: 9600004f [#1] PREEMPT SMP
Cc James Morse <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Mark Rutland <[email protected]>

---

Changes from v1:
* Move into __do_kernel_fault() (Mark Rutland)

 arch/arm64/mm/fault.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
arch/arm64/mm/fault.c +29 -18
index 156169c6981b..8bd4e7f11c70 100644
@@ -160,12 +160,28 @@ static bool is_el1_instruction_abort(unsigned int esr)
 	return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
 }
 
+static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs)
+{
+	unsigned int ec       = ESR_ELx_EC(esr);
+	unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
+
+	if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
+		return false;
+
+	if (system_uses_ttbr0_pan())
+		return fsc_type == ESR_ELx_FSC_FAULT &&
+			(regs->pstate & PSR_PAN_BIT);
+	else
+		return fsc_type == ESR_ELx_FSC_PERM;
+}
+
 /*
  * The kernel tried to access some page that wasn't present.
  */
 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
 			      unsigned int esr, struct pt_regs *regs)
 {
+	const char *msg;
 	/*
 	 * Are we prepared to handle this kernel fault?
 	 * We are almost certainly not prepared to handle instruction faults.
@@ -177,9 +193,19 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
 	 * No handler, we'll have to terminate things with extreme prejudice.
 	 */
 	bust_spinlocks(1);
-	pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
-		 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
-		 "paging request", addr);
+
+	if (is_permission_fault(esr, regs)) {
+		if (esr & ESR_ELx_WNR)
+			msg = "write to read-only memory";
+		else
+			msg = "read from unreadable memory";
+	} else if (addr < PAGE_SIZE)
+		msg = "NULL pointer dereference";
+	else
+		msg = "paging request";
+
+	pr_alert("Unable to handle kernel %s@virtual address %08lx\n", msg,
+		 addr);
 
 	show_pte(mm, addr);
 	die("Oops", regs, esr);
@@ -269,21 +295,6 @@ static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
 	return fault;
 }
 
-static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs)
-{
-	unsigned int ec       = ESR_ELx_EC(esr);
-	unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
-
-	if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
-		return false;
-
-	if (system_uses_ttbr0_pan())
-		return fsc_type == ESR_ELx_FSC_FAULT &&
-			(regs->pstate & PSR_PAN_BIT);
-	else
-		return fsc_type == ESR_ELx_FSC_PERM;
-}
-
 static bool is_el0_instruction_abort(unsigned int esr)
 {
 	return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
Signature · 1 line
2.10.0.297.gf6727b0
#1 lore raw

Hi Stephen,

Quoted 13 lines · Stephen Boyd

If a page is marked read only we should print out that fact, instead of printing out that there was a page fault. Right now we get a cryptic error message that something went wrong with an unhandled fault, but we don't evaluate the esr to figure out that it was a read/write permission fault.

Instead of seeing:

  Unable to handle kernel paging request at virtual address ffff000008e460d8
  pgd = ffff800003504000
  [ffff000008e460d8] *pgd=0000000083473003, *pud=0000000083503003, *pmd=0000000000000000
  Internal error: Oops: 9600004f [#1] PREEMPT SMP
Quoted 6 lines

we'll see:

  Unable to handle kernel write to read-only memory at virtual address ffff000008e760d8
  pgd = ffff80003d3de000
  [ffff000008e760d8] *pgd=0000000083472003, *pud=0000000083435003, *pmd=0000000000000000
  Internal error: Oops: 9600004f [#1] PREEMPT SMP

This looks like a good idea..

Quoted 16 lines
arch/arm64/mm/fault.c +1 -0
index 156169c6981b..8bd4e7f11c70 100644
 
 /*
  * The kernel tried to access some page that wasn't present.
  */
 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
 			      unsigned int esr, struct pt_regs *regs)
 {
+	const char *msg;
 	/*
 	 * Are we prepared to handle this kernel fault?
 	 * We are almost certainly not prepared to handle instruction faults.

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 197

Quoted 8 lines
arch/arm64/mm/fault.c +2 -3
@@ -177,9 +193,19 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
 	 * No handler, we'll have to terminate things with extreme prejudice.
 	 */
 	bust_spinlocks(1);
-	pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
-		 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
-		 "paging request", addr);
+
+	if (is_permission_fault(esr, regs)) {

is_permission_fault() was previously guarded with a 'addr<USER_DS' check, this is because it assumes software-PAN is relevant.

The corner case is when the kernel accesses TTBR1-mapped memory while software-PAN happens to have swivelled TTBR0. Translation faults will be matched by is_permission_fault(), but permission faults won't.

Juggling is_permission_fault() to look something like:
---%<---

	if (fsc_type == ESR_ELx_FSC_PERM)
		return true;

	if (addr < USER_DS && system_uses_ttbr0_pan())
		return fsc_type == ESR_ELx_FSC_FAULT &&
			(regs->pstate & PSR_PAN_BIT);

	return false;

---%<---
... should fix this.

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 205

Quoted 8 lines
arch/arm64/mm/fault.c +8 -0
+		if (esr & ESR_ELx_WNR)
+			msg = "write to read-only memory";
+		else
+			msg = "read from unreadable memory";
+	} else if (addr < PAGE_SIZE)
+		msg = "NULL pointer dereference";
+	else
+		msg = "paging request";

Nit: {} all the way down!

Quoted 14 lines
arch/arm64/mm/fault.c +3 -4
+
+	pr_alert("Unable to handle kernel %s at virtual address %08lx\n", msg,
+		 addr);
 
 	show_pte(mm, addr);
 	die("Oops", regs, esr);
@@ -269,21 +295,6 @@ static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
 	return fault;
 }
 
-static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs)
-{
-	unsigned int ec       = ESR_ELx_EC(esr);
-	unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;

arch/arm64/mm/fault.c static int __do_page_fault(struct mm_struct *mm, unsigned long addr, · line 285

Quoted 10 lines
arch/arm64/mm/fault.c +0 -10
-
-	if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
-		return false;
-
-	if (system_uses_ttbr0_pan())
-		return fsc_type == ESR_ELx_FSC_FAULT &&
-			(regs->pstate & PSR_PAN_BIT);
-	else
-		return fsc_type == ESR_ELx_FSC_PERM;
-}

Thanks!

James

#2 lore raw

PATCH v2 1/1 arm64: print a fault message when attempting to write RO memory

Quoting James Morse (2017-02-17 03:00:39)

Quoted 29 lines · Stephen Boyd
Quoted 16 lines
arch/arm64/mm/fault.c +1 -0
index 156169c6981b..8bd4e7f11c70 100644
 
 /*
  * The kernel tried to access some page that wasn't present.
  */
 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
                            unsigned int esr, struct pt_regs *regs)
 {
+     const char *msg;
      /*
       * Are we prepared to handle this kernel fault?
       * We are almost certainly not prepared to handle instruction faults.

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 197

Quoted 8 lines
arch/arm64/mm/fault.c +2 -3
@@ -177,9 +193,19 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
       * No handler, we'll have to terminate things with extreme prejudice.
       */
      bust_spinlocks(1);
-     pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
-              (addr < PAGE_SIZE) ? "NULL pointer dereference" :
-              "paging request", addr);
+
+     if (is_permission_fault(esr, regs)) {

is_permission_fault() was previously guarded with a 'addr<USER_DS' check, this is because it assumes software-PAN is relevant.

Quoted 3 lines

The corner case is when the kernel accesses TTBR1-mapped memory while software-PAN happens to have swivelled TTBR0. Translation faults will be matched by is_permission_fault(), but permission faults won't.

If I understand correctly, and I most definitely don't because there are quite a few combinations, you're saying that __do_kernel_fault() could be called if the kernel attempts to access some userspace address with software PAN? That won't be caught in do_page_fault() with the previous is_permission_fault() check?

Quoted 11 lines

Juggling is_permission_fault() to look something like:
---%<---

        if (fsc_type == ESR_ELx_FSC_PERM)
                return true;

        if (addr < USER_DS && system_uses_ttbr0_pan())
                return fsc_type == ESR_ELx_FSC_FAULT &&
                        (regs->pstate & PSR_PAN_BIT);

        return false;
Quoted 2 lines

---%<---
... should fix this.

But we don't need to check ec anymore?

	if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
		return false;
Quoted 11 lines

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 205

Quoted 10 lines
arch/arm64/mm/fault.c +8 -0
+             if (esr & ESR_ELx_WNR)
+                     msg = "write to read-only memory";
+             else
+                     msg = "read from unreadable memory";
+     } else if (addr < PAGE_SIZE)
+             msg = "NULL pointer dereference";
+     else
+             msg = "paging request";
Quoted 1 line

Nit: {} all the way down!

Ok.

#3 lore raw

Hi Stephen,

Quoted 23 lines · Stephen Boyd

Quoting James Morse (2017-02-17 03:00:39)

Quoted 18 lines · Stephen Boyd

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 197

Quoted 13 lines
arch/arm64/mm/fault.c +2 -3
index 156169c6981b..8bd4e7f11c70 100644
@@ -177,9 +193,19 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
       * No handler, we'll have to terminate things with extreme prejudice.
       */
      bust_spinlocks(1);
-     pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
-              (addr < PAGE_SIZE) ? "NULL pointer dereference" :
-              "paging request", addr);
+
+     if (is_permission_fault(esr, regs)) {

is_permission_fault() was previously guarded with a 'addr<USER_DS' check, this is because it assumes software-PAN is relevant.

Quoted 3 lines

The corner case is when the kernel accesses TTBR1-mapped memory while software-PAN happens to have swivelled TTBR0. Translation faults will be matched by is_permission_fault(), but permission faults won't.

Quoted 5 lines

If I understand correctly, and I most definitely don't because there are quite a few combinations, you're saying that __do_kernel_fault() could be called if the kernel attempts to access some userspace address with software PAN? That won't be caught in do_page_fault() with the previous is_permission_fault() check?

You're right the user-address side of things will get caught in do_page_fault(). I was trying to badly-explain 'is_permission_fault(esr)' isn't as general purpose as its name and prototype suggest, it only gives the results that the PAN checks expect when called with a user address.

Quoted 9 lines
Quoted 9 lines

Juggling is_permission_fault() to look something like:
---%<---

        if (fsc_type == ESR_ELx_FSC_PERM)
                return true;

        if (addr < USER_DS && system_uses_ttbr0_pan())
                return fsc_type == ESR_ELx_FSC_FAULT &&
                        (regs->pstate & PSR_PAN_BIT);
Quoted 5 lines
Quoted 3 lines
        return false;

---%<---
... should fix this.

But we don't need to check ec anymore?

Sorry, I was being sloppy, something like the above could replace the if/else block at the end of is_permission_fault(). You're right we still need the ec check!

Thanks,

James

#4 lore raw

Quoting James Morse (2017-02-20 03:10:10)

Quoted 32 lines

Hi Stephen,

Quoted 23 lines · Stephen Boyd

Quoting James Morse (2017-02-17 03:00:39)

Quoted 18 lines · Stephen Boyd

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 197

Quoted 13 lines
arch/arm64/mm/fault.c +2 -3
index 156169c6981b..8bd4e7f11c70 100644
@@ -177,9 +193,19 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
       * No handler, we'll have to terminate things with extreme prejudice.
       */
      bust_spinlocks(1);
-     pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
-              (addr < PAGE_SIZE) ? "NULL pointer dereference" :
-              "paging request", addr);
+
+     if (is_permission_fault(esr, regs)) {

is_permission_fault() was previously guarded with a 'addr<USER_DS' check, this is because it assumes software-PAN is relevant.

Quoted 3 lines

The corner case is when the kernel accesses TTBR1-mapped memory while software-PAN happens to have swivelled TTBR0. Translation faults will be matched by is_permission_fault(), but permission faults won't.

Quoted 5 lines

If I understand correctly, and I most definitely don't because there are quite a few combinations, you're saying that __do_kernel_fault() could be called if the kernel attempts to access some userspace address with software PAN? That won't be caught in do_page_fault() with the previous is_permission_fault() check?

Quoted 4 lines

You're right the user-address side of things will get caught in do_page_fault(). I was trying to badly-explain 'is_permission_fault(esr)' isn't as general purpose as its name and prototype suggest, it only gives the results that the PAN checks expect when called with a user address.

Ok. I'd rather not change the function in this patch because I'm only moving the code around to use it higher up in the file. But if you prefer I can combine the code movement with the addition of a new 'addr' argument to this function and rework things based on that.

#5 lore raw

Hi Stephen,

Quoted 36 lines · Stephen Boyd

Quoting James Morse (2017-02-20 03:10:10)

Quoted 30 lines · Stephen Boyd
Quoted 23 lines

Quoting James Morse (2017-02-17 03:00:39)

Quoted 18 lines · Stephen Boyd

arch/arm64/mm/fault.c static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, · line 197

Quoted 13 lines
arch/arm64/mm/fault.c +2 -3
index 156169c6981b..8bd4e7f11c70 100644
@@ -177,9 +193,19 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
       * No handler, we'll have to terminate things with extreme prejudice.
       */
      bust_spinlocks(1);
-     pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
-              (addr < PAGE_SIZE) ? "NULL pointer dereference" :
-              "paging request", addr);
+
+     if (is_permission_fault(esr, regs)) {

is_permission_fault() was previously guarded with a 'addr<USER_DS' check, this is because it assumes software-PAN is relevant.

Quoted 3 lines

The corner case is when the kernel accesses TTBR1-mapped memory while software-PAN happens to have swivelled TTBR0. Translation faults will be matched by is_permission_fault(), but permission faults won't.

Quoted 5 lines

If I understand correctly, and I most definitely don't because there are quite a few combinations, you're saying that __do_kernel_fault() could be called if the kernel attempts to access some userspace address with software PAN? That won't be caught in do_page_fault() with the previous is_permission_fault() check?

Quoted 4 lines

You're right the user-address side of things will get caught in do_page_fault(). I was trying to badly-explain 'is_permission_fault(esr)' isn't as general purpose as its name and prototype suggest, it only gives the results that the PAN checks expect when called with a user address.

Quoted 4 lines

Ok. I'd rather not change the function in this patch because I'm only moving the code around to use it higher up in the file. But if you prefer I can combine the code movement with the addition of a new 'addr' argument to this function and rework things based on that.

Are you planning to send a v3 of this?

Will

#6 lore raw

Quoting Will Deacon (2017-03-23 07:22:39)

Quoted 8 lines · Stephen Boyd
Quoted 7 lines

Quoting James Morse (2017-02-20 03:10:10)

Quoted 5 lines

You're right the user-address side of things will get caught in do_page_fault(). I was trying to badly-explain 'is_permission_fault(esr)' isn't as general purpose as its name and prototype suggest, it only gives the results that the PAN checks expect when called with a user address.

Quoted 7 lines
Quoted 4 lines

Ok. I'd rather not change the function in this patch because I'm only moving the code around to use it higher up in the file. But if you prefer I can combine the code movement with the addition of a new 'addr' argument to this function and rework things based on that.

Are you planning to send a v3 of this?

Sorry for the late reply. I was hoping that James would indicate preference one way or the other. I suppose no reply means "yes" here, so I'll go ahead and fold everything together into one patch and resend.