]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
confile: improve network vetting
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 27 Mar 2021 10:37:26 +0000 (11:37 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 29 Mar 2021 15:02:17 +0000 (17:02 +0200)
Move all input sanity checks up and add two missing checks for the
correct network type when using veth-vlan and vlan network types.

Link: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=32513
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/confile.c

index fff58859fbf4aaf291b670882558895976547082..6aa0f444f3c34f4e023c5d69ffc48de2ea8c228d 100644 (file)
@@ -302,12 +302,12 @@ static int set_config_net_type(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_type(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_type(key, lxc_conf, data);
+
        if (strequal(value, "veth")) {
                netdev->type = LXC_NET_VETH;
                lxc_list_init(&netdev->priv.veth_attr.ipv4_routes);
@@ -345,12 +345,12 @@ static int set_config_net_flags(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_flags(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_flags(key, lxc_conf, data);
+
        netdev->flags |= IFF_UP;
 
        return 0;
@@ -403,12 +403,12 @@ static int set_config_net_link(const char *key, const char *value,
        struct lxc_netdev *netdev = data;
        int ret = 0;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_link(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_link(key, lxc_conf, data);
+
        if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
                ret = create_matched_ifnames(value, lxc_conf, netdev);
        else
@@ -424,12 +424,12 @@ static int set_config_net_l2proxy(const char *key, const char *value,
        unsigned int val = 0;
        int ret;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_l2proxy(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_l2proxy(key, lxc_conf, data);
+
        ret = lxc_safe_uint(value, &val);
        if (ret < 0)
                return ret_errno(ret);
@@ -451,12 +451,12 @@ static int set_config_net_name(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_name(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_name(key, lxc_conf, data);
+
        return network_ifname(netdev->name, value, sizeof(netdev->name));
 }
 
@@ -466,6 +466,12 @@ static int set_config_net_veth_mode(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
+       if (netdev->type != LXC_NET_VETH)
+               return ret_errno(EINVAL);
+
        if (lxc_config_value_empty(value))
                return clr_config_net_veth_mode(key, lxc_conf, data);
 
@@ -480,12 +486,15 @@ static int set_config_net_veth_pair(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_veth_pair(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_VETH)
+               return ret_errno(EINVAL);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_veth_pair(key, lxc_conf, data);
+
        return network_ifname(netdev->priv.veth_attr.pair, value,
                              sizeof(netdev->priv.veth_attr.pair));
 }
@@ -495,49 +504,48 @@ static int set_config_net_macvlan_mode(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_macvlan_mode(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_MACVLAN)
+               return ret_errno(EINVAL);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_macvlan_mode(key, lxc_conf, data);
+
        return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
 }
 
 static int set_config_net_ipvlan_mode(const char *key, const char *value,
-                                      struct lxc_conf *lxc_conf, void *data)
+                                     struct lxc_conf *lxc_conf, void *data)
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_ipvlan_mode(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_IPVLAN)
-               return log_error_errno(-EINVAL,
-                                      EINVAL, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network",
-                                      value);
+               return syserror_set(-EINVAL, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network", value);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_ipvlan_mode(key, lxc_conf, data);
 
        return lxc_ipvlan_mode_to_flag(&netdev->priv.ipvlan_attr.mode, value);
 }
 
 static int set_config_net_ipvlan_isolation(const char *key, const char *value,
-                                      struct lxc_conf *lxc_conf, void *data)
+                                          struct lxc_conf *lxc_conf, void *data)
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_ipvlan_isolation(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_IPVLAN)
-               return log_error_errno(-EINVAL,
-                                      EINVAL, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network",
-                                      value);
+               return syserror_set(-EINVAL, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network", value);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_ipvlan_isolation(key, lxc_conf, data);
 
        return lxc_ipvlan_isolation_to_flag(&netdev->priv.ipvlan_attr.isolation, value);
 }
@@ -548,12 +556,12 @@ static int set_config_net_hwaddr(const char *key, const char *value,
        __do_free char *new_value = NULL;
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_hwaddr(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_hwaddr(key, lxc_conf, data);
+
        new_value = strdup(value);
        if (!new_value)
                return ret_errno(ENOMEM);
@@ -574,12 +582,15 @@ static int set_config_net_vlan_id(const char *key, const char *value,
        int ret;
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_vlan_id(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_VLAN)
+               return ret_errno(EINVAL);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_vlan_id(key, lxc_conf, data);
+
        ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0);
        if (ret < 0)
                return ret;
@@ -592,12 +603,12 @@ static int set_config_net_mtu(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_mtu(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_mtu(key, lxc_conf, data);
+
        return set_config_string_item(&netdev->mtu, value);
 }
 
@@ -612,12 +623,12 @@ static int set_config_net_ipv4_address(const char *key, const char *value,
        char *cursor, *slash;
        char *bcast = NULL, *prefix = NULL;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_ipv4_address(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_ipv4_address(key, lxc_conf, data);
+
        inetdev = zalloc(sizeof(*inetdev));
        if (!inetdev)
                return ret_errno(ENOMEM);
@@ -683,12 +694,12 @@ static int set_config_net_ipv4_gateway(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (lxc_config_value_empty(value))
                return clr_config_net_ipv4_gateway(key, lxc_conf, data);
 
-       if (!netdev)
-               return -1;
-
        free(netdev->ipv4_gateway);
 
        if (strequal(value, "auto")) {
@@ -718,7 +729,7 @@ static int set_config_net_ipv4_gateway(const char *key, const char *value,
 }
 
 static int set_config_net_veth_ipv4_route(const char *key, const char *value,
-                                      struct lxc_conf *lxc_conf, void *data)
+                                         struct lxc_conf *lxc_conf, void *data)
 {
        __do_free char *valdup = NULL;
        __do_free struct lxc_inetdev *inetdev = NULL;
@@ -727,16 +738,14 @@ static int set_config_net_veth_ipv4_route(const char *key, const char *value,
        char *netmask, *slash;
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_veth_ipv4_route(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VETH)
-               return log_error_errno(-EINVAL,
-                                      EINVAL, "Invalid ipv4 route \"%s\", can only be used with veth network",
-                                      value);
+               return syserror_set(-EINVAL, "Invalid ipv4 route \"%s\", can only be used with veth network", value);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_veth_ipv4_route(key, lxc_conf, data);
 
        inetdev = zalloc(sizeof(*inetdev));
        if (!inetdev)
@@ -788,12 +797,12 @@ static int set_config_net_ipv6_address(const char *key, const char *value,
        struct lxc_netdev *netdev = data;
        char *slash, *netmask;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_ipv6_address(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_ipv6_address(key, lxc_conf, data);
+
        inet6dev = zalloc(sizeof(*inet6dev));
        if (!inet6dev)
                return ret_errno(ENOMEM);
@@ -834,12 +843,12 @@ static int set_config_net_ipv6_gateway(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_ipv6_gateway(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_ipv6_gateway(key, lxc_conf, data);
+
        free(netdev->ipv6_gateway);
 
        if (strequal(value, "auto")) {
@@ -870,7 +879,7 @@ static int set_config_net_ipv6_gateway(const char *key, const char *value,
 }
 
 static int set_config_net_veth_ipv6_route(const char *key, const char *value,
-                                      struct lxc_conf *lxc_conf, void *data)
+                                         struct lxc_conf *lxc_conf, void *data)
 {
        __do_free char *valdup = NULL;
        __do_free struct lxc_inet6dev *inet6dev = NULL;
@@ -879,16 +888,14 @@ static int set_config_net_veth_ipv6_route(const char *key, const char *value,
        char *netmask, *slash;
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_veth_ipv6_route(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VETH)
-               return log_error_errno(-EINVAL,
-                                      EINVAL, "Invalid ipv6 route \"%s\", can only be used with veth network",
-                                      value);
+               return syserror_set(-EINVAL, "Invalid ipv6 route \"%s\", can only be used with veth network", value);
+
+       if (lxc_config_value_empty(value))
+               return clr_config_net_veth_ipv6_route(key, lxc_conf, data);
 
        inet6dev = zalloc(sizeof(*inet6dev));
        if (!inet6dev)
@@ -934,12 +941,12 @@ static int set_config_net_script_up(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_script_up(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_script_up(key, lxc_conf, data);
+
        return set_config_string_item(&netdev->upscript, value);
 }
 
@@ -948,12 +955,12 @@ static int set_config_net_script_down(const char *key, const char *value,
 {
        struct lxc_netdev *netdev = data;
 
-       if (lxc_config_value_empty(value))
-               return clr_config_net_script_down(key, lxc_conf, data);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (lxc_config_value_empty(value))
+               return clr_config_net_script_down(key, lxc_conf, data);
+
        return set_config_string_item(&netdev->downscript, value);
 }
 
@@ -4939,7 +4946,7 @@ static int clr_config_net_ipvlan_mode(const char *key,
 }
 
 static int clr_config_net_ipvlan_isolation(const char *key,
-                                      struct lxc_conf *lxc_conf, void *data)
+                                          struct lxc_conf *lxc_conf, void *data)
 {
        struct lxc_netdev *netdev = data;
 
@@ -4978,6 +4985,9 @@ static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_VETH)
+               return 0;
+
        netdev->priv.veth_attr.pair[0] = '\0';
 
        return 0;
@@ -5043,6 +5053,9 @@ static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_VLAN)
+               return 0;
+
        netdev->priv.vlan_attr.vid = 0;
 
        return 0;
@@ -5088,6 +5101,9 @@ static int clr_config_net_veth_ipv4_route(const char *key,
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_VETH)
+               return 0;
+
        lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
                lxc_list_del(cur);
                free(cur->elem);
@@ -5137,6 +5153,9 @@ static int clr_config_net_veth_ipv6_route(const char *key,
        if (!netdev)
                return ret_errno(EINVAL);
 
+       if (netdev->type != LXC_NET_VETH)
+               return 0;
+
        lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
                lxc_list_del(cur);
                free(cur->elem);
@@ -5177,14 +5196,14 @@ static int get_config_net_type(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
 
        return fulllen;
@@ -5197,14 +5216,14 @@ static int get_config_net_flags(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->flags & IFF_UP)
                strprint(retv, inlen, "up");
 
@@ -5218,14 +5237,14 @@ static int get_config_net_link(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->link[0] != '\0')
                strprint(retv, inlen, "%s", netdev->link);
 
@@ -5236,6 +5255,10 @@ static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
                                  struct lxc_conf *c, void *data)
 {
        struct lxc_netdev *netdev = data;
+
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
 }
 
@@ -5246,14 +5269,14 @@ static int get_config_net_name(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->name[0] != '\0')
                strprint(retv, inlen, "%s", netdev->name);
 
@@ -5268,16 +5291,16 @@ static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
        const char *mode;
        struct lxc_netdev *netdev = data;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_MACVLAN)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        switch (netdev->priv.macvlan_attr.mode) {
        case MACVLAN_MODE_PRIVATE:
@@ -5310,16 +5333,16 @@ static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
        int len;
        const char *mode;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_IPVLAN)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        switch (netdev->priv.ipvlan_attr.mode) {
        case IPVLAN_MODE_L3:
@@ -5349,16 +5372,16 @@ static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inle
        int len;
        const char *mode;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_IPVLAN)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        switch (netdev->priv.ipvlan_attr.isolation) {
        case IPVLAN_ISOLATION_BRIDGE:
@@ -5381,23 +5404,23 @@ static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inle
 }
 
 static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
-                                      struct lxc_conf *c, void *data)
+                                   struct lxc_conf *c, void *data)
 {
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
        int len;
        const char *mode;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VETH)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        switch (netdev->priv.veth_attr.mode) {
        case VETH_MODE_BRIDGE:
@@ -5423,16 +5446,16 @@ static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VETH)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        strprint(retv, inlen, "%s",
                 netdev->priv.veth_attr.pair[0] != '\0'
@@ -5449,14 +5472,14 @@ static int get_config_net_script_up(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->upscript)
                strprint(retv, inlen, "%s", netdev->upscript);
 
@@ -5470,14 +5493,14 @@ static int get_config_net_script_down(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->downscript)
                strprint(retv, inlen, "%s", netdev->downscript);
 
@@ -5491,14 +5514,14 @@ static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->hwaddr)
                strprint(retv, inlen, "%s", netdev->hwaddr);
 
@@ -5512,14 +5535,14 @@ static int get_config_net_mtu(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->mtu)
                strprint(retv, inlen, "%s", netdev->mtu);
 
@@ -5533,16 +5556,16 @@ static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VLAN)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
 
@@ -5557,14 +5580,14 @@ static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->ipv4_gateway_auto) {
                strprint(retv, inlen, "auto");
        } else if (netdev->ipv4_gateway_dev) {
@@ -5588,14 +5611,14 @@ static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        listlen = lxc_list_len(&netdev->ipv4);
 
        lxc_list_for_each(it, &netdev->ipv4) {
@@ -5610,7 +5633,7 @@ static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
 }
 
 static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
-                                      struct lxc_conf *c, void *data)
+                                         struct lxc_conf *c, void *data)
 {
        int len;
        size_t listlen;
@@ -5619,16 +5642,16 @@ static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VETH)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
 
@@ -5651,14 +5674,14 @@ static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        if (netdev->ipv6_gateway_auto) {
                strprint(retv, inlen, "auto");
        } else if (netdev->ipv6_gateway_dev) {
@@ -5682,14 +5705,14 @@ static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
+       if (!netdev)
+               return ret_errno(EINVAL);
+
        if (!retv)
                inlen = 0;
        else
                memset(retv, 0, inlen);
 
-       if (!netdev)
-               return ret_errno(EINVAL);
-
        listlen = lxc_list_len(&netdev->ipv6);
 
        lxc_list_for_each(it, &netdev->ipv6) {
@@ -5713,16 +5736,16 @@ static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen
        int fulllen = 0;
        struct lxc_netdev *netdev = data;
 
-       if (!retv)
-               inlen = 0;
-       else
-               memset(retv, 0, inlen);
-
        if (!netdev)
                return ret_errno(EINVAL);
 
        if (netdev->type != LXC_NET_VETH)
-               return 0;
+               return ret_errno(EINVAL);
+
+       if (!retv)
+               inlen = 0;
+       else
+               memset(retv, 0, inlen);
 
        listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);