Skip to content

Commit 9ac25fc

Browse files
edumazetdavem330
authored andcommitted
net: fix socket refcounting in skb_complete_tx_timestamp()
TX skbs do not necessarily hold a reference on skb->sk->sk_refcnt By the time TX completion happens, sk_refcnt might be already 0. sock_hold()/sock_put() would then corrupt critical state, like sk_wmem_alloc and lead to leaks or use after free. Fixes: 62bccb8 ("net-timestamp: Make the clone operation stand-alone from phy timestamping") Signed-off-by: Eric Dumazet <[email protected]> Cc: Alexander Duyck <[email protected]> Cc: Johannes Berg <[email protected]> Cc: Soheil Hassas Yeganeh <[email protected]> Cc: Willem de Bruijn <[email protected]> Acked-by: Soheil Hassas Yeganeh <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent dd4f107 commit 9ac25fc

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

net/core/skbuff.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,13 +3828,14 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
38283828
if (!skb_may_tx_timestamp(sk, false))
38293829
return;
38303830

3831-
/* take a reference to prevent skb_orphan() from freeing the socket */
3832-
sock_hold(sk);
3833-
3834-
*skb_hwtstamps(skb) = *hwtstamps;
3835-
__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3836-
3837-
sock_put(sk);
3831+
/* Take a reference to prevent skb_orphan() from freeing the socket,
3832+
* but only if the socket refcount is not zero.
3833+
*/
3834+
if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3835+
*skb_hwtstamps(skb) = *hwtstamps;
3836+
__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3837+
sock_put(sk);
3838+
}
38383839
}
38393840
EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
38403841

0 commit comments

Comments
 (0)