Skip to content

Commit 04c3eee

Browse files
Mohamed Khalfellagregkh
Mohamed Khalfella
authored andcommitted
skbuff: skb_segment, Call zero copy functions before using skbuff frags
commit 2ea3528 upstream. 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 <mkhalfella@purestorage.com> Reported-by: Amit Goyal <agoyal@purestorage.com> Cc: stable@vger.kernel.org Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4921f93 commit 04c3eee

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

net/core/skbuff.c

+20-14
Original file line numberDiff line numberDiff line change
@@ -4135,21 +4135,20 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
41354135
struct sk_buff *segs = NULL;
41364136
struct sk_buff *tail = NULL;
41374137
struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4138-
skb_frag_t *frag = skb_shinfo(head_skb)->frags;
41394138
unsigned int mss = skb_shinfo(head_skb)->gso_size;
41404139
unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
4141-
struct sk_buff *frag_skb = head_skb;
41424140
unsigned int offset = doffset;
41434141
unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
41444142
unsigned int partial_segs = 0;
41454143
unsigned int headroom;
41464144
unsigned int len = head_skb->len;
4145+
struct sk_buff *frag_skb;
4146+
skb_frag_t *frag;
41474147
__be16 proto;
41484148
bool csum, sg;
4149-
int nfrags = skb_shinfo(head_skb)->nr_frags;
41504149
int err = -ENOMEM;
41514150
int i = 0;
4152-
int pos;
4151+
int nfrags, pos;
41534152

41544153
if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
41554154
mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
@@ -4226,6 +4225,13 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
42264225
headroom = skb_headroom(head_skb);
42274226
pos = skb_headlen(head_skb);
42284227

4228+
if (skb_orphan_frags(head_skb, GFP_ATOMIC))
4229+
return ERR_PTR(-ENOMEM);
4230+
4231+
nfrags = skb_shinfo(head_skb)->nr_frags;
4232+
frag = skb_shinfo(head_skb)->frags;
4233+
frag_skb = head_skb;
4234+
42294235
do {
42304236
struct sk_buff *nskb;
42314237
skb_frag_t *nskb_frag;
@@ -4246,6 +4252,10 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
42464252
(skb_headlen(list_skb) == len || sg)) {
42474253
BUG_ON(skb_headlen(list_skb) > len);
42484254

4255+
nskb = skb_clone(list_skb, GFP_ATOMIC);
4256+
if (unlikely(!nskb))
4257+
goto err;
4258+
42494259
i = 0;
42504260
nfrags = skb_shinfo(list_skb)->nr_frags;
42514261
frag = skb_shinfo(list_skb)->frags;
@@ -4264,12 +4274,8 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
42644274
frag++;
42654275
}
42664276

4267-
nskb = skb_clone(list_skb, GFP_ATOMIC);
42684277
list_skb = list_skb->next;
42694278

4270-
if (unlikely(!nskb))
4271-
goto err;
4272-
42734279
if (unlikely(pskb_trim(nskb, len))) {
42744280
kfree_skb(nskb);
42754281
goto err;
@@ -4345,12 +4351,16 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
43454351
skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
43464352
SKBFL_SHARED_FRAG;
43474353

4348-
if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4349-
skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4354+
if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
43504355
goto err;
43514356

43524357
while (pos < offset + len) {
43534358
if (i >= nfrags) {
4359+
if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
4360+
skb_zerocopy_clone(nskb, list_skb,
4361+
GFP_ATOMIC))
4362+
goto err;
4363+
43544364
i = 0;
43554365
nfrags = skb_shinfo(list_skb)->nr_frags;
43564366
frag = skb_shinfo(list_skb)->frags;
@@ -4364,10 +4374,6 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
43644374
i--;
43654375
frag--;
43664376
}
4367-
if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4368-
skb_zerocopy_clone(nskb, frag_skb,
4369-
GFP_ATOMIC))
4370-
goto err;
43714377

43724378
list_skb = list_skb->next;
43734379
}

0 commit comments

Comments
 (0)