Re: [PATCH] nfc: pn533: Fix null-ptr-deref in pn533_recv_frame()

From: Krzysztof Kozlowski
Date: Wed Apr 24 2024 - 14:13:49 EST


On 24/04/2024 16:00, Yuxuan-Hu wrote:
> Hi Krzysztof,
> Thanks for your reply.
>
> On 2024/4/24 13:36, Krzysztof Kozlowski wrote:
>> A bit better solution would be to NULL-ify dev->cmd at the beginning of
>> pn533_send_async_complete(), because that seems logical. The complete
>> callback takes ownership of dev->cmd, so why it performs the assignment
>> at the end?
>>
>> However even above code will keep the race open for short period.
>> Probably some locking would solve it or checking for dev->cmd in few
>> places with barriers.
>>
>> Best regards,
>> Krzysztof
>
> I think adding a lock seems to be a better solution, however, acquire a
> lock on each access to dev->cmd does not seem to be an appropriate
> implementation.
> I wonder whether you think it is appropriate to acquire a lock at the
> beginning of pn533_recv_frame(), and release it when
> pn533_wq_cmd_complete() is finished, thus ensuring that another work
> will not start when one pn533_wq_cmd_complete() work is not yet finished.

I don't know this driver well enough, but it currently does not have any
locking around this so it assumes pn533_recv_frame() cannot start
concurrently, before complete callback finishes.

This could be solved, if it makes sense.

However maybe it would be enough to take:
1. Move contents of dev->cmd to another member in dev (e.g.
dev->cmd_in_progress), first checking if that member is empty (then
maybe wait? or return -EBUSY?)
2. dev->cmd = NULL
3. queue_work (see also explanation of barriers there)
4. The cmd_complete_work workqueue takes ownership of
dev->cmd_in_progress and NULL-ifies it at end of work + barrier.

Best regards,
Krzysztof