linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init()
@ 2023-03-21  6:14 Harshit Mogalapalli
  2023-03-21  8:01 ` Greg Kroah-Hartman
  2023-03-29 21:59 ` Jarkko Sakkinen
  0 siblings, 2 replies; 4+ messages in thread
From: Harshit Mogalapalli @ 2023-03-21  6:14 UTC (permalink / raw)
  Cc: error27, Harshit Mogalapalli, Peter Huewe, Jarkko Sakkinen,
	Jason Gunthorpe, Greg Kroah-Hartman, linux-integrity,
	linux-kernel

Smatch reports:
	drivers/char/tpm/tpm-interface.c:470 tpm_init() error:
	'tpm_class' dereferencing possible ERR_PTR()

If class_create() returns error pointer, we are dereferencing a possible
error pointer. Fix this by moving the dereference post error handling.

Fixes: a010eb881243 ("tpm: fix up the tpm_class shutdown_pre pointer when created")
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
This is based on static analysis, only compile tested.
---
 drivers/char/tpm/tpm-interface.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 43e23a04433a..4463d0018290 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -467,12 +467,13 @@ static int __init tpm_init(void)
 	int rc;
 
 	tpm_class = class_create("tpm");
-	tpm_class->shutdown_pre = tpm_class_shutdown;
 	if (IS_ERR(tpm_class)) {
 		pr_err("couldn't create tpm class\n");
 		return PTR_ERR(tpm_class);
 	}
 
+	tpm_class->shutdown_pre = tpm_class_shutdown;
+
 	tpmrm_class = class_create("tpmrm");
 	if (IS_ERR(tpmrm_class)) {
 		pr_err("couldn't create tpmrm class\n");
-- 
2.38.1


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

* Re: [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init()
  2023-03-21  6:14 [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init() Harshit Mogalapalli
@ 2023-03-21  8:01 ` Greg Kroah-Hartman
  2023-03-29 21:59   ` Jarkko Sakkinen
  2023-03-29 21:59 ` Jarkko Sakkinen
  1 sibling, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2023-03-21  8:01 UTC (permalink / raw)
  To: Harshit Mogalapalli
  Cc: error27, Peter Huewe, Jarkko Sakkinen, Jason Gunthorpe,
	linux-integrity, linux-kernel

On Mon, Mar 20, 2023 at 11:14:15PM -0700, Harshit Mogalapalli wrote:
> Smatch reports:
> 	drivers/char/tpm/tpm-interface.c:470 tpm_init() error:
> 	'tpm_class' dereferencing possible ERR_PTR()
> 
> If class_create() returns error pointer, we are dereferencing a possible
> error pointer. Fix this by moving the dereference post error handling.
> 
> Fixes: a010eb881243 ("tpm: fix up the tpm_class shutdown_pre pointer when created")
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
> This is based on static analysis, only compile tested.

Thanks for this, good catch, I've queued it up in my tree now as I have
the offending change that caused this.

greg k-h

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

* Re: [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init()
  2023-03-21  6:14 [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init() Harshit Mogalapalli
  2023-03-21  8:01 ` Greg Kroah-Hartman
@ 2023-03-29 21:59 ` Jarkko Sakkinen
  1 sibling, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2023-03-29 21:59 UTC (permalink / raw)
  To: Harshit Mogalapalli
  Cc: error27, Peter Huewe, Jason Gunthorpe, Greg Kroah-Hartman,
	linux-integrity, linux-kernel

On Mon, Mar 20, 2023 at 11:14:15PM -0700, Harshit Mogalapalli wrote:
> Smatch reports:
> 	drivers/char/tpm/tpm-interface.c:470 tpm_init() error:
> 	'tpm_class' dereferencing possible ERR_PTR()
> 
> If class_create() returns error pointer, we are dereferencing a possible
> error pointer. Fix this by moving the dereference post error handling.
> 
> Fixes: a010eb881243 ("tpm: fix up the tpm_class shutdown_pre pointer when created")
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
> This is based on static analysis, only compile tested.
> ---
>  drivers/char/tpm/tpm-interface.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
> index 43e23a04433a..4463d0018290 100644
> --- a/drivers/char/tpm/tpm-interface.c
> +++ b/drivers/char/tpm/tpm-interface.c
> @@ -467,12 +467,13 @@ static int __init tpm_init(void)
>  	int rc;
>  
>  	tpm_class = class_create("tpm");
> -	tpm_class->shutdown_pre = tpm_class_shutdown;
>  	if (IS_ERR(tpm_class)) {
>  		pr_err("couldn't create tpm class\n");
>  		return PTR_ERR(tpm_class);
>  	}
>  
> +	tpm_class->shutdown_pre = tpm_class_shutdown;
> +
>  	tpmrm_class = class_create("tpmrm");
>  	if (IS_ERR(tpmrm_class)) {
>  		pr_err("couldn't create tpmrm class\n");
> -- 
> 2.38.1
> 


Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko

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

* Re: [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init()
  2023-03-21  8:01 ` Greg Kroah-Hartman
@ 2023-03-29 21:59   ` Jarkko Sakkinen
  0 siblings, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2023-03-29 21:59 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Harshit Mogalapalli, error27, Peter Huewe, Jason Gunthorpe,
	linux-integrity, linux-kernel

On Tue, Mar 21, 2023 at 09:01:08AM +0100, Greg Kroah-Hartman wrote:
> On Mon, Mar 20, 2023 at 11:14:15PM -0700, Harshit Mogalapalli wrote:
> > Smatch reports:
> > 	drivers/char/tpm/tpm-interface.c:470 tpm_init() error:
> > 	'tpm_class' dereferencing possible ERR_PTR()
> > 
> > If class_create() returns error pointer, we are dereferencing a possible
> > error pointer. Fix this by moving the dereference post error handling.
> > 
> > Fixes: a010eb881243 ("tpm: fix up the tpm_class shutdown_pre pointer when created")
> > Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> > ---
> > This is based on static analysis, only compile tested.
> 
> Thanks for this, good catch, I've queued it up in my tree now as I have
> the offending change that caused this.

OK, thank you!

BR, Jarkko

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

end of thread, other threads:[~2023-03-29 21:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-21  6:14 [PATCH next] tpm: Fix a possible dereference of ERR_PTR in tpm_init() Harshit Mogalapalli
2023-03-21  8:01 ` Greg Kroah-Hartman
2023-03-29 21:59   ` Jarkko Sakkinen
2023-03-29 21:59 ` Jarkko Sakkinen

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