Skip to content

Commit 2638595

Browse files
ereshetovadavem330
authored andcommitted
net: convert sk_buff_fclones.fclone_ref from atomic_t to refcount_t
refcount_t type and corresponding API should be used instead of atomic_t when the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: David Windsor <dwindsor@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6335479 commit 2638595

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ struct sk_buff_fclones {
915915

916916
struct sk_buff skb2;
917917

918-
atomic_t fclone_ref;
918+
refcount_t fclone_ref;
919919
};
920920

921921
/**
@@ -935,7 +935,7 @@ static inline bool skb_fclone_busy(const struct sock *sk,
935935
fclones = container_of(skb, struct sk_buff_fclones, skb1);
936936

937937
return skb->fclone == SKB_FCLONE_ORIG &&
938-
atomic_read(&fclones->fclone_ref) > 1 &&
938+
refcount_read(&fclones->fclone_ref) > 1 &&
939939
fclones->skb2.sk == sk;
940940
}
941941

‎net/core/skbuff.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
268268

269269
kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
270270
skb->fclone = SKB_FCLONE_ORIG;
271-
atomic_set(&fclones->fclone_ref, 1);
271+
refcount_set(&fclones->fclone_ref, 1);
272272

273273
fclones->skb2.fclone = SKB_FCLONE_CLONE;
274274
}
@@ -629,15 +629,15 @@ static void kfree_skbmem(struct sk_buff *skb)
629629
* This test would have no chance to be true for the clone,
630630
* while here, branch prediction will be good.
631631
*/
632-
if (atomic_read(&fclones->fclone_ref) == 1)
632+
if (refcount_read(&fclones->fclone_ref) == 1)
633633
goto fastpath;
634634
break;
635635

636636
default: /* SKB_FCLONE_CLONE */
637637
fclones = container_of(skb, struct sk_buff_fclones, skb2);
638638
break;
639639
}
640-
if (!atomic_dec_and_test(&fclones->fclone_ref))
640+
if (!refcount_dec_and_test(&fclones->fclone_ref))
641641
return;
642642
fastpath:
643643
kmem_cache_free(skbuff_fclone_cache, fclones);
@@ -1027,9 +1027,9 @@ struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
10271027
return NULL;
10281028

10291029
if (skb->fclone == SKB_FCLONE_ORIG &&
1030-
atomic_read(&fclones->fclone_ref) == 1) {
1030+
refcount_read(&fclones->fclone_ref) == 1) {
10311031
n = &fclones->skb2;
1032-
atomic_set(&fclones->fclone_ref, 2);
1032+
refcount_set(&fclones->fclone_ref, 2);
10331033
} else {
10341034
if (skb_pfmemalloc(skb))
10351035
gfp_mask |= __GFP_MEMALLOC;

0 commit comments

Comments
 (0)