inet: inet_defrag: prevent sk release while still in use
Subsystems (4)
Florian Westphal
maintainerPATCH 1/1 inet: inet_defrag: prevent sk release while still in use
ip_local_out() and other functions can pass skb->sk as function argument.
If the skb is a fragment and reassembly happens before such function call returns, the sk must not be released.
This affects skb fragments reassembled via netfilter or similar modules, e.g. openvswitch or ct_act.c, when run as part of tx pipeline.
Eric Dumazet made an initial analysis of this bug. Quoting Eric:
Calling ip_defrag() in output path is also implying skb_orphan(), which is buggy because output path relies on sk not disappearing.
A relevant old patch about the issue was :
8282f27449bf ("inet: frag: Always orphan skbs inside ip_defrag()")
[..]
net/ipv4/ip_output.c depends on skb->sk being set, and probably to an inet socket, not an arbitrary one.
If we orphan the packet in ipvlan, then downstream things like FQ packet scheduler will not work properly.
We need to change ip_defrag() to only use skb_orphan() when really needed, ie whenever frag_list is going to be used.
Eric suggested to stash sk in fragment queue and made an initial patch. However there is a problem with this:
If skb is refragmented again right after, ip_do_fragment() will copy head->sk to the new fragments, and sets up destructor to sock_wfree. IOW, we have no choice but to fix up sk_wmem accouting to reflect the fully reassembled skb, else wmem will underflow.
This change moves the orphan down into the core, to last possible moment. As ip_defrag_offset is aliased with sk_buff->sk member, we must move the offset into the FRAG_CB, else skb->sk gets clobbered.
This allows to delay the orphaning long enough to learn if the skb has to be queued or if the skb is completing the reasm queue.
In the former case, things work as before, skb is orphaned. This is safe because skb gets queued/stolen and won't continue past reasm engine.
In the latter case, we will steal the skb->sk reference, reattach it to the head skb, and fix up wmem accouting when inet_frag inflates truesize.
--- include/linux/skbuff.h | 7 +-- net/ipv4/inet_fragment.c | 71 ++++++++++++++++++++----- net/ipv4/ip_fragment.c | 2 +- net/ipv6/netfilter/nf_conntrack_reasm.c | 2 +- 4 files changed, 61 insertions(+), 21 deletions(-)
include/linux/skbuff.h +1 -6
@@ -753,8 +753,6 @@ typedef unsigned char *sk_buff_data_t;* @list: queue head * @ll_node: anchor in an llist (eg socket defer_list) * @sk: Socket we are owned by -* @ip_defrag_offset: (aka @sk) alternate use of @sk, used in -* fragmentation management * @dev: Device we arrived on/are leaving by * @dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL * @cb: Control buffer. Free for use by every layer. Put private vars here
@@ -875,10 +873,7 @@ struct sk_buff {struct llist_node ll_node; }; -union { -struct sock *sk; -int ip_defrag_offset; - }; +struct sock *sk;union {ktime_t tstamp;
net/ipv4/inet_fragment.c +58 -13
162 lines not on this page · raw message
net/ipv4/ip_fragment.c +1 -1
@@ -384,6 +384,7 @@ static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) }skb_dst_drop (skb); +skb_orphan (skb);return -EINPROGRESS ; insert_error:
@@ -487,7 +488,6 @@ int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)struct ipq *qp;__IP_INC_STATS (net,IPSTATS_MIB_REASMREQDS ); -skb_orphan (skb);/* Lookup (or create) queue header */ qp =ip_find (net,ip_hdr (skb), user, vif);
net/ipv6/netfilter/nf_conntrack_reasm.c +1 -1
@@ -294,6 +294,7 @@ static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb, }skb_dst_drop (skb); +skb_orphan (skb);return -EINPROGRESS ; insert_error:
@@ -469,7 +470,6 @@ int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user) hdr =ipv6_hdr (skb); fhdr = (struct frag_hdr *)skb_transport_header (skb); -skb_orphan (skb); fq =fq_find (net, fhdr->identification, user, hdr, skb->dev ? skb->dev->ifindex :0 );if (fq ==NULL ) {
Signature · 1 line
2.43.2
Quoted 141 lines · Florian Westphal
ip_local_out()and other functions can passskb->skas function argument.If the skb is a fragment and reassembly happens before such function call returns, the sk must not be released.
This affects skb fragments reassembled via netfilter or similar modules, e.g. openvswitch or
ct_act.c, when run as part of tx pipeline.Eric Dumazet made an initial analysis of this bug. Quoting Eric:
Callingip_defrag()in output path is also implyingskb_orphan(), which is buggy because output path relies on sk not disappearing.A relevant old patch about the issue was :
8282f27449bf("inet: frag: Always orphan skbs insideip_defrag()")[..]
net/ipv4/ip_output.cdepends onskb->skbeing set, and probably to an inet socket, not an arbitrary one.If we orphan the packet in ipvlan, then downstream things like FQ packet scheduler will not work properly.
We need to change
ip_defrag()to only useskb_orphan()when really needed, ie wheneverfrag_listis going to be used.Eric suggested to stash sk in fragment queue and made an initial patch. However there is a problem with this:
If skb is refragmented again right after,
ip_do_fragment()will copyhead->skto the new fragments, and sets up destructor tosock_wfree. IOW, we have no choice but to fix upsk_wmemaccouting to reflect the fully reassembled skb, else wmem will underflow.This change moves the orphan down into the core, to last possible moment. As
ip_defrag_offsetis aliased withsk_buff->skmember, we must move the offset into theFRAG_CB, elseskb->skgets clobbered.This allows to delay the orphaning long enough to learn if the skb has to be queued or if the skb is completing the reasm queue.
In the former case, things work as before, skb is orphaned. This is safe because skb gets queued/stolen and won't continue past reasm engine.
In the latter case, we will steal the
skb->skreference, reattach it to the head skb, and fix up wmem accouting wheninet_fraginflates truesize.Diagnosed-by Eric Dumazet <[email protected]>Reported-by xingwei lee <[email protected]>Reported-by yue sun <[email protected]>Reported-by [email protected]Signed-off-by Florian Westphal <[email protected]>--- include/linux/skbuff.h | 7 +-- net/ipv4/inet_fragment.c | 71 ++++++++++++++++++++----- net/ipv4/ip_fragment.c | 2 +- net/ipv6/netfilter/nf_conntrack_reasm.c | 2 +- 4 files changed, 61 insertions(+), 21 deletions(-)include/linux/skbuff.h +1
-6@@ -753,8 +753,6 @@ typedef unsigned char *sk_buff_data_t;* @list: queue head * @ll_node: anchor in an llist (eg socket defer_list) * @sk: Socket we are owned by -* @ip_defrag_offset: (aka @sk) alternate use of @sk, used in -* fragmentation management * @dev: Device we arrived on/are leaving by * @dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL * @cb: Control buffer. Free for use by every layer. Put private vars here @@ -875,10 +873,7 @@ struct sk_buff {struct llist_node ll_node; }; -union { -struct sock *sk; -int ip_defrag_offset; - }; +struct sock *sk;union {ktime_t tstamp;net/ipv4/inet_fragment.c +9
-6@@ -24,6 +24,8 @@#include <net/ip.h> #include <net/ipv6.h> +#include "../core/sock_destructor.h" +/* Use skb->cb to track consecutive/adjacent fragments coming at * the end of the queue. Nodes in the rb-tree queue will * contain "runs" of one or more adjacent fragments. @@ -39,6 +41,7 @@ struct ipfrag_skb_cb { };struct sk_buff *next_frag;int frag_run_len; +int ip_defrag_offset; };#define FRAG_CB (skb) ((struct ipfrag_skb_cb *)((skb)->cb))@@ -396,12 +399,12 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,*/ if (!last)fragrun_create (q, skb);/* First fragment. */ -else if (last->ip_defrag_offset + last->len < end) { +else if (FRAG_CB (last)->ip_defrag_offset + last->len < end) {/* This is the common case: skb goes to the end. */ /* Detect and discard overlaps. */ -if (offset < last->ip_defrag_offset + last->len) +if (offset <FRAG_CB (last)->ip_defrag_offset + last->len)return IPFRAG_OVERLAP ; -if (offset == last->ip_defrag_offset + last->len) +if (offset ==FRAG_CB (last)->ip_defrag_offset + last->len)fragrun_append_to_last (q, skb);else fragrun_create (q, skb);@@ -418,13 +421,13 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, parent = *rbn; curr =rb_to_skb (parent); - curr_run_end = curr->ip_defrag_offset + + curr_run_end =FRAG_CB (curr)->ip_defrag_offset +FRAG_CB (curr)->frag_run_len; -if (end <= curr->ip_defrag_offset) +if (end <=FRAG_CB (curr)->ip_defrag_offset) rbn = &parent->rb_left;else if (offset >= curr_run_end) rbn = &parent->rb_right; -else if (offset >= curr->ip_defrag_offset && +else if (offset >=FRAG_CB (curr)->ip_defrag_offset && end <= curr_run_end)return IPFRAG_DUP ;else @@ -438,23 +441,39 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,rb_insert_color (&skb->rbnode, &q->rb_fragments);
net/ipv4/inet_fragment.c int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, · line 450
Quoted 10 lines
net/ipv4/inet_fragment.c +2
-1} - skb->ip_defrag_offset = offset; +FRAG_CB (skb)->ip_defrag_offset = offset;return IPFRAG_OK ; }EXPORT_SYMBOL (inet_frag_queue_insert); +void tcp_wfree (struct sk_buff *skb);
Thanks a lot Florian for looking at this !
Since you had : #include "../core/sock_destructor.h", perhaps the line can be removed,
because it includes <net/tcp.h>
Paolo Abeni
maintainerHi,
Quoted 46 lines · Eric Dumazet
Quoted 45 lines · Florian Westphal
ip_local_out()and other functions can passskb->skas function argument.If the skb is a fragment and reassembly happens before such function call returns, the sk must not be released.
This affects skb fragments reassembled via netfilter or similar modules, e.g. openvswitch or
ct_act.c, when run as part of tx pipeline.Eric Dumazet made an initial analysis of this bug. Quoting Eric:
Callingip_defrag()in output path is also implyingskb_orphan(), which is buggy because output path relies on sk not disappearing.A relevant old patch about the issue was :
8282f27449bf("inet: frag: Always orphan skbs insideip_defrag()")[..]
net/ipv4/ip_output.cdepends onskb->skbeing set, and probably to an inet socket, not an arbitrary one.If we orphan the packet in ipvlan, then downstream things like FQ packet scheduler will not work properly.
We need to change
ip_defrag()to only useskb_orphan()when really needed, ie wheneverfrag_listis going to be used.Eric suggested to stash sk in fragment queue and made an initial patch. However there is a problem with this:
If skb is refragmented again right after,
ip_do_fragment()will copyhead->skto the new fragments, and sets up destructor tosock_wfree. IOW, we have no choice but to fix upsk_wmemaccouting to reflect the fully reassembled skb, else wmem will underflow.This change moves the orphan down into the core, to last possible moment. As
ip_defrag_offsetis aliased withsk_buff->skmember, we must move the offset into theFRAG_CB, elseskb->skgets clobbered.This allows to delay the orphaning long enough to learn if the skb has to be queued or if the skb is completing the reasm queue.
In the former case, things work as before, skb is orphaned. This is safe because skb gets queued/stolen and won't continue past reasm engine.
Quoted 7 lines
Quoted 7 lines
In the latter case, we will steal the
skb->skreference, reattach it to the head skb, and fix up wmem accouting wheninet_fraginflates truesize.Diagnosed-by Eric Dumazet <[email protected]>Reported-by xingwei lee <[email protected]>Reported-by yue sun <[email protected]>Reported-by [email protected]
Possibly:
it's not very accurate but should be a reasonable oldest affected version.
Quoted 98 lines
Quoted 98 lines
Signed-off-by Florian Westphal <[email protected]>--- include/linux/skbuff.h | 7 +-- net/ipv4/inet_fragment.c | 71 ++++++++++++++++++++----- net/ipv4/ip_fragment.c | 2 +- net/ipv6/netfilter/nf_conntrack_reasm.c | 2 +- 4 files changed, 61 insertions(+), 21 deletions(-)include/linux/skbuff.h +1
-6@@ -753,8 +753,6 @@ typedef unsigned char *sk_buff_data_t;* @list: queue head * @ll_node: anchor in an llist (eg socket defer_list) * @sk: Socket we are owned by -* @ip_defrag_offset: (aka @sk) alternate use of @sk, used in -* fragmentation management * @dev: Device we arrived on/are leaving by * @dev_scratch: (aka @dev) alternate use of @dev when @dev would be %NULL * @cb: Control buffer. Free for use by every layer. Put private vars here @@ -875,10 +873,7 @@ struct sk_buff {struct llist_node ll_node; }; -union { -struct sock *sk; -int ip_defrag_offset; - }; +struct sock *sk;union {ktime_t tstamp;net/ipv4/inet_fragment.c +10
-7@@ -24,6 +24,8 @@#include <net/ip.h> #include <net/ipv6.h> +#include "../core/sock_destructor.h" +/* Use skb->cb to track consecutive/adjacent fragments coming at * the end of the queue. Nodes in the rb-tree queue will * contain "runs" of one or more adjacent fragments. @@ -39,6 +41,7 @@ struct ipfrag_skb_cb { };struct sk_buff *next_frag;int frag_run_len; +int ip_defrag_offset; };#define FRAG_CB (skb) ((struct ipfrag_skb_cb *)((skb)->cb))@@ -396,12 +399,12 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,*/ if (!last)fragrun_create (q, skb);/* First fragment. */ -else if (last->ip_defrag_offset + last->len < end) { +else if (FRAG_CB (last)->ip_defrag_offset + last->len < end) {/* This is the common case: skb goes to the end. */ /* Detect and discard overlaps. */ -if (offset < last->ip_defrag_offset + last->len) +if (offset <FRAG_CB (last)->ip_defrag_offset + last->len)return IPFRAG_OVERLAP ; -if (offset == last->ip_defrag_offset + last->len) +if (offset ==FRAG_CB (last)->ip_defrag_offset + last->len)fragrun_append_to_last (q, skb);else fragrun_create (q, skb);@@ -418,13 +421,13 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, parent = *rbn; curr =rb_to_skb (parent); - curr_run_end = curr->ip_defrag_offset + + curr_run_end =FRAG_CB (curr)->ip_defrag_offset +FRAG_CB (curr)->frag_run_len; -if (end <= curr->ip_defrag_offset) +if (end <=FRAG_CB (curr)->ip_defrag_offset) rbn = &parent->rb_left;else if (offset >= curr_run_end) rbn = &parent->rb_right; -else if (offset >= curr->ip_defrag_offset && +else if (offset >=FRAG_CB (curr)->ip_defrag_offset && end <= curr_run_end)return IPFRAG_DUP ;else @@ -438,23 +441,39 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,rb_insert_color (&skb->rbnode, &q->rb_fragments); } - skb->ip_defrag_offset = offset; +FRAG_CB (skb)->ip_defrag_offset = offset;return IPFRAG_OK ; }EXPORT_SYMBOL (inet_frag_queue_insert);
Quoted 7 lines
net/ipv4/inet_fragment.c int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, · line 450
Quoted 1 line
net/ipv4/inet_fragment.c +1
-0+void tcp_wfree (struct sk_buff *skb);Thanks a lot Florian for looking at this !
Since you had : #include "
../core/sock_destructor.h", perhaps the line can be removed,
because it includes <net/tcp.h>
I think Florian will not able to reply for a few days.
Since the issue looks ancient and we are early in the cycle, I guess there are no problems with that.
Cheers,
Paolo