* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-11-24 5:56 ` Charan Teja Kalla
0 siblings, 0 replies; 28+ messages in thread
From: Charan Teja Kalla @ 2022-11-24 5:56 UTC (permalink / raw)
To: Christian König, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, sumit.semwal, linaro-mm-sig,
Gaosheng Cui, Dan Carpenter, linux-media
Thanks T.J and Christian for the inputs.
On 11/19/2022 7:00 PM, Christian König wrote:
>>
>> 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.
>
>
> Oh, very good point as well. Yeah, then creating the file should
> probably come last.
>
@Gaosheng: Could you please make these changes or you let me to do?
> Regards,
> Christian.
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-11-24 5:56 ` Charan Teja Kalla
@ 2022-11-24 11:31 ` cuigaosheng
-1 siblings, 0 replies; 28+ messages in thread
From: cuigaosheng @ 2022-11-24 11:31 UTC (permalink / raw)
To: Charan Teja Kalla, Christian König, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
Thanks T.J and Christian, thanks everyone for taking time to review this patch.
Charan, actually I don't have a good patch to to fix it, if you can submit
a new patch to solve it, please feel free to do it.
By the way, I'd appreciate it if you could send to me the new patch when you submit it.
Thanks again!
Gaosheng.
On 2022/11/24 13:56, Charan Teja Kalla wrote:
> Thanks T.J and Christian for the inputs.
>
> On 11/19/2022 7:00 PM, Christian König wrote:
>>> 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.
>>
>> Oh, very good point as well. Yeah, then creating the file should
>> probably come last.
>>
> @Gaosheng: Could you please make these changes or you let me to do?
>
>> Regards,
>> Christian.
> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-11-24 11:31 ` cuigaosheng
0 siblings, 0 replies; 28+ messages in thread
From: cuigaosheng @ 2022-11-24 11:31 UTC (permalink / raw)
To: Charan Teja Kalla, Christian König, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
Thanks T.J and Christian, thanks everyone for taking time to review this patch.
Charan, actually I don't have a good patch to to fix it, if you can submit
a new patch to solve it, please feel free to do it.
By the way, I'd appreciate it if you could send to me the new patch when you submit it.
Thanks again!
Gaosheng.
On 2022/11/24 13:56, Charan Teja Kalla wrote:
> Thanks T.J and Christian for the inputs.
>
> On 11/19/2022 7:00 PM, Christian König wrote:
>>> 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.
>>
>> Oh, very good point as well. Yeah, then creating the file should
>> probably come last.
>>
> @Gaosheng: Could you please make these changes or you let me to do?
>
>> Regards,
>> Christian.
> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-11-24 5:56 ` Charan Teja Kalla
@ 2022-11-24 12:05 ` cuigaosheng
-1 siblings, 0 replies; 28+ messages in thread
From: cuigaosheng @ 2022-11-24 12:05 UTC (permalink / raw)
To: Charan Teja Kalla, Christian König, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
Some tips:
Before we call the dma_buf_stats_setup(), we have to finish creating the file,
otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need to think about
this when making a new patch.
Hope these tips are useful, thanks!
On 2022/11/24 13:56, Charan Teja Kalla wrote:
> Thanks T.J and Christian for the inputs.
>
> On 11/19/2022 7:00 PM, Christian König wrote:
>>> 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.
>>
>> Oh, very good point as well. Yeah, then creating the file should
>> probably come last.
>>
> @Gaosheng: Could you please make these changes or you let me to do?
>
>> Regards,
>> Christian.
> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-11-24 12:05 ` cuigaosheng
0 siblings, 0 replies; 28+ messages in thread
From: cuigaosheng @ 2022-11-24 12:05 UTC (permalink / raw)
To: Charan Teja Kalla, Christian König, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
Some tips:
Before we call the dma_buf_stats_setup(), we have to finish creating the file,
otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need to think about
this when making a new patch.
Hope these tips are useful, thanks!
On 2022/11/24 13:56, Charan Teja Kalla wrote:
> Thanks T.J and Christian for the inputs.
>
> On 11/19/2022 7:00 PM, Christian König wrote:
>>> 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.
>>
>> Oh, very good point as well. Yeah, then creating the file should
>> probably come last.
>>
> @Gaosheng: Could you please make these changes or you let me to do?
>
>> Regards,
>> Christian.
> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-11-24 12:05 ` cuigaosheng
@ 2022-11-24 12:37 ` Christian König
-1 siblings, 0 replies; 28+ messages in thread
From: Christian König @ 2022-11-24 12:37 UTC (permalink / raw)
To: cuigaosheng, Charan Teja Kalla, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
Am 24.11.22 um 13:05 schrieb cuigaosheng:
> Some tips:
> Before we call the dma_buf_stats_setup(), we have to finish
> creating the file,
> otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need to
> think about
> this when making a new patch.
I was already wondering why the order is this way.
Why is dma_buf_stats_setup() needing the file in the first place?
Thanks,
Christian.
>
> Hope these tips are useful, thanks!
>
> On 2022/11/24 13:56, Charan Teja Kalla wrote:
>> Thanks T.J and Christian for the inputs.
>>
>> On 11/19/2022 7:00 PM, Christian König wrote:
>>>> 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.
>>>
>>> Oh, very good point as well. Yeah, then creating the file should
>>> probably come last.
>>>
>> @Gaosheng: Could you please make these changes or you let me to do?
>>
>>> Regards,
>>> Christian.
>> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-11-24 12:37 ` Christian König
0 siblings, 0 replies; 28+ messages in thread
From: Christian König @ 2022-11-24 12:37 UTC (permalink / raw)
To: cuigaosheng, Charan Teja Kalla, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
Am 24.11.22 um 13:05 schrieb cuigaosheng:
> Some tips:
> Before we call the dma_buf_stats_setup(), we have to finish
> creating the file,
> otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need to
> think about
> this when making a new patch.
I was already wondering why the order is this way.
Why is dma_buf_stats_setup() needing the file in the first place?
Thanks,
Christian.
>
> Hope these tips are useful, thanks!
>
> On 2022/11/24 13:56, Charan Teja Kalla wrote:
>> Thanks T.J and Christian for the inputs.
>>
>> On 11/19/2022 7:00 PM, Christian König wrote:
>>>> 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.
>>>
>>> Oh, very good point as well. Yeah, then creating the file should
>>> probably come last.
>>>
>> @Gaosheng: Could you please make these changes or you let me to do?
>>
>>> Regards,
>>> Christian.
>> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-11-24 12:37 ` Christian König
@ 2022-11-24 12:49 ` cuigaosheng
-1 siblings, 0 replies; 28+ messages in thread
From: cuigaosheng @ 2022-11-24 12:49 UTC (permalink / raw)
To: Christian König, Charan Teja Kalla, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
> I was already wondering why the order is this way.
>
> Why is dma_buf_stats_setup() needing the file in the first place?
dmabuf->file will be used in dma_buf_stats_setup(), the
dma_buf_stats_setup() as follows:
> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
> 172 {
> 173 struct dma_buf_sysfs_entry *sysfs_entry;
> 174 int ret;
> 175
> 176 if (!dmabuf || !dmabuf->file)
> 177 return -EINVAL;
> 178
> 179 if (!dmabuf->exp_name) {
> 180 pr_err("exporter name must not be empty if stats
> needed\n");
> 181 return -EINVAL;
> 182 }
> 183
> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
> GFP_KERNEL);
> 185 if (!sysfs_entry)
> 186 return -ENOMEM;
> 187
> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
> 189 sysfs_entry->dmabuf = dmabuf;
> 190
> 191 dmabuf->sysfs_entry = sysfs_entry;
> 192
> 193 /* create the directory for buffer stats */
> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
> &dma_buf_ktype, NULL,
> 195 "%lu",
> file_inode(dmabuf->file)->i_ino);
> 196 if (ret)
> 197 goto err_sysfs_dmabuf;
> 198
> 199 return 0;
> 200
> 201 err_sysfs_dmabuf:
> 202 kobject_put(&sysfs_entry->kobj);
> 203 dmabuf->sysfs_entry = NULL;
> 204 return ret;
> 205 }
Did I miss something?
Thanks.
On 2022/11/24 20:37, Christian König wrote:
>
>
> Am 24.11.22 um 13:05 schrieb cuigaosheng:
>> Some tips:
>> Before we call the dma_buf_stats_setup(), we have to finish
>> creating the file,
>> otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need to
>> think about
>> this when making a new patch.
>
> I was already wondering why the order is this way.
>
> Why is dma_buf_stats_setup() needing the file in the first place?
>
> Thanks,
> Christian.
>
>>
>> Hope these tips are useful, thanks!
>>
>> On 2022/11/24 13:56, Charan Teja Kalla wrote:
>>> Thanks T.J and Christian for the inputs.
>>>
>>> On 11/19/2022 7:00 PM, Christian König wrote:
>>>>> 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.
>>>>
>>>> Oh, very good point as well. Yeah, then creating the file should
>>>> probably come last.
>>>>
>>> @Gaosheng: Could you please make these changes or you let me to do?
>>>
>>>> Regards,
>>>> Christian.
>>> .
>
> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-11-24 12:49 ` cuigaosheng
0 siblings, 0 replies; 28+ messages in thread
From: cuigaosheng @ 2022-11-24 12:49 UTC (permalink / raw)
To: Christian König, Charan Teja Kalla, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
> I was already wondering why the order is this way.
>
> Why is dma_buf_stats_setup() needing the file in the first place?
dmabuf->file will be used in dma_buf_stats_setup(), the
dma_buf_stats_setup() as follows:
> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
> 172 {
> 173 struct dma_buf_sysfs_entry *sysfs_entry;
> 174 int ret;
> 175
> 176 if (!dmabuf || !dmabuf->file)
> 177 return -EINVAL;
> 178
> 179 if (!dmabuf->exp_name) {
> 180 pr_err("exporter name must not be empty if stats
> needed\n");
> 181 return -EINVAL;
> 182 }
> 183
> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
> GFP_KERNEL);
> 185 if (!sysfs_entry)
> 186 return -ENOMEM;
> 187
> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
> 189 sysfs_entry->dmabuf = dmabuf;
> 190
> 191 dmabuf->sysfs_entry = sysfs_entry;
> 192
> 193 /* create the directory for buffer stats */
> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
> &dma_buf_ktype, NULL,
> 195 "%lu",
> file_inode(dmabuf->file)->i_ino);
> 196 if (ret)
> 197 goto err_sysfs_dmabuf;
> 198
> 199 return 0;
> 200
> 201 err_sysfs_dmabuf:
> 202 kobject_put(&sysfs_entry->kobj);
> 203 dmabuf->sysfs_entry = NULL;
> 204 return ret;
> 205 }
Did I miss something?
Thanks.
On 2022/11/24 20:37, Christian König wrote:
>
>
> Am 24.11.22 um 13:05 schrieb cuigaosheng:
>> Some tips:
>> Before we call the dma_buf_stats_setup(), we have to finish
>> creating the file,
>> otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need to
>> think about
>> this when making a new patch.
>
> I was already wondering why the order is this way.
>
> Why is dma_buf_stats_setup() needing the file in the first place?
>
> Thanks,
> Christian.
>
>>
>> Hope these tips are useful, thanks!
>>
>> On 2022/11/24 13:56, Charan Teja Kalla wrote:
>>> Thanks T.J and Christian for the inputs.
>>>
>>> On 11/19/2022 7:00 PM, Christian König wrote:
>>>>> 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.
>>>>
>>>> Oh, very good point as well. Yeah, then creating the file should
>>>> probably come last.
>>>>
>>> @Gaosheng: Could you please make these changes or you let me to do?
>>>
>>>> Regards,
>>>> Christian.
>>> .
>
> .
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [Linaro-mm-sig] Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-11-24 12:49 ` cuigaosheng
@ 2022-11-24 12:55 ` Christian König
-1 siblings, 0 replies; 28+ messages in thread
From: Christian König @ 2022-11-24 12:55 UTC (permalink / raw)
To: cuigaosheng, Christian König, Charan Teja Kalla, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
Am 24.11.22 um 13:49 schrieb cuigaosheng:
>> I was already wondering why the order is this way.
>>
>> Why is dma_buf_stats_setup() needing the file in the first place?
>
> dmabuf->file will be used in dma_buf_stats_setup(), the
> dma_buf_stats_setup() as follows:
>
>> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
>> 172 {
>> 173 struct dma_buf_sysfs_entry *sysfs_entry;
>> 174 int ret;
>> 175
>> 176 if (!dmabuf || !dmabuf->file)
>> 177 return -EINVAL;
>> 178
>> 179 if (!dmabuf->exp_name) {
>> 180 pr_err("exporter name must not be empty if stats
>> needed\n");
>> 181 return -EINVAL;
>> 182 }
>> 183
>> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
>> GFP_KERNEL);
>> 185 if (!sysfs_entry)
>> 186 return -ENOMEM;
>> 187
>> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
>> 189 sysfs_entry->dmabuf = dmabuf;
>> 190
>> 191 dmabuf->sysfs_entry = sysfs_entry;
>> 192
>> 193 /* create the directory for buffer stats */
>> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
>> &dma_buf_ktype, NULL,
>> 195 "%lu",
>> file_inode(dmabuf->file)->i_ino);
Ah, so it uses the i_ino of the file for the sysfs unique name.
I'm going to take another look how to properly clean this up.
Thanks for pointing this out,
Christian.
>> 196 if (ret)
>> 197 goto err_sysfs_dmabuf;
>> 198
>> 199 return 0;
>> 200
>> 201 err_sysfs_dmabuf:
>> 202 kobject_put(&sysfs_entry->kobj);
>> 203 dmabuf->sysfs_entry = NULL;
>> 204 return ret;
>> 205 }
> Did I miss something?
>
> Thanks.
>
> On 2022/11/24 20:37, Christian König wrote:
>>
>>
>> Am 24.11.22 um 13:05 schrieb cuigaosheng:
>>> Some tips:
>>> Before we call the dma_buf_stats_setup(), we have to finish
>>> creating the file,
>>> otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need
>>> to think about
>>> this when making a new patch.
>>
>> I was already wondering why the order is this way.
>>
>> Why is dma_buf_stats_setup() needing the file in the first place?
>>
>> Thanks,
>> Christian.
>>
>>>
>>> Hope these tips are useful, thanks!
>>>
>>> On 2022/11/24 13:56, Charan Teja Kalla wrote:
>>>> Thanks T.J and Christian for the inputs.
>>>>
>>>> On 11/19/2022 7:00 PM, Christian König wrote:
>>>>>> 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.
>>>>>
>>>>> Oh, very good point as well. Yeah, then creating the file should
>>>>> probably come last.
>>>>>
>>>> @Gaosheng: Could you please make these changes or you let me to do?
>>>>
>>>>> Regards,
>>>>> Christian.
>>>> .
>>
>> .
> _______________________________________________
> Linaro-mm-sig mailing list -- linaro-mm-sig@lists.linaro.org
> To unsubscribe send an email to linaro-mm-sig-leave@lists.linaro.org
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [Linaro-mm-sig] Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-11-24 12:55 ` Christian König
0 siblings, 0 replies; 28+ messages in thread
From: Christian König @ 2022-11-24 12:55 UTC (permalink / raw)
To: cuigaosheng, Christian König, Charan Teja Kalla, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
Am 24.11.22 um 13:49 schrieb cuigaosheng:
>> I was already wondering why the order is this way.
>>
>> Why is dma_buf_stats_setup() needing the file in the first place?
>
> dmabuf->file will be used in dma_buf_stats_setup(), the
> dma_buf_stats_setup() as follows:
>
>> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
>> 172 {
>> 173 struct dma_buf_sysfs_entry *sysfs_entry;
>> 174 int ret;
>> 175
>> 176 if (!dmabuf || !dmabuf->file)
>> 177 return -EINVAL;
>> 178
>> 179 if (!dmabuf->exp_name) {
>> 180 pr_err("exporter name must not be empty if stats
>> needed\n");
>> 181 return -EINVAL;
>> 182 }
>> 183
>> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
>> GFP_KERNEL);
>> 185 if (!sysfs_entry)
>> 186 return -ENOMEM;
>> 187
>> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
>> 189 sysfs_entry->dmabuf = dmabuf;
>> 190
>> 191 dmabuf->sysfs_entry = sysfs_entry;
>> 192
>> 193 /* create the directory for buffer stats */
>> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
>> &dma_buf_ktype, NULL,
>> 195 "%lu",
>> file_inode(dmabuf->file)->i_ino);
Ah, so it uses the i_ino of the file for the sysfs unique name.
I'm going to take another look how to properly clean this up.
Thanks for pointing this out,
Christian.
>> 196 if (ret)
>> 197 goto err_sysfs_dmabuf;
>> 198
>> 199 return 0;
>> 200
>> 201 err_sysfs_dmabuf:
>> 202 kobject_put(&sysfs_entry->kobj);
>> 203 dmabuf->sysfs_entry = NULL;
>> 204 return ret;
>> 205 }
> Did I miss something?
>
> Thanks.
>
> On 2022/11/24 20:37, Christian König wrote:
>>
>>
>> Am 24.11.22 um 13:05 schrieb cuigaosheng:
>>> Some tips:
>>> Before we call the dma_buf_stats_setup(), we have to finish
>>> creating the file,
>>> otherwise dma_buf_stats_setup() will return -EINVAL, maybe we need
>>> to think about
>>> this when making a new patch.
>>
>> I was already wondering why the order is this way.
>>
>> Why is dma_buf_stats_setup() needing the file in the first place?
>>
>> Thanks,
>> Christian.
>>
>>>
>>> Hope these tips are useful, thanks!
>>>
>>> On 2022/11/24 13:56, Charan Teja Kalla wrote:
>>>> Thanks T.J and Christian for the inputs.
>>>>
>>>> On 11/19/2022 7:00 PM, Christian König wrote:
>>>>>> 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.
>>>>>
>>>>> Oh, very good point as well. Yeah, then creating the file should
>>>>> probably come last.
>>>>>
>>>> @Gaosheng: Could you please make these changes or you let me to do?
>>>>
>>>>> Regards,
>>>>> Christian.
>>>> .
>>
>> .
> _______________________________________________
> Linaro-mm-sig mailing list -- linaro-mm-sig@lists.linaro.org
> To unsubscribe send an email to linaro-mm-sig-leave@lists.linaro.org
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [Linaro-mm-sig] Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-11-24 12:55 ` Christian König
@ 2022-12-06 12:55 ` Charan Teja Kalla
-1 siblings, 0 replies; 28+ messages in thread
From: Charan Teja Kalla @ 2022-12-06 12:55 UTC (permalink / raw)
To: Christian König, cuigaosheng, Christian König, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
Thanks Christian/TJ for all your inputs!!
On 11/24/2022 6:25 PM, Christian König wrote:
>>> I was already wondering why the order is this way.
>>>
>>> Why is dma_buf_stats_setup() needing the file in the first place?
>>
>> dmabuf->file will be used in dma_buf_stats_setup(), the
>> dma_buf_stats_setup() as follows:
>>
>>> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
>>> 172 {
>>> 173 struct dma_buf_sysfs_entry *sysfs_entry;
>>> 174 int ret;
>>> 175
>>> 176 if (!dmabuf || !dmabuf->file)
>>> 177 return -EINVAL;
>>> 178
>>> 179 if (!dmabuf->exp_name) {
>>> 180 pr_err("exporter name must not be empty if stats
>>> needed\n");
>>> 181 return -EINVAL;
>>> 182 }
>>> 183
>>> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
>>> GFP_KERNEL);
>>> 185 if (!sysfs_entry)
>>> 186 return -ENOMEM;
>>> 187
>>> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
>>> 189 sysfs_entry->dmabuf = dmabuf;
>>> 190
>>> 191 dmabuf->sysfs_entry = sysfs_entry;
>>> 192
>>> 193 /* create the directory for buffer stats */
>>> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
>>> &dma_buf_ktype, NULL,
>>> 195 "%lu",
>>> file_inode(dmabuf->file)->i_ino);
>
> Ah, so it uses the i_ino of the file for the sysfs unique name.
>
> I'm going to take another look how to properly clean this up.
>
How about deleting the dmabuf from the db_list directly in the error
path (which is usually done by the fput()) and then continue with the
normal fput() here.
Just compile tested the below code and If the logic make sense for you,
will send the final tested patch.
----------------------><---------------------------------------------
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index e6f36c0..10a1727 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -87,19 +87,28 @@ static void dma_buf_release(struct dentry *dentry)
kfree(dmabuf);
}
-static int dma_buf_file_release(struct inode *inode, struct file *file)
+static void dma_buf_db_list_remove(struct file *file)
{
struct dma_buf *dmabuf;
- if (!is_dma_buf_file(file))
- return -EINVAL;
-
dmabuf = file->private_data;
+ if (!dmabuf)
+ return;
mutex_lock(&db_list.lock);
list_del(&dmabuf->list_node);
mutex_unlock(&db_list.lock);
+ file->private_data = NULL;
+}
+
+static int dma_buf_file_release(struct inode *inode, struct file *file)
+{
+ if (!is_dma_buf_file(file))
+ return -EINVAL;
+
+ dma_buf_db_list_remove(file);
+
return 0;
}
@@ -688,6 +697,8 @@ struct dma_buf *dma_buf_export(const struct
dma_buf_export_info *exp_info)
* early before calling the release() dma_buf op.
*/
file->f_path.dentry->d_fsdata = NULL;
+
+ dma_buf_db_list_remove(file);
fput(file);
err_dmabuf:
kfree(dmabuf);
--------------------><-----------------------------
> Thanks for pointing this out,
> Christian.
^ permalink raw reply related [flat|nested] 28+ messages in thread
* Re: [Linaro-mm-sig] Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-12-06 12:55 ` Charan Teja Kalla
0 siblings, 0 replies; 28+ messages in thread
From: Charan Teja Kalla @ 2022-12-06 12:55 UTC (permalink / raw)
To: Christian König, cuigaosheng, Christian König, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
Thanks Christian/TJ for all your inputs!!
On 11/24/2022 6:25 PM, Christian König wrote:
>>> I was already wondering why the order is this way.
>>>
>>> Why is dma_buf_stats_setup() needing the file in the first place?
>>
>> dmabuf->file will be used in dma_buf_stats_setup(), the
>> dma_buf_stats_setup() as follows:
>>
>>> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
>>> 172 {
>>> 173 struct dma_buf_sysfs_entry *sysfs_entry;
>>> 174 int ret;
>>> 175
>>> 176 if (!dmabuf || !dmabuf->file)
>>> 177 return -EINVAL;
>>> 178
>>> 179 if (!dmabuf->exp_name) {
>>> 180 pr_err("exporter name must not be empty if stats
>>> needed\n");
>>> 181 return -EINVAL;
>>> 182 }
>>> 183
>>> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
>>> GFP_KERNEL);
>>> 185 if (!sysfs_entry)
>>> 186 return -ENOMEM;
>>> 187
>>> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
>>> 189 sysfs_entry->dmabuf = dmabuf;
>>> 190
>>> 191 dmabuf->sysfs_entry = sysfs_entry;
>>> 192
>>> 193 /* create the directory for buffer stats */
>>> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
>>> &dma_buf_ktype, NULL,
>>> 195 "%lu",
>>> file_inode(dmabuf->file)->i_ino);
>
> Ah, so it uses the i_ino of the file for the sysfs unique name.
>
> I'm going to take another look how to properly clean this up.
>
How about deleting the dmabuf from the db_list directly in the error
path (which is usually done by the fput()) and then continue with the
normal fput() here.
Just compile tested the below code and If the logic make sense for you,
will send the final tested patch.
----------------------><---------------------------------------------
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index e6f36c0..10a1727 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -87,19 +87,28 @@ static void dma_buf_release(struct dentry *dentry)
kfree(dmabuf);
}
-static int dma_buf_file_release(struct inode *inode, struct file *file)
+static void dma_buf_db_list_remove(struct file *file)
{
struct dma_buf *dmabuf;
- if (!is_dma_buf_file(file))
- return -EINVAL;
-
dmabuf = file->private_data;
+ if (!dmabuf)
+ return;
mutex_lock(&db_list.lock);
list_del(&dmabuf->list_node);
mutex_unlock(&db_list.lock);
+ file->private_data = NULL;
+}
+
+static int dma_buf_file_release(struct inode *inode, struct file *file)
+{
+ if (!is_dma_buf_file(file))
+ return -EINVAL;
+
+ dma_buf_db_list_remove(file);
+
return 0;
}
@@ -688,6 +697,8 @@ struct dma_buf *dma_buf_export(const struct
dma_buf_export_info *exp_info)
* early before calling the release() dma_buf op.
*/
file->f_path.dentry->d_fsdata = NULL;
+
+ dma_buf_db_list_remove(file);
fput(file);
err_dmabuf:
kfree(dmabuf);
--------------------><-----------------------------
> Thanks for pointing this out,
> Christian.
^ permalink raw reply related [flat|nested] 28+ messages in thread
* Re: [Linaro-mm-sig] Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
2022-12-06 12:55 ` Charan Teja Kalla
@ 2022-12-06 13:08 ` Christian König
-1 siblings, 0 replies; 28+ messages in thread
From: Christian König @ 2022-12-06 13:08 UTC (permalink / raw)
To: Charan Teja Kalla, Christian König, cuigaosheng, T.J. Mercier
Cc: Pavan Kondeti, dri-devel, linaro-mm-sig, sumit.semwal,
Dan Carpenter, linux-media
Am 06.12.22 um 13:55 schrieb Charan Teja Kalla:
> Thanks Christian/TJ for all your inputs!!
>
> On 11/24/2022 6:25 PM, Christian König wrote:
>>>> I was already wondering why the order is this way.
>>>>
>>>> Why is dma_buf_stats_setup() needing the file in the first place?
>>> dmabuf->file will be used in dma_buf_stats_setup(), the
>>> dma_buf_stats_setup() as follows:
>>>
>>>> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
>>>> 172 {
>>>> 173 struct dma_buf_sysfs_entry *sysfs_entry;
>>>> 174 int ret;
>>>> 175
>>>> 176 if (!dmabuf || !dmabuf->file)
>>>> 177 return -EINVAL;
>>>> 178
>>>> 179 if (!dmabuf->exp_name) {
>>>> 180 pr_err("exporter name must not be empty if stats
>>>> needed\n");
>>>> 181 return -EINVAL;
>>>> 182 }
>>>> 183
>>>> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
>>>> GFP_KERNEL);
>>>> 185 if (!sysfs_entry)
>>>> 186 return -ENOMEM;
>>>> 187
>>>> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
>>>> 189 sysfs_entry->dmabuf = dmabuf;
>>>> 190
>>>> 191 dmabuf->sysfs_entry = sysfs_entry;
>>>> 192
>>>> 193 /* create the directory for buffer stats */
>>>> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
>>>> &dma_buf_ktype, NULL,
>>>> 195 "%lu",
>>>> file_inode(dmabuf->file)->i_ino);
>> Ah, so it uses the i_ino of the file for the sysfs unique name.
>>
>> I'm going to take another look how to properly clean this up.
>>
> How about deleting the dmabuf from the db_list directly in the error
> path (which is usually done by the fput()) and then continue with the
> normal fput() here.
No, that's not really clean either.
Give me 10 Minutes, going to come up with something.
Regards,
Christian.
>
> Just compile tested the below code and If the logic make sense for you,
> will send the final tested patch.
> ----------------------><---------------------------------------------
>
> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
> index e6f36c0..10a1727 100644
> --- a/drivers/dma-buf/dma-buf.c
> +++ b/drivers/dma-buf/dma-buf.c
> @@ -87,19 +87,28 @@ static void dma_buf_release(struct dentry *dentry)
> kfree(dmabuf);
> }
>
> -static int dma_buf_file_release(struct inode *inode, struct file *file)
> +static void dma_buf_db_list_remove(struct file *file)
> {
> struct dma_buf *dmabuf;
>
> - if (!is_dma_buf_file(file))
> - return -EINVAL;
> -
> dmabuf = file->private_data;
> + if (!dmabuf)
> + return;
>
> mutex_lock(&db_list.lock);
> list_del(&dmabuf->list_node);
> mutex_unlock(&db_list.lock);
>
> + file->private_data = NULL;
> +}
> +
> +static int dma_buf_file_release(struct inode *inode, struct file *file)
> +{
> + if (!is_dma_buf_file(file))
> + return -EINVAL;
> +
> + dma_buf_db_list_remove(file);
> +
> return 0;
> }
>
> @@ -688,6 +697,8 @@ struct dma_buf *dma_buf_export(const struct
> dma_buf_export_info *exp_info)
> * early before calling the release() dma_buf op.
> */
> file->f_path.dentry->d_fsdata = NULL;
> +
> + dma_buf_db_list_remove(file);
> fput(file);
> err_dmabuf:
> kfree(dmabuf);
>
> --------------------><-----------------------------
>
>
>> Thanks for pointing this out,
>> Christian.
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: [Linaro-mm-sig] Re: [PATCH] dma-buf: Fix possible UAF in dma_buf_export
@ 2022-12-06 13:08 ` Christian König
0 siblings, 0 replies; 28+ messages in thread
From: Christian König @ 2022-12-06 13:08 UTC (permalink / raw)
To: Charan Teja Kalla, Christian König, cuigaosheng, T.J. Mercier
Cc: sumit.semwal, Dan Carpenter, Pavan Kondeti, linux-media,
dri-devel, linaro-mm-sig
Am 06.12.22 um 13:55 schrieb Charan Teja Kalla:
> Thanks Christian/TJ for all your inputs!!
>
> On 11/24/2022 6:25 PM, Christian König wrote:
>>>> I was already wondering why the order is this way.
>>>>
>>>> Why is dma_buf_stats_setup() needing the file in the first place?
>>> dmabuf->file will be used in dma_buf_stats_setup(), the
>>> dma_buf_stats_setup() as follows:
>>>
>>>> 171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
>>>> 172 {
>>>> 173 struct dma_buf_sysfs_entry *sysfs_entry;
>>>> 174 int ret;
>>>> 175
>>>> 176 if (!dmabuf || !dmabuf->file)
>>>> 177 return -EINVAL;
>>>> 178
>>>> 179 if (!dmabuf->exp_name) {
>>>> 180 pr_err("exporter name must not be empty if stats
>>>> needed\n");
>>>> 181 return -EINVAL;
>>>> 182 }
>>>> 183
>>>> 184 sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry),
>>>> GFP_KERNEL);
>>>> 185 if (!sysfs_entry)
>>>> 186 return -ENOMEM;
>>>> 187
>>>> 188 sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
>>>> 189 sysfs_entry->dmabuf = dmabuf;
>>>> 190
>>>> 191 dmabuf->sysfs_entry = sysfs_entry;
>>>> 192
>>>> 193 /* create the directory for buffer stats */
>>>> 194 ret = kobject_init_and_add(&sysfs_entry->kobj,
>>>> &dma_buf_ktype, NULL,
>>>> 195 "%lu",
>>>> file_inode(dmabuf->file)->i_ino);
>> Ah, so it uses the i_ino of the file for the sysfs unique name.
>>
>> I'm going to take another look how to properly clean this up.
>>
> How about deleting the dmabuf from the db_list directly in the error
> path (which is usually done by the fput()) and then continue with the
> normal fput() here.
No, that's not really clean either.
Give me 10 Minutes, going to come up with something.
Regards,
Christian.
>
> Just compile tested the below code and If the logic make sense for you,
> will send the final tested patch.
> ----------------------><---------------------------------------------
>
> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
> index e6f36c0..10a1727 100644
> --- a/drivers/dma-buf/dma-buf.c
> +++ b/drivers/dma-buf/dma-buf.c
> @@ -87,19 +87,28 @@ static void dma_buf_release(struct dentry *dentry)
> kfree(dmabuf);
> }
>
> -static int dma_buf_file_release(struct inode *inode, struct file *file)
> +static void dma_buf_db_list_remove(struct file *file)
> {
> struct dma_buf *dmabuf;
>
> - if (!is_dma_buf_file(file))
> - return -EINVAL;
> -
> dmabuf = file->private_data;
> + if (!dmabuf)
> + return;
>
> mutex_lock(&db_list.lock);
> list_del(&dmabuf->list_node);
> mutex_unlock(&db_list.lock);
>
> + file->private_data = NULL;
> +}
> +
> +static int dma_buf_file_release(struct inode *inode, struct file *file)
> +{
> + if (!is_dma_buf_file(file))
> + return -EINVAL;
> +
> + dma_buf_db_list_remove(file);
> +
> return 0;
> }
>
> @@ -688,6 +697,8 @@ struct dma_buf *dma_buf_export(const struct
> dma_buf_export_info *exp_info)
> * early before calling the release() dma_buf op.
> */
> file->f_path.dentry->d_fsdata = NULL;
> +
> + dma_buf_db_list_remove(file);
> fput(file);
> err_dmabuf:
> kfree(dmabuf);
>
> --------------------><-----------------------------
>
>
>> Thanks for pointing this out,
>> Christian.
^ permalink raw reply [flat|nested] 28+ messages in thread