Validate required parameters in inet6_validate_link_af [Linux 5.2]

Validate required parameters in inet6_validate_link_af [Linux 5.2]

This Linux kernel change "Validate required parameters in inet6_validate_link_af" is included in the Linux 5.2 release. This change is authored by Maxim Mikityanskiy <maximmi [at] mellanox.com> on Tue May 21 06:40:04 2019 +0000. The commit for this change in Linux stable tree is 7dc2bcc (patch).

Validate required parameters in inet6_validate_link_af

inet6_set_link_af requires that at least one of IFLA_INET6_TOKEN or
IFLA_INET6_ADDR_GET_MODE is passed. If none of them is passed, it
returns -EINVAL, which may cause do_setlink() to fail in the middle of
processing other commands and give the following warning message:

  A link change request failed with some changes committed already.
  Interface eth0 may have been left with an inconsistent configuration,
  please check.

Check the presence of at least one of them in inet6_validate_link_af to
detect invalid parameters at an early stage, before do_setlink does
anything. Also validate the address generation mode at an early stage.

Signed-off-by: Maxim Mikityanskiy <[email protected]>
Signed-off-by: David S. Miller <[email protected]>

There are 57 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 net/ipv6/addrconf.c | 57 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 22 deletions(-)

diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index f96d1de..b51630d 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -5661,18 +5661,6 @@ static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
    [IFLA_INET6_TOKEN]      = { .len = sizeof(struct in6_addr) },
 };

-static int inet6_validate_link_af(const struct net_device *dev,
-                 const struct nlattr *nla)
-{
-   struct nlattr *tb[IFLA_INET6_MAX + 1];
-
-   if (dev && !__in6_dev_get(dev))
-       return -EAFNOSUPPORT;
-
-   return nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
-                      inet6_af_policy, NULL);
-}
-
 static int check_addr_gen_mode(int mode)
 {
    if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
@@ -5693,14 +5681,44 @@ static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
    return 1;
 }

+static int inet6_validate_link_af(const struct net_device *dev,
+                 const struct nlattr *nla)
+{
+   struct nlattr *tb[IFLA_INET6_MAX + 1];
+   struct inet6_dev *idev = NULL;
+   int err;
+
+   if (dev) {
+       idev = __in6_dev_get(dev);
+       if (!idev)
+           return -EAFNOSUPPORT;
+   }
+
+   err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
+                     inet6_af_policy, NULL);
+   if (err)
+       return err;
+
+   if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
+       return -EINVAL;
+
+   if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
+       u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
+
+       if (check_addr_gen_mode(mode) < 0)
+           return -EINVAL;
+       if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
+           return -EINVAL;
+   }
+
+   return 0;
+}
+
 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
 {
-   int err = -EINVAL;
    struct inet6_dev *idev = __in6_dev_get(dev);
    struct nlattr *tb[IFLA_INET6_MAX + 1];
-
-   if (!idev)
-       return -EAFNOSUPPORT;
+   int err;

    if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
        BUG();
@@ -5714,15 +5732,10 @@ static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
    if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
        u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);

-       if (check_addr_gen_mode(mode) < 0 ||
-           check_stable_privacy(idev, dev_net(dev), mode) < 0)
-           return -EINVAL;
-
        idev->cnf.addr_gen_mode = mode;
-       err = 0;
    }

-   return err;
+   return 0;
 }

 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,

Leave a Reply

Your email address will not be published. Required fields are marked *