Skip to content
This repository was archived by the owner on Jun 4, 2020. It is now read-only.

Commit 61cc261

Browse files
edumazetgregkh
authored andcommitted
net: fix socket refcounting in skb_complete_tx_timestamp()
commit 9ac25fc063751379cb77434fef9f3b088cd3e2f7 upstream. 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 <edumazet@google.com> Cc: Alexander Duyck <alexander.h.duyck@intel.com> Cc: Johannes Berg <johannes@sipsolutions.net> Cc: Soheil Hassas Yeganeh <soheil@google.com> Cc: Willem de Bruijn <willemb@google.com> Acked-by: Soheil Hassas Yeganeh <soheil@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 66aebd4 commit 61cc261

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

net/core/skbuff.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,13 +3617,14 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
36173617
{
36183618
struct sock *sk = skb->sk;
36193619

3620-
/* take a reference to prevent skb_orphan() from freeing the socket */
3621-
sock_hold(sk);
3622-
3623-
*skb_hwtstamps(skb) = *hwtstamps;
3624-
__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3625-
3626-
sock_put(sk);
3620+
/* Take a reference to prevent skb_orphan() from freeing the socket,
3621+
* but only if the socket refcount is not zero.
3622+
*/
3623+
if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3624+
*skb_hwtstamps(skb) = *hwtstamps;
3625+
__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3626+
sock_put(sk);
3627+
}
36273628
}
36283629
EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
36293630

0 commit comments

Comments
 (0)