Skip to content

Commit e3989e6

Browse files
committed
skbuff: skb_segment, Call zero copy functions before using skbuff frags
jira VULN-155407 cve CVE-2023-53354 commit-author Mohamed Khalfella <[email protected]> commit 2ea3528 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]> (cherry picked from commit 2ea3528) Signed-off-by: Roxana Nicolescu <[email protected]>
1 parent bcf7d6d commit e3989e6

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
@@ -3864,21 +3864,20 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
38643864
struct sk_buff *segs = NULL;
38653865
struct sk_buff *tail = NULL;
38663866
struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3867-
skb_frag_t *frag = skb_shinfo(head_skb)->frags;
38683867
unsigned int mss = skb_shinfo(head_skb)->gso_size;
38693868
unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3870-
struct sk_buff *frag_skb = head_skb;
38713869
unsigned int offset = doffset;
38723870
unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
38733871
unsigned int partial_segs = 0;
38743872
unsigned int headroom;
38753873
unsigned int len = head_skb->len;
3874+
struct sk_buff *frag_skb;
3875+
skb_frag_t *frag;
38763876
__be16 proto;
38773877
bool csum, sg;
3878-
int nfrags = skb_shinfo(head_skb)->nr_frags;
38793878
int err = -ENOMEM;
38803879
int i = 0;
3881-
int pos;
3880+
int nfrags, pos;
38823881

38833882
if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
38843883
mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
@@ -3955,6 +3954,13 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
39553954
headroom = skb_headroom(head_skb);
39563955
pos = skb_headlen(head_skb);
39573956

3957+
if (skb_orphan_frags(head_skb, GFP_ATOMIC))
3958+
return ERR_PTR(-ENOMEM);
3959+
3960+
nfrags = skb_shinfo(head_skb)->nr_frags;
3961+
frag = skb_shinfo(head_skb)->frags;
3962+
frag_skb = head_skb;
3963+
39583964
do {
39593965
struct sk_buff *nskb;
39603966
skb_frag_t *nskb_frag;
@@ -3979,6 +3985,10 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
39793985
(skb_headlen(list_skb) == len || sg)) {
39803986
BUG_ON(skb_headlen(list_skb) > len);
39813987

3988+
nskb = skb_clone(list_skb, GFP_ATOMIC);
3989+
if (unlikely(!nskb))
3990+
goto err;
3991+
39823992
i = 0;
39833993
nfrags = skb_shinfo(list_skb)->nr_frags;
39843994
frag = skb_shinfo(list_skb)->frags;
@@ -3997,12 +4007,8 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
39974007
frag++;
39984008
}
39994009

4000-
nskb = skb_clone(list_skb, GFP_ATOMIC);
40014010
list_skb = list_skb->next;
40024011

4003-
if (unlikely(!nskb))
4004-
goto err;
4005-
40064012
if (unlikely(pskb_trim(nskb, len))) {
40074013
kfree_skb(nskb);
40084014
goto err;
@@ -4067,12 +4073,16 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
40674073
skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
40684074
SKBTX_SHARED_FRAG;
40694075

4070-
if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4071-
skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4076+
if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
40724077
goto err;
40734078

40744079
while (pos < offset + len) {
40754080
if (i >= nfrags) {
4081+
if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
4082+
skb_zerocopy_clone(nskb, list_skb,
4083+
GFP_ATOMIC))
4084+
goto err;
4085+
40764086
i = 0;
40774087
nfrags = skb_shinfo(list_skb)->nr_frags;
40784088
frag = skb_shinfo(list_skb)->frags;
@@ -4086,10 +4096,6 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
40864096
i--;
40874097
frag--;
40884098
}
4089-
if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4090-
skb_zerocopy_clone(nskb, frag_skb,
4091-
GFP_ATOMIC))
4092-
goto err;
40934099

40944100
list_skb = list_skb->next;
40954101
}

0 commit comments

Comments
 (0)