]> git.ipfire.org Git - thirdparty/kernel/stable.git/blobdiff - net/ipv4/route.c
net: ipv4: Fix NULL pointer dereference in route lookup
[thirdparty/kernel/stable.git] / net / ipv4 / route.c
index 5163b64f8fb3e31a8ebb3a0f3d49aad0191d2197..e3ac458b5d8be26af72a313225bcf1cef00fb605 100644 (file)
@@ -1611,7 +1611,8 @@ int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
                return -EINVAL;
 
        if (ipv4_is_zeronet(saddr)) {
-               if (!ipv4_is_local_multicast(daddr))
+               if (!ipv4_is_local_multicast(daddr) &&
+                   ip_hdr(skb)->protocol != IPPROTO_IGMP)
                        return -EINVAL;
        } else {
                err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
@@ -1819,6 +1820,7 @@ out:
 int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
                       const struct sk_buff *skb, struct flow_keys *flkeys)
 {
+       u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0;
        struct flow_keys hash_keys;
        u32 mhash;
 
@@ -1869,6 +1871,9 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
        }
        mhash = flow_hash_from_keys(&hash_keys);
 
+       if (multipath_hash)
+               mhash = jhash_2words(mhash, multipath_hash, 0);
+
        return mhash >> 1;
 }
 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
@@ -2766,6 +2771,75 @@ static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst,
        return skb;
 }
 
+static int inet_rtm_valid_getroute_req(struct sk_buff *skb,
+                                      const struct nlmsghdr *nlh,
+                                      struct nlattr **tb,
+                                      struct netlink_ext_ack *extack)
+{
+       struct rtmsg *rtm;
+       int i, err;
+
+       if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
+               NL_SET_ERR_MSG(extack,
+                              "ipv4: Invalid header for route get request");
+               return -EINVAL;
+       }
+
+       if (!netlink_strict_get_check(skb))
+               return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
+                                  rtm_ipv4_policy, extack);
+
+       rtm = nlmsg_data(nlh);
+       if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
+           (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
+           rtm->rtm_table || rtm->rtm_protocol ||
+           rtm->rtm_scope || rtm->rtm_type) {
+               NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request");
+               return -EINVAL;
+       }
+
+       if (rtm->rtm_flags & ~(RTM_F_NOTIFY |
+                              RTM_F_LOOKUP_TABLE |
+                              RTM_F_FIB_MATCH)) {
+               NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request");
+               return -EINVAL;
+       }
+
+       err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
+                                rtm_ipv4_policy, extack);
+       if (err)
+               return err;
+
+       if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
+           (tb[RTA_DST] && !rtm->rtm_dst_len)) {
+               NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
+               return -EINVAL;
+       }
+
+       for (i = 0; i <= RTA_MAX; i++) {
+               if (!tb[i])
+                       continue;
+
+               switch (i) {
+               case RTA_IIF:
+               case RTA_OIF:
+               case RTA_SRC:
+               case RTA_DST:
+               case RTA_IP_PROTO:
+               case RTA_SPORT:
+               case RTA_DPORT:
+               case RTA_MARK:
+               case RTA_UID:
+                       break;
+               default:
+                       NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request");
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
 static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
                             struct netlink_ext_ack *extack)
 {
@@ -2786,8 +2860,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
        int err;
        int mark;
 
-       err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy,
-                         extack);
+       err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
        if (err < 0)
                return err;