]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/commitdiff
ipv4: convert dst_metrics.refcnt from atomic_t to refcount_t
authorEric Dumazet <edumazet@google.com>
Mon, 7 May 2018 21:23:00 +0000 (23:23 +0200)
committerStefan Bader <stefan.bader@canonical.com>
Wed, 23 May 2018 07:08:32 +0000 (09:08 +0200)
BugLink: http://bugs.launchpad.net/bugs/1746474
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: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
(cherry picked from commit 9620fef27ed2cdb37bf6fd028f32bea2ef5119a8)
Signed-off-by: Joseph Salisbury <joseph.salisbury@canonical.com>
Acked-by: Khalid Elmously <khalid.elmously@canonical.com>
Acked-by: AceLan Kao <acelan.kao@canonical.com>
Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
include/net/dst.h
net/core/dst.c
net/ipv4/fib_semantics.c
net/ipv4/route.c

index f73611ec401754d4f52b5310a24da53566dafce6..93568bd0a3520bb7402f04d90cf04ac99c81cfbe 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/rcupdate.h>
 #include <linux/bug.h>
 #include <linux/jiffies.h>
+#include <linux/refcount.h>
 #include <net/neighbour.h>
 #include <asm/processor.h>
 
@@ -107,7 +108,7 @@ struct dst_entry {
 
 struct dst_metrics {
        u32             metrics[RTAX_MAX];
-       atomic_t        refcnt;
+       refcount_t      refcnt;
 };
 extern const struct dst_metrics dst_default_metrics;
 
index 00aa972ad1a1a451c24f3f8211243ad35c19433a..d6ead757c25895da01eb61bc9636c7e9b3cdfb3e 100644 (file)
@@ -55,7 +55,7 @@ const struct dst_metrics dst_default_metrics = {
         * We really want to avoid false sharing on this variable, and catch
         * any writes on it.
         */
-       .refcnt = ATOMIC_INIT(1),
+       .refcnt = REFCOUNT_INIT(1),
 };
 
 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)
                struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
                unsigned long prev, new;
 
-               atomic_set(&p->refcnt, 1);
+               refcount_set(&p->refcnt, 1);
                memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
 
                new = (unsigned long) p;
@@ -225,7 +225,7 @@ u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
                        if (prev & DST_METRICS_READ_ONLY)
                                p = NULL;
                } 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);
                }
        }
index ec3a9ce281a6ffb86b62e21f7284fd7c801668f0..a663c09c9910cd882c68ed845f7bcb8c710c80f9 100644 (file)
@@ -219,7 +219,7 @@ static void free_fib_info_rcu(struct rcu_head *head)
        } endfor_nexthops(fi);
 
        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(fi);
 }
@@ -1089,7 +1089,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg,
                        kfree(fi);
                        return ERR_PTR(err);
                }
-               atomic_set(&fi->fib_metrics->refcnt, 1);
+               refcount_set(&fi->fib_metrics->refcnt, 1);
        } else {
                fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
        }
index 72afa4cfb022511f4d84a8c1471224eb4c866d03..83a2c49487b8a508376159770d470de6c97a5558 100644 (file)
@@ -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 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);
 
        if (!list_empty(&rt->rt_uncached)) {
@@ -1456,7 +1456,7 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
                dst_init_metrics(&rt->dst, fi->fib_metrics->metrics, true);
                if (fi->fib_metrics != &dst_default_metrics) {
                        rt->dst._metrics |= DST_METRICS_REFCOUNTED;
-                       atomic_inc(&fi->fib_metrics->refcnt);
+                       refcount_inc(&fi->fib_metrics->refcnt);
                }
 #ifdef CONFIG_IP_ROUTE_CLASSID
                rt->dst.tclassid = nh->nh_tclassid;