linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: libfc: Fix a NULL pointer dereference in fc_lport_ptp_setup()
@ 2021-11-30 17:10 Zhou Qingyang
  2021-11-30 18:13 ` James Bottomley
  2021-12-01  6:41 ` Hannes Reinecke
  0 siblings, 2 replies; 3+ messages in thread
From: Zhou Qingyang @ 2021-11-30 17:10 UTC (permalink / raw)
  To: zhou1615
  Cc: kjlu, Hannes Reinecke, James E.J. Bottomley, Martin K. Petersen,
	Johannes Thumshirn, linux-scsi, linux-kernel

In fc_lport_ptp_setup(), fc_rport_create() is assigned to
lport->ptp_rdata and there is a dereference of in fc_lport_ptp_setup(),
which could lead to a NULL pointer dereference on failure of
fc_rport_create().

Fix this bug by adding a check of fc_rport_create().

This bug was found by a static analyzer. The analysis employs
differential checking to identify inconsistent security operations
(e.g., checks or kfrees) between two code paths and confirms that the
inconsistent operations are not recovered in the current function or
the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false
positive or hard to trigger. Multiple researchers have cross-reviewed
the bug.

Builds with CONFIG_LIBFC=m show no new warnings,
and our static analyzer no longer warns about this code.

Fixes: 2580064b5ec6 ("scsi: libfc: Replace ->rport_create callback with function call")
Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
---
 drivers/scsi/libfc/fc_lport.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/scsi/libfc/fc_lport.c b/drivers/scsi/libfc/fc_lport.c
index 19cd4a95d354..5cd716afb711 100644
--- a/drivers/scsi/libfc/fc_lport.c
+++ b/drivers/scsi/libfc/fc_lport.c
@@ -241,6 +241,13 @@ static void fc_lport_ptp_setup(struct fc_lport *lport,
 	}
 	mutex_lock(&lport->disc.disc_mutex);
 	lport->ptp_rdata = fc_rport_create(lport, remote_fid);
+	if (!lport->ptp_rdata) {
+		mutex_unlock(&lport->disc.disc_mutex);
+		printk(KERN_WARNING "libfc: Failed to allocate for the port (%6.6x)\n",
+				remote_fid);
+		return;
+	}
+
 	kref_get(&lport->ptp_rdata->kref);
 	lport->ptp_rdata->ids.port_name = remote_wwpn;
 	lport->ptp_rdata->ids.node_name = remote_wwnn;
-- 
2.25.1


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

* Re: [PATCH] scsi: libfc: Fix a NULL pointer dereference in fc_lport_ptp_setup()
  2021-11-30 17:10 [PATCH] scsi: libfc: Fix a NULL pointer dereference in fc_lport_ptp_setup() Zhou Qingyang
@ 2021-11-30 18:13 ` James Bottomley
  2021-12-01  6:41 ` Hannes Reinecke
  1 sibling, 0 replies; 3+ messages in thread
From: James Bottomley @ 2021-11-30 18:13 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: kjlu, Hannes Reinecke, Martin K. Petersen, Johannes Thumshirn,
	linux-scsi, linux-kernel

On Wed, 2021-12-01 at 01:10 +0800, Zhou Qingyang wrote:
> In fc_lport_ptp_setup(), fc_rport_create() is assigned to
> lport->ptp_rdata and there is a dereference of in
> fc_lport_ptp_setup(),
> which could lead to a NULL pointer dereference on failure of
> fc_rport_create().
> 
> Fix this bug by adding a check of fc_rport_create().
> 
> This bug was found by a static analyzer. The analysis employs
> differential checking to identify inconsistent security operations
> (e.g., checks or kfrees) between two code paths and confirms that the
> inconsistent operations are not recovered in the current function or
> the callers, so they constitute bugs.
> 
> Note that, as a bug found by static analysis, it can be a false
> positive or hard to trigger. Multiple researchers have cross-reviewed
> the bug.
> 
> Builds with CONFIG_LIBFC=m show no new warnings,
> and our static analyzer no longer warns about this code.
> 
> Fixes: 2580064b5ec6 ("scsi: libfc: Replace ->rport_create callback
> with function call")
> Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> ---
>  drivers/scsi/libfc/fc_lport.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/scsi/libfc/fc_lport.c
> b/drivers/scsi/libfc/fc_lport.c
> index 19cd4a95d354..5cd716afb711 100644
> --- a/drivers/scsi/libfc/fc_lport.c
> +++ b/drivers/scsi/libfc/fc_lport.c
> @@ -241,6 +241,13 @@ static void fc_lport_ptp_setup(struct fc_lport
> *lport,
>  	}
>  	mutex_lock(&lport->disc.disc_mutex);
>  	lport->ptp_rdata = fc_rport_create(lport, remote_fid);
> +	if (!lport->ptp_rdata) {
> +		mutex_unlock(&lport->disc.disc_mutex);
> +		printk(KERN_WARNING "libfc: Failed to allocate for the
> port (%6.6x)\n",
> +				remote_fid);
> +		return;
> +	}
> +

This really doesn't look like a good idea.  Most GFP_KERNEL allocations
aren't going to fail unless the kernel is about to wedge anyway under
reclaim pressure.  fc_lport_ptp_setup is assumed to succeed if it
returns, there's no error handling, so the kernel would now continue in
an unexpected state if it recovers from the reclaim issue.

The kmalloc failure will have printed a message anyway and the oops
trace from the NULL deref would identify the location if it's relevant
and likely kill the iscsi daemon, so setting up a time bomb for someone
else really doesn't look to be improving the code.

James



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

* Re: [PATCH] scsi: libfc: Fix a NULL pointer dereference in fc_lport_ptp_setup()
  2021-11-30 17:10 [PATCH] scsi: libfc: Fix a NULL pointer dereference in fc_lport_ptp_setup() Zhou Qingyang
  2021-11-30 18:13 ` James Bottomley
@ 2021-12-01  6:41 ` Hannes Reinecke
  1 sibling, 0 replies; 3+ messages in thread
From: Hannes Reinecke @ 2021-12-01  6:41 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: kjlu, James E.J. Bottomley, Martin K. Petersen,
	Johannes Thumshirn, linux-scsi, linux-kernel

On 11/30/21 6:10 PM, Zhou Qingyang wrote:
> In fc_lport_ptp_setup(), fc_rport_create() is assigned to
> lport->ptp_rdata and there is a dereference of in fc_lport_ptp_setup(),
> which could lead to a NULL pointer dereference on failure of
> fc_rport_create().
> 
> Fix this bug by adding a check of fc_rport_create().
> 
> This bug was found by a static analyzer. The analysis employs
> differential checking to identify inconsistent security operations
> (e.g., checks or kfrees) between two code paths and confirms that the
> inconsistent operations are not recovered in the current function or
> the callers, so they constitute bugs.
> 
> Note that, as a bug found by static analysis, it can be a false
> positive or hard to trigger. Multiple researchers have cross-reviewed
> the bug.
> 
> Builds with CONFIG_LIBFC=m show no new warnings,
> and our static analyzer no longer warns about this code.
> 
> Fixes: 2580064b5ec6 ("scsi: libfc: Replace ->rport_create callback with function call")
> Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
> ---
>   drivers/scsi/libfc/fc_lport.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/scsi/libfc/fc_lport.c b/drivers/scsi/libfc/fc_lport.c
> index 19cd4a95d354..5cd716afb711 100644
> --- a/drivers/scsi/libfc/fc_lport.c
> +++ b/drivers/scsi/libfc/fc_lport.c
> @@ -241,6 +241,13 @@ static void fc_lport_ptp_setup(struct fc_lport *lport,
>   	}
>   	mutex_lock(&lport->disc.disc_mutex);
>   	lport->ptp_rdata = fc_rport_create(lport, remote_fid);
> +	if (!lport->ptp_rdata) {
> +		mutex_unlock(&lport->disc.disc_mutex);
> +		printk(KERN_WARNING "libfc: Failed to allocate for the port (%6.6x)\n",
> +				remote_fid);
> +		return;
> +	}
> +
>   	kref_get(&lport->ptp_rdata->kref);
>   	lport->ptp_rdata->ids.port_name = remote_wwpn;
>   	lport->ptp_rdata->ids.node_name = remote_wwnn;
> 
Reviewed-by: Hannes Reinecke <hare@suse.de>

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                Kernel Storage Architect
hare@suse.de                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer

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

end of thread, other threads:[~2021-12-01  6:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 17:10 [PATCH] scsi: libfc: Fix a NULL pointer dereference in fc_lport_ptp_setup() Zhou Qingyang
2021-11-30 18:13 ` James Bottomley
2021-12-01  6:41 ` Hannes Reinecke

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