From mboxrd@z Thu Jan 1 00:00:00 1970 From: Russell King - ARM Linux Subject: Re: [PATCH v2 4/4] arm64/syscalls: Specific usage of verify_pre_usermode_state Date: Thu, 9 Mar 2017 17:05:53 +0000 Message-ID: <20170309170553.GO21222@n2100.armlinux.org.uk> References: <20170309012456.5631-1-thgarnie@google.com> <20170309012456.5631-4-thgarnie@google.com> <20170309122354.GB6320@leverpostej> <20170309162613.GN21222@n2100.armlinux.org.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: List-Post: List-Help: List-Unsubscribe: List-Subscribe: Content-Disposition: inline In-Reply-To: Sender: Russell King - ARM Linux To: Thomas Garnier Cc: Mark Rutland , David Howells , Dave Hansen , Arnd Bergmann , Al Viro , =?iso-8859-1?Q?Ren=E9?= Nyffenegger , Andrew Morton , Kees Cook , "Paul E . McKenney" , "David S . Miller" , Andy Lutomirski , Ard Biesheuvel , Nicolas Pitre , Petr Mladek , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , Ingo Molnar , Oleg Nesterov , John Stultz , Thomas Gleixner , Pavel List-Id: linux-api@vger.kernel.org On Thu, Mar 09, 2017 at 08:35:30AM -0800, Thomas Garnier wrote: > On Thu, Mar 9, 2017 at 8:26 AM, Russell King - ARM Linux > wrote: > > I wouldn't call what you've done on ARM an "optimisation", because my > > comment about making the fast path worthless still stands. > > Why does it still stands on the latest proposal? It's still having to needlessly save stuff when there's nothing wrong. Remember, syscalls are a fast path, so the minimum we can do is good. Calling into C functions is not ideal, because they will tend to be _very_ expensive compared to hand crafted assembly, especially for something like this. It's possible to check the address limit in just three instructions, which is way less than will be incurred by calling a C function. Note: This patch is completely untested. arch/arm/kernel/entry-common.S | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index eb5cd77bf1d8..6b43c6d73117 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -41,8 +41,11 @@ UNWIND(.cantunwind ) disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK bne fast_work_pending + cmp r2, #TASK_SIZE + blgt addr_limit_fail /* perform architecture specific actions before user return */ arch_ret_to_user r1, lr @@ -67,6 +70,7 @@ ENDPROC(ret_fast_syscall) str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK beq no_work_pending UNWIND(.fnend ) @@ -82,6 +86,7 @@ ENDPROC(ret_fast_syscall) mov r2, why @ 'syscall' bl do_work_pending cmp r0, #0 + ldreq r2, [tsk, #TI_ADDR_LIMIT] beq no_work_pending movlt scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE) ldmia sp, {r0 - r6} @ have to reload r0 - r6 @@ -99,9 +104,12 @@ ENTRY(ret_to_user) disable_irq_notrace @ disable interrupts ENTRY(ret_to_user_from_irq) ldr r1, [tsk, #TI_FLAGS] + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_WORK_MASK bne slow_work_pending no_work_pending: + cmp r2, #TASK_SIZE + blgt addr_limit_fail asm_trace_hardirqs_on save = 0 /* perform architecture specific actions before user return */ @@ -125,6 +133,16 @@ ENTRY(ret_from_fork) b ret_slow_syscall ENDPROC(ret_from_fork) +addr_limit_fail: +#ifdef CONFIG_BUG_ON_DATA_CORRUPTION + stmfd sp!, {r0, lr} + bl verify_pre_usermode_state + ldmfd sp!, {r0, lr} +#endif + mov r2, #TASK_SIZE + str r2, [tsk, #TI_ADDR_LIMIT] + ret lr + /*============================================================================= * SWI handler *----------------------------------------------------------------------------- -- RMK's Patch system: http://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up according to speedtest.net. From mboxrd@z Thu Jan 1 00:00:00 1970 From: linux@armlinux.org.uk (Russell King - ARM Linux) Date: Thu, 9 Mar 2017 17:05:53 +0000 Subject: [PATCH v2 4/4] arm64/syscalls: Specific usage of verify_pre_usermode_state In-Reply-To: References: <20170309012456.5631-1-thgarnie@google.com> <20170309012456.5631-4-thgarnie@google.com> <20170309122354.GB6320@leverpostej> <20170309162613.GN21222@n2100.armlinux.org.uk> Message-ID: <20170309170553.GO21222@n2100.armlinux.org.uk> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org On Thu, Mar 09, 2017 at 08:35:30AM -0800, Thomas Garnier wrote: > On Thu, Mar 9, 2017 at 8:26 AM, Russell King - ARM Linux > wrote: > > I wouldn't call what you've done on ARM an "optimisation", because my > > comment about making the fast path worthless still stands. > > Why does it still stands on the latest proposal? It's still having to needlessly save stuff when there's nothing wrong. Remember, syscalls are a fast path, so the minimum we can do is good. Calling into C functions is not ideal, because they will tend to be _very_ expensive compared to hand crafted assembly, especially for something like this. It's possible to check the address limit in just three instructions, which is way less than will be incurred by calling a C function. Note: This patch is completely untested. arch/arm/kernel/entry-common.S | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index eb5cd77bf1d8..6b43c6d73117 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -41,8 +41,11 @@ UNWIND(.cantunwind ) disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK bne fast_work_pending + cmp r2, #TASK_SIZE + blgt addr_limit_fail /* perform architecture specific actions before user return */ arch_ret_to_user r1, lr @@ -67,6 +70,7 @@ ENDPROC(ret_fast_syscall) str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK beq no_work_pending UNWIND(.fnend ) @@ -82,6 +86,7 @@ ENDPROC(ret_fast_syscall) mov r2, why @ 'syscall' bl do_work_pending cmp r0, #0 + ldreq r2, [tsk, #TI_ADDR_LIMIT] beq no_work_pending movlt scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE) ldmia sp, {r0 - r6} @ have to reload r0 - r6 @@ -99,9 +104,12 @@ ENTRY(ret_to_user) disable_irq_notrace @ disable interrupts ENTRY(ret_to_user_from_irq) ldr r1, [tsk, #TI_FLAGS] + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_WORK_MASK bne slow_work_pending no_work_pending: + cmp r2, #TASK_SIZE + blgt addr_limit_fail asm_trace_hardirqs_on save = 0 /* perform architecture specific actions before user return */ @@ -125,6 +133,16 @@ ENTRY(ret_from_fork) b ret_slow_syscall ENDPROC(ret_from_fork) +addr_limit_fail: +#ifdef CONFIG_BUG_ON_DATA_CORRUPTION + stmfd sp!, {r0, lr} + bl verify_pre_usermode_state + ldmfd sp!, {r0, lr} +#endif + mov r2, #TASK_SIZE + str r2, [tsk, #TI_ADDR_LIMIT] + ret lr + /*============================================================================= * SWI handler *----------------------------------------------------------------------------- -- RMK's Patch system: http://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up according to speedtest.net. From mboxrd@z Thu Jan 1 00:00:00 1970 Date: Thu, 9 Mar 2017 17:05:53 +0000 From: Russell King - ARM Linux Message-ID: <20170309170553.GO21222@n2100.armlinux.org.uk> References: <20170309012456.5631-1-thgarnie@google.com> <20170309012456.5631-4-thgarnie@google.com> <20170309122354.GB6320@leverpostej> <20170309162613.GN21222@n2100.armlinux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: Russell King - ARM Linux Subject: [kernel-hardening] Re: [PATCH v2 4/4] arm64/syscalls: Specific usage of verify_pre_usermode_state To: Thomas Garnier Cc: Mark Rutland , David Howells , Dave Hansen , Arnd Bergmann , Al Viro , =?iso-8859-1?Q?Ren=E9?= Nyffenegger , Andrew Morton , Kees Cook , "Paul E . McKenney" , "David S . Miller" , Andy Lutomirski , Ard Biesheuvel , Nicolas Pitre , Petr Mladek , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , Ingo Molnar , Oleg Nesterov , John Stultz , Thomas Gleixner , Pavel Tikhomirov , Frederic Weisbecker , Stephen Smalley , Stanislav Kinsburskiy , Ingo Molnar , "H . Peter Anvin" , Paolo Bonzini , Borislav Petkov , Josh Poimboeuf , Brian Gerst , Jan Beulich , Christian Borntraeger , "Luis R . Rodriguez" , He Chen , Will Deacon , Catalin Marinas , James Morse , Pratyush Anand , Vladimir Murzin , Chris Metcalf , Andre Przywara , Linux API , LKML , the arch/x86 maintainers , linux-arm-kernel@lists.infradead.org, Kernel Hardening List-ID: On Thu, Mar 09, 2017 at 08:35:30AM -0800, Thomas Garnier wrote: > On Thu, Mar 9, 2017 at 8:26 AM, Russell King - ARM Linux > wrote: > > I wouldn't call what you've done on ARM an "optimisation", because my > > comment about making the fast path worthless still stands. > > Why does it still stands on the latest proposal? It's still having to needlessly save stuff when there's nothing wrong. Remember, syscalls are a fast path, so the minimum we can do is good. Calling into C functions is not ideal, because they will tend to be _very_ expensive compared to hand crafted assembly, especially for something like this. It's possible to check the address limit in just three instructions, which is way less than will be incurred by calling a C function. Note: This patch is completely untested. arch/arm/kernel/entry-common.S | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index eb5cd77bf1d8..6b43c6d73117 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -41,8 +41,11 @@ UNWIND(.cantunwind ) disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK bne fast_work_pending + cmp r2, #TASK_SIZE + blgt addr_limit_fail /* perform architecture specific actions before user return */ arch_ret_to_user r1, lr @@ -67,6 +70,7 @@ ENDPROC(ret_fast_syscall) str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 disable_irq_notrace @ disable interrupts ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK beq no_work_pending UNWIND(.fnend ) @@ -82,6 +86,7 @@ ENDPROC(ret_fast_syscall) mov r2, why @ 'syscall' bl do_work_pending cmp r0, #0 + ldreq r2, [tsk, #TI_ADDR_LIMIT] beq no_work_pending movlt scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE) ldmia sp, {r0 - r6} @ have to reload r0 - r6 @@ -99,9 +104,12 @@ ENTRY(ret_to_user) disable_irq_notrace @ disable interrupts ENTRY(ret_to_user_from_irq) ldr r1, [tsk, #TI_FLAGS] + ldr r2, [tsk, #TI_ADDR_LIMIT] tst r1, #_TIF_WORK_MASK bne slow_work_pending no_work_pending: + cmp r2, #TASK_SIZE + blgt addr_limit_fail asm_trace_hardirqs_on save = 0 /* perform architecture specific actions before user return */ @@ -125,6 +133,16 @@ ENTRY(ret_from_fork) b ret_slow_syscall ENDPROC(ret_from_fork) +addr_limit_fail: +#ifdef CONFIG_BUG_ON_DATA_CORRUPTION + stmfd sp!, {r0, lr} + bl verify_pre_usermode_state + ldmfd sp!, {r0, lr} +#endif + mov r2, #TASK_SIZE + str r2, [tsk, #TI_ADDR_LIMIT] + ret lr + /*============================================================================= * SWI handler *----------------------------------------------------------------------------- -- RMK's Patch system: http://www.armlinux.org.uk/developer/patches/ FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up according to speedtest.net.