<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Am 18.11.22 um 18:05 schrieb T.J. Mercier:<br>
<blockquote type="cite" cite="mid:CABdmKX0KJJV0iQwy0aUNXcLc1DGyLjmh6_Y53asHEoh-uyHzAA@mail.gmail.com">
<div dir="ltr">
<div dir="ltr">
<div class="gmail_default" style="font-family:arial,sans-serif;font-size:small"><br>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Fri, Nov 18, 2022 at
12:27 AM Christian König <<a href="mailto:christian.koenig@amd.com" moz-do-not-send="true" class="moz-txt-link-freetext">christian.koenig@amd.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">Am 18.11.22 um 03:36
schrieb T.J. Mercier:<br>
> On Thu, Nov 17, 2022 at 2:16 AM Christian König<br>
> <<a href="mailto:christian.koenig@amd.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">christian.koenig@amd.com</a>>
wrote:<br>
>> Am 17.11.22 um 08:48 schrieb Charan Teja Kalla:<br>
>>> Sometime back Dan also reported the same
issue[1] where I do mentioned<br>
>>> that fput()-->dma_buf_file_release() will
remove it from the list.<br>
>>><br>
>>> But it seems that I failed to notice fput()
here calls the<br>
>>> dma_buf_file_release() asynchronously i.e.
dmabuf that is accessed in<br>
>>> the close path is already freed. Am I wrong
here?<br>
>>><br>
>>> Should we have the __fput_sync(file) here
instead of just fput(file)<br>
>>> which can solve this problem?<br>
>>><br>
>>> [1]<a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20220516084704.GG29930%40kadam%2F&data=05%7C01%7Cchristian.koenig%40amd.com%7Ca7d0f99c9d8440a4c6d208dac98717cd%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C638043879326367851%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=V38k91cCy446dyGpmRQWTNn5U8u2zSfCAgIRmACukq4%3D&reserved=0" originalsrc="https://lore.kernel.org/all/20220516084704.GG29930@kadam/" shash="ieRIwtZCsqNLyz+VNp3uDCXDEBO2AipZlVgH68yc0pi73UxVelyvquG6ezi3TfichHqBK3iW3rV/NiwhiXbMZfPrvlEOVQU7erD2EE3HrSOQn5pXaUbfPArTnw6TXeAEOQk0x1L/Jf+pBvPreiEGB53nViw5koD0pBgbRWf1xxY=" rel="noreferrer" target="_blank" moz-do-not-send="true">https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2F20220516084704.GG29930%40kadam%2F&data=05%7C01%7Cchristian.koenig%40amd.com%7C7d87a302d300479ecfa608dac90dc9f4%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C638043358319479671%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=erPl1hGdfLbfCxK3J3xiIR9boJbgj6hPUnCBvZFobog%3D&reserved=0</a><br>
>> That doesn't look like the right solution to me
either.<br>
>><br>
>> Essentially we have two separate tear down methods
for the dma_buf<br>
>> object here:<br>
>><br>
>> 1. It's not completely initialized and we can call
kfree()+module_put()<br>
>> to clean up.<br>
>> There is actually a dma_resv_fini() here.
That should probably be<br>
>> fixed.<br>
>><br>
>> 2. The dma_buf object is fully initialized, but
creating the sysfs stats<br>
>> file failed.<br>
>> In this case we should *not* clean it up like
we currently do, but<br>
>> rather call fput().<br>
>><br>
>> So the right thing to do is a) fix the missing
dma_resv_fini() call and<br>
>> b) drop the setting d_fsdata=NULL hack and properly
return after the fput().<br>
>><br>
> This looks right to me if by properly return you mean
return<br>
> ERR_PTR(ret); at the end of err_sysfs after the fput.
(letting<br>
> dma_buf_file_release and dma_buf_release do the full
cleanup)<br>
<br>
Yes, exactly that's the idea.<br>
<br>
The only alternatives I can see would be to either move
allocating the <br>
file and so completing the dma_buf initialization last again
or just <br>
ignore errors from sysfs.<br>
<br>
> If we still want to avoid calling
dmabuf->ops->release(dmabuf) in<br>
> dma_buf_release like the comment says I guess we could
use sysfs_entry<br>
> and ERR_PTR to flag that, otherwise it looks like we'd
need a bit<br>
> somewhere.<br>
<br>
No, this should be dropped as far as I can see. The sysfs
cleanup code <br>
looks like it can handle not initialized kobj pointers.<br>
</blockquote>
<div><br>
</div>
<div class="gmail_default" style="font-family:arial,sans-serif;font-size:small">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. <br>
</div>
</div>
</div>
</blockquote>
<br>
<br>
Oh, very good point as well. Yeah, then creating the file should
probably come last.<br>
<br>
Regards,<br>
Christian.<br>
<br>
<blockquote type="cite" cite="mid:CABdmKX0KJJV0iQwy0aUNXcLc1DGyLjmh6_Y53asHEoh-uyHzAA@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<br>
Regards,<br>
Christian.<br>
<br>
><br>
> ><br>
>> Regards,<br>
>> Christian.<br>
>><br>
>>> Thanks,<br>
>>> Charan<br>
>>> On 11/17/2022 11:51 AM, Gaosheng Cui wrote:<br>
>>>> Smatch report warning as follows:<br>
>>>><br>
>>>> drivers/dma-buf/dma-buf.c:681
dma_buf_export() warn:<br>
>>>> '&dmabuf->list_node' not removed
from list<br>
>>>><br>
>>>> If dma_buf_stats_setup() fails in
dma_buf_export(), goto err_sysfs<br>
>>>> and dmabuf will be freed, but
dmabuf->list_node will not be removed<br>
>>>> from db_list.head, then list traversal may
cause UAF.<br>
>>>><br>
>>>> Fix by removeing it from db_list.head
before free().<br>
>>>><br>
>>>> Fixes: ef3a6b70507a ("dma-buf: call
dma_buf_stats_setup after dmabuf is in valid list")<br>
>>>> Signed-off-by: Gaosheng Cui <<a href="mailto:cuigaosheng1@huawei.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">cuigaosheng1@huawei.com</a>><br>
>>>> ---<br>
>>>> drivers/dma-buf/dma-buf.c | 3 +++<br>
>>>> 1 file changed, 3 insertions(+)<br>
>>>><br>
>>>> diff --git a/drivers/dma-buf/dma-buf.c
b/drivers/dma-buf/dma-buf.c<br>
>>>> index b809513b03fe..6848f50226d5 100644<br>
>>>> --- a/drivers/dma-buf/dma-buf.c<br>
>>>> +++ b/drivers/dma-buf/dma-buf.c<br>
>>>> @@ -675,6 +675,9 @@ struct dma_buf
*dma_buf_export(const struct dma_buf_export_info *exp_info)<br>
>>>> return dmabuf;<br>
>>>><br>
>>>> err_sysfs:<br>
>>>> + mutex_lock(&db_list.lock);<br>
>>>> + list_del(&dmabuf->list_node);<br>
>>>> + mutex_unlock(&db_list.lock);<br>
>>>> /*<br>
>>>> * Set
file->f_path.dentry->d_fsdata to NULL so that when<br>
>>>> * dma_buf_release() gets invoked by
dentry_ops, it exits<br>
<br>
</blockquote>
</div>
</div>
</blockquote>
<br>
</body>
</html>