net/dst: improve dst_ops refcounting with per-dst bit
Subsystems (7)
PATCH 1/1 net/dst: improve dst_ops refcounting with per-dst bit
Before a netns is destroyed, we make sure that all its dst_entries have been removed, or went through dst_dev_put().
Problem:
dst that went through dst_dev_put() might call dst_release() way after the netns has been dismantled/freed.
IPv6 keeps its ip6_dst_ops embedded in struct netns_ipv6.
This means dst_count_dec() might be called with a dst->ops pointing to freed memory.
Similarly dst->ops->kmem_cachep can cause UAF.
In this patch, I added a dst_count_dec() call from dst_dev_put(), and I added an atomic bit to make sure the dst_ops refcount is released at most once.
Then when dst_dev_put() is called, switch dst->ops to the 'template' object, so that dst->ops points back to static memory.
We might later add more READ_ONCE(dst->ops) to avoid hypothetical load-tearing.
Or we could move the counters into a separate structure, so that dst->ops is never changed.
DEBUG_NET_WARN_ON_ONCE(dst_entries_get_slow(dst) > 0) is added to dst_entries_destroy() to warn if a dst_ops is destroyed while still having active dst_entry references.
DEBUG_NET_WARN_ON_ONCE(dst->dst_ops_refcounted) is added to dst_destroy() to warn if a dst_entry is being freed but its dst_ops_refcounted bit is still set, indicating a potential refcount leak.
Note: IFF_XMIT_DST_RELEASE was added as a performance improvement, not specifically to fix this bug.
We can still audit drivers to make sure they call skb_dst_drop() before holding an skb for arbitrary amount of time.
--- include/net/dst.h | 1 + include/net/dst_ops.h | 3 +++ net/bridge/br_nf_core.c | 1 + net/core/dst.c | 31 +++++++++++++++++++++---------- net/core/pktgen.c | 1 + net/ipv4/route.c | 2 ++ net/ipv4/xfrm4_policy.c | 1 + net/ipv6/route.c | 2 ++ net/ipv6/xfrm6_policy.c | 1 + net/openvswitch/actions.c | 1 + net/sched/sch_frag.c | 1 + 11 files changed, 35 insertions(+), 10 deletions(-)
include/net/dst.h +1 -0
@@ -95,6 +95,7 @@ struct dst_entry {#ifdef CONFIG_64BIT struct lwtunnel_state *lwtstate;#endif +unsigned long dst_ops_refcounted;/* Use one (atomic) bit */ };struct dst_metrics {
include/net/dst_ops.h +3 -0
@@ -4,6 +4,7 @@#include <linux/types.h> #include <linux/percpu_counter.h> #include <linux/cache.h> +#include <net/net_debug.h> struct dst_entry ;struct kmem_cachep ;
@@ -39,6 +40,7 @@ struct dst_ops { const void *daddr);struct kmem_cache *kmem_cachep; +struct dst_ops *template;struct percpu_counter pcpuc_entries____cacheline_aligned_in_smp ; };
@@ -67,6 +69,7 @@ static inline int dst_entries_init(struct dst_ops *dst)static inline void dst_entries_destroy (struct dst_ops *dst) { +DEBUG_NET_WARN_ON_ONCE (dst_entries_get_slow (dst) >0 );percpu_counter_destroy (&dst->pcpuc_entries); }
net/bridge/br_nf_core.c +1 -0
@@ -56,6 +56,7 @@ static struct dst_ops fake_dst_ops = { .cow_metrics = fake_cow_metrics, .neigh_lookup = fake_neigh_lookup, .mtu = fake_mtu, + .template = &fake_dst_ops, };/*
net/core/dst.c +21 -10
@@ -72,8 +72,11 @@ void dst_init(struct dst_entry *dst, struct dst_ops *ops, dst->__use =0 ; dst->lastuse = jiffies; dst->flags = flags; -if (!(flags &DST_NOCOUNT )) + dst->dst_ops_refcounted =0 ; +if (!(flags &DST_NOCOUNT )) { + dst->dst_ops_refcounted =1 ;dst_entries_add (ops,1 ); + } }EXPORT_SYMBOL (dst_init);
@@ -100,6 +103,7 @@ EXPORT_SYMBOL(dst_alloc);static void dst_destroy (struct dst_entry *dst) {struct dst_entry *child =NULL ; +const struct dst_ops *ops;smp_rmb ();
@@ -110,16 +114,18 @@ static void dst_destroy(struct dst_entry *dst) child = xdst->child; }#endif -if (dst->ops->destroy) - dst->ops->destroy (dst); + ops =READ_ONCE (dst->ops); +if (ops->destroy) + ops->destroy (dst);netdev_put (dst->dev, &dst->dev_tracker);lwtstate_put (dst->lwtstate); +DEBUG_NET_WARN_ON_ONCE (dst->dst_ops_refcounted);if (dst->flags &DST_METADATA )metadata_dst_free ((struct metadata_dst *)dst);else -kmem_cache_free (dst->ops->kmem_cachep, dst); +kmem_cache_free (ops->kmem_cachep, dst); dst = child;if (dst)
@@ -133,6 +139,14 @@ static void dst_destroy_rcu(struct rcu_head *head)dst_destroy (dst); } +static void dst_count_dec (struct dst_entry *dst) +{ +struct dst_ops *ops =READ_ONCE (dst->ops); + +if (cmpxchg (&dst->dst_ops_refcounted,1 ,0 ) ==1 ) +dst_entries_add (ops,-1 ); +} +/* Operations to mark dst as DEAD and clean up the net device referenced * by dst: * 1. put the dst under blackhole interface and discard all tx/rx packets
@@ -146,9 +160,11 @@ void dst_dev_put(struct dst_entry *dst) {struct net_device *dev = dst->dev; +dst_count_dec (dst);WRITE_ONCE (dst->obsolete,DST_OBSOLETE_DEAD );if (dst->ops->ifdown) dst->ops->ifdown (dst, dev); +WRITE_ONCE (dst->ops, dst->ops->template);WRITE_ONCE (dst->input, dst_discard);WRITE_ONCE (dst->output, dst_discard_out);rcu_assign_pointer (dst->dev_rcu, blackhole_netdev);
@@ -157,12 +173,6 @@ void dst_dev_put(struct dst_entry *dst) }EXPORT_SYMBOL (dst_dev_put); -static void dst_count_dec (struct dst_entry *dst) -{ -if (!(dst->flags &DST_NOCOUNT )) -dst_entries_add (dst->ops,-1 ); -} -void dst_release (struct dst_entry *dst) {if (dst &&rcuref_put (&dst->__rcuref)) {
@@ -276,6 +286,7 @@ static struct dst_ops dst_blackhole_ops = { .update_pmtu = dst_blackhole_update_pmtu, .redirect = dst_blackhole_redirect, .mtu = dst_blackhole_mtu, + .template = &dst_blackhole_ops, }; static void __metadata_dst_init(struct metadata_dst *md_dst,
net/core/pktgen.c +1 -0
@@ -3894,6 +3894,7 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)* performance under such circumstance. */ pkt_dev->dstops.family =AF_INET ; + pkt_dev->dstops.template = &pkt_dev->dstops; pkt_dev->xdst.u.dst.dev = pkt_dev->odev;dst_init_metrics (&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false); pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
net/ipv4/route.c +2 -0
@@ -165,6 +165,7 @@ static struct dst_ops ipv4_dst_ops = { .local_out = __ip_local_out, .neigh_lookup = ipv4_neigh_lookup, .confirm_neigh = ipv4_confirm_neigh, + .template = &ipv4_dst_ops, };#define ECN_OR_COST (class) TC_PRIO_##class
@@ -2887,6 +2888,7 @@ static struct dst_ops ipv4_dst_blackhole_ops = { .update_pmtu = dst_blackhole_update_pmtu, .redirect = dst_blackhole_redirect, .mtu = dst_blackhole_mtu, + .template = &ipv4_dst_blackhole_ops, };struct dst_entry *ipv4_blackhole_route (struct net *net,struct dst_entry *dst_orig)
net/ipv4/xfrm4_policy.c +1 -0
@@ -130,6 +130,7 @@ static struct dst_ops xfrm4_dst_ops_template = { .ifdown = xfrm_dst_ifdown, .local_out = __ip_local_out, .gc_thresh = 32768, + .template = &xfrm4_dst_ops_template, };static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
net/ipv6/route.c +2 -0
@@ -263,6 +263,7 @@ static struct dst_ops ip6_dst_ops_template = { .local_out = __ip6_local_out, .neigh_lookup = ip6_dst_neigh_lookup, .confirm_neigh = ip6_confirm_neigh, + .template = &ip6_dst_ops_template, };static struct dst_ops ip6_dst_blackhole_ops = {
@@ -275,6 +276,7 @@ static struct dst_ops ip6_dst_blackhole_ops = { .update_pmtu = dst_blackhole_update_pmtu, .redirect = dst_blackhole_redirect, .mtu = dst_blackhole_mtu, + .template = &ip6_dst_blackhole_ops, };static const u32 ip6_template_metrics[RTAX_MAX ] = {
net/ipv6/xfrm6_policy.c +1 -0
@@ -165,6 +165,7 @@ static struct dst_ops xfrm6_dst_ops_template = { .ifdown = xfrm6_dst_ifdown, .local_out = __ip6_local_out, .gc_thresh = 32768, + .template = &xfrm6_dst_ops_template, };static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
net/openvswitch/actions.c +1 -0
@@ -731,6 +731,7 @@ ovs_dst_get_mtu(const struct dst_entry *dst)static struct dst_ops ovs_dst_ops = { .family =AF_UNSPEC , .mtu = ovs_dst_get_mtu, + .template = &ovs_dst_ops, };/* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
net/sched/sch_frag.c +1 -0
@@ -83,6 +83,7 @@ sch_frag_dst_get_mtu(const struct dst_entry *dst)static struct dst_ops sch_frag_dst_ops = { .family =AF_UNSPEC , .mtu = sch_frag_dst_get_mtu, + .template = &sch_frag_dst_ops, }; static int sch_fragment(struct net *net, struct sk_buff *skb,
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
Quoted 299 lines · Eric Dumazet
Before a netns is destroyed, we make sure that all its
dst_entrieshave been removed, or went throughdst_dev_put().Problem:
dst that went through
dst_dev_put()might calldst_release()way after the netns has been dismantled/freed.IPv6 keeps its
ip6_dst_opsembedded in structnetns_ipv6.This means
dst_count_dec()might be called with adst->opspointing to freed memory.Similarly
dst->ops->kmem_cachepcan cause UAF.In this patch, I added a
dst_count_dec()call fromdst_dev_put(), and I added an atomic bit to make sure thedst_opsrefcount is released at most once.Then when
dst_dev_put()is called, switchdst->opsto the 'template' object, so thatdst->opspoints back to static memory.We might later add more
READ_ONCE(dst->ops)to avoid hypothetical load-tearing.Or we could move the counters into a separate structure, so that
dst->opsis never changed.
DEBUG_NET_WARN_ON_ONCE(dst_entries_get_slow(dst) > 0)is added todst_entries_destroy()to warn if adst_opsis destroyed while still having activedst_entryreferences.
DEBUG_NET_WARN_ON_ONCE(dst->dst_ops_refcounted)is added todst_destroy()to warn if adst_entryis being freed but itsdst_ops_refcountedbit is still set, indicating a potential refcount leak.Note:
IFF_XMIT_DST_RELEASEwas added as a performance improvement, not specifically to fix this bug.We can still audit drivers to make sure they call
skb_dst_drop()before holding an skb for arbitrary amount of time.Fixes f2fc6a54585a ("[NETNS][IPV6] route6 - move ip6_dst_ops inside the network namespace")Signed-off-by Eric Dumazet <[email protected]>Cc xietangxin <[email protected]>--- include/net/dst.h | 1 + include/net/dst_ops.h | 3 +++ net/bridge/br_nf_core.c | 1 + net/core/dst.c | 31 +++++++++++++++++++++---------- net/core/pktgen.c | 1 + net/ipv4/route.c | 2 ++ net/ipv4/xfrm4_policy.c | 1 + net/ipv6/route.c | 2 ++ net/ipv6/xfrm6_policy.c | 1 + net/openvswitch/actions.c | 1 + net/sched/sch_frag.c | 1 + 11 files changed, 35 insertions(+), 10 deletions(-)include/net/dst.h +1
-0@@ -95,6 +95,7 @@ struct dst_entry {#ifdef CONFIG_64BIT struct lwtunnel_state *lwtstate;#endif +unsigned long dst_ops_refcounted;/* Use one (atomic) bit */ };struct dst_metrics {include/net/dst_ops.h +3
-0@@ -4,6 +4,7 @@#include <linux/types.h> #include <linux/percpu_counter.h> #include <linux/cache.h> +#include <net/net_debug.h> struct dst_entry ;struct kmem_cachep ;@@ -39,6 +40,7 @@ struct dst_ops { const void *daddr);struct kmem_cache *kmem_cachep; +struct dst_ops *template;struct percpu_counter pcpuc_entries____cacheline_aligned_in_smp ; };@@ -67,6 +69,7 @@ static inline int dst_entries_init(struct dst_ops *dst)static inline void dst_entries_destroy (struct dst_ops *dst) { +DEBUG_NET_WARN_ON_ONCE (dst_entries_get_slow (dst) >0 );percpu_counter_destroy (&dst->pcpuc_entries); }net/bridge/br_nf_core.c +1
-0@@ -56,6 +56,7 @@ static struct dst_ops fake_dst_ops = { .cow_metrics = fake_cow_metrics, .neigh_lookup = fake_neigh_lookup, .mtu = fake_mtu, + .template = &fake_dst_ops, };/* net/core/dst.c +21
-10@@ -72,8 +72,11 @@ void dst_init(struct dst_entry *dst, struct dst_ops *ops, dst->__use =0 ; dst->lastuse = jiffies; dst->flags = flags; -if (!(flags &DST_NOCOUNT )) + dst->dst_ops_refcounted =0 ; +if (!(flags &DST_NOCOUNT )) { + dst->dst_ops_refcounted =1 ;dst_entries_add (ops,1 ); + } }EXPORT_SYMBOL (dst_init);@@ -100,6 +103,7 @@ EXPORT_SYMBOL(dst_alloc);static void dst_destroy (struct dst_entry *dst) {struct dst_entry *child =NULL ; +const struct dst_ops *ops;smp_rmb ();@@ -110,16 +114,18 @@ static void dst_destroy(struct dst_entry *dst) child = xdst->child; }#endif -if (dst->ops->destroy) - dst->ops->destroy (dst); + ops =READ_ONCE (dst->ops); +if (ops->destroy) + ops->destroy (dst);netdev_put (dst->dev, &dst->dev_tracker);lwtstate_put (dst->lwtstate); +DEBUG_NET_WARN_ON_ONCE (dst->dst_ops_refcounted);if (dst->flags &DST_METADATA )metadata_dst_free ((struct metadata_dst *)dst);else -kmem_cache_free (dst->ops->kmem_cachep, dst); +kmem_cache_free (ops->kmem_cachep, dst); dst = child;if (dst)@@ -133,6 +139,14 @@ static void dst_destroy_rcu(struct rcu_head *head)dst_destroy (dst); } +static void dst_count_dec (struct dst_entry *dst) +{ +struct dst_ops *ops =READ_ONCE (dst->ops); + +if (cmpxchg (&dst->dst_ops_refcounted,1 ,0 ) ==1 ) +dst_entries_add (ops,-1 ); +} +/* Operations to mark dst as DEAD and clean up the net device referenced * by dst: * 1. put the dst under blackhole interface and discard all tx/rx packets @@ -146,9 +160,11 @@ void dst_dev_put(struct dst_entry *dst) {struct net_device *dev = dst->dev; +dst_count_dec (dst);WRITE_ONCE (dst->obsolete,DST_OBSOLETE_DEAD );if (dst->ops->ifdown) dst->ops->ifdown (dst, dev); +WRITE_ONCE (dst->ops, dst->ops->template);WRITE_ONCE (dst->input, dst_discard);WRITE_ONCE (dst->output, dst_discard_out);rcu_assign_pointer (dst->dev_rcu, blackhole_netdev);@@ -157,12 +173,6 @@ void dst_dev_put(struct dst_entry *dst) }EXPORT_SYMBOL (dst_dev_put); -static void dst_count_dec (struct dst_entry *dst) -{ -if (!(dst->flags &DST_NOCOUNT )) -dst_entries_add (dst->ops,-1 ); -} -void dst_release (struct dst_entry *dst) {if (dst &&rcuref_put (&dst->__rcuref)) {@@ -276,6 +286,7 @@ static struct dst_ops dst_blackhole_ops = { .update_pmtu = dst_blackhole_update_pmtu, .redirect = dst_blackhole_redirect, .mtu = dst_blackhole_mtu, + .template = &dst_blackhole_ops, }; static void __metadata_dst_init(struct metadata_dst *md_dst,net/core/pktgen.c +1
-0@@ -3894,6 +3894,7 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)* performance under such circumstance. */ pkt_dev->dstops.family =AF_INET ; + pkt_dev->dstops.template = &pkt_dev->dstops; pkt_dev->xdst.u.dst.dev = pkt_dev->odev;dst_init_metrics (&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false); pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;net/ipv4/route.c +2
-0@@ -165,6 +165,7 @@ static struct dst_ops ipv4_dst_ops = { .local_out = __ip_local_out, .neigh_lookup = ipv4_neigh_lookup, .confirm_neigh = ipv4_confirm_neigh, + .template = &ipv4_dst_ops, };#define ECN_OR_COST (class) TC_PRIO_##class@@ -2887,6 +2888,7 @@ static struct dst_ops ipv4_dst_blackhole_ops = { .update_pmtu = dst_blackhole_update_pmtu, .redirect = dst_blackhole_redirect, .mtu = dst_blackhole_mtu, + .template = &ipv4_dst_blackhole_ops, };struct dst_entry *ipv4_blackhole_route (struct net *net,struct dst_entry *dst_orig)net/ipv4/xfrm4_policy.c +1
-0@@ -130,6 +130,7 @@ static struct dst_ops xfrm4_dst_ops_template = { .ifdown = xfrm_dst_ifdown, .local_out = __ip_local_out, .gc_thresh = 32768, + .template = &xfrm4_dst_ops_template, };static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {net/ipv6/route.c +2
-0@@ -263,6 +263,7 @@ static struct dst_ops ip6_dst_ops_template = { .local_out = __ip6_local_out, .neigh_lookup = ip6_dst_neigh_lookup, .confirm_neigh = ip6_confirm_neigh, + .template = &ip6_dst_ops_template, };static struct dst_ops ip6_dst_blackhole_ops = {@@ -275,6 +276,7 @@ static struct dst_ops ip6_dst_blackhole_ops = { .update_pmtu = dst_blackhole_update_pmtu, .redirect = dst_blackhole_redirect, .mtu = dst_blackhole_mtu, + .template = &ip6_dst_blackhole_ops, };static const u32 ip6_template_metrics[RTAX_MAX ] = {net/ipv6/xfrm6_policy.c +1
-0@@ -165,6 +165,7 @@ static struct dst_ops xfrm6_dst_ops_template = { .ifdown = xfrm6_dst_ifdown, .local_out = __ip6_local_out, .gc_thresh = 32768, + .template = &xfrm6_dst_ops_template, };static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {net/openvswitch/actions.c +1
-0@@ -731,6 +731,7 @@ ovs_dst_get_mtu(const struct dst_entry *dst)static struct dst_ops ovs_dst_ops = { .family =AF_UNSPEC , .mtu = ovs_dst_get_mtu, + .template = &ovs_dst_ops, };/* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is net/sched/sch_frag.c +0
-0
net/sched/sch_frag.c sch_frag_dst_get_mtu(const struct dst_entry *dst) · line 89
Quoted 7 lines
net/sched/sch_frag.c +1
-0@@ -83,6 +83,7 @@ sch_frag_dst_get_mtu(const struct dst_entry *dst)static struct dst_ops sch_frag_dst_ops = { .family =AF_UNSPEC , .mtu = sch_frag_dst_get_mtu, + .template = &sch_frag_dst_ops, }; static int sch_fragment(struct net *net, struct sk_buff *skb,
Hi Eric,
I tested your patch with my reproduction script, but the UAF still occurs with a warning in dst_entries_destroy() just before the crash.
The reason is that for IPv6, dst_entries on the rt6_uncached_list are handled by rt6_uncached_list_flush_dev() during netns destruction, which bypasses dst_dev_put().
I tested add changes like dst_dev_put() inside rt6_uncached_list_flush_dev(), the UAF is gone and the dst_entries_destroy warning no longer appears.
Best regards,
Tangxin Xie
Quoted 7 lines · xietangxin
Hi Eric,
I tested your patch with my reproduction script, but the UAF still occurs with a warning in
dst_entries_destroy()just before the crash.
Quoted 6 lines
The reason is that for IPv6,
dst_entrieson thert6_uncached_listare handled byrt6_uncached_list_flush_dev()during netns destruction, which bypassesdst_dev_put().I tested add changes like
dst_dev_put()insidert6_uncached_list_flush_dev(), the UAF is gone and thedst_entries_destroywarning no longer appears.
Could you share the incremental patch you added on top of mine ?
Thanks.
Quoted 12 lines · Eric Dumazet
Quoted 11 lines · xietangxin
Hi Eric,
I tested your patch with my reproduction script, but the UAF still occurs with a warning in
dst_entries_destroy()just before the crash.The reason is that for IPv6,
dst_entrieson thert6_uncached_listare handled byrt6_uncached_list_flush_dev()during netns destruction, which bypassesdst_dev_put().
Quoted 7 lines
Quoted 2 lines
I tested add changes like
dst_dev_put()insidert6_uncached_list_flush_dev(), the UAF is gone and thedst_entries_destroywarning no longer appears.Could you share the incremental patch you added on top of mine ?
Thanks.
Hi Eric,
Sure. Here is the incremental patch that I added on top of your patch.
---
include/net/dst.h +1 -0
@@ -401,6 +401,7 @@ void dst_init(struct dst_entry *dst, struct dst_ops *ops, struct net_device *dev, int initial_obsolete, unsigned short flags);void dst_dev_put (struct dst_entry *dst); +void dst_count_dec (struct dst_entry *dst);static inline void dst_confirm (struct dst_entry *dst) {
net/core/dst.c +1 -1
@@ -139,7 +139,7 @@ static void dst_destroy_rcu(struct rcu_head *head)dst_destroy (dst); } -static void dst_count_dec (struct dst_entry *dst) +void dst_count_dec (struct dst_entry *dst) {struct dst_ops *ops =READ_ONCE (dst->ops);
net/ipv6/route.c +6 -3
@@ -173,6 +173,7 @@ static void rt6_uncached_list_flush_dev(struct net_device *dev)struct inet6_dev *rt_idev = rt->rt6i_idev;struct net_device *rt_dev = rt->dst.dev;bool handled = false; +struct dst_entry *dst = &rt->dst;if (rt_idev && rt_idev->dev == dev) { rt->rt6i_idev =in6_dev_get (blackhole_netdev);
@@ -181,14 +182,16 @@ static void rt6_uncached_list_flush_dev(struct net_device *dev) }if (rt_dev == dev) { - rt->dst.dev = blackhole_netdev; + dst->dev = blackhole_netdev; +dst_count_dec (dst); +WRITE_ONCE (dst->ops, dst->ops->template);netdev_ref_replace (rt_dev, blackhole_netdev, - &rt->dst.dev_tracker, + &dst->dev_tracker,GFP_ATOMIC ); handled = true; }if (handled) -list_del_init (&rt->dst.rt_uncached); +list_del_init (&dst->rt_uncached); }spin_unlock_bh (&ul->lock); }
Signature · 2 lines
Best regards, Tangxin Xie