linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ARM: fix race in for_each_frame
@ 2019-11-12 13:29 Mark-PK Tsai
  2019-11-12 14:14 ` 黄吕强 (Lvqiang Huang)
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Mark-PK Tsai @ 2019-11-12 13:29 UTC (permalink / raw)
  To: linux
  Cc: matthias.bgg, kstewart, allison, lvqiang.huang, gregkh, info,
	tglx, linux-arm-kernel, linux-kernel, linux-mediatek, yj.chiang,
	mark-pk.tsai, alix.wu, mike-sl.lin, eddy.lin, phil.chang

The sv_pc, which is saved in the stack, may be an invalid address
if the target thread is running on another processor in the meantime.
It will cause kernel crash at `ldr r2, [sv_pc, #-4]`.

Check if sv_pc is valid before use it like unwind_frame in
arch/arm/kernel/unwind.c.

Signed-off-by: Mike-SL Lin <mike-sl.lin@mediatek.com>
Signed-off-by: Mark-PK Tsai <mark-pk.tsai@mediatek.com>
---
 arch/arm/lib/backtrace.S | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/lib/backtrace.S b/arch/arm/lib/backtrace.S
index 582925238d65..84f06381bbfb 100644
--- a/arch/arm/lib/backtrace.S
+++ b/arch/arm/lib/backtrace.S
@@ -64,6 +64,11 @@ for_each_frame:	tst	frame, mask		@ Check for address exceptions
 		sub	sv_pc, sv_pc, offset	@ Correct PC for prefetching
 		bic	sv_pc, sv_pc, mask	@ mask PC/LR for the mode
 
+		mov	r0, sv_pc
+		bl	kernel_text_address	@ check if sv_pc is valid
+		cmp	r0, #0			@ if sv_pc is not kernel text
+		beq	1006f			@ address, abort backtrace
+
 1003:		ldr	r2, [sv_pc, #-4]	@ if stmfd sp!, {args} exists,
 		ldr	r3, .Ldsi+4		@ adjust saved 'pc' back one
 		teq	r3, r2, lsr #11		@ instruction
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ARM: fix race in for_each_frame
  2019-11-12 13:29 [PATCH] ARM: fix race in for_each_frame Mark-PK Tsai
@ 2019-11-12 14:14 ` 黄吕强 (Lvqiang Huang)
  2019-11-12 14:35 ` 黄吕强 (Lvqiang Huang)
  2019-11-12 15:20 ` Mark-PK Tsai
  2 siblings, 0 replies; 5+ messages in thread
From: 黄吕强 (Lvqiang Huang) @ 2019-11-12 14:14 UTC (permalink / raw)
  To: Mark-PK Tsai
  Cc: linux, matthias.bgg, kstewart, allison, gregkh, info, tglx,
	linux-arm-kernel, linux-kernel, linux-mediatek, yj.chiang,
	alix.wu, mike-sl.lin, eddy.lin, phil.chang,
	楚恩来 (Enlai Chu)


> 在 2019年11月12日,21:31,Mark-PK Tsai <mark-pk.tsai@mediatek.com> 写道:
> 
> The sv_pc, which is saved in the stack, may be an invalid address
> if the target thread is running on another processor in the meantime.
> It will cause kernel crash at `ldr r2, [sv_pc, #-4]`.
> 
> Check if sv_pc is valid before use it like unwind_frame in
> arch/arm/kernel/unwind.c.
> 
> Signed-off-by: Mike-SL Lin <mike-sl.lin@mediatek.com>
> Signed-off-by: Mark-PK Tsai <mark-pk.tsai@mediatek.com>
> ---
> arch/arm/lib/backtrace.S | 5 +++++
> 1 file changed, 5 insertions(+)
> 
> diff --git a/arch/arm/lib/backtrace.S b/arch/arm/lib/backtrace.S
> index 582925238d65..84f06381bbfb 100644
> --- a/arch/arm/lib/backtrace.S
> +++ b/arch/arm/lib/backtrace.S
> @@ -64,6 +64,11 @@ for_each_frame:    tst    frame, mask        @ Check for address exceptions
>        sub    sv_pc, sv_pc, offset    @ Correct PC for prefetching
>        bic    sv_pc, sv_pc, mask    @ mask PC/LR for the mode
> 
> +        mov    r0, sv_pc
> +        bl    kernel_text_address    @ check if sv_pc is valid
> +        cmp    r0, #0            @ if sv_pc is not kernel text
> +        beq    1006f            @ address, abort backtrace
> +

The sv_pc can be a kernel module text. 

> 1003:        ldr    r2, [sv_pc, #-4]    @ if stmfd sp!, {args} exists,
>        ldr    r3, .Ldsi+4        @ adjust saved 'pc' back one
>        teq    r3, r2, lsr #11        @ instruction
> -- 
> 2.18.0


============================================================================
This email (including its attachments) is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. Unauthorized use, dissemination, distribution or copying of this email or the information herein or taking any action in reliance on the contents of this email or the information herein, by anyone other than the intended recipient, or an employee or agent responsible for delivering the message to the intended recipient, is strictly prohibited. If you are not the intended recipient, please do not read, copy, use or disclose any part of this e-mail to others. Please notify the sender immediately and permanently delete this e-mail and any attachments if you received it in error. Internet communications cannot be guaranteed to be timely, secure, error-free or virus-free. The sender does not accept liability for any errors or omissions. 
本邮件及其附件具有保密性质,受法律保护不得泄露,仅发送给本邮件所指特定收件人。严禁非经授权使用、宣传、发布或复制本邮件或其内容。若非该特定收件人,请勿阅读、复制、 使用或披露本邮件的任何内容。若误收本邮件,请从系统中永久性删除本邮件及所有附件,并以回复邮件的方式即刻告知发件人。无法保证互联网通信及时、安全、无误或防毒。发件人对任何错漏均不承担责任。

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ARM: fix race in for_each_frame
  2019-11-12 13:29 [PATCH] ARM: fix race in for_each_frame Mark-PK Tsai
  2019-11-12 14:14 ` 黄吕强 (Lvqiang Huang)
@ 2019-11-12 14:35 ` 黄吕强 (Lvqiang Huang)
  2019-11-13  3:25   ` Mark-PK Tsai
  2019-11-12 15:20 ` Mark-PK Tsai
  2 siblings, 1 reply; 5+ messages in thread
From: 黄吕强 (Lvqiang Huang) @ 2019-11-12 14:35 UTC (permalink / raw)
  To: Mark-PK Tsai
  Cc: linux, matthias.bgg, kstewart, allison, gregkh, info, tglx,
	linux-arm-kernel, linux-kernel, linux-mediatek, yj.chiang,
	alix.wu, mike-sl.lin, eddy.lin, phil.chang,
	楚恩来 (Enlai Chu)

Dear Mark,
Thanks a lot for the reply. 

As said in last reply, sv_pc can be a module text, then more check needed.

And beside crash at 1003, we may also get crash at 1001, the frame is invalid. (The last sv_pv is valid and sv_frame is invalid), then more check needed. 

And we often show_data around the general proposal registers when kernel crash. When they contain an address mapping for a hw register but can’t access because clock gated, it will crash again because do_bad() is involved. (continuous crash in arm and hang at die_lock in arm64)

So, why not check the __ex_table in do_bad() ?

> 在 2019年11月12日,21:31,Mark-PK Tsai <mark-pk.tsai@mediatek.com> 写道:
> 
> The sv_pc, which is saved in the stack, may be an invalid address
> if the target thread is running on another processor in the meantime.
> It will cause kernel crash at `ldr r2, [sv_pc, #-4]`.
> 
> Check if sv_pc is valid before use it like unwind_frame in
> arch/arm/kernel/unwind.c.
> 
> Signed-off-by: Mike-SL Lin <mike-sl.lin@mediatek.com>
> Signed-off-by: Mark-PK Tsai <mark-pk.tsai@mediatek.com>
> ---
> arch/arm/lib/backtrace.S | 5 +++++
> 1 file changed, 5 insertions(+)
> 
> diff --git a/arch/arm/lib/backtrace.S b/arch/arm/lib/backtrace.S
> index 582925238d65..84f06381bbfb 100644
> --- a/arch/arm/lib/backtrace.S
> +++ b/arch/arm/lib/backtrace.S
> @@ -64,6 +64,11 @@ for_each_frame:    tst    frame, mask        @ Check for address exceptions
>        sub    sv_pc, sv_pc, offset    @ Correct PC for prefetching
>        bic    sv_pc, sv_pc, mask    @ mask PC/LR for the mode
> 
> +        mov    r0, sv_pc
> +        bl    kernel_text_address    @ check if sv_pc is valid
> +        cmp    r0, #0            @ if sv_pc is not kernel text
> +        beq    1006f            @ address, abort backtrace
> +
> 1003:        ldr    r2, [sv_pc, #-4]    @ if stmfd sp!, {args} exists,
>        ldr    r3, .Ldsi+4        @ adjust saved 'pc' back one
>        teq    r3, r2, lsr #11        @ instruction
> -- 
> 2.18.0


============================================================================
This email (including its attachments) is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. Unauthorized use, dissemination, distribution or copying of this email or the information herein or taking any action in reliance on the contents of this email or the information herein, by anyone other than the intended recipient, or an employee or agent responsible for delivering the message to the intended recipient, is strictly prohibited. If you are not the intended recipient, please do not read, copy, use or disclose any part of this e-mail to others. Please notify the sender immediately and permanently delete this e-mail and any attachments if you received it in error. Internet communications cannot be guaranteed to be timely, secure, error-free or virus-free. The sender does not accept liability for any errors or omissions. 
本邮件及其附件具有保密性质,受法律保护不得泄露,仅发送给本邮件所指特定收件人。严禁非经授权使用、宣传、发布或复制本邮件或其内容。若非该特定收件人,请勿阅读、复制、 使用或披露本邮件的任何内容。若误收本邮件,请从系统中永久性删除本邮件及所有附件,并以回复邮件的方式即刻告知发件人。无法保证互联网通信及时、安全、无误或防毒。发件人对任何错漏均不承担责任。

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ARM: fix race in for_each_frame
  2019-11-12 13:29 [PATCH] ARM: fix race in for_each_frame Mark-PK Tsai
  2019-11-12 14:14 ` 黄吕强 (Lvqiang Huang)
  2019-11-12 14:35 ` 黄吕强 (Lvqiang Huang)
@ 2019-11-12 15:20 ` Mark-PK Tsai
  2 siblings, 0 replies; 5+ messages in thread
From: Mark-PK Tsai @ 2019-11-12 15:20 UTC (permalink / raw)
  To: lvqiang.huang
  Cc: alix.wu, allison, eddy.lin, enlai.chu, gregkh, info, kstewart,
	linux-arm-kernel, linux-kernel, linux-mediatek, linux,
	mark-pk.tsai, matthias.bgg, mike-sl.lin, phil.chang, tglx,
	yj.chiang


> . 2019.11.12..21:31.Mark-PK Tsai <mark-pk.tsai@mediatek.com> ...
> 
> The sv_pc, which is saved in the stack, may be an invalid address
> if the target thread is running on another processor in the meantime.
> It will cause kernel crash at `ldr r2, [sv_pc, #-4]`.
> 
> Check if sv_pc is valid before use it like unwind_frame in
> arch/arm/kernel/unwind.c.
> 
> Signed-off-by: Mike-SL Lin <mike-sl.lin@mediatek.com>
> Signed-off-by: Mark-PK Tsai <mark-pk.tsai@mediatek.com>
> ---
> arch/arm/lib/backtrace.S | 5 +++++
> 1 file changed, 5 insertions(+)
> 
> diff --git a/arch/arm/lib/backtrace.S b/arch/arm/lib/backtrace.S
> index 582925238d65..84f06381bbfb 100644
> --- a/arch/arm/lib/backtrace.S
> +++ b/arch/arm/lib/backtrace.S
> @@ -64,6 +64,11 @@ for_each_frame:    tst    frame, mask        @ Check for address exceptions
>        sub    sv_pc, sv_pc, offset    @ Correct PC for prefetching
>        bic    sv_pc, sv_pc, mask    @ mask PC/LR for the mode
> 
> +        mov    r0, sv_pc
> +        bl    kernel_text_address    @ check if sv_pc is valid
> +        cmp    r0, #0            @ if sv_pc is not kernel text
> +        beq    1006f            @ address, abort backtrace
> +

The sv_pc can be a kernel module text. 

The module text area is ok for kernel_text_address().

> 1003:        ldr    r2, [sv_pc, #-4]    @ if stmfd sp!, {args} exists,
>        ldr    r3, .Ldsi+4        @ adjust saved 'pc' back one
>        teq    r3, r2, lsr #11        @ instruction
> -- 
> 2.18.0

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ARM: fix race in for_each_frame
  2019-11-12 14:35 ` 黄吕强 (Lvqiang Huang)
@ 2019-11-13  3:25   ` Mark-PK Tsai
  0 siblings, 0 replies; 5+ messages in thread
From: Mark-PK Tsai @ 2019-11-13  3:25 UTC (permalink / raw)
  To: lvqiang.huang
  Cc: alix.wu, allison, eddy.lin, enlai.chu, gregkh, info, kstewart,
	linux-arm-kernel, linux-kernel, linux-mediatek, linux,
	mark-pk.tsai, matthias.bgg, mike-sl.lin, phil.chang, tglx,
	yj.chiang

> Dear Mark,
> Thanks a lot for the reply. 
> 
> As said in last reply, sv_pc can be a module text, then more check needed.

If sv_pc is in module text area, kernel_text_address() returns true.

> 
> And beside crash at 1003, we may also get crash at 1001, the frame is invalid. (The last sv_pv is valid and sv_frame is invalid), then more check needed. 

There's a basic check for sv_fp at the end of 1004.
But I'm not sure is it enough to prevent the 1001 crash you mentioned.
Should we add a verify_stack for sv_fp?

> 
> And we often show_data around the general proposal registers when kernel crash. When they contain an address mapping for a hw register but can’t access because clock gated, it will crash again because do_bad() is involved. (continuous crash in arm and hang at die_lock in arm64)
> 
> So, why not check the __ex_table in do_bad() ?
> 

On our arm platform, kernel just die becuase the Unhandled fault in for_each_frame.
So I'd rather to fix it before the continuous crash happen.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-11-13  3:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-12 13:29 [PATCH] ARM: fix race in for_each_frame Mark-PK Tsai
2019-11-12 14:14 ` 黄吕强 (Lvqiang Huang)
2019-11-12 14:35 ` 黄吕强 (Lvqiang Huang)
2019-11-13  3:25   ` Mark-PK Tsai
2019-11-12 15:20 ` Mark-PK Tsai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).