Skip to content

Commit ec4d869

Browse files
edumazetgregkh
authored andcommitted
net: fix socket refcounting in skb_complete_tx_timestamp()
[ Upstream commit 9ac25fc063751379cb77434fef9f3b088cd3e2f7 ] 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 9e76833 commit ec4d869

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
@@ -3678,13 +3678,14 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
36783678
if (!skb_may_tx_timestamp(sk, false))
36793679
return;
36803680

3681-
/* take a reference to prevent skb_orphan() from freeing the socket */
3682-
sock_hold(sk);
3683-
3684-
*skb_hwtstamps(skb) = *hwtstamps;
3685-
__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3686-
3687-
sock_put(sk);
3681+
/* Take a reference to prevent skb_orphan() from freeing the socket,
3682+
* but only if the socket refcount is not zero.
3683+
*/
3684+
if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3685+
*skb_hwtstamps(skb) = *hwtstamps;
3686+
__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3687+
sock_put(sk);
3688+
}
36883689
}
36893690
EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
36903691

0 commit comments

Comments
 (0)