Skip to content

Commit 2ea3528

Browse files
Mohamed Khalfelladavem330
Mohamed Khalfella
authored andcommitted
skbuff: skb_segment, Call zero copy functions before using skbuff frags
Commit bf5c25d ("skbuff: in skb_segment, call zerocopy functions once per nskb") added the call to zero copy functions in skb_segment(). The change introduced a bug in skb_segment() because skb_orphan_frags() may possibly change the number of fragments or allocate new fragments altogether leaving nrfrags and frag to point to the old values. This can cause a panic with stacktrace like the one below. [ 193.894380] BUG: kernel NULL pointer dereference, address: 00000000000000bc [ 193.895273] CPU: 13 PID: 18164 Comm: vh-net-17428 Kdump: loaded Tainted: G O 5.15.123+ #26 [ 193.903919] RIP: 0010:skb_segment+0xb0e/0x12f0 [ 194.021892] Call Trace: [ 194.027422] <TASK> [ 194.072861] tcp_gso_segment+0x107/0x540 [ 194.082031] inet_gso_segment+0x15c/0x3d0 [ 194.090783] skb_mac_gso_segment+0x9f/0x110 [ 194.095016] __skb_gso_segment+0xc1/0x190 [ 194.103131] netem_enqueue+0x290/0xb10 [sch_netem] [ 194.107071] dev_qdisc_enqueue+0x16/0x70 [ 194.110884] __dev_queue_xmit+0x63b/0xb30 [ 194.121670] bond_start_xmit+0x159/0x380 [bonding] [ 194.128506] dev_hard_start_xmit+0xc3/0x1e0 [ 194.131787] __dev_queue_xmit+0x8a0/0xb30 [ 194.138225] macvlan_start_xmit+0x4f/0x100 [macvlan] [ 194.141477] dev_hard_start_xmit+0xc3/0x1e0 [ 194.144622] sch_direct_xmit+0xe3/0x280 [ 194.147748] __dev_queue_xmit+0x54a/0xb30 [ 194.154131] tap_get_user+0x2a8/0x9c0 [tap] [ 194.157358] tap_sendmsg+0x52/0x8e0 [tap] [ 194.167049] handle_tx_zerocopy+0x14e/0x4c0 [vhost_net] [ 194.173631] handle_tx+0xcd/0xe0 [vhost_net] [ 194.176959] vhost_worker+0x76/0xb0 [vhost] [ 194.183667] kthread+0x118/0x140 [ 194.190358] ret_from_fork+0x1f/0x30 [ 194.193670] </TASK> In this case calling skb_orphan_frags() updated nr_frags leaving nrfrags local variable in skb_segment() stale. This resulted in the code hitting i >= nrfrags prematurely and trying to move to next frag_skb using list_skb pointer, which was NULL, and caused kernel panic. Move the call to zero copy functions before using frags and nr_frags. Fixes: bf5c25d ("skbuff: in skb_segment, call zerocopy functions once per nskb") Signed-off-by: Mohamed Khalfella <[email protected]> Reported-by: Amit Goyal <[email protected]> Cc: [email protected] Reviewed-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f2e977f commit 2ea3528

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

net/core/skbuff.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,21 +4423,20 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
44234423
struct sk_buff *segs = NULL;
44244424
struct sk_buff *tail = NULL;
44254425
struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4426-
skb_frag_t *frag = skb_shinfo(head_skb)->frags;
44274426
unsigned int mss = skb_shinfo(head_skb)->gso_size;
44284427
unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
4429-
struct sk_buff *frag_skb = head_skb;
44304428
unsigned int offset = doffset;
44314429
unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
44324430
unsigned int partial_segs = 0;
44334431
unsigned int headroom;
44344432
unsigned int len = head_skb->len;
4433+
struct sk_buff *frag_skb;
4434+
skb_frag_t *frag;
44354435
__be16 proto;
44364436
bool csum, sg;
4437-
int nfrags = skb_shinfo(head_skb)->nr_frags;
44384437
int err = -ENOMEM;
44394438
int i = 0;
4440-
int pos;
4439+
int nfrags, pos;
44414440

44424441
if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
44434442
mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
@@ -4514,6 +4513,13 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
45144513
headroom = skb_headroom(head_skb);
45154514
pos = skb_headlen(head_skb);
45164515

4516+
if (skb_orphan_frags(head_skb, GFP_ATOMIC))
4517+
return ERR_PTR(-ENOMEM);
4518+
4519+
nfrags = skb_shinfo(head_skb)->nr_frags;
4520+
frag = skb_shinfo(head_skb)->frags;
4521+
frag_skb = head_skb;
4522+
45174523
do {
45184524
struct sk_buff *nskb;
45194525
skb_frag_t *nskb_frag;
@@ -4534,6 +4540,10 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
45344540
(skb_headlen(list_skb) == len || sg)) {
45354541
BUG_ON(skb_headlen(list_skb) > len);
45364542

4543+
nskb = skb_clone(list_skb, GFP_ATOMIC);
4544+
if (unlikely(!nskb))
4545+
goto err;
4546+
45374547
i = 0;
45384548
nfrags = skb_shinfo(list_skb)->nr_frags;
45394549
frag = skb_shinfo(list_skb)->frags;
@@ -4552,12 +4562,8 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
45524562
frag++;
45534563
}
45544564

4555-
nskb = skb_clone(list_skb, GFP_ATOMIC);
45564565
list_skb = list_skb->next;
45574566

4558-
if (unlikely(!nskb))
4559-
goto err;
4560-
45614567
if (unlikely(pskb_trim(nskb, len))) {
45624568
kfree_skb(nskb);
45634569
goto err;
@@ -4633,12 +4639,16 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
46334639
skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
46344640
SKBFL_SHARED_FRAG;
46354641

4636-
if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4637-
skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4642+
if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
46384643
goto err;
46394644

46404645
while (pos < offset + len) {
46414646
if (i >= nfrags) {
4647+
if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
4648+
skb_zerocopy_clone(nskb, list_skb,
4649+
GFP_ATOMIC))
4650+
goto err;
4651+
46424652
i = 0;
46434653
nfrags = skb_shinfo(list_skb)->nr_frags;
46444654
frag = skb_shinfo(list_skb)->frags;
@@ -4652,10 +4662,6 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
46524662
i--;
46534663
frag--;
46544664
}
4655-
if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4656-
skb_zerocopy_clone(nskb, frag_skb,
4657-
GFP_ATOMIC))
4658-
goto err;
46594665

46604666
list_skb = list_skb->next;
46614667
}

0 commit comments

Comments
 (0)