net: convert fib_rule.refcnt from atomic_t to refcount_t
authorReshetova, Elena <elena.reshetova@intel.com>
Fri, 30 Jun 2017 10:08:06 +0000 (13:08 +0300)
committerDavid S. Miller <davem@davemloft.net>
Sat, 1 Jul 2017 14:39:09 +0000 (07:39 -0700)
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: Elena Reshetova <elena.reshetova@intel.com>
Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: David Windsor <dwindsor@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/fib_rules.h
net/core/fib_rules.c

index 76c7300626d675bd70374efe45e5608c65f44f84..c487bfa2f47907c8c2f059f3162e33c1d9f43b79 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/netdevice.h>
 #include <linux/fib_rules.h>
+#include <linux/refcount.h>
 #include <net/flow.h>
 #include <net/rtnetlink.h>
 
@@ -29,7 +30,7 @@ struct fib_rule {
        struct fib_rule __rcu   *ctarget;
        struct net              *fr_net;
 
-       atomic_t                refcnt;
+       refcount_t              refcnt;
        u32                     pref;
        int                     suppress_ifgroup;
        int                     suppress_prefixlen;
@@ -103,12 +104,12 @@ struct fib_rules_ops {
 
 static inline void fib_rule_get(struct fib_rule *rule)
 {
-       atomic_inc(&rule->refcnt);
+       refcount_inc(&rule->refcnt);
 }
 
 static inline void fib_rule_put(struct fib_rule *rule)
 {
-       if (atomic_dec_and_test(&rule->refcnt))
+       if (refcount_dec_and_test(&rule->refcnt))
                kfree_rcu(rule, rcu);
 }
 
index 3bba291c6c32e4359a6d626fbd492f7d07fd3e4c..c4ecd9f75a47f1c861e11b21f55768053609b649 100644 (file)
@@ -46,7 +46,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
        if (r == NULL)
                return -ENOMEM;
 
-       atomic_set(&r->refcnt, 1);
+       refcount_set(&r->refcnt, 1);
        r->action = FR_ACT_TO_TBL;
        r->pref = pref;
        r->table = table;
@@ -283,7 +283,7 @@ jumped:
 
                if (err != -EAGAIN) {
                        if ((arg->flags & FIB_LOOKUP_NOREF) ||
-                           likely(atomic_inc_not_zero(&rule->refcnt))) {
+                           likely(refcount_inc_not_zero(&rule->refcnt))) {
                                arg->rule = rule;
                                goto out;
                        }