Skip to contentSkip to conversation

x86/irq: Make run_on_irqstack_cond() typesafe

PATCH Applied Thomas Gleixner 6 participants 6 messages Sep 22, 2020 – Sep 25, 2020

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

Sami reported that run_on_irqstack_cond() requires the caller to cast functions to mismatching types, which trips indirect call Control-Flow Integrity (CFI) in Clang.

Instead of disabling CFI on that function, provide proper helpers for the three call variants. The actual ASM code stays the same as that is out of reach.

Fixes 931b94145981 ("x86/entry: Provide helpers for executing on the irqstack")
---
 arch/x86/entry/common.c          |    2 -
 arch/x86/entry/entry_64.S        |    2 +
 arch/x86/include/asm/idtentry.h  |    2 -
 arch/x86/include/asm/irq_stack.h |   69 ++++++++++++++++++++++++++++++++++-----
 arch/x86/kernel/irq.c            |    2 -
 arch/x86/kernel/irq_64.c         |    2 -
 6 files changed, 67 insertions(+), 12 deletions(-)
arch/x86/entry/common.c +1 -1
@@ -299,7 +299,7 @@ static void __xen_pv_evtchn_do_upcall(vo
 	old_regs = set_irq_regs(regs);
 
 	instrumentation_begin();
-	run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, NULL, regs);
+	run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, regs);
 	instrumentation_begin();
 
 	set_irq_regs(old_regs);
arch/x86/entry/entry_64.S +2 -0
@@ -762,6 +762,8 @@ SYM_CODE_END(.Lbad_gs)
  * rdx: Function argument (can be NULL if none)
  */
 SYM_FUNC_START(asm_call_on_stack)
+SYM_INNER_LABEL(asm_call_sysvec_on_stack, SYM_L_GLOBAL)
+SYM_INNER_LABEL(asm_call_irq_on_stack, SYM_L_GLOBAL)
 	/*
 	 * Save the frame pointer unconditionally. This allows the ORC
 	 * unwinder to handle the stack switch.
arch/x86/include/asm/idtentry.h +1 -1
@@ -242,7 +242,7 @@ static void __##func(struct pt_regs *reg
 	instrumentation_begin();					\
 	irq_enter_rcu();						\
 	kvm_set_cpu_l1tf_flush_l1d();					\
-	run_on_irqstack_cond(__##func, regs, regs);			\
+	run_sysvec_on_irqstack_cond(__##func, regs);			\
 	irq_exit_rcu();							\
 	instrumentation_end();						\
 	irqentry_exit(regs, state);					\
arch/x86/include/asm/irq_stack.h +61 -8

98 lines not on this page · raw message

arch/x86/kernel/irq.c +1 -1
@@ -227,7 +227,7 @@ static __always_inline void handle_irq(s
 				       struct pt_regs *regs)
 {
 	if (IS_ENABLED(CONFIG_X86_64))
-		run_on_irqstack_cond(desc->handle_irq, desc, regs);
+		run_irq_on_irqstack_cond(desc->handle_irq, desc, regs);
 	else
 		__handle_irq(desc, regs);
 }
arch/x86/kernel/irq_64.c +1 -1
@@ -74,5 +74,5 @@ int irq_init_percpu_irqstack(unsigned in
 
 void do_softirq_own_stack(void)
 {
-	run_on_irqstack_cond(__do_softirq, NULL, NULL);
+	run_on_irqstack_cond(__do_softirq, NULL);
 }
Tested-by
#1 lore raw

Hi Thomas,

Quoted 5 lines · Thomas Gleixner

Sami reported that run_on_irqstack_cond() requires the caller to cast functions to mismatching types, which trips indirect call Control-Flow Integrity (CFI) in Clang.

Quoted 7 lines

Instead of disabling CFI on that function, provide proper helpers for the three call variants. The actual ASM code stays the same as that is out of reach.

Fixes 931b94145981 ("x86/entry: Provide helpers for executing on the irqstack")

Thank you for the patch! I confirmed that this fixes the type mismatch issue with CFI for me:

Also, please note that this was first reported by Nathan in this GitHub issue:
https://github.com/ClangBuiltLinux/linux/issues/1052

Sami

#2 lore raw
In reply to Thomas Gleixner
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     a7b3474cbb2864d5500d5e4f48dd57c903975cab
Gitweb:        https://git.kernel.org/tip/a7b3474cbb2864d5500d5e4f48dd57c903975cab
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 22 Sep 2020 09:58:52 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 22 Sep 2020 22:13:34 +02:00

x86/irq: Make run_on_irqstack_cond() typesafe

Sami reported that run_on_irqstack_cond() requires the caller to cast functions to mismatching types, which trips indirect call Control-Flow Integrity (CFI) in Clang.

Instead of disabling CFI on that function, provide proper helpers for the three call variants. The actual ASM code stays the same as that is out of reach.

[ bp: Fix __run_on_irqstack() prototype to match. ]

---
 arch/x86/entry/common.c          |  2 +-
 arch/x86/entry/entry_64.S        |  2 +-
 arch/x86/include/asm/idtentry.h  |  2 +-
 arch/x86/include/asm/irq_stack.h | 69 +++++++++++++++++++++++++++----
 arch/x86/kernel/irq.c            |  2 +-
 arch/x86/kernel/irq_64.c         |  2 +-
 6 files changed, 67 insertions(+), 12 deletions(-)
arch/x86/entry/common.c +1 -1
index 2f84c7c..870efee 100644
@@ -299,7 +299,7 @@ __visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)
 	old_regs = set_irq_regs(regs);
 
 	instrumentation_begin();
-	run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, NULL, regs);
+	run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, regs);
 	instrumentation_begin();
 
 	set_irq_regs(old_regs);
arch/x86/entry/entry_64.S +2 -0
index 70dea93..d977079 100644
@@ -682,6 +682,8 @@ SYM_CODE_END(.Lbad_gs)
  * rdx: Function argument (can be NULL if none)
  */
 SYM_FUNC_START(asm_call_on_stack)
+SYM_INNER_LABEL(asm_call_sysvec_on_stack, SYM_L_GLOBAL)
+SYM_INNER_LABEL(asm_call_irq_on_stack, SYM_L_GLOBAL)
 	/*
 	 * Save the frame pointer unconditionally. This allows the ORC
 	 * unwinder to handle the stack switch.
arch/x86/include/asm/idtentry.h +1 -1
index a433661..a063864 100644
@@ -242,7 +242,7 @@ __visible noinstr void func(struct pt_regs *regs)			\
 	instrumentation_begin();					\
 	irq_enter_rcu();						\
 	kvm_set_cpu_l1tf_flush_l1d();					\
-	run_on_irqstack_cond(__##func, regs, regs);			\
+	run_sysvec_on_irqstack_cond(__##func, regs);			\
 	irq_exit_rcu();							\
 	instrumentation_end();						\
 	irqentry_exit(regs, state);					\
arch/x86/include/asm/irq_stack.h +61 -8

98 lines not on this page · raw message

arch/x86/kernel/irq.c +1 -1
index 1810602..c5dd503 100644
@@ -227,7 +227,7 @@ static __always_inline void handle_irq(struct irq_desc *desc,
 				       struct pt_regs *regs)
 {
 	if (IS_ENABLED(CONFIG_X86_64))
-		run_on_irqstack_cond(desc->handle_irq, desc, regs);
+		run_irq_on_irqstack_cond(desc->handle_irq, desc, regs);
 	else
 		__handle_irq(desc, regs);
 }
arch/x86/kernel/irq_64.c +1 -1
index 1b4fe93..440eed5 100644
@@ -74,5 +74,5 @@ int irq_init_percpu_irqstack(unsigned int cpu)
 
 void do_softirq_own_stack(void)
 {
-	run_on_irqstack_cond(__do_softirq, NULL, NULL);
+	run_on_irqstack_cond(__do_softirq, NULL);
 }
#3 lore raw
In reply to Thomas Gleixner

arch/x86/include/asm/irq_stack.h static __always_inline bool irqstack_act · line 19

Quoted 6 lines · Thomas Gleixner
arch/x86/include/asm/irq_stack.h +5 -1
-void asm_call_on_stack(void *sp, void *func, void *arg);
+void asm_call_on_stack(void *sp, void (*func)(void), void *arg);
+void asm_call_sysvec_on_stack(void *sp, void (*func)(struct pt_regs *regs),
+			      struct pt_regs *regs);
+void asm_call_irq_on_stack(void *sp, void (*func)(struct irq_desc *desc),
+			   struct irq_desc *desc);

Eeeh, err. So, this is nice for the CFI case, but can we instead just inline asm_call_on_stack() instead? Having any of these as distinct functions in the kernel is really not safe: it provides a trivial global stack-pivot[1] function for use in ROP attacks, which is one of the central requirements for mounting such attacks. This allows a completely arbitrary sp argument, function, and first argument. :(

Much better would be to keep asm_call_on_stack() as an inline so the stack pointer is always coming from percpu variables, and to have the irq_count actually checked (i.e. freak out if it falls below zero to catch jumps into the middle of a function when an attempt to bypass the load from the percpu area happens). I would expect this form to be much robust:

inc
load sp from per-cpu
pivot sp
make call
restore sp
WARN(dec_and_test)

-Kees

[1] https://security.stackexchange.com/questions/44418/return-oriented-programming-how-to-find-a-stack-pivot

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 22 lines · Kees Cook

arch/x86/include/asm/irq_stack.h static __always_inline bool irqstack_act · line 19

Quoted 6 lines · Thomas Gleixner
arch/x86/include/asm/irq_stack.h +5 -1
-void asm_call_on_stack(void *sp, void *func, void *arg);
+void asm_call_on_stack(void *sp, void (*func)(void), void *arg);
+void asm_call_sysvec_on_stack(void *sp, void (*func)(struct pt_regs *regs),
+                           struct pt_regs *regs);
+void asm_call_irq_on_stack(void *sp, void (*func)(struct irq_desc *desc),
+                        struct irq_desc *desc);

Eeeh, err. So, this is nice for the CFI case, but can we instead just inline asm_call_on_stack() instead? Having any of these as distinct functions in the kernel is really not safe: it provides a trivial global stack-pivot[1] function for use in ROP attacks, which is one of the central requirements for mounting such attacks. This allows a completely arbitrary sp argument, function, and first argument. :(

Much better would be to keep asm_call_on_stack() as an inline so the stack pointer is always coming from percpu variables, and to have the irq_count actually checked (i.e. freak out if it falls below zero to catch jumps into the middle of a function when an attempt to bypass the load from the percpu area happens). I would expect this form to be much robust:

Quoted 6 lines

inc
load sp from per-cpu
pivot sp
make call
restore sp
WARN(dec_and_test)

I don't see the point. If you can already jump to arbitrary kernel instructions, I would be extremely surprised if you could't find some other way to get full kernel read/write. Even just jumping to the epilogue of some function that increments the stack pointer and then tries to return (maybe even after loading RBP from that spot on the stack) will probably get you quite far.

#5 lore raw
In reply to Thomas Gleixner
I love your patch! Yet something to improve:

Hi Thomas,

I love your patch! Yet something to improve:

[auto build test ERROR on tip/master]
[also build test ERROR on tip/x86/asm linus/master v5.9-rc6] [cannot apply to tip/x86/core next-20200925]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Thomas-Gleixner/x86-irq-Make-run_on_irqstack_cond-typesafe/20200922-160014
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 45c1c667ec05586335a6eaecbcb95733ff196274
config: i386-randconfig-a002-20200921 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/65df891c3f7e34199a0abea30babb0667efe5b74
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Thomas-Gleixner/x86-irq-Make-run_on_irqstack_cond-typesafe/20200922-160014
        git checkout 65df891c3f7e34199a0abea30babb0667efe5b74
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from arch/x86/include/asm/idtentry.h:12,

                    from arch/x86/include/asm/traps.h:9,
                    from arch/x86/mm/fault.c:23:
   arch/x86/include/asm/irq_stack.h: In function 'run_on_irqstack_cond':
>> arch/x86/include/asm/irq_stack.h:77:3: error: too few arguments to function '__run_on_irqstack'
      77 |   __run_on_irqstack(func);
         |   ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/irq_stack.h:54:20: note: declared here
      54 | static inline void __run_on_irqstack(void (*func)(void), void *arg) { }
         |                    ^~~~~~~~~~~~~~~~~
--
   In file included from arch/x86/include/asm/idtentry.h:12,
                    from arch/x86/include/asm/traps.h:9,
                    from arch/x86/mm/extable.c:9:
   arch/x86/include/asm/irq_stack.h: In function 'run_on_irqstack_cond':
>> arch/x86/include/asm/irq_stack.h:77:3: error: too few arguments to function '__run_on_irqstack'
      77 |   __run_on_irqstack(func);
         |   ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/irq_stack.h:54:20: note: declared here
      54 | static inline void __run_on_irqstack(void (*func)(void), void *arg) { }
         |                    ^~~~~~~~~~~~~~~~~

arch/x86/mm/extable.c: At top level:
arch/x86/mm/extable.c:27:16: warning: no previous prototype for 'ex_handler_default' [-Wmissing-prototypes]
27 | __visible bool ex_handler_default(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:37:16: warning: no previous prototype for 'ex_handler_fault' [-Wmissing-prototypes]
37 | __visible bool ex_handler_fault(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:58:16: warning: no previous prototype for 'ex_handler_fprestore' [-Wmissing-prototypes]
58 | __visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:73:16: warning: no previous prototype for 'ex_handler_uaccess' [-Wmissing-prototypes]
73 | __visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:84:16: warning: no previous prototype for 'ex_handler_rdmsr_unsafe' [-Wmissing-prototypes]
84 | __visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:101:16: warning: no previous prototype for 'ex_handler_wrmsr_unsafe' [-Wmissing-prototypes]
101 | __visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:117:16: warning: no previous prototype for 'ex_handler_clear_fs' [-Wmissing-prototypes]
117 | __visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~
--
In file included from arch/x86/include/asm/idtentry.h:12,

                    from arch/x86/include/asm/traps.h:9,
                    from arch/x86/entry/common.c:29:
   arch/x86/include/asm/irq_stack.h: In function 'run_on_irqstack_cond':
>> arch/x86/include/asm/irq_stack.h:77:3: error: too few arguments to function '__run_on_irqstack'
      77 |   __run_on_irqstack(func);
         |   ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/irq_stack.h:54:20: note: declared here
      54 | static inline void __run_on_irqstack(void (*func)(void), void *arg) { }
         |                    ^~~~~~~~~~~~~~~~~

arch/x86/entry/common.c: At top level:
arch/x86/entry/common.c:84:24: warning: no previous prototype for 'do_int80_syscall_32' [-Wmissing-prototypes]
84 | __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
| ^~~~~~~~~~~~~~~~~~~
arch/x86/entry/common.c:143:24: warning: no previous prototype for 'do_fast_syscall_32' [-Wmissing-prototypes]
143 | __visible noinstr long do_fast_syscall_32(struct pt_regs *regs)
| ^~~~~~~~~~~~~~~~~~
arch/x86/entry/common.c:195:24: warning: no previous prototype for 'do_SYSENTER_32' [-Wmissing-prototypes]
195 | __visible noinstr long do_SYSENTER_32(struct pt_regs *regs)
| ^~~~~~~~~~~~~~
--
In file included from arch/x86/include/asm/idtentry.h:12,

                    from arch/x86/include/asm/traps.h:9,
                    from arch/x86/kernel/doublefault_32.c:11:
   arch/x86/include/asm/irq_stack.h: In function 'run_on_irqstack_cond':
>> arch/x86/include/asm/irq_stack.h:77:3: error: too few arguments to function '__run_on_irqstack'
      77 |   __run_on_irqstack(func);
         |   ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/irq_stack.h:54:20: note: declared here
      54 | static inline void __run_on_irqstack(void (*func)(void), void *arg) { }
         |                    ^~~~~~~~~~~~~~~~~

arch/x86/kernel/doublefault_32.c: At top level:
arch/x86/kernel/doublefault_32.c:23:36: warning: no previous prototype for 'doublefault_shim' [-Wmissing-prototypes]
23 | asmlinkage noinstr void __noreturn doublefault_shim(void)
| ^~~~~~~~~~~~~~~~
arch/x86/kernel/doublefault_32.c:119:6: warning: no previous prototype for 'doublefault_init_cpu_tss' [-Wmissing-prototypes]
119 | void doublefault_init_cpu_tss(void)
| ^~~~~~~~~~~~~~~~~~~~~~~~
--
In file included from arch/x86/include/asm/idtentry.h:12,

                    from arch/x86/include/asm/traps.h:9,
                    from arch/x86/kernel/kvm.c:31:
   arch/x86/include/asm/irq_stack.h: In function 'run_on_irqstack_cond':
>> arch/x86/include/asm/irq_stack.h:77:3: error: too few arguments to function '__run_on_irqstack'
      77 |   __run_on_irqstack(func);
         |   ^~~~~~~~~~~~~~~~~
   arch/x86/include/asm/irq_stack.h:54:20: note: declared here
      54 | static inline void __run_on_irqstack(void (*func)(void), void *arg) { }
         |                    ^~~~~~~~~~~~~~~~~

arch/x86/kernel/kvm.c: At top level:
arch/x86/kernel/kvm.c:876:16: warning: no previous prototype for '__kvm_vcpu_is_preempted' [-Wmissing-prototypes]
876 | __visible bool __kvm_vcpu_is_preempted(long cpu)
| ^~~~~~~~~~~~~~~~~~~~~~~

vim +/__run_on_irqstack +77 arch/x86/include/asm/irq_stack.h

    69	
    70	
    71	static __always_inline void run_on_irqstack_cond(void (*func)(void),
    72							 struct pt_regs *regs)
    73	{
    74		lockdep_assert_irqs_disabled();
    75	
    76		if (irq_needs_irq_stack(regs))
  > 77			__run_on_irqstack(func);
    78		else
    79			func();
    80	}
    81	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org