OSDN Git Service

rtnetlink: Update rtnl_bridge_getlink for strict data checking
authorDavid Ahern <dsahern@gmail.com>
Mon, 8 Oct 2018 03:16:31 +0000 (20:16 -0700)
committerDavid S. Miller <davem@davemloft.net>
Mon, 8 Oct 2018 17:39:04 +0000 (10:39 -0700)
Update rtnl_bridge_getlink for strict data checking. If the flag is set,
the dump request is expected to have an ifinfomsg struct as the header
potentially followed by one or more attributes. Any data passed in the
header or as an attribute is taken as a request to influence the data
returned. Only values supported by the dump handler are allowed to be
non-0 or set in the request. At the moment only the IFLA_EXT_MASK
attribute is supported.

Signed-off-by: David Ahern <dsahern@gmail.com>
Acked-by: Christian Brauner <christian@brauner.io>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/core/rtnetlink.c

index 12fd521..e38e1f1 100644 (file)
@@ -4021,28 +4021,72 @@ nla_put_failure:
 }
 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
 
+static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
+                                   bool strict_check, u32 *filter_mask,
+                                   struct netlink_ext_ack *extack)
+{
+       struct nlattr *tb[IFLA_MAX+1];
+       int err, i;
+
+       if (strict_check) {
+               struct ifinfomsg *ifm;
+
+               if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
+                       NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
+                       return -EINVAL;
+               }
+
+               ifm = nlmsg_data(nlh);
+               if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
+                   ifm->ifi_change || ifm->ifi_index) {
+                       NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
+                       return -EINVAL;
+               }
+
+               err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
+                                        IFLA_MAX, ifla_policy, extack);
+       } else {
+               err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
+                                 IFLA_MAX, ifla_policy, extack);
+       }
+       if (err < 0)
+               return err;
+
+       /* new attributes should only be added with strict checking */
+       for (i = 0; i <= IFLA_MAX; ++i) {
+               if (!tb[i])
+                       continue;
+
+               switch (i) {
+               case IFLA_EXT_MASK:
+                       *filter_mask = nla_get_u32(tb[i]);
+                       break;
+               default:
+                       if (strict_check) {
+                               NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
+                               return -EINVAL;
+                       }
+               }
+       }
+
+       return 0;
+}
+
 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       const struct nlmsghdr *nlh = cb->nlh;
        struct net *net = sock_net(skb->sk);
        struct net_device *dev;
        int idx = 0;
        u32 portid = NETLINK_CB(cb->skb).portid;
-       u32 seq = cb->nlh->nlmsg_seq;
+       u32 seq = nlh->nlmsg_seq;
        u32 filter_mask = 0;
        int err;
 
-       if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
-               struct nlattr *extfilt;
-
-               extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
-                                         IFLA_EXT_MASK);
-               if (extfilt) {
-                       if (nla_len(extfilt) < sizeof(filter_mask))
-                               return -EINVAL;
-
-                       filter_mask = nla_get_u32(extfilt);
-               }
-       }
+       err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
+                                      cb->extack);
+       if (err < 0 && cb->strict_check)
+               return err;
 
        rcu_read_lock();
        for_each_netdev_rcu(net, dev) {