net: ipv6: fix dst refleaks in rpl, seg6 and ioam6 lwtunnels
Subsystems (4)
Jakub Kicinski
maintainerPATCH 1/2 net: ipv6: fix dst refleaks in rpl, seg6 and ioam6 lwtunnels
dst_cache_get() gives us a reference, we need to release it.
Discovered by the ioam6.sh test, kmemleak was recently fixed to catch per-cpu memory leaks.
---
CC: [email protected]
CC: [email protected]
--- net/ipv6/ioam6_iptunnel.c | 5 +++-- net/ipv6/rpl_iptunnel.c | 6 ++++-- net/ipv6/seg6_iptunnel.c | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-)
net/ipv6/ioam6_iptunnel.c +3 -2
@@ -336,7 +336,7 @@ static int ioam6_do_encap(struct net *net, struct sk_buff *skb,static int ioam6_output (struct net *net,struct sock *sk,struct sk_buff *skb) { -struct dst_entry *dst =skb_dst (skb), *cache_dst; +struct dst_entry *dst =skb_dst (skb), *cache_dst =NULL ;struct in6_addr orig_daddr;struct ioam6_lwt *ilwt;int err = -EINVAL ;
@@ -407,7 +407,6 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) cache_dst =ip6_route_output (net,NULL , &fl6);if (cache_dst->error) { err = cache_dst->error; -dst_release (cache_dst); goto drop; }
@@ -426,8 +425,10 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb)return dst_output (net, sk, skb); } out: +dst_release (cache_dst);return dst->lwtstate->orig_output (net, sk, skb); drop: +dst_release (cache_dst);kfree_skb (skb);return err; }
net/ipv6/rpl_iptunnel.c +4 -2
@@ -232,7 +232,6 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb) dst =ip6_route_output (net,NULL , &fl6);if (dst->error) { err = dst->error; -dst_release (dst); goto drop; }
@@ -251,6 +250,7 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)return dst_output (net, sk, skb); drop: +dst_release (dst);kfree_skb (skb);return err; }
@@ -269,8 +269,10 @@ static int rpl_input(struct sk_buff *skb)local_bh_enable (); err =rpl_do_srh (skb, rlwt, dst); -if (unlikely (err)) +if (unlikely (err)) { +dst_release (dst); goto drop; + }if (!dst) {ip6_route_input (skb);
net/ipv6/seg6_iptunnel.c +4 -2
@@ -482,8 +482,10 @@ static int seg6_input_core(struct net *net, struct sock *sk,local_bh_enable (); err =seg6_do_srh (skb, dst); -if (unlikely (err)) +if (unlikely (err)) { +dst_release (dst); goto drop; + }if (!dst) {ip6_route_input (skb);
@@ -571,7 +573,6 @@ static int seg6_output_core(struct net *net, struct sock *sk, dst =ip6_route_output (net,NULL , &fl6);if (dst->error) { err = dst->error; -dst_release (dst); goto drop; }
@@ -593,6 +594,7 @@ static int seg6_output_core(struct net *net, struct sock *sk,return dst_output (net, sk, skb); drop: +dst_release (dst);kfree_skb (skb);return err; }
Signature · 1 line
2.48.1
Jakub Kicinski
maintainerPATCH 2/2 net: ipv6: fix dst ref loops in rpl, seg6 and ioam6 lwtunnels
Some lwtunnels have a dst cache for post-transformation dst. If the packet destination did not change we may end up recording a reference to the lwtunnel in its own cache, and the lwtunnel state will never be freed.
Discovered by the ioam6.sh test, kmemleak was recently fixed to catch per-cpu memory leaks. I'm not sure if rpl and seg6 can actually hit this, but in principle I don't see why not.
---
CC: [email protected]
CC: [email protected]
--- net/ipv6/ioam6_iptunnel.c | 9 ++++++--- net/ipv6/rpl_iptunnel.c | 9 ++++++--- net/ipv6/seg6_iptunnel.c | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-)
net/ipv6/ioam6_iptunnel.c +6 -3
@@ -410,9 +410,12 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) goto drop; } -local_bh_disable (); -dst_cache_set_ip6 (&ilwt->cache, cache_dst, &fl6.saddr); -local_bh_enable (); +/* cache only if we don't create a dst refrence loop */ +if (dst->lwtstate != cache_dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&ilwt->cache, cache_dst, &fl6.saddr); +local_bh_enable (); + } err =skb_cow_head (skb,LL_RESERVED_SPACE (cache_dst->dev));if (unlikely (err))
net/ipv6/rpl_iptunnel.c +6 -3
@@ -235,9 +235,12 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb) goto drop; } -local_bh_disable (); -dst_cache_set_ip6 (&rlwt->cache, dst, &fl6.saddr); -local_bh_enable (); +/* cache only if we don't create a dst refrence loop */ +if (orig_dst->lwtstate != dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&rlwt->cache, dst, &fl6.saddr); +local_bh_enable (); + } err =skb_cow_head (skb,LL_RESERVED_SPACE (dst->dev));if (unlikely (err))
net/ipv6/seg6_iptunnel.c +6 -3
@@ -576,9 +576,12 @@ static int seg6_output_core(struct net *net, struct sock *sk, goto drop; } -local_bh_disable (); -dst_cache_set_ip6 (&slwt->cache, dst, &fl6.saddr); -local_bh_enable (); +/* cache only if we don't create a dst refrence loop */ +if (orig_dst->lwtstate != dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&slwt->cache, dst, &fl6.saddr); +local_bh_enable (); + } err =skb_cow_head (skb,LL_RESERVED_SPACE (dst->dev));if (unlikely (err))
Signature · 1 line
2.48.1
Re: [PATCH net 2/2] net: ipv6: fix dst ref loops in rpl, seg6 and ioam6 lwtunnels
Quoted 8 lines · Jakub Kicinski
Some lwtunnels have a dst cache for post-transformation dst. If the packet destination did not change we may end up recording a reference to the lwtunnel in its own cache, and the lwtunnel state will never be freed.
Discovered by the
ioam6.shtest, kmemleak was recently fixed to catch per-cpu memory leaks. I'm not sure if rpl and seg6 can actually hit this, but in principle I don't see why not.
Agree, both can theoretically hit this too.
Quoted 18 lines
Fixes 985ec6f5e623 ("net: ipv6: rpl_iptunnel: mitigate 2-realloc issue")Fixes 40475b63761a ("net: ipv6: seg6_iptunnel: mitigate 2-realloc issue")Fixes dce525185bc9 ("net: ipv6: ioam6_iptunnel: mitigate 2-realloc issue")Signed-off-by Jakub Kicinski <[email protected]>---
CC: [email protected]
CC: [email protected]--- net/ipv6/ioam6_iptunnel.c | 9 ++++++--- net/ipv6/rpl_iptunnel.c | 9 ++++++--- net/ipv6/seg6_iptunnel.c | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-)net/ipv6/ioam6_iptunnel.c +0
-0
net/ipv6/ioam6_iptunnel.c static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) · line 413
Quoted 7 lines
net/ipv6/ioam6_iptunnel.c +1
-3@@ -410,9 +410,12 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) goto drop; } -local_bh_disable (); -dst_cache_set_ip6 (&ilwt->cache, cache_dst, &fl6.saddr); -local_bh_enable (); +/* cache only if we don't create a dst refrence loop */
s/refrence/reference
net/ipv6/ioam6_iptunnel.c static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) · line 418
Quoted 5 lines
net/ipv6/ioam6_iptunnel.c +5
-0+if (dst->lwtstate != cache_dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&ilwt->cache, cache_dst, &fl6.saddr); +local_bh_enable (); + }
I agree the above patch fixes what kmemleak reported. However, I think it'd bring the double-reallocation issue back when the packet destination did not change (i.e., cache will always be empty). I'll try to come up with a solution...
Quoted 8 lines
net/ipv6/ioam6_iptunnel.c +0
-0err =skb_cow_head (skb,LL_RESERVED_SPACE (cache_dst->dev));if (unlikely (err))net/ipv6/rpl_iptunnel.c +0
-0
net/ipv6/rpl_iptunnel.c static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb) · line 238
Quoted 7 lines
net/ipv6/rpl_iptunnel.c +1
-3@@ -235,9 +235,12 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb) goto drop; } -local_bh_disable (); -dst_cache_set_ip6 (&rlwt->cache, dst, &fl6.saddr); -local_bh_enable (); +/* cache only if we don't create a dst refrence loop */
s/refrence/reference
Same comment as above.
Quoted 13 lines
net/ipv6/rpl_iptunnel.c +5
-0+if (orig_dst->lwtstate != dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&rlwt->cache, dst, &fl6.saddr); +local_bh_enable (); + } err =skb_cow_head (skb,LL_RESERVED_SPACE (dst->dev));if (unlikely (err))net/ipv6/seg6_iptunnel.c +0
-0
net/ipv6/seg6_iptunnel.c static int seg6_output_core(struct net *net, struct sock *sk, · line 579
Quoted 7 lines
net/ipv6/seg6_iptunnel.c +1
-3@@ -576,9 +576,12 @@ static int seg6_output_core(struct net *net, struct sock *sk, goto drop; } -local_bh_disable (); -dst_cache_set_ip6 (&slwt->cache, dst, &fl6.saddr); -local_bh_enable (); +/* cache only if we don't create a dst refrence loop */
s/refrence/reference
Same comment as above.
Quoted 8 lines
net/ipv6/seg6_iptunnel.c +5
-0+if (orig_dst->lwtstate != dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&slwt->cache, dst, &fl6.saddr); +local_bh_enable (); + } err =skb_cow_head (skb,LL_RESERVED_SPACE (dst->dev));if (unlikely (err))
Jakub Kicinski
maintainerRe: [PATCH net 2/2] net: ipv6: fix dst ref loops in rpl, seg6 and ioam6 lwtunnels
Quoted 10 lines · On Wed, 29 Jan 2025 17:50:14 +0100 Justin Iurman wrote:
net/ipv6/ioam6_iptunnel.c static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) · line 418
Quoted 5 lines
net/ipv6/ioam6_iptunnel.c +5
-0+if (dst->lwtstate != cache_dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&ilwt->cache, cache_dst, &fl6.saddr); +local_bh_enable (); + }I agree the above patch fixes what kmemleak reported. However, I think it'd bring the double-reallocation issue back when the packet destination did not change (i.e., cache will always be empty). I'll try to come up with a solution...
True, dunno enough about use cases so I may be missing the point. But the naive solution would be to remember that the tunnel "doesn't re-route" and use dst directly, instead of cache_dst?
Re: [PATCH net 2/2] net: ipv6: fix dst ref loops in rpl, seg6 and ioam6 lwtunnels
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Wed, 29 Jan 2025 17:50:14 +0100 Justin Iurman wrote:
net/ipv6/ioam6_iptunnel.c static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) · line 418
Quoted 5 lines
net/ipv6/ioam6_iptunnel.c +5
-0+if (dst->lwtstate != cache_dst->lwtstate) { +local_bh_disable (); +dst_cache_set_ip6 (&ilwt->cache, cache_dst, &fl6.saddr); +local_bh_enable (); + }I agree the above patch fixes what kmemleak reported. However, I think it'd bring the double-reallocation issue back when the packet destination did not change (i.e., cache will always be empty). I'll try to come up with a solution...
Quoted 1 line
True, dunno enough about use cases so I may be missing the point.
Possible use cases: (i) with inline mode, or (ii) with encap mode using the same destination address. For (ii), the egress node of the IOAM domain also happens to be the actual destination of the packet, but it's not "your" packet... so you use a tunnel to stay compliant with RFC8200.
Quoted 2 lines
But the naive solution would be to remember that the tunnel "doesn't re-route" and use dst directly, instead of
cache_dst?
Correct, that'd work.
Quoted 9 lines · Jakub Kicinski
dst_cache_get()gives us a reference, we need to release it.Discovered by the
ioam6.shtest, kmemleak was recently fixed to catch per-cpu memory leaks.Fixes 985ec6f5e623 ("net: ipv6: rpl_iptunnel: mitigate 2-realloc issue")Fixes 40475b63761a ("net: ipv6: seg6_iptunnel: mitigate 2-realloc issue")Fixes dce525185bc9 ("net: ipv6: ioam6_iptunnel: mitigate 2-realloc issue")Signed-off-by Jakub Kicinski <[email protected]>
Quoted 8 lines
---
CC: [email protected]
CC: [email protected]--- net/ipv6/ioam6_iptunnel.c | 5 +++-- net/ipv6/rpl_iptunnel.c | 6 ++++-- net/ipv6/seg6_iptunnel.c | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-)
I think both ila_output() and tipc_udp_xmit() should also be patched accordingly. Other users seem fine.
Quoted 96 lines
net/ipv6/ioam6_iptunnel.c +3
-2@@ -336,7 +336,7 @@ static int ioam6_do_encap(struct net *net, struct sk_buff *skb,static int ioam6_output (struct net *net,struct sock *sk,struct sk_buff *skb) { -struct dst_entry *dst =skb_dst (skb), *cache_dst; +struct dst_entry *dst =skb_dst (skb), *cache_dst =NULL ;struct in6_addr orig_daddr;struct ioam6_lwt *ilwt;int err = -EINVAL ;@@ -407,7 +407,6 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) cache_dst =ip6_route_output (net,NULL , &fl6);if (cache_dst->error) { err = cache_dst->error; -dst_release (cache_dst); goto drop; }@@ -426,8 +425,10 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb)return dst_output (net, sk, skb); } out: +dst_release (cache_dst);return dst->lwtstate->orig_output (net, sk, skb); drop: +dst_release (cache_dst);kfree_skb (skb);return err; }net/ipv6/rpl_iptunnel.c +4
-2@@ -232,7 +232,6 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb) dst =ip6_route_output (net,NULL , &fl6);if (dst->error) { err = dst->error; -dst_release (dst); goto drop; }@@ -251,6 +250,7 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)return dst_output (net, sk, skb); drop: +dst_release (dst);kfree_skb (skb);return err; }@@ -269,8 +269,10 @@ static int rpl_input(struct sk_buff *skb)local_bh_enable (); err =rpl_do_srh (skb, rlwt, dst); -if (unlikely (err)) +if (unlikely (err)) { +dst_release (dst); goto drop; + }if (!dst) {ip6_route_input (skb);net/ipv6/seg6_iptunnel.c +4
-2@@ -482,8 +482,10 @@ static int seg6_input_core(struct net *net, struct sock *sk,local_bh_enable (); err =seg6_do_srh (skb, dst); -if (unlikely (err)) +if (unlikely (err)) { +dst_release (dst); goto drop; + }if (!dst) {ip6_route_input (skb);@@ -571,7 +573,6 @@ static int seg6_output_core(struct net *net, struct sock *sk, dst =ip6_route_output (net,NULL , &fl6);if (dst->error) { err = dst->error; -dst_release (dst); goto drop; }@@ -593,6 +594,7 @@ static int seg6_output_core(struct net *net, struct sock *sk,return dst_output (net, sk, skb); drop: +dst_release (dst);kfree_skb (skb);return err; }
Jakub Kicinski
maintainerQuoted 7 lines · On Wed, 29 Jan 2025 17:17:28 +0100 Justin Iurman wrote:
Quoted 4 lines
net/ipv6/ioam6_iptunnel.c | 5 +++-- net/ipv6/rpl_iptunnel.c | 6 ++++-- net/ipv6/seg6_iptunnel.c | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-)I think both
ila_output()andtipc_udp_xmit()should also be patched accordingly. Other users seem fine.
TIPC is not a lwt, the cache belongs to a socket not another dst, AFAICT.
I think in ILA ilwt->connected will only be set if we re-routed the traffic to the real address already? So the dst can't match.
CC: Tom, full patch:
https://lore.kernel.org/all/[email protected]/
Quoted 11 lines · Jakub Kicinski
Quoted 7 lines · On Wed, 29 Jan 2025 17:17:28 +0100 Justin Iurman wrote:
Quoted 4 lines
net/ipv6/ioam6_iptunnel.c | 5 +++-- net/ipv6/rpl_iptunnel.c | 6 ++++-- net/ipv6/seg6_iptunnel.c | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-)I think both
ila_output()andtipc_udp_xmit()should also be patched accordingly. Other users seem fine.TIPC is not a lwt, the cache belongs to a socket not another dst, AFAICT.
*sigh* You're right, my bad.
Quoted 4 lines
I think in ILA
ilwt->connectedwill only be set if we re-routed the traffic to the real address already? So the dst can't match.
CC: Tom, full patch:
https://lore.kernel.org/all/[email protected]/
Looks like I was in a hurry when I reported this one. On second reading, this is also how I understand it.
Quoted 107 lines · Jakub Kicinski
dst_cache_get()gives us a reference, we need to release it.Discovered by the
ioam6.shtest, kmemleak was recently fixed to catch per-cpu memory leaks.Fixes 985ec6f5e623 ("net: ipv6: rpl_iptunnel: mitigate 2-realloc issue")Fixes 40475b63761a ("net: ipv6: seg6_iptunnel: mitigate 2-realloc issue")Fixes dce525185bc9 ("net: ipv6: ioam6_iptunnel: mitigate 2-realloc issue")Signed-off-by Jakub Kicinski <[email protected]>---
CC: [email protected]
CC: [email protected]--- net/ipv6/ioam6_iptunnel.c | 5 +++-- net/ipv6/rpl_iptunnel.c | 6 ++++-- net/ipv6/seg6_iptunnel.c | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-)net/ipv6/ioam6_iptunnel.c +3
-2@@ -336,7 +336,7 @@ static int ioam6_do_encap(struct net *net, struct sk_buff *skb,static int ioam6_output (struct net *net,struct sock *sk,struct sk_buff *skb) { -struct dst_entry *dst =skb_dst (skb), *cache_dst; +struct dst_entry *dst =skb_dst (skb), *cache_dst =NULL ;struct in6_addr orig_daddr;struct ioam6_lwt *ilwt;int err = -EINVAL ;@@ -407,7 +407,6 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) cache_dst =ip6_route_output (net,NULL , &fl6);if (cache_dst->error) { err = cache_dst->error; -dst_release (cache_dst); goto drop; }@@ -426,8 +425,10 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb)return dst_output (net, sk, skb); } out: +dst_release (cache_dst);return dst->lwtstate->orig_output (net, sk, skb); drop: +dst_release (cache_dst);kfree_skb (skb);return err; }net/ipv6/rpl_iptunnel.c +4
-2@@ -232,7 +232,6 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb) dst =ip6_route_output (net,NULL , &fl6);if (dst->error) { err = dst->error; -dst_release (dst); goto drop; }@@ -251,6 +250,7 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)return dst_output (net, sk, skb); drop: +dst_release (dst);kfree_skb (skb);return err; }@@ -269,8 +269,10 @@ static int rpl_input(struct sk_buff *skb)local_bh_enable (); err =rpl_do_srh (skb, rlwt, dst); -if (unlikely (err)) +if (unlikely (err)) { +dst_release (dst); goto drop; + }if (!dst) {ip6_route_input (skb);net/ipv6/seg6_iptunnel.c +3
-2@@ -482,8 +482,10 @@ static int seg6_input_core(struct net *net, struct sock *sk,local_bh_enable (); err =seg6_do_srh (skb, dst); -if (unlikely (err)) +if (unlikely (err)) { +dst_release (dst); goto drop; + }if (!dst) {ip6_route_input (skb);@@ -571,7 +573,6 @@ static int seg6_output_core(struct net *net, struct sock *sk, dst =ip6_route_output (net,NULL , &fl6);if (dst->error) { err = dst->error; -dst_release (dst); goto drop; }
net/ipv6/seg6_iptunnel.c static int seg6_output_core(struct net *net, struct sock *sk, · line 600
Quoted 7 lines
net/ipv6/seg6_iptunnel.c +1
-0@@ -593,6 +594,7 @@ static int seg6_output_core(struct net *net, struct sock *sk,return dst_output (net, sk, skb); drop: +dst_release (dst);kfree_skb (skb);return err; }
Thanks Jakub!