Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp931097rwi; Wed, 2 Nov 2022 21:31:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5aSlt0fRUL6EVK7M+gYJMVzFsXaBfIVO2QOqcyMyUpGvI/K9kHhbGW13v3gbY5ManVdDy5 X-Received: by 2002:a17:906:9be5:b0:7ad:d0be:3467 with SMTP id de37-20020a1709069be500b007add0be3467mr19792187ejc.208.1667449894861; Wed, 02 Nov 2022 21:31:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667449894; cv=none; d=google.com; s=arc-20160816; b=VF8Ol+XFV5ScWo9mSi8Nqs173iWIo/tDRebbL9wmjRl/a7bWxoMMw/8BSNwAFVawBF zsgyH7ursiY2wCBUXS2uZQhKXdomyQUvFBC9hI7mTfaZjJGxDdFdTLB2SmyvxcgFZuOa u0E/bWVc0tu+h/5svS+x1wcV7n2VgDAgdAhraOjFOp8AaFgGS34WdsqLIuuU/3cPalSK DTVL6YZsJUp1zNSm/w0d+NpE0Q65k9ldgeDOV9XVrgh3yr9WqHu8/4SxxBOTC3nQEnTC wCIRtvMjuh5/e3N/uKByVBfb8ibHACAbEiU52y62lNuILmPSHD9KmcxQC4mxIV48K7DJ KUnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=F+25QrJbrFzP445hxbERz2MlFDjVBgfsITwfv/Zbzww=; b=GS5NOoSDxF3aMk+gYO2WFmZ4iM3L92nU8M7CZTPszU71gkAyZM5Ba+mazWNpcLh+nT 1XBmDSepUkszN+e4pjNJ4CCBA4oDSktuOyX3trsmO1VQixN0YuQ9LsNvAOj/t3T1LEmW 62NC8vABdlEoq24XcRJ0hHT3rReWi/hZSduDUG17yIkEn9PTO7gMHg5W+sVuo9G23iYB 80KzOsechvRN4nCI4Qfi8+Su8Me+ZaMn6jPy29/8MS9ODTqKDZ3pfleWSexw1FtZP93T dP6pwPj+Lhm4lNAtXnxzAPxRvLWDS2fP45CCTucx/nG7LP5NglqXN5dtxZKe2pVRaizk 5OzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ck0fdPTn; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bl2-20020a170906c24200b0077c5ec87ec2si14035734ejb.297.2022.11.02.21.31.12; Wed, 02 Nov 2022 21:31:34 -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=@gmail.com header.s=20210112 header.b=Ck0fdPTn; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229866AbiKCENW (ORCPT + 97 others); Thu, 3 Nov 2022 00:13:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229830AbiKCENN (ORCPT ); Thu, 3 Nov 2022 00:13:13 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1FFB13F1A; Wed, 2 Nov 2022 21:13:12 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id t16so397504qvm.9; Wed, 02 Nov 2022 21:13:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=F+25QrJbrFzP445hxbERz2MlFDjVBgfsITwfv/Zbzww=; b=Ck0fdPTnKcav/x3nHbHg8fjUSvMBOKipfGIyHC7Io6+DUUyJnO3ShH2fSOefd5UPo1 plv9fW0f3ZgR1ywrfvaCfedLakl+P7l0j8xcav9yQAG2lDkuwJRhJaYnC+4uTf8B9Sbs SvIHhhJ9y0yHWX4KL+KfY65kXN+9NwFLXWV6/nJ4x+B9Kjot1R9XkD7OWWGgQiLBcocn kbGvS16N3xnfa1JVIp3clTiQYOHrnb/iUO+1EzuxVdtTG8Aoaqn5bEuXXQIbNs/j0xkd 46ix9XU3LeauBNQuI9cauegx9TPSHRcH34K5Rx+9kIUFNulPSVCS3FCwMidJh3F+djPu dwNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F+25QrJbrFzP445hxbERz2MlFDjVBgfsITwfv/Zbzww=; b=eksX2BiqZQ2Lmpkf38LLROsggISgOq2Zw09tNWoBOeEu06c4KxXfVtDin7hXgj91uJ 0+U9f6PNwgMV+PQMpGpqwzwNe3xFYXNBQIl0AHF/uY86ntZwJ0GkqGUpHEQl7b5R2Zav AOa/tqUUg1brUWhva7M9/RdAXtBjTEtBTid6sZz/GYEfjCXjXrodkSja1e/uTmw9mSA9 7TJKbGylaweh62mqzOW17WnLGYEwBRnBsaDuIXvK8nQxD3lNc6nAKfOhGtpUoFR1idyf bl0H+SSkF8TUMB/HCwh549KHbBLkCPUCoufuSg6tvXd5jL6oVv7I+1l05vwv1nEpgYGe cv6g== X-Gm-Message-State: ACrzQf0e+buhXSlMSjXjxx3H3e0s/RwHQTbLu+zdgGDKCV4xuj8QXUrk Tm+DZ6n881JG+S5d3N8StXN05YIke4yQPVYGoC8= X-Received: by 2002:a05:6214:3005:b0:4ad:8042:128a with SMTP id ke5-20020a056214300500b004ad8042128amr25151091qvb.66.1667448791800; Wed, 02 Nov 2022 21:13:11 -0700 (PDT) MIME-Version: 1.0 References: <20221028050736.151185-1-zyytlz.wz@163.com> <0ce67c60-7b55-6f29-2f97-9b17c1e175c0@gmail.com> In-Reply-To: <0ce67c60-7b55-6f29-2f97-9b17c1e175c0@gmail.com> From: Zheng Hacker Date: Thu, 3 Nov 2022 12:12:59 +0800 Message-ID: Subject: Re: [PATCH] scsi: lpfc: fix double free bug in lpfc_bsg_write_ebuf_set To: James Smart Cc: Zheng Wang , james.smart@broadcom.com, dick.kennedy@broadcom.com, jejb@linux.ibm.com, martin.petersen@oracle.com, linux-scsi@vger.kernel.org, alex000young@gmail.com, security@kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, 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: X-Mailing-List: linux-kernel@vger.kernel.org James Smart =E4=BA=8E2022=E5=B9=B411=E6=9C=883=E6=97= =A5=E5=91=A8=E5=9B=9B 00:37=E5=86=99=E9=81=93=EF=BC=9A > 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, st= ruct bsg_job *job, > > lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC, > > "2970 Failed to issue SLI_CONFIG ext-buff= er " > > "mailbox command, rc:x%x\n", rc); > > - rc =3D -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