Message ID | 20221028050736.151185-1-zyytlz.wz@163.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp626437wru; Thu, 27 Oct 2022 22:12:52 -0700 (PDT) X-Google-Smtp-Source: AMsMyM67MN1pOTM5wzHZZZpZMC89Xyl9sxo9Ul36Yw96UWgNOUY7fANd3xa33/PXTX+wp+CpZcAH X-Received: by 2002:a17:906:ef8b:b0:791:9980:b7b9 with SMTP id ze11-20020a170906ef8b00b007919980b7b9mr45377645ejb.636.1666933962369; Thu, 27 Oct 2022 22:12:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666933962; cv=none; d=google.com; s=arc-20160816; b=GnUbIHZ4AxjBjUE66IjRmiRnH2LQatmL1bGHh2ezKHmtMw+kQDO1Qbr803Ss4MY41h SSJgn20v1+o9jooMO+8wOTKn3tTQ7hgJGZniNYONdCIuUXcVgT9jFwwR5H3laueKfOGH kBXOl1xJQs78kQ4cE8/sUrXjAWFWFaq7PMqwxS8ARjzBUi2fkYnAGy2Gk2xcaBJMANuY 9qXbx0T1nYHilO+2FNUVAYSlP8hUkS97oIvSCSB0YTcWMtHlsuzxZlxpHxWanL1swegY 7eHrBzO/05jjbTG9ux8hn5U4E9neFs/2lRR9NK5MjNcvAe+Wx996FwWlG6ROhEBea3ey AAuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=kt3iKsmMMKWgKoyXI3XPdA6XBPWW/6C1tCL/uRPd9MA=; b=ukZ3iwuKODG+4loh1XiPX31ZNQAFeq+Zn3C/pl3RGwu792lxqHHXGNe3lKLDp/Biqs mBJhtBrsoiZ3YGqvyXCoBxnSyesphiE3oQN9DVx1drsRdzVtsNoEohZ+GL5+vF7mVhLS OHFN7L5iX2SmTyh1E+EZ2bkxVoK8Dg+4/yS1h39Qfp2WIt30fHRCkYXHu7sAyPNmq/VA deVXLikgaNt94pds8dYy85Zdn9RgCFBWlL4LKjtAVQtFxJdUw0N9bwYDLj5XYjoNXVJs OW/jrq32QyN6CbKYKg9n87SjnwSVCqqwAh3FxECyi2gkAcnxRd07gHFyjYgbZ4ejkr2E JgaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=AglJgsWJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nb37-20020a1709071ca500b0078217f3a033si3224712ejc.652.2022.10.27.22.12.19; Thu, 27 Oct 2022 22:12:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=AglJgsWJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229739AbiJ1FIU (ORCPT <rfc822;chrisfriedt@gmail.com> + 99 others); Fri, 28 Oct 2022 01:08:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229457AbiJ1FIT (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 28 Oct 2022 01:08:19 -0400 Received: from mail-m975.mail.163.com (mail-m975.mail.163.com [123.126.97.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 157EE1B156D; Thu, 27 Oct 2022 22:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=kt3iK smMMKWgKoyXI3XPdA6XBPWW/6C1tCL/uRPd9MA=; b=AglJgsWJOGqK4TLQzKDZU bb4PEmfDKj22TOMzGdnRhz/leTmXASNH5IA/Ehaz5/lc+8Z/d1+NBTyk5QLA8gaV maBRVzTgthNnuguZ8A0lktughEd4DAMGcANe8Hk5b9hIQLWJPrr4YkS/3iEI+uW3 2XcFNQdtZOeyDLnCq/9zzY= Received: from localhost.localdomain (unknown [111.206.145.21]) by smtp5 (Coremail) with SMTP id HdxpCgCHr_CZY1tj9i5AoA--.10196S2; Fri, 28 Oct 2022 13:07:37 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: 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, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH] scsi: lpfc: fix double free bug in lpfc_bsg_write_ebuf_set Date: Fri, 28 Oct 2022 13:07:36 +0800 Message-Id: <20221028050736.151185-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: HdxpCgCHr_CZY1tj9i5AoA--.10196S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7WrW3Zr1rGw1rKw1kZFyxXwb_yoW8ZrW8pa y3C3Z7ArZrtF4IqrWDA34Yv3s3tayfXFy2kFZFg3WrWF1FvryjyF1UtF18XFWYkFyI9rZ8 tF4UK34UWF17Xa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziLZ2hUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXBmoU1Xl4nrbGgAAsX X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747906946403527336?= X-GMAIL-MSGID: =?utf-8?q?1747906946403527336?= |
Series |
scsi: lpfc: fix double free bug in lpfc_bsg_write_ebuf_set
|
|
Commit Message
Zheng Wang
Oct. 28, 2022, 5:07 a.m. UTC
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(-)
Comments
Friendly ping :)
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
James Smart <jsmart2021@gmail.com> 于2022年11月3日周四 00:37写道: > Minimally, just looking at this one snippet, by returning after the > mempool_alloc() failure, we are leaking the dd_data memory just allocated. > Yes, this is a bad patch. > > @@ -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. So Here it is. > > 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 > Hi James! Really apprecaite for your reply. I was not sure if it it a really issue. Sorry for the bad patch. > 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 I understood your point. Here is a call chain : lpfc_bsg_issue_mbox->lpfc_bsg_handle_sli_cfg_ext->lpfc_bsg_handle_sli_cfg_ebuf->lpfc_bsg_write_ebuf_set->lpfc_bsg_dma_page_free->kfree(dmabuf) It leads to another kfree in lpfc_bsg_mbox_cmd : job_done: /* common exit for error or job completed inline */ if (pmboxq) mempool_free(pmboxq, phba->mbox_mem_pool); [7] lpfc_bsg_dma_page_free(phba, dmabuf); kfree(dd_data); So the key point is whether phba->mbox_ext_buf_ctx.mboxType can be mbox_wr. If not, just as you illustrated, all is fine. It will get into SLI_CONFIG_HANDLED path and handle dmabuf as expected. But if not, it will have a chance to trigger a double-free bug. I found phda is assigned in lpfc_bsg_mbox_cmd. But I am still not sure about its value. Appreciate if you can help me to understand more about the key condition :) > 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 ? > Yes, I found it using Codeql. I didn't have a poc to verify. Best Regards, Zheng Wang
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; @@ -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; } /* 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; } /**