mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Smart <jsmart2021@gmail.com>
To: Zheng Wang <zyytlz.wz@163.com>, james.smart@broadcom.com
Cc: dick.kennedy@broadcom.com, jejb@linux.ibm.com,
	martin.petersen@oracle.com, linux-scsi@vger.kernel.org,
	hackerzheng666@gmail.com, alex000young@gmail.com,
	security@kernel.org, linux-kernel@vger.kernel.org,
	James Smart <jsmart2021@gmail.com>
Subject: Re: [PATCH] scsi: lpfc: fix double free bug in lpfc_bsg_write_ebuf_set
Date: Wed, 2 Nov 2022 09:37:49 -0700	[thread overview]
Message-ID: <0ce67c60-7b55-6f29-2f97-9b17c1e175c0@gmail.com> (raw)
In-Reply-To: <20221028050736.151185-1-zyytlz.wz@163.com>

On 10/27/2022 10:07 PM, Zheng Wang wrote:
> When error occurs, it frees dmabuf in both lpfc_bsg_write_ebuf_set
> and lpfc_bsg_issue_mbox.
> 
> Fix it by removing free code in lpfc_bsg_write_ebuf_set.
> 
> Reported-by: Zheng Wang <hackerzheng666@gmail.com>
> Reported-by: Zhuorao Yang <alex000young@gmail.com>
> 
> Fixes: 7ad20aa9d39a ("[SCSI] lpfc 8.3.24: Extend BSG infrastructure and add link diagnostics")
> 
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---
>   drivers/scsi/lpfc/lpfc_bsg.c | 17 +++--------------
>   1 file changed, 3 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/scsi/lpfc/lpfc_bsg.c b/drivers/scsi/lpfc/lpfc_bsg.c
> index ac0c7ccf2eae..7362d9c1a50b 100644
> --- a/drivers/scsi/lpfc/lpfc_bsg.c
> +++ b/drivers/scsi/lpfc/lpfc_bsg.c
> @@ -4439,15 +4439,13 @@ lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
>   
>   		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
>   		if (!dd_data) {
> -			rc = -ENOMEM;
> -			goto job_error;
> +			return -ENOMEM;
>   		}
>   
>   		/* mailbox command structure for base driver */
>   		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
>   		if (!pmboxq) {
> -			rc = -ENOMEM;
> -			goto job_error;
> +			return -ENOMEM;
>   		}
>   		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
>   		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;

Minimally, just looking at this one snippet, by returning after the 
mempool_alloc() failure, we are leaking the dd_data memory just allocated.

> @@ -4480,8 +4478,7 @@ lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
>   		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
>   				"2970 Failed to issue SLI_CONFIG ext-buffer "
>   				"mailbox command, rc:x%x\n", rc);
> -		rc = -EPIPE;
> -		goto job_error;
> +		return -EPIPE;

and this leaks both the dd_data and pmboxq memory.

>   	}
>   
>   	/* wait for additional external buffers */
> @@ -4489,14 +4486,6 @@ lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
>   	bsg_job_done(job, bsg_reply->result,
>   		       bsg_reply->reply_payload_rcv_len);
>   	return SLI_CONFIG_HANDLED;
> -
> -job_error:
> -	if (pmboxq)
> -		mempool_free(pmboxq, phba->mbox_mem_pool);
> -	lpfc_bsg_dma_page_free(phba, dmabuf);
> -	kfree(dd_data);
> -
> -	return rc;
>   }
>   
>   /**

all of these errors should cause:
   lpfc_bsg_write_ebuf_set() to return -Exxx
   causing lpfc_bsg_handle_sli_cfg_ebuf() to return -Exxx
   causing lpfc_bsg_handle_sli_cfg_ext() to return -Exxx
   which causes lpfc_bsg_issue_mbox() to jump to job_done

I understand the argument is that issue_mbox deletes them, but....

job_done:
   checks/frees pmboxq is allocated after the jump so it will be NULL
   frees dmabuf - which was allocated prior to the jump; is freed
      in freedlpfc_bsg_handle_sli_cfg_ebuf() but only in a block
      that returns SLI_CONFIG_HANDLED, which is not the block that
      invokes lpfc_bsg_write_ebuf_set. So it's valid to delete.
      Note: there's a special case for SLI_CONFIG_HANDLED which skips
      over these deletes so it's ok.
   frees dd_data - which is allocated after the jump so it too will
      be NULL

So - the code is fine.  The SLI_CONFIG_HANDLED is a little weird, but 
the logic is fine. If the patch were added it would leak memory.

I take it this was identified by some tool ?

-- james




  parent reply	other threads:[~2022-11-02 16:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-28  5:07 Zheng Wang
2022-11-02 10:36 ` Zheng Hacker
2022-11-02 16:37 ` James Smart [this message]
2022-11-03  4:12   ` Zheng Hacker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0ce67c60-7b55-6f29-2f97-9b17c1e175c0@gmail.com \
    --to=jsmart2021@gmail.com \
    --cc=alex000young@gmail.com \
    --cc=dick.kennedy@broadcom.com \
    --cc=hackerzheng666@gmail.com \
    --cc=james.smart@broadcom.com \
    --cc=jejb@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=security@kernel.org \
    --cc=zyytlz.wz@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®