net/neigh: fix NULL deref in pneigh_dump_table()
[linux-2.6-microblaze.git] / net / core / neighbour.c
index 4e07824..41954e4 100644 (file)
@@ -232,7 +232,8 @@ static void pneigh_queue_purge(struct sk_buff_head *list)
        }
 }
 
-static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
+static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev,
+                           bool skip_perm)
 {
        int i;
        struct neigh_hash_table *nht;
@@ -250,6 +251,10 @@ static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
                                np = &n->next;
                                continue;
                        }
+                       if (skip_perm && n->nud_state & NUD_PERMANENT) {
+                               np = &n->next;
+                               continue;
+                       }
                        rcu_assign_pointer(*np,
                                   rcu_dereference_protected(n->next,
                                                lockdep_is_held(&tbl->lock)));
@@ -285,21 +290,35 @@ static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev)
 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
 {
        write_lock_bh(&tbl->lock);
-       neigh_flush_dev(tbl, dev);
+       neigh_flush_dev(tbl, dev, false);
        write_unlock_bh(&tbl->lock);
 }
 EXPORT_SYMBOL(neigh_changeaddr);
 
-int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
+static int __neigh_ifdown(struct neigh_table *tbl, struct net_device *dev,
+                         bool skip_perm)
 {
        write_lock_bh(&tbl->lock);
-       neigh_flush_dev(tbl, dev);
+       neigh_flush_dev(tbl, dev, skip_perm);
        pneigh_ifdown_and_unlock(tbl, dev);
 
        del_timer_sync(&tbl->proxy_timer);
        pneigh_queue_purge(&tbl->proxy_queue);
        return 0;
 }
+
+int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev)
+{
+       __neigh_ifdown(tbl, dev, true);
+       return 0;
+}
+EXPORT_SYMBOL(neigh_carrier_down);
+
+int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
+{
+       __neigh_ifdown(tbl, dev, false);
+       return 0;
+}
 EXPORT_SYMBOL(neigh_ifdown);
 
 static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev)
@@ -1279,11 +1298,8 @@ int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
                neigh->arp_queue_len_bytes = 0;
        }
 out:
-       if (update_isrouter) {
-               neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
-                       (neigh->flags | NTF_ROUTER) :
-                       (neigh->flags & ~NTF_ROUTER);
-       }
+       if (update_isrouter)
+               neigh_update_is_router(neigh, flags, &notify);
        write_unlock_bh(&neigh->lock);
 
        if (notify)
@@ -1711,7 +1727,8 @@ out:
 static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
                     struct netlink_ext_ack *extack)
 {
-       int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE;
+       int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE |
+               NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
        struct net *net = sock_net(skb->sk);
        struct ndmsg *ndm;
        struct nlattr *tb[NDA_MAX+1];
@@ -1786,12 +1803,16 @@ static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh,
                }
 
                if (!(nlh->nlmsg_flags & NLM_F_REPLACE))
-                       flags &= ~NEIGH_UPDATE_F_OVERRIDE;
+                       flags &= ~(NEIGH_UPDATE_F_OVERRIDE |
+                                  NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
        }
 
        if (ndm->ndm_flags & NTF_EXT_LEARNED)
                flags |= NEIGH_UPDATE_F_EXT_LEARNED;
 
+       if (ndm->ndm_flags & NTF_ROUTER)
+               flags |= NEIGH_UPDATE_F_ISROUTER;
+
        if (ndm->ndm_flags & NTF_USE) {
                neigh_event_send(neigh, NULL);
                err = 0;
@@ -2161,15 +2182,47 @@ errout:
        return err;
 }
 
+static int neightbl_valid_dump_info(const struct nlmsghdr *nlh,
+                                   struct netlink_ext_ack *extack)
+{
+       struct ndtmsg *ndtm;
+
+       if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndtm))) {
+               NL_SET_ERR_MSG(extack, "Invalid header for neighbor table dump request");
+               return -EINVAL;
+       }
+
+       ndtm = nlmsg_data(nlh);
+       if (ndtm->ndtm_pad1  || ndtm->ndtm_pad2) {
+               NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor table dump request");
+               return -EINVAL;
+       }
+
+       if (nlmsg_attrlen(nlh, sizeof(*ndtm))) {
+               NL_SET_ERR_MSG(extack, "Invalid data after header in neighbor table dump request");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       const struct nlmsghdr *nlh = cb->nlh;
        struct net *net = sock_net(skb->sk);
        int family, tidx, nidx = 0;
        int tbl_skip = cb->args[0];
        int neigh_skip = cb->args[1];
        struct neigh_table *tbl;
 
-       family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
+       if (cb->strict_check) {
+               int err = neightbl_valid_dump_info(nlh, cb->extack);
+
+               if (err < 0)
+                       return err;
+       }
+
+       family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
 
        for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) {
                struct neigh_parms *p;
@@ -2182,7 +2235,7 @@ static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
                        continue;
 
                if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid,
-                                      cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
+                                      nlh->nlmsg_seq, RTM_NEWNEIGHTBL,
                                       NLM_F_MULTI) < 0)
                        break;
 
@@ -2197,7 +2250,7 @@ static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
 
                        if (neightbl_fill_param_info(skb, tbl, p,
                                                     NETLINK_CB(cb->skb).portid,
-                                                    cb->nlh->nlmsg_seq,
+                                                    nlh->nlmsg_seq,
                                                     RTM_NEWNEIGHTBL,
                                                     NLM_F_MULTI) < 0)
                                goto out;
@@ -2311,7 +2364,7 @@ static bool neigh_master_filtered(struct net_device *dev, int master_idx)
        if (!master_idx)
                return false;
 
-       master = netdev_master_upper_dev_get(dev);
+       master = dev ? netdev_master_upper_dev_get(dev) : NULL;
        if (!master || master->ifindex != master_idx)
                return true;
 
@@ -2320,41 +2373,30 @@ static bool neigh_master_filtered(struct net_device *dev, int master_idx)
 
 static bool neigh_ifindex_filtered(struct net_device *dev, int filter_idx)
 {
-       if (filter_idx && dev->ifindex != filter_idx)
+       if (filter_idx && (!dev || dev->ifindex != filter_idx))
                return true;
 
        return false;
 }
 
+struct neigh_dump_filter {
+       int master_idx;
+       int dev_idx;
+};
+
 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
-                           struct netlink_callback *cb)
+                           struct netlink_callback *cb,
+                           struct neigh_dump_filter *filter)
 {
        struct net *net = sock_net(skb->sk);
-       const struct nlmsghdr *nlh = cb->nlh;
-       struct nlattr *tb[NDA_MAX + 1];
        struct neighbour *n;
        int rc, h, s_h = cb->args[1];
        int idx, s_idx = idx = cb->args[2];
        struct neigh_hash_table *nht;
-       int filter_master_idx = 0, filter_idx = 0;
        unsigned int flags = NLM_F_MULTI;
-       int err;
 
-       err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX, NULL, NULL);
-       if (!err) {
-               if (tb[NDA_IFINDEX]) {
-                       if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
-                               return -EINVAL;
-                       filter_idx = nla_get_u32(tb[NDA_IFINDEX]);
-               }
-               if (tb[NDA_MASTER]) {
-                       if (nla_len(tb[NDA_MASTER]) != sizeof(u32))
-                               return -EINVAL;
-                       filter_master_idx = nla_get_u32(tb[NDA_MASTER]);
-               }
-               if (filter_idx || filter_master_idx)
-                       flags |= NLM_F_DUMP_FILTERED;
-       }
+       if (filter->dev_idx || filter->master_idx)
+               flags |= NLM_F_DUMP_FILTERED;
 
        rcu_read_lock_bh();
        nht = rcu_dereference_bh(tbl->nht);
@@ -2367,8 +2409,8 @@ static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
                     n = rcu_dereference_bh(n->next)) {
                        if (idx < s_idx || !net_eq(dev_net(n->dev), net))
                                goto next;
-                       if (neigh_ifindex_filtered(n->dev, filter_idx) ||
-                           neigh_master_filtered(n->dev, filter_master_idx))
+                       if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
+                           neigh_master_filtered(n->dev, filter->master_idx))
                                goto next;
                        if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
                                            cb->nlh->nlmsg_seq,
@@ -2390,12 +2432,17 @@ out:
 }
 
 static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
-                            struct netlink_callback *cb)
+                            struct netlink_callback *cb,
+                            struct neigh_dump_filter *filter)
 {
        struct pneigh_entry *n;
        struct net *net = sock_net(skb->sk);
        int rc, h, s_h = cb->args[3];
        int idx, s_idx = idx = cb->args[4];
+       unsigned int flags = NLM_F_MULTI;
+
+       if (filter->dev_idx || filter->master_idx)
+               flags |= NLM_F_DUMP_FILTERED;
 
        read_lock_bh(&tbl->lock);
 
@@ -2405,10 +2452,12 @@ static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
                for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) {
                        if (idx < s_idx || pneigh_net(n) != net)
                                goto next;
+                       if (neigh_ifindex_filtered(n->dev, filter->dev_idx) ||
+                           neigh_master_filtered(n->dev, filter->master_idx))
+                               goto next;
                        if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid,
                                            cb->nlh->nlmsg_seq,
-                                           RTM_NEWNEIGH,
-                                           NLM_F_MULTI, tbl) < 0) {
+                                           RTM_NEWNEIGH, flags, tbl) < 0) {
                                read_unlock_bh(&tbl->lock);
                                rc = -1;
                                goto out;
@@ -2427,22 +2476,91 @@ out:
 
 }
 
+static int neigh_valid_dump_req(const struct nlmsghdr *nlh,
+                               bool strict_check,
+                               struct neigh_dump_filter *filter,
+                               struct netlink_ext_ack *extack)
+{
+       struct nlattr *tb[NDA_MAX + 1];
+       int err, i;
+
+       if (strict_check) {
+               struct ndmsg *ndm;
+
+               if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
+                       NL_SET_ERR_MSG(extack, "Invalid header for neighbor dump request");
+                       return -EINVAL;
+               }
+
+               ndm = nlmsg_data(nlh);
+               if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_ifindex ||
+                   ndm->ndm_state || ndm->ndm_flags || ndm->ndm_type) {
+                       NL_SET_ERR_MSG(extack, "Invalid values in header for neighbor dump request");
+                       return -EINVAL;
+               }
+
+               err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
+                                        NULL, extack);
+       } else {
+               err = nlmsg_parse(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
+                                 NULL, extack);
+       }
+       if (err < 0)
+               return err;
+
+       for (i = 0; i <= NDA_MAX; ++i) {
+               if (!tb[i])
+                       continue;
+
+               /* all new attributes should require strict_check */
+               switch (i) {
+               case NDA_IFINDEX:
+                       if (nla_len(tb[i]) != sizeof(u32)) {
+                               NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in neighbor dump request");
+                               return -EINVAL;
+                       }
+                       filter->dev_idx = nla_get_u32(tb[i]);
+                       break;
+               case NDA_MASTER:
+                       if (nla_len(tb[i]) != sizeof(u32)) {
+                               NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in neighbor dump request");
+                               return -EINVAL;
+                       }
+                       filter->master_idx = nla_get_u32(tb[i]);
+                       break;
+               default:
+                       if (strict_check) {
+                               NL_SET_ERR_MSG(extack, "Unsupported attribute in neighbor dump request");
+                               return -EINVAL;
+                       }
+               }
+       }
+
+       return 0;
+}
+
 static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       const struct nlmsghdr *nlh = cb->nlh;
+       struct neigh_dump_filter filter = {};
        struct neigh_table *tbl;
        int t, family, s_t;
        int proxy = 0;
        int err;
 
-       family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family;
+       family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
 
        /* check for full ndmsg structure presence, family member is
         * the same for both structures
         */
-       if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) &&
-           ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY)
+       if (nlmsg_len(nlh) >= sizeof(struct ndmsg) &&
+           ((struct ndmsg *)nlmsg_data(nlh))->ndm_flags == NTF_PROXY)
                proxy = 1;
 
+       err = neigh_valid_dump_req(nlh, cb->strict_check, &filter, cb->extack);
+       if (err < 0 && cb->strict_check)
+               return err;
+
        s_t = cb->args[0];
 
        for (t = 0; t < NEIGH_NR_TABLES; t++) {
@@ -2456,9 +2574,9 @@ static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
                        memset(&cb->args[1], 0, sizeof(cb->args) -
                                                sizeof(cb->args[0]));
                if (proxy)
-                       err = pneigh_dump_table(tbl, skb, cb);
+                       err = pneigh_dump_table(tbl, skb, cb, &filter);
                else
-                       err = neigh_dump_table(tbl, skb, cb);
+                       err = neigh_dump_table(tbl, skb, cb, &filter);
                if (err < 0)
                        break;
        }