From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 405BEC4332F for ; Fri, 18 Nov 2022 17:05:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B4F7A10E79A; Fri, 18 Nov 2022 17:05:31 +0000 (UTC) Received: from mail-yw1-x1132.google.com (mail-yw1-x1132.google.com [IPv6:2607:f8b0:4864:20::1132]) by gabe.freedesktop.org (Postfix) with ESMTPS id 28C3E10E79A for ; Fri, 18 Nov 2022 17:05:28 +0000 (UTC) Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-368edbc2c18so54967977b3.13 for ; Fri, 18 Nov 2022 09:05:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pYaO4frsra4fw6h53DPHPnYJpAOilXGJ6vC54BFE2Kk=; b=djHYj3FUbzZdZg37yc4Igv/IRvLcb6mqU5V2Uwm6tOtJRNs0J7H3gpvKqCX9RvzED1 0P5LettHbtzsmOpfd1GLqt/g/EXkicFlh/9XKQBWMs/MEGMuBHXk692DkY3ncnY8MBvg XUeD+PzQJ4C+BHG4NYnjEv68I63G9IZ8nt7jwpfsT9lepT5VnCNct98/rOBBIYSpxZGE 0W9fU3BfnBtnkjtpj4RAGBLCVUEJ/8NY7ACvvn4d3ydHWzfe9OY9gC/VomcYjI+H8OjC 4fQM2c2sHqTkWXRA2YZPpebcyCeI5zt5JcGmGga3n1NeduoRPQYW/4pj0CoNNtOIwWh3 vr1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=pYaO4frsra4fw6h53DPHPnYJpAOilXGJ6vC54BFE2Kk=; b=SwVarFL3BWPlqmoioV47lTQhHYrgEmoMNIaqtZ1GikfZ/eBqo14gOmH3FuLhWNNafa JrUctXV1HGNTI7gHFOcPzCwfmWyf32aSxKP+U05nU6L3wcM+AL7gU84R8JKIrBKs3K9n nABaJuK+ViFxMnlgGkyRZoEEtdYP98bFz7/bA+q/A1jVa4wimBgyqBdLvjvreCzNpFf4 S0E1PUkgI7l3lzDA334Zl9FtrB8J3aern4wEkmDP+MzQwYcqLHCrC+vNs3CmwgYWp7kx zhzbHo2LFciJp3nuaNHdhnDC7++B7m2AIOddAH/nLLRVGPo4Gz4yagJx8fYMtNt6CKAb Uyyw== X-Gm-Message-State: ANoB5pkSWySU9YtSUIWCzVRrZwJaUYHJZFPs7uGPHhmfTts5GUytnb+Y 4ex3O+PYZTEhPWADqiwgk0PpTs4R3NdedP23+PX9KA== X-Google-Smtp-Source: AA0mqf6kYHjsb6cu7KeYD/Mn97m8h6gicsJm1ZP/wL2CBHiJ8/o9DeqQtFcPT83VCJF0cJ84uNPXpCEtXkqxuPA27Xg= X-Received: by 2002:a0d:d684:0:b0:36d:2169:3d65 with SMTP id y126-20020a0dd684000000b0036d21693d65mr7131722ywd.317.1668791126861; Fri, 18 Nov 2022 09:05:26 -0800 (PST) MIME-Version: 1.0 References: <20221117062152.3029018-1-cuigaosheng1@huawei.com> <99d3aee6-ba3e-5333-6f79-ddbcfc0e8843@amd.com> <2c9fa595-e788-5474-4f2b-ffbd08a70d13@amd.com> In-Reply-To: <2c9fa595-e788-5474-4f2b-ffbd08a70d13@amd.com> From: "T.J. Mercier" Date: Fri, 18 Nov 2022 09:05:12 -0800 Message-ID: Subject: Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export To: =?UTF-8?Q?Christian_K=C3=B6nig?= Content-Type: multipart/alternative; boundary="00000000000052805605edc1b6f9" X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pavan Kondeti , dri-devel@lists.freedesktop.org, sumit.semwal@linaro.org, linaro-mm-sig@lists.linaro.org, Charan Teja Kalla , Gaosheng Cui , Dan Carpenter , linux-media@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" --00000000000052805605edc1b6f9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Nov 18, 2022 at 12:27 AM Christian K=C3=B6nig wrote: > Am 18.11.22 um 03:36 schrieb T.J. Mercier: > > On Thu, Nov 17, 2022 at 2:16 AM Christian K=C3=B6nig > > wrote: > >> Am 17.11.22 um 08:48 schrieb Charan Teja Kalla: > >>> Sometime back Dan also reported the same issue[1] where I do mentione= d > >>> that fput()-->dma_buf_file_release() will remove it from the list. > >>> > >>> But it seems that I failed to notice fput() here calls the > >>> dma_buf_file_release() asynchronously i.e. dmabuf that is accessed in > >>> the close path is already freed. Am I wrong here? > >>> > >>> Should we have the __fput_sync(file) here instead of just fput(file) > >>> which can solve this problem? > >>> > >>> [1] > https://nam11.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%2Flore.= kernel.org%2Fall%2F20220516084704.GG29930%40kadam%2F&data=3D05%7C01%7Cc= hristian.koenig%40amd.com%7C7d87a302d300479ecfa608dac90dc9f4%7C3dd8961fe488= 4e608e11a82d994e183d%7C0%7C0%7C638043358319479671%7CUnknown%7CTWFpbGZsb3d8e= yJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C= %7C%7C&sdata=3DerPl1hGdfLbfCxK3J3xiIR9boJbgj6hPUnCBvZFobog%3D&reser= ved=3D0 > >> That doesn't look like the right solution to me either. > >> > >> Essentially we have two separate tear down methods for the dma_buf > >> object here: > >> > >> 1. It's not completely initialized and we can call kfree()+module_put(= ) > >> to clean up. > >> There is actually a dma_resv_fini() here. That should probably b= e > >> fixed. > >> > >> 2. The dma_buf object is fully initialized, but creating the sysfs sta= ts > >> file failed. > >> In this case we should *not* clean it up like we currently do, b= ut > >> rather call fput(). > >> > >> So the right thing to do is a) fix the missing dma_resv_fini() call an= d > >> b) drop the setting d_fsdata=3DNULL hack and properly return after the > fput(). > >> > > This looks right to me if by properly return you mean return > > ERR_PTR(ret); at the end of err_sysfs after the fput. (letting > > dma_buf_file_release and dma_buf_release do the full cleanup) > > Yes, exactly that's the idea. > > The only alternatives I can see would be to either move allocating the > file and so completing the dma_buf initialization last again or just > ignore errors from sysfs. > > > If we still want to avoid calling dmabuf->ops->release(dmabuf) in > > dma_buf_release like the comment says I guess we could use sysfs_entry > > and ERR_PTR to flag that, otherwise it looks like we'd need a bit > > somewhere. > > No, this should be dropped as far as I can see. The sysfs cleanup code > looks like it can handle not initialized kobj pointers. > Yeah there is also the null check in dma_buf_stats_teardown() that would prevent it from running, but I understood the comment to be referring to the release() dma_buf_ops call into the exporter which comes right after the teardown call. That looks like it's preventing the fput task work calling back into the exporter after the exporter already got an error from dma_buf_export(). Otherwise the exporter sees a release() for a buffer that it doesn't know about / thinks shouldn't exist. So I could imagine an exporter trying to double free: once for the failed dma_buf_export() call, and again when the release() op is called later. > > Regards, > Christian. > > > > > > > >> Regards, > >> Christian. > >> > >>> Thanks, > >>> Charan > >>> On 11/17/2022 11:51 AM, Gaosheng Cui wrote: > >>>> Smatch report warning as follows: > >>>> > >>>> drivers/dma-buf/dma-buf.c:681 dma_buf_export() warn: > >>>> '&dmabuf->list_node' not removed from list > >>>> > >>>> If dma_buf_stats_setup() fails in dma_buf_export(), goto err_sysfs > >>>> and dmabuf will be freed, but dmabuf->list_node will not be removed > >>>> from db_list.head, then list traversal may cause UAF. > >>>> > >>>> Fix by removeing it from db_list.head before free(). > >>>> > >>>> Fixes: ef3a6b70507a ("dma-buf: call dma_buf_stats_setup after dmabuf > is in valid list") > >>>> Signed-off-by: Gaosheng Cui > >>>> --- > >>>> drivers/dma-buf/dma-buf.c | 3 +++ > >>>> 1 file changed, 3 insertions(+) > >>>> > >>>> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c > >>>> index b809513b03fe..6848f50226d5 100644 > >>>> --- a/drivers/dma-buf/dma-buf.c > >>>> +++ b/drivers/dma-buf/dma-buf.c > >>>> @@ -675,6 +675,9 @@ struct dma_buf *dma_buf_export(const struct > dma_buf_export_info *exp_info) > >>>> return dmabuf; > >>>> > >>>> err_sysfs: > >>>> + mutex_lock(&db_list.lock); > >>>> + list_del(&dmabuf->list_node); > >>>> + mutex_unlock(&db_list.lock); > >>>> /* > >>>> * Set file->f_path.dentry->d_fsdata to NULL so that when > >>>> * dma_buf_release() gets invoked by dentry_ops, it exits > > --00000000000052805605edc1b6f9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, Nov 18, 2022= at 12:27 AM Christian K=C3=B6nig <christian.koenig@amd.com> wrote:
Am 18.11.22 um 03:36 schrieb T.J. Mercier:=
> On Thu, Nov 17, 2022 at 2:16 AM Christian K=C3=B6nig
> <chri= stian.koenig@amd.com> wrote:
>> Am 17.11.22 um 08:48 schrieb Charan Teja Kalla:
>>> Sometime back Dan also reported the same issue[1] where I do m= entioned
>>> that fput()-->dma_buf_file_release() will remove it from th= e list.
>>>
>>> But it seems that I failed to notice fput() here calls the
>>> dma_buf_file_release() asynchronously i.e. dmabuf that is acce= ssed in
>>> the close path is already freed. Am I wrong here?
>>>
>>> Should we have the __fput_sync(file) here instead of just fput= (file)
>>> which can solve this problem?
>>>
>>> [1]https://nam11.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%2= Flore.kernel.org%2Fall%2F20220516084704.GG29930%40kadam%2F&amp;data=3D0= 5%7C01%7Cchristian.koenig%40amd.com%7C7d87a302d300479ecfa608dac90dc9f4%7C3d= d8961fe4884e608e11a82d994e183d%7C0%7C0%7C638043358319479671%7CUnknown%7CTWF= pbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D= %7C3000%7C%7C%7C&amp;sdata=3DerPl1hGdfLbfCxK3J3xiIR9boJbgj6hPUnCBvZFobo= g%3D&amp;reserved=3D0
>> That doesn't look like the right solution to me either.
>>
>> Essentially we have two separate tear down methods for the dma_buf=
>> object here:
>>
>> 1. It's not completely initialized and we can call kfree()+mod= ule_put()
>> to clean up.
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0There is actually a dma_resv_fini() here= . That should probably be
>> fixed.
>>
>> 2. The dma_buf object is fully initialized, but creating the sysfs= stats
>> file failed.
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0In this case we should *not* clean it up= like we currently do, but
>> rather call fput().
>>
>> So the right thing to do is a) fix the missing dma_resv_fini() cal= l and
>> b) drop the setting d_fsdata=3DNULL hack and properly return after= the fput().
>>
> This looks right to me if by properly return you mean return
> ERR_PTR(ret); at the end of err_sysfs after the fput. (letting
> dma_buf_file_release and dma_buf_release do the full cleanup)

Yes, exactly that's the idea.

The only alternatives I can see would be to either move allocating the
file and so completing the dma_buf initialization last again or just
ignore errors from sysfs.

> If we still want to avoid calling dmabuf->ops->release(dmabuf) i= n
> dma_buf_release like the comment says I guess we could use sysfs_entry=
> and ERR_PTR to flag that, otherwise it looks like we'd need a bit<= br> > somewhere.

No, this should be dropped as far as I can see. The sysfs cleanup code
looks like it can handle not initialized kobj pointers.

Yeah there is also the null check in dma_buf_stats_tea= rdown() that would prevent it from running, but I understood the comment to= be referring to the release() dma_buf_ops call into the exporter which com= es right after the teardown call. That looks like it's preventing the f= put task work calling back into the exporter after the exporter already got= an error from dma_buf_export(). Otherwise the exporter sees a release() fo= r a buffer that it doesn't know about / thinks shouldn't exist. So = I could imagine an exporter trying to double free: once for the failed dma_= buf_export() call, and again when the release() op is called later.=C2=A0

Regards,
Christian.

>
>=C2=A0 =C2=A0>
>> Regards,
>> Christian.
>>
>>> Thanks,
>>> Charan
>>> On 11/17/2022 11:51 AM, Gaosheng Cui wrote:
>>>> Smatch report warning as follows:
>>>>
>>>> drivers/dma-buf/dma-buf.c:681 dma_buf_export() warn:
>>>>=C2=A0 =C2=A0 =C2=A0'&dmabuf->list_node' not= removed from list
>>>>
>>>> If dma_buf_stats_setup() fails in dma_buf_export(), goto e= rr_sysfs
>>>> and dmabuf will be freed, but dmabuf->list_node will no= t be removed
>>>> from db_list.head, then list traversal may cause UAF.
>>>>
>>>> Fix by removeing it from db_list.head before free().
>>>>
>>>> Fixes: ef3a6b70507a ("dma-buf: call dma_buf_stats_set= up after dmabuf is in valid list")
>>>> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
>>>> ---
>>>>=C2=A0 =C2=A0 drivers/dma-buf/dma-buf.c | 3 +++
>>>>=C2=A0 =C2=A0 1 file changed, 3 insertions(+)
>>>>
>>>> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/d= ma-buf.c
>>>> index b809513b03fe..6848f50226d5 100644
>>>> --- a/drivers/dma-buf/dma-buf.c
>>>> +++ b/drivers/dma-buf/dma-buf.c
>>>> @@ -675,6 +675,9 @@ struct dma_buf *dma_buf_export(const s= truct dma_buf_export_info *exp_info)
>>>>=C2=A0 =C2=A0 =C2=A0 =C2=A0return dmabuf;
>>>>
>>>>=C2=A0 =C2=A0 err_sysfs:
>>>> +=C2=A0 =C2=A0 mutex_lock(&db_list.lock);
>>>> +=C2=A0 =C2=A0 list_del(&dmabuf->list_node);
>>>> +=C2=A0 =C2=A0 mutex_unlock(&db_list.lock);
>>>>=C2=A0 =C2=A0 =C2=A0 =C2=A0/*
>>>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 * Set file->f_path.dentry-&g= t;d_fsdata to NULL so that when
>>>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 * dma_buf_release() gets invoke= d by dentry_ops, it exits

--00000000000052805605edc1b6f9--