linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Avoid dereferening NULL regs in die()
@ 2022-09-20 20:00 Palmer Dabbelt
  2022-09-20 21:36 ` Conor Dooley
  2022-10-13  5:15 ` Palmer Dabbelt
  0 siblings, 2 replies; 3+ messages in thread
From: Palmer Dabbelt @ 2022-09-20 20:00 UTC (permalink / raw)
  To: linux-riscv; +Cc: Palmer Dabbelt, kernel test robot, Dan Carpenter

I don't think we can actually die() without a regs pointer, but the
compiler was warning about a NULL check after a dereference.  It seems
prudent to just avoid the possibly-NULL dereference, given that when
die()ing the system is already toast so who knows how we got there.

Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
---
 arch/riscv/kernel/traps.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 635e6ec26938..f3e96d60a2ff 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -33,6 +33,7 @@ void die(struct pt_regs *regs, const char *str)
 {
 	static int die_counter;
 	int ret;
+	long cause;
 
 	oops_enter();
 
@@ -42,11 +43,13 @@ void die(struct pt_regs *regs, const char *str)
 
 	pr_emerg("%s [#%d]\n", str, ++die_counter);
 	print_modules();
-	show_regs(regs);
+	if (regs)
+		show_regs(regs);
 
-	ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
+	cause = regs ? regs->cause : -1;
+	ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
 
-	if (regs && kexec_should_crash(current))
+	if (kexec_should_crash(current))
 		crash_kexec(regs);
 
 	bust_spinlocks(0);
-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH] RISC-V: Avoid dereferening NULL regs in die()
  2022-09-20 20:00 [PATCH] RISC-V: Avoid dereferening NULL regs in die() Palmer Dabbelt
@ 2022-09-20 21:36 ` Conor Dooley
  2022-10-13  5:15 ` Palmer Dabbelt
  1 sibling, 0 replies; 3+ messages in thread
From: Conor Dooley @ 2022-09-20 21:36 UTC (permalink / raw)
  To: Palmer Dabbelt; +Cc: linux-riscv, kernel test robot, Dan Carpenter

On Tue, Sep 20, 2022 at 01:00:37PM -0700, Palmer Dabbelt wrote:
> I don't think we can actually die() without a regs pointer, but the
> compiler was warning about a NULL check after a dereference.  It seems
> prudent to just avoid the possibly-NULL dereference, given that when
> die()ing the system is already toast so who knows how we got there.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>

I excitedly tried this in the hopes that I was actually hitting this
somehow with the crash-during-boot-with-no-splat that I am currently
bisecting, but sadly it was not the solution.

Either way it looks like a sane change so we at least can get something
out in an edge failure case.

> ---
>  arch/riscv/kernel/traps.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index 635e6ec26938..f3e96d60a2ff 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -33,6 +33,7 @@ void die(struct pt_regs *regs, const char *str)
>  {
>  	static int die_counter;
>  	int ret;
> +	long cause;
>  
>  	oops_enter();
>  
> @@ -42,11 +43,13 @@ void die(struct pt_regs *regs, const char *str)
>  
>  	pr_emerg("%s [#%d]\n", str, ++die_counter);
>  	print_modules();
> -	show_regs(regs);
> +	if (regs)
> +		show_regs(regs);
>  
> -	ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
> +	cause = regs ? regs->cause : -1;
> +	ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
>  
> -	if (regs && kexec_should_crash(current))
> +	if (kexec_should_crash(current))
>  		crash_kexec(regs);

Had to go down the rabbit hole to see that there is an if(regs) around
the eventual user of this, so:
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>

>  
>  	bust_spinlocks(0);
> -- 
> 2.34.1
> 
> 
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH] RISC-V: Avoid dereferening NULL regs in die()
  2022-09-20 20:00 [PATCH] RISC-V: Avoid dereferening NULL regs in die() Palmer Dabbelt
  2022-09-20 21:36 ` Conor Dooley
@ 2022-10-13  5:15 ` Palmer Dabbelt
  1 sibling, 0 replies; 3+ messages in thread
From: Palmer Dabbelt @ 2022-10-13  5:15 UTC (permalink / raw)
  To: Palmer Dabbelt, linux-riscv; +Cc: Dan Carpenter, kernel test robot

On Tue, 20 Sep 2022 13:00:37 -0700, Palmer Dabbelt wrote:
> I don't think we can actually die() without a regs pointer, but the
> compiler was warning about a NULL check after a dereference.  It seems
> prudent to just avoid the possibly-NULL dereference, given that when
> die()ing the system is already toast so who knows how we got there.
> 
> 

Applied, thanks!

[1/1] RISC-V: Avoid dereferening NULL regs in die()
      https://git.kernel.org/palmer/c/f2913d006fcd

Best regards,
-- 
Palmer Dabbelt <palmer@rivosinc.com>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

end of thread, other threads:[~2022-10-13  5:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-20 20:00 [PATCH] RISC-V: Avoid dereferening NULL regs in die() Palmer Dabbelt
2022-09-20 21:36 ` Conor Dooley
2022-10-13  5:15 ` Palmer Dabbelt

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).