Commit 9620fef2 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

ipv4: convert dst_metrics.refcnt from atomic_t to refcount_t

refcount_t type and corresponding API should be
used instead of atomic_t when the variable is used as
a reference counter. This allows to avoid accidental
refcounter overflows that might lead to use-after-free
situations.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 633cefe3
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <linux/rcupdate.h> #include <linux/rcupdate.h>
#include <linux/bug.h> #include <linux/bug.h>
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/refcount.h>
#include <net/neighbour.h> #include <net/neighbour.h>
#include <asm/processor.h> #include <asm/processor.h>
...@@ -107,7 +108,7 @@ struct dst_entry { ...@@ -107,7 +108,7 @@ struct dst_entry {
struct dst_metrics { struct dst_metrics {
u32 metrics[RTAX_MAX]; u32 metrics[RTAX_MAX];
atomic_t refcnt; refcount_t refcnt;
}; };
extern const struct dst_metrics dst_default_metrics; extern const struct dst_metrics dst_default_metrics;
......
...@@ -55,7 +55,7 @@ const struct dst_metrics dst_default_metrics = { ...@@ -55,7 +55,7 @@ const struct dst_metrics dst_default_metrics = {
* We really want to avoid false sharing on this variable, and catch * We really want to avoid false sharing on this variable, and catch
* any writes on it. * any writes on it.
*/ */
.refcnt = ATOMIC_INIT(1), .refcnt = REFCOUNT_INIT(1),
}; };
void dst_init(struct dst_entry *dst, struct dst_ops *ops, void dst_init(struct dst_entry *dst, struct dst_ops *ops,
...@@ -213,7 +213,7 @@ u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old) ...@@ -213,7 +213,7 @@ u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old); struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
unsigned long prev, new; unsigned long prev, new;
atomic_set(&p->refcnt, 1); refcount_set(&p->refcnt, 1);
memcpy(p->metrics, old_p->metrics, sizeof(p->metrics)); memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
new = (unsigned long) p; new = (unsigned long) p;
...@@ -225,7 +225,7 @@ u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old) ...@@ -225,7 +225,7 @@ u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
if (prev & DST_METRICS_READ_ONLY) if (prev & DST_METRICS_READ_ONLY)
p = NULL; p = NULL;
} else if (prev & DST_METRICS_REFCOUNTED) { } else if (prev & DST_METRICS_REFCOUNTED) {
if (atomic_dec_and_test(&old_p->refcnt)) if (refcount_dec_and_test(&old_p->refcnt))
kfree(old_p); kfree(old_p);
} }
} }
......
...@@ -220,7 +220,7 @@ static void free_fib_info_rcu(struct rcu_head *head) ...@@ -220,7 +220,7 @@ static void free_fib_info_rcu(struct rcu_head *head)
} endfor_nexthops(fi); } endfor_nexthops(fi);
m = fi->fib_metrics; m = fi->fib_metrics;
if (m != &dst_default_metrics && atomic_dec_and_test(&m->refcnt)) if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
kfree(m); kfree(m);
kfree(fi); kfree(fi);
} }
...@@ -1090,7 +1090,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg, ...@@ -1090,7 +1090,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg,
kfree(fi); kfree(fi);
return ERR_PTR(err); return ERR_PTR(err);
} }
atomic_set(&fi->fib_metrics->refcnt, 1); refcount_set(&fi->fib_metrics->refcnt, 1);
} else { } else {
fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics; fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
} }
......
...@@ -1398,7 +1398,7 @@ static void ipv4_dst_destroy(struct dst_entry *dst) ...@@ -1398,7 +1398,7 @@ static void ipv4_dst_destroy(struct dst_entry *dst)
struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst); struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
struct rtable *rt = (struct rtable *) dst; struct rtable *rt = (struct rtable *) dst;
if (p != &dst_default_metrics && atomic_dec_and_test(&p->refcnt)) if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt))
kfree(p); kfree(p);
if (!list_empty(&rt->rt_uncached)) { if (!list_empty(&rt->rt_uncached)) {
...@@ -1456,7 +1456,7 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr, ...@@ -1456,7 +1456,7 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
dst_init_metrics(&rt->dst, fi->fib_metrics->metrics, true); dst_init_metrics(&rt->dst, fi->fib_metrics->metrics, true);
if (fi->fib_metrics != &dst_default_metrics) { if (fi->fib_metrics != &dst_default_metrics) {
rt->dst._metrics |= DST_METRICS_REFCOUNTED; rt->dst._metrics |= DST_METRICS_REFCOUNTED;
atomic_inc(&fi->fib_metrics->refcnt); refcount_inc(&fi->fib_metrics->refcnt);
} }
#ifdef CONFIG_IP_ROUTE_CLASSID #ifdef CONFIG_IP_ROUTE_CLASSID
rt->dst.tclassid = nh->nh_tclassid; rt->dst.tclassid = nh->nh_tclassid;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment