ptp: annotate data-race around q->head and q->tail
Subsystems (3)
PATCH 1/1 ptp: annotate data-race around q->head and q->tail
As I was working on a syzbot report, I found that KCSAN would probably complain that reading q->head or q->tail without barriers could lead to invalid results.
Add corresponding READ_ONCE() and WRITE_ONCE() to avoid load-store tearing.
--- drivers/ptp/ptp_chardev.c | 3 ++- drivers/ptp/ptp_clock.c | 5 +++-- drivers/ptp/ptp_private.h | 8 ++++++-- drivers/ptp/ptp_sysfs.c | 3 ++- 4 files changed, 13 insertions(+), 6 deletions(-)
drivers/ptp/ptp_chardev.c +2 -1
@@ -572,7 +572,8 @@ ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,for (i =0 ; i < cnt; i++) { event[i] = queue->buf[queue->head]; - queue->head = (queue->head +1 ) %PTP_MAX_TIMESTAMPS ; +/* Paired with READ_ONCE() in queue_cnt() */ +WRITE_ONCE (queue->head, (queue->head +1 ) %PTP_MAX_TIMESTAMPS ); }spin_unlock_irqrestore (&queue->lock, flags);
drivers/ptp/ptp_clock.c +3 -2
@@ -57,10 +57,11 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue, dst->t.sec = seconds; dst->t.nsec = remainder; +/* Both WRITE_ONCE() are paired with READ_ONCE() in queue_cnt() */ if (!queue_free (queue)) - queue->head = (queue->head +1 ) %PTP_MAX_TIMESTAMPS ; +WRITE_ONCE (queue->head, (queue->head +1 ) %PTP_MAX_TIMESTAMPS ); - queue->tail = (queue->tail +1 ) %PTP_MAX_TIMESTAMPS ; +WRITE_ONCE (queue->tail, (queue->tail +1 ) %PTP_MAX_TIMESTAMPS );spin_unlock_irqrestore (&queue->lock, flags); }
drivers/ptp/ptp_private.h +6 -2
@@ -85,9 +85,13 @@ struct ptp_vclock {* that a writer might concurrently increment the tail does not * matter, since the queue remains nonempty nonetheless. */ -static inline int queue_cnt (struct timestamp_event_queue *q) +static inline int queue_cnt (const struct timestamp_event_queue *q) { -int cnt = q->tail - q->head; +/* +* Paired with WRITE_ONCE() in enqueue_external_timestamp(), +* ptp_read(), extts_fifo_show(). +*/ +int cnt =READ_ONCE (q->tail) -READ_ONCE (q->head);return cnt <0 ?PTP_MAX_TIMESTAMPS + cnt : cnt; }
drivers/ptp/ptp_sysfs.c +2 -1
@@ -94,7 +94,8 @@ static ssize_t extts_fifo_show(struct device *dev, qcnt =queue_cnt (queue);if (qcnt) { event = queue->buf[queue->head]; - queue->head = (queue->head +1 ) %PTP_MAX_TIMESTAMPS ; +/* Paired with READ_ONCE() in queue_cnt() */ +WRITE_ONCE (queue->head, (queue->head +1 ) %PTP_MAX_TIMESTAMPS ); }spin_unlock_irqrestore (&queue->lock, flags);
Signature · 1 line
2.42.0.869.gea05f2083d-goog
Quoted 6 lines · Eric Dumazet
As I was working on a syzbot report, I found that KCSAN would probably complain that reading
q->headorq->tailwithout barriers could lead to invalid results.Add corresponding
READ_ONCE()andWRITE_ONCE()to avoid load-store tearing.
Quoted 10 lines · Richard Cochran
Quoted 6 lines · Eric Dumazet
As I was working on a syzbot report, I found that KCSAN would probably complain that reading
q->headorq->tailwithout barriers could lead to invalid results.Add corresponding
READ_ONCE()andWRITE_ONCE()to avoid load-store tearing.Acked-by Richard Cochran <[email protected]>
Note the syzbot report I am looking at point to bugs added in
commit 8f5de6fb245326704f37d91780b9a10253a8a100 ptp: support
multiple timestamp event readers
For instance ptp_poll() can crash.
I saw the following patch being merged (without me being CC ?)
commit 8a4f030dbced6fc255cbe67b2d0a129947e18493
Author: Yuran Pereira <[email protected]> Date: Wed Nov 8 02:18:36 2023 +0530
ptp: Fixes a null pointer dereference in ptp_ioctl
I do not see how races are solved... Shouldn'tpccontext->private_clkdata be protected by RCU ?
Richard Cochran
maintainerQuoted 2 lines · Eric Dumazet
I do not see how races are solved... Shouldn't
pccontext->private_clkdatabe protected by RCU ?
Yeah, the test is useless, because the memory is allocated in open() and later freed in release(). During ioctl() the pointer must be valid.
However, there was a bogus call to ptp_release() in the read() method, but that has now been removed, and so the test is now bogus.
Thanks,
Richard
Jakub Kicinski
maintainerQuoted 9 lines · On Fri, 10 Nov 2023 09:09:45 -0800 Richard Cochran wrote:
Quoted 2 lines
I do not see how races are solved... Shouldn't
pccontext->private_clkdatabe protected by RCU ?Yeah, the test is useless, because the memory is allocated in
open()and later freed inrelease(). Duringioctl()the pointer must be valid.However, there was a bogus call to
ptp_release()in theread()method, but that has now been removed, and so the test is now bogus.
Meaning we should revert 8a4f030dbced ("ptp: Fixes a null pointer dereference in ptp_ioctl") ?
Richard Cochran
maintainerQuoted 2 lines · Jakub Kicinski
Meaning we should revert
8a4f030dbced("ptp: Fixes a null pointer dereference inptp_ioctl") ?
Yes, I think that would be ideal.
The test itself is harmless, but keeping it will make people think, "oh this pointer can be invalid."
In fact the core stack ensures that ioctl() can't be invoked after release(), otherwise Bad Stuff happens.
Thanks,
Richard
Jakub Kicinski
maintainerQuoted 4 lines · On Sat, 11 Nov 2023 16:44:46 -0800 Richard Cochran wrote:
Quoted 2 lines
Meaning we should revert
8a4f030dbced("ptp: Fixes a null pointer dereference inptp_ioctl") ?Yes, I think that would be ideal.
Done!
BTW Eric patch is series id 800000 in patchwork.
Nice and round number :)
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Thu, 9 Nov 2023 17:48:59 +0000 you wrote:
As I was working on a syzbot report, I found that KCSAN would probably complain that reading
q->headorq->tailwithout barriers could lead to invalid results.Add corresponding
READ_ONCE()andWRITE_ONCE()to avoid load-store tearing.[...]
Here is the summary with links:
- [net] ptp: annotate data-race around q->head and q->tail
https://git.kernel.org/netdev/net/c/73bde5a32948
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html