strparser: Use worker disable API instead of cancellation in strp_done()
Subsystems (2)
PATCH 1/1 strparser: Use worker disable API instead of cancellation in strp_done()
When strp_stop() and strp_done() are called without holding lock_sock(), they can race with worker-scheduling paths such as the Delayed ACK handler and ksoftirqd.
Specifically, after cancel_delayed_work_sync() and cancel_work_sync() are invoked from strp_done(), the workers may still be scheduled. As a result, the workers may dereference freed objects.
To prevent these races, the cancellation APIs are replaced with worker-disabling APIs.
--- net/strparser/strparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/strparser/strparser.c +2 -2
@@ -503,8 +503,8 @@ void strp_done(struct strparser *strp) {WARN_ON (!strp->stopped); -cancel_delayed_work_sync (&strp->msg_timer_work); -cancel_work_sync (&strp->work); +disable_delayed_work_sync (&strp->msg_timer_work); +disable_work_sync (&strp->work);if (strp->skb_head) {kfree_skb (strp->skb_head);
Signature · 43 lines
2.43.0
---
Dear,
The following is a simplified scenario illustrating how each race can occur. Since espintcp_close() does not hold lock_sock(), the race is possible.
Although cancel_work_sync(&strp->work) does not appear to be easy to trigger in practice here, it still seems better to fix it as well.
```
cpu0 cpu1
espintcp_close()
espintcp_data_ready()
if (unlikely(strp->stopped)) return;
strp_stop()
strp->stopped = 1;
strp_done()
cancel_delayed_work_sync(&strp->msg_timer_work);
strp_data_ready()
strp_read_sock()
tcp_read_sock()
__tcp_read_sock()
strp_recv()
__strp_recv()
strp_start_timer()
mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
```
```
cpu0 cpu1
espintcp_close()
sk->sk_data_ready()
espintcp_data_ready()
if (unlikely(strp->stopped)) return;
strp_stop()
strp->stopped = 1;
strp_done()
cancel_work_sync(&strp->work);
if (strp_read_sock(strp) == -ENOMEM)
queue_work()
```
Best regards,
Hyunwoo KimQuoted 11 lines · Hyunwoo Kim
When
strp_stop()andstrp_done()are called without holdinglock_sock(), they can race with worker-scheduling paths such as the Delayed ACK handler and ksoftirqd.
Specifically, aftercancel_delayed_work_sync()andcancel_work_sync()are invoked fromstrp_done(), the workers may still be scheduled. As a result, the workers may dereference freed objects.To prevent these races, the cancellation APIs are replaced with worker-disabling APIs.
Fixes 829385f08ae9 ("strparser: Use delayed work instead of timer for msg timeout")
That's the correct commit for msg_timer_work, but not for strp->work. No race was possible when msg timeout was using a timer? Your second scenario relies only on strp->work so I would think yes.
Quoted 10 lines
Signed-off-by Hyunwoo Kim <[email protected]>--- net/strparser/strparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/strparser/strparser.c +0
-0
net/strparser/strparser.c void strp_done(struct strparser *strp) · line 507
Quoted 7 lines
net/strparser/strparser.c +2
-2@@ -503,8 +503,8 @@ void strp_done(struct strparser *strp) {WARN_ON (!strp->stopped); -cancel_delayed_work_sync (&strp->msg_timer_work); -cancel_work_sync (&strp->work); +disable_delayed_work_sync (&strp->msg_timer_work); +disable_work_sync (&strp->work);
The change itself looks reasonable.
Quoted 10 lines
net/strparser/strparser.c +0
-0if (strp->skb_head) {kfree_skb (strp->skb_head);Signature · 7 lines
2.43.0 --- Dear, The following is a simplified scenario illustrating how each race can occur. Since espintcp_close() does not hold lock_sock(), the race is possible. Although cancel_work_sync(&strp->work) does not appear to be easy to trigger in practice here, it still seems better to fix it as well.
What about the other users of strp? Only espintcp is racy?
If strp_done can run concurrently with __strp_recv, it seems we could also end up leaking strp->skb_head, if __strp_recv stores a new one after we've cleared the old?
Quoted 11 lines
```
cpu0 cpu1
espintcp_close()espintcp_data_ready() if (unlikely(strp->stopped)) return; strp_stop() strp->stopped = 1; strp_done() cancel_delayed_work_sync(&strp->msg_timer_work); strp_data_ready()
In this order, strp_data_ready will see strp->stopped and return without doing anything.
(I'm confused by the "if (unlikely(strp->stopped))" above though, maybe you meant espintcp_data_ready -> strp_data_ready -> if (...))
Quoted 12 lines
strp_read_sock() tcp_read_sock() __tcp_read_sock() strp_recv() __strp_recv() strp_start_timer() mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo); ``` ```cpu0 cpu1
espintcp_close()
Quoted 9 lines
sk->sk_data_ready()
espintcp_data_ready()
if (unlikely(strp->stopped)) return;
strp_stop()
strp->stopped = 1;
strp_done()
cancel_work_sync(&strp->work);
if (strp_read_sock(strp) == -ENOMEM)
queue_work()Here the problem would be if we enter do_strp_work after all the socket data has already been freed? Otherwise again the test on strp->stopped will make do_strp_work return early. (this would be unexpected but should be safe)
Quoted 4 lines
```
Best regards,
Hyunwoo Kim
Signature · 1 line
Sabrina
Quoted 8 lines · Sabrina Dubroca
Quoted 7 lines · Hyunwoo Kim
When
strp_stop()andstrp_done()are called without holdinglock_sock(), they can race with worker-scheduling paths such as the Delayed ACK handler and ksoftirqd.
Specifically, aftercancel_delayed_work_sync()andcancel_work_sync()are invoked fromstrp_done(), the workers may still be scheduled. As a result, the workers may dereference freed objects.
Quoted 7 lines
Quoted 4 lines
To prevent these races, the cancellation APIs are replaced with worker-disabling APIs.
Fixes 829385f08ae9 ("strparser: Use delayed work instead of timer for msg timeout")That's the correct commit for
msg_timer_work, but not forstrp->work. No race was possible when msg timeout was using a timer?
Of course, the race could also occur when the message timeout was implemented using a timer.
Quoted 1 line
Your second scenario relies only on
strp->workso I would think yes.
Using Fixes: bbb0302 ("strparser: Generalize strparser") should cover both cases.
Quoted 26 lines
Quoted 11 lines
Signed-off-by Hyunwoo Kim <[email protected]>--- net/strparser/strparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/strparser/strparser.c +0
-0net/strparser/strparser.c void strp_done(struct strparser *strp) · line 507
Quoted 7 lines
net/strparser/strparser.c +2
-2@@ -503,8 +503,8 @@ void strp_done(struct strparser *strp) {WARN_ON (!strp->stopped); -cancel_delayed_work_sync (&strp->msg_timer_work); -cancel_work_sync (&strp->work); +disable_delayed_work_sync (&strp->msg_timer_work); +disable_work_sync (&strp->work);The change itself looks reasonable.
Quoted 5 lines
net/strparser/strparser.c +0
-0if (strp->skb_head) {kfree_skb (strp->skb_head);Signature · 1 line
2.43.0
Quoted 7 lines
Quoted 5 lines
---
Dear,The following is a simplified scenario illustrating how each race can occur. Since
espintcp_close()does not holdlock_sock(), the race is possible. Althoughcancel_work_sync(&strp->work)does not appear to be easy to trigger in practice here, it still seems better to fix it as well.What about the other users of strp? Only espintcp is racy?
Any subsystem that calls strp_stop() and strp_done() outside of lock_sock() is racy.
Quoted 4 lines
If
strp_donecan run concurrently with__strp_recv, it seems we could also end up leakingstrp->skb_head, if__strp_recvstores a new one after we've cleared the old?
I am not fully sure about this part yet, so I think it should be discussed separately in another thread.
Quoted 14 lines
Quoted 5 lines
```
cpu0 cpu1Quoted 8 lines
espintcp_close()espintcp_data_ready() if (unlikely(strp->stopped)) return; strp_stop() strp->stopped = 1; strp_done() cancel_delayed_work_sync(&strp->msg_timer_work); strp_data_ready()
Quoted 5 lines
In this order,
strp_data_readywill seestrp->stoppedand return without doing anything.(I'm confused by the "if (
unlikely(strp->stopped))" above though, maybe you meantespintcp_data_ready->strp_data_ready-> if (...))
Sorry for the confusion. I accidentally mixed up the call order in the strp_data_ready() path.
More precisely, the scenario is that espintcp_data_ready() → strp_data_ready() runs first, passes the if (unlikely(strp->stopped)) check, and only after that strp->stopped = 1 is set.
```
cpu0 cpu1
espintcp_close()
espintcp_data_ready()
strp_data_ready()
if (unlikely(strp->stopped)) return;
strp_stop()
strp->stopped = 1;
strp_done()
cancel_delayed_work_sync(&strp->msg_timer_work);
strp_read_sock()
tcp_read_sock()
__tcp_read_sock()
strp_recv()
__strp_recv()
strp_start_timer()
mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo);
```
```
cpu0 cpu1
espintcp_close()
espintcp_data_ready()
strp_data_ready()
if (unlikely(strp->stopped)) return;
strp_stop()
strp->stopped = 1;
strp_done()
cancel_work_sync(&strp->work);
if (strp_read_sock(strp) == -ENOMEM)
queue_work()
```Quoted 23 lines
Quoted 13 lines
strp_read_sock() tcp_read_sock() __tcp_read_sock() strp_recv() __strp_recv() strp_start_timer() mod_delayed_work(strp_wq, &strp->msg_timer_work, timeo); ``` ```cpu0 cpu1
espintcp_close()Quoted 9 lines
sk->sk_data_ready() espintcp_data_ready() if (unlikely(strp->stopped)) return; strp_stop() strp->stopped = 1; strp_done() cancel_work_sync(&strp->work); if (strp_read_sock(strp) == -ENOMEM) queue_work()
Quoted 4 lines
Here the problem would be if we enter
do_strp_workafter all the socket data has already been freed? Otherwise again the test onstrp->stoppedwill makedo_strp_workreturn early. (this would be unexpected but should be safe)
If the worker is scheduled after the cancel call, then during espintcp_close() → tcp_close(), the sk will be freed and the ctx will be freed as well.
As a result, the kworker will access the freed ctx->strp. This access to the freed ctx happens before the actual worker handler is called, so the problem occurs regardless of the condition checks in do_strp_work().
```
worker_thread()
assign_work(work, &worker->scheduled)
move_linked_works(work) // work: ctx->strp->msg_timer_work or ctx->strp->work
```Quoted 8 lines
Quoted 5 lines
```
Best regards,
Hyunwoo KimSignature · 1 line
Sabrina
Quoted 17 lines · Hyunwoo Kim
Quoted 8 lines · Sabrina Dubroca
Quoted 7 lines · Hyunwoo Kim
When
strp_stop()andstrp_done()are called without holdinglock_sock(), they can race with worker-scheduling paths such as the Delayed ACK handler and ksoftirqd.
Specifically, aftercancel_delayed_work_sync()andcancel_work_sync()are invoked fromstrp_done(), the workers may still be scheduled. As a result, the workers may dereference freed objects.Quoted 7 lines
Quoted 4 lines
To prevent these races, the cancellation APIs are replaced with worker-disabling APIs.
Fixes 829385f08ae9 ("strparser: Use delayed work instead of timer for msg timeout")That's the correct commit for
msg_timer_work, but not forstrp->work. No race was possible when msg timeout was using a timer?
Quoted 7 lines
Of course, the race could also occur when the message timeout was implemented using a timer.
Quoted 1 line
Your second scenario relies only on
strp->workso I would think yes.Using Fixes: bbb0302 ("strparser: Generalize strparser") should cover both cases.
Ok.
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines
Signed-off-by Hyunwoo Kim <[email protected]>--- net/strparser/strparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/strparser/strparser.c +0
-0net/strparser/strparser.c void strp_done(struct strparser *strp) · line 507
Quoted 7 lines
net/strparser/strparser.c +2
-2@@ -503,8 +503,8 @@ void strp_done(struct strparser *strp) {WARN_ON (!strp->stopped); -cancel_delayed_work_sync (&strp->msg_timer_work); -cancel_work_sync (&strp->work); +disable_delayed_work_sync (&strp->msg_timer_work); +disable_work_sync (&strp->work);The change itself looks reasonable.
Quoted 8 lines
net/strparser/strparser.c +0
-0if (strp->skb_head) {kfree_skb (strp->skb_head);Signature · 4 lines
2.43.0 --- Dear,
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
The following is a simplified scenario illustrating how each race can occur. Since
espintcp_close()does not holdlock_sock(), the race is possible. Althoughcancel_work_sync(&strp->work)does not appear to be easy to trigger in practice here, it still seems better to fix it as well.What about the other users of strp? Only espintcp is racy?
Any subsystem that calls
strp_stop()andstrp_done()outside oflock_sock()is racy.
strp_done() has to be called outside of lock_sock(), since both strp_work() and strp_msg_timeout() need to take lock_sock.
Quoted 8 lines
Quoted 5 lines
If
strp_donecan run concurrently with__strp_recv, it seems we could also end up leakingstrp->skb_head, if__strp_recvstores a new one after we've cleared the old?I am not fully sure about this part yet, so I think it should be discussed separately in another thread.
Maybe not, if it's the same race condition (same code running concurrently), just with different symptoms.
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
```
cpu0 cpu1
espintcp_close()espintcp_data_ready() if (unlikely(strp->stopped)) return; strp_stop() strp->stopped = 1; strp_done() cancel_delayed_work_sync(&strp->msg_timer_work); strp_data_ready()In this order,
strp_data_readywill seestrp->stoppedand return without doing anything.
Quoted 8 lines
Quoted 2 lines
(I'm confused by the "if (
unlikely(strp->stopped))" above though, maybe you meantespintcp_data_ready->strp_data_ready-> if (...))Sorry for the confusion. I accidentally mixed up the call order in the
strp_data_ready()path.
More precisely, the scenario is thatespintcp_data_ready()→strp_data_ready()runs first, passes the if (unlikely(strp->stopped)) check, and only after thatstrp->stopped= 1 is set.
Ok, thanks.
Quoted 19 lines
Quoted 14 lines
Quoted 4 lines
```
cpu0 cpu1
espintcp_close()Quoted 9 lines
sk->sk_data_ready() espintcp_data_ready() if (unlikely(strp->stopped)) return; strp_stop() strp->stopped = 1; strp_done() cancel_work_sync(&strp->work); if (strp_read_sock(strp) == -ENOMEM) queue_work()Quoted 4 lines
Here the problem would be if we enter
do_strp_workafter all the socket data has already been freed? Otherwise again the test onstrp->stoppedwill makedo_strp_workreturn early. (this would be unexpected but should be safe)
Quoted 7 lines
If the worker is scheduled after the cancel call, then during
espintcp_close()→tcp_close(), the sk will be freed and the ctx will be freed as well.
As a result, the kworker will access the freedctx->strp. This access to the freed ctx happens before the actual worker handler is called, so the problem occurs regardless of the condition checks indo_strp_work().
If we're lucky:
cancel_work_sync(strp->work)
queue_work(strp->work)
...
do_strp_work
strp->stopped...
free(sk)
[anyway, doesn't matter, seems it could indeed happen as you say]
I'm thinking that all those races are not very likely to happen in real life, since syzbot has not seen them, and it's usually pretty good at finding races. (which doesn't mean they're not worth fixing)
Signature · 1 line
Sabrina