Skip to contentSkip to conversation

Handle delay slot for extable lookup

PATCH In review Jiaxun Yang 4 participants 14 messages Feb 2, 2024 – Feb 12, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover Handle delay slot for extable lookup

Hi all,

This series fixed extable handling for architecture delay slot (MIPS).

Please see previous discussions at [1].

There are some other places in kernel not handling delay slots properly, such as uprobe and kgdb, I'll sort them later.

Thanks!

[1]: https://lore.kernel.org/lkml/[email protected]

To: Oleg Nesterov <[email protected]>

To: Thomas Bogendoerfer <[email protected]>

To: Andrew Morton <[email protected]>
To: Ben Hutchings <[email protected]>

---
Changes in v2:
- Reduce diffstat by implemente fallback macro in linux/ptrace.h (linus)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jiaxun Yang (3):
ptrace: Introduce exception_ip arch hook
MIPS: Clear Cause.BD in instruction_pointer_set mm/memory: Use exception ip to search exception tables

 arch/mips/include/asm/ptrace.h | 3 +++
 arch/mips/kernel/ptrace.c      | 7 +++++++
 include/linux/ptrace.h         | 4 ++++
 mm/memory.c                    | 4 ++--
 4 files changed, 16 insertions(+), 2 deletions(-)

---
base-commit: 06f658aadff0e483ee4f807b0b46c9e5cba62bfa
change-id: 20240131-exception_ip-194e4ad0e6ca

Best regards,

Signature · 1 line
Jiaxun Yang <[email protected]>
#1 lore raw

PATCH v2 1/3 ptrace: Introduce exception_ip arch hook

On architectures with delay slot, architecture level instruction pointer (or program counter) in pt_regs may differ from where exception was triggered.

Introduce exception_ip hook to invoke architecture code and determine actual instruction pointer to the exception.

---
 arch/mips/include/asm/ptrace.h | 2 ++
 arch/mips/kernel/ptrace.c      | 7 +++++++
 include/linux/ptrace.h         | 4 ++++
 3 files changed, 13 insertions(+)
arch/mips/include/asm/ptrace.h +2 -0
index daf3cf244ea9..701a233583c2 100644
@@ -154,6 +154,8 @@ static inline long regs_return_value(struct pt_regs *regs)
 }
 
 #define instruction_pointer(regs) ((regs)->cp0_epc)
+extern unsigned long exception_ip(struct pt_regs *regs);
+#define exception_ip(regs) exception_ip(regs)
 #define profile_pc(regs) instruction_pointer(regs)
 
 extern asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall);
arch/mips/kernel/ptrace.c +7 -0
index d9df543f7e2c..59288c13b581 100644
@@ -31,6 +31,7 @@
 #include <linux/seccomp.h>
 #include <linux/ftrace.h>
 
+#include <asm/branch.h>
 #include <asm/byteorder.h>
 #include <asm/cpu.h>
 #include <asm/cpu-info.h>
@@ -48,6 +49,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
 
+unsigned long exception_ip(struct pt_regs *regs)
+{
+	return exception_epc(regs);
+}
+EXPORT_SYMBOL(exception_ip);
+
 /*
  * Called by kernel/ptrace.c when detaching..
  *
include/linux/ptrace.h +4 -0
index eaaef3ffec22..90507d4afcd6 100644
@@ -393,6 +393,10 @@ static inline void user_single_step_report(struct pt_regs *regs)
 #define current_user_stack_pointer() user_stack_pointer(current_pt_regs())
 #endif
 
+#ifndef exception_ip
+#define exception_ip(x) instruction_pointer(x)
+#endif
+
 extern int task_current_syscall(struct task_struct *target, struct syscall_info *info);
 
 extern void sigaction_compat_abi(struct k_sigaction *act, struct k_sigaction *oact);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/3 MIPS: Clear Cause.BD in instruction_pointer_set

In reply to Jiaxun Yang

Clear Cause.BD after we use instruction_pointer_set to override EPC.

This can prevent exception_epc check against instruction code at new return address.
It won't be considered as "in delay slot" after epc being overridden anyway.

---
 arch/mips/include/asm/ptrace.h | 1 +
 1 file changed, 1 insertion(+)
arch/mips/include/asm/ptrace.h +1 -0
index 701a233583c2..d14d0e37ad02 100644
@@ -60,6 +60,7 @@ static inline void instruction_pointer_set(struct pt_regs *regs,
                                            unsigned long val)
 {
 	regs->cp0_epc = val;
+	regs->cp0_cause &= ~CAUSEF_BD;
 }
 
 /* Query offset/name of register from its name/offset */
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/3 mm/memory: Use exception ip to search exception tables

In reply to Jiaxun Yang

On architectures with delay slot, instruction_pointer() may differ from where exception was triggered.

Use exception_ip we just introduced to search exception tables to get rid of the problem.

Fixes 4bce37a68ff8 ("mips/mm: Convert to using lock_mm_and_find_vma()")
---
 mm/memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/memory.c +2 -2
index 8d14ba440929..49433612444a 100644
@@ -5481,7 +5481,7 @@ static inline bool get_mmap_lock_carefully(struct mm_struct *mm, struct pt_regs
 		return true;
 
 	if (regs && !user_mode(regs)) {
-		unsigned long ip = instruction_pointer(regs);
+		unsigned long ip = exception_ip(regs);
 		if (!search_exception_tables(ip))
 			return false;
 	}
@@ -5506,7 +5506,7 @@ static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, struct pt_r
 {
 	mmap_read_unlock(mm);
 	if (regs && !user_mode(regs)) {
-		unsigned long ip = instruction_pointer(regs);
+		unsigned long ip = exception_ip(regs);
 		if (!search_exception_tables(ip))
 			return false;
 	}
Signature · 1 line
2.43.0
#4 lore raw
In reply to Jiaxun Yang
Quoted 4 lines · Jiaxun Yang

ptrace: Introduce exception_ip arch hook
MIPS: Clear Cause.BD in instruction_pointer_set mm/memory: Use exception ip to search exception tables

Just to clarify: does that second patch fix the problem that __isa_exception_epc() does a __get_user()?

Because that mm/memory.c use of "exception_ip()" most definitely cannot take a page fault.

So if MIPS cannot do that whole exception IP thing without potentially touching user space, I do worry that we might just have to add a

   #ifndef __MIPS__

around this all.

Possibly somehow limited to just the microMIPS/MIPS16e case in Kconfig instead?

Linus

#5 lore raw
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Jiaxun Yang

ptrace: Introduce exception_ip arch hook
MIPS: Clear Cause.BD in instruction_pointer_set mm/memory: Use exception ip to search exception tables

Just to clarify: does that second patch fix the problem that __isa_exception_epc() does a __get_user()?

No it only covers an obvious case when I was playing around exception_ip with kgdb. There are still potential cases __isa_exception_epc may touch user space.

Quoted 9 lines

Because that mm/memory.c use of "exception_ip()" most definitely cannot take a page fault.

So if MIPS cannot do that whole exception IP thing without potentially touching user space, I do worry that we might just have to add a

    #ifndef __MIPS__

around this all.

It is possible to perform exception_ip() without touching user space by saving "BadInstr" in pt_regs at exception entries. For newer hardware it's as simple as saving an extra CP0 register, on older hardware we may have to read it from user space.

+ Thomas (MIPS maintainer), what's your opinion?

Thanks

Quoted 4 lines

Possibly somehow limited to just the microMIPS/MIPS16e case in Kconfig instead?

Linus

Signature · 2 lines
---
Jiaxun Yang
#6 lore raw
In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Jiaxun Yang

ptrace: Introduce exception_ip arch hook
MIPS: Clear Cause.BD in instruction_pointer_set mm/memory: Use exception ip to search exception tables

Just to clarify: does that second patch fix the problem that __isa_exception_epc() does a __get_user()?

Because that mm/memory.c use of "exception_ip()" most definitely cannot take a page fault.

I reconsidered this issue today and I believe that exception_ip() usage here won't trigger page_fault anyway.

Given that exception_ip is guarded by !user_mode(regs), EPC must points to a kernel text address. There is no way accessing kernel text will generate such exception..

Thanks

Quoted 11 lines

So if MIPS cannot do that whole exception IP thing without potentially touching user space, I do worry that we might just have to add a

    #ifndef __MIPS__

around this all.

Possibly somehow limited to just the microMIPS/MIPS16e case in Kconfig instead?

Linus

Signature · 2 lines
---
Jiaxun Yang
#7 lore raw
Quoted 4 lines · Jiaxun Yang

Given that exception_ip is guarded by !user_mode(regs), EPC must points to a kernel text address. There is no way accessing kernel text will generate such exception..

Sadly, that's not actually likely true.

The thing is, the only reason for the code in get_mmap_lock_carefully() is for kernel bugs. IOW, some kernel bug with a wild pointer, and we do not want to deadlock on the mm semaphore, we want to get back to the fault handler and it should report an oops.

... and one of the "wild pointers" might in fact be the instruction pointer, in case we've branched through a NULL function pointer or similar. IOW, the exception *source* might be the instruction pointer itself.

So I realy think that MIPS needs to have some kind of "safe kernel exception pointer" helper. One that is guaranteed not to fault when evaluating the exception pointer.

Assuming the kernel itself is never built with MIPS16e instructions, maybe that's a safe assumption thanks to the "get_isa16_mode()" check of EPC. But all of this makes me nervous.

Linus

#8 lore raw

在 2024/2/4 07:41, Linus Torvalds 写道:
[...]

Quoted 10 lines

The thing is, the only reason for the code in get_mmap_lock_carefully() is for kernel bugs. IOW, some kernel bug with a wild pointer, and we do not want to deadlock on the mm semaphore, we want to get back to the fault handler and it should report an oops.

... and one of the "wild pointers" might in fact be the instruction pointer, in case we've branched through a NULL function pointer or similar. IOW, the exception *source* might be the instruction pointer itself.

Well this is the tricky part of my assumption.
In exception_epc() __isa_exception_epc() stuff is only called if we are in delay slot. It is impossible for a invalid instruction_pointer to be considered as "in delay slot" by hardware.

I'd agree that sounds fragile though.

Thanks

Quoted 10 lines

So I realy think that MIPS needs to have some kind of "safe kernel exception pointer" helper. One that is guaranteed not to fault when evaluating the exception pointer.

Assuming the kernel itself is never built with MIPS16e instructions, maybe that's a safe assumption thanks to the "get_isa16_mode()" check of EPC. But all of this makes me nervous.

Linus

Signature · 2 lines
---
Jiaxun Yang
#9 lore raw
Quoted 6 lines · Jiaxun Yang

Well this is the tricky part of my assumption.
In exception_epc() __isa_exception_epc() stuff is only called if we are in delay slot.
It is impossible for a invalid instruction_pointer to be considered as "in delay slot" by hardware.

Ok, I guess I'm convinced this is all safe. Not great, and not exactly giving me the warm fuzzies, but not buggy.

Linus

#10 lore raw
In reply to Jiaxun Yang
Quoted 8 lines · Jiaxun Yang

Hi all,

This series fixed extable handling for architecture delay slot (MIPS).

Please see previous discussions at [1].

There are some other places in kernel not handling delay slots properly, such as uprobe and kgdb, I'll sort them later.

A gentle ping :-)

This series fixes a regression, perhaps it should go through fixes tree.

Thanks
- Jiaxun

Quoted 41 lines

Thanks!

[1]: https://lore.kernel.org/lkml/[email protected]

To: Oleg Nesterov <[email protected]>

To: Thomas Bogendoerfer <[email protected]>

To: Andrew Morton <[email protected]>
To: Ben Hutchings <[email protected]>

---
Changes in v2:
- Reduce diffstat by implemente fallback macro in linux/ptrace.h (linus)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jiaxun Yang (3):
ptrace: Introduce exception_ip arch hook
MIPS: Clear Cause.BD in instruction_pointer_set mm/memory: Use exception ip to search exception tables

  arch/mips/include/asm/ptrace.h | 3 +++
  arch/mips/kernel/ptrace.c      | 7 +++++++
  include/linux/ptrace.h         | 4 ++++
  mm/memory.c                    | 4 ++--
  4 files changed, 16 insertions(+), 2 deletions(-)

---
base-commit: 06f658aadff0e483ee4f807b0b46c9e5cba62bfa
change-id: 20240131-exception_ip-194e4ad0e6ca

Best regards,

Signature · 2 lines
---
Jiaxun Yang
#11 lore raw
Quoted 7 lines · Jiaxun Yang
Quoted 4 lines · Jiaxun Yang

Hi all,

This series fixed extable handling for architecture delay slot (MIPS).

Quoted 8 lines
Quoted 4 lines

Please see previous discussions at [1].

There are some other places in kernel not handling delay slots properly, such as uprobe and kgdb, I'll sort them later.

A gentle ping :-)

This series fixes a regression, perhaps it should go through fixes tree.

If you have Fixes: {sha} and Cc: [email protected] Greg will pick it up once it's in Linus tree.

Signature · 2 lines
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University
#12 lore raw
Quoted 9 lines · Xi Ruoyao
Quoted 8 lines · Jiaxun Yang
Quoted 6 lines · Jiaxun Yang

Hi all,

This series fixed extable handling for architecture delay slot (MIPS).

Please see previous discussions at [1].

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

There are some other places in kernel not handling delay slots properly, such as uprobe and kgdb, I'll sort them later.

A gentle ping :-)

This series fixes a regression, perhaps it should go through fixes tree.

If you have Fixes: {sha} and Cc: [email protected] Greg will pick it up once it's in Linus tree.

In this case I'd like to backport it manually, as there is only one patch in this series actually fixing the problem.

Thanks
- Jiaxun

Quoted 1 line
Signature · 2 lines
---
Jiaxun Yang
#13 lore raw
In reply to Jiaxun Yang
Quoted 39 lines · Jiaxun Yang

Hi all,

This series fixed extable handling for architecture delay slot (MIPS).

Please see previous discussions at [1].

There are some other places in kernel not handling delay slots properly, such as uprobe and kgdb, I'll sort them later.

Thanks!

[1]: https://lore.kernel.org/lkml/[email protected]

To: Oleg Nesterov <[email protected]>

To: Thomas Bogendoerfer <[email protected]>

To: Andrew Morton <[email protected]>
To: Ben Hutchings <[email protected]>

---
Changes in v2:
- Reduce diffstat by implemente fallback macro in linux/ptrace.h (linus)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jiaxun Yang (3):
ptrace: Introduce exception_ip arch hook
MIPS: Clear Cause.BD in instruction_pointer_set mm/memory: Use exception ip to search exception tables

Quoted 5 lines
 arch/mips/include/asm/ptrace.h | 3 +++
 arch/mips/kernel/ptrace.c      | 7 +++++++
 include/linux/ptrace.h         | 4 ++++
 mm/memory.c                    | 4 ++--
 4 files changed, 16 insertions(+), 2 deletions(-)

series applied to mips-fixes.

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]