]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
network: use static memory for net device names
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 31 Aug 2017 22:23:30 +0000 (00:23 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 4 Sep 2017 11:19:21 +0000 (13:19 +0200)
All network devices can only be of size < IFNAMSIZ. So let's spare the useless
heap allocations and use static memory.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/confile.c
src/lxc/confile_utils.c
src/lxc/confile_utils.h
src/lxc/criu.c
src/lxc/network.c
src/lxc/network.h
src/lxc/start.c

index 57d1d5607a805504fca5b57b3b0d65ff5bcf83b3..d61e7b8f2dec9d28efda0394a7391aee3d844abf 100644 (file)
@@ -764,11 +764,6 @@ static struct lxc_netdev *network_netdev(const char *key, const char *value,
        return netdev;
 }
 
-static int network_ifname(char **valuep, const char *value)
-{
-       return set_config_string_item_max(valuep, value, IFNAMSIZ);
-}
-
 #ifndef MACVLAN_MODE_PRIVATE
 #define MACVLAN_MODE_PRIVATE 1
 #endif
@@ -870,7 +865,7 @@ static int set_config_network_link(const char *key, const char *value,
        if (!netdev)
                return -1;
 
-       return network_ifname(&netdev->link, value);
+       return network_ifname(netdev->link, value);
 }
 
 static int set_config_network_name(const char *key, const char *value,
@@ -882,7 +877,7 @@ static int set_config_network_name(const char *key, const char *value,
        if (!netdev)
                return -1;
 
-       return network_ifname(&netdev->name, value);
+       return network_ifname(netdev->name, value);
 }
 
 static int set_config_network_veth_pair(const char *key, const char *value,
@@ -899,7 +894,7 @@ static int set_config_network_veth_pair(const char *key, const char *value,
                return -1;
        }
 
-       return network_ifname(&netdev->priv.veth_attr.pair, value);
+       return network_ifname(netdev->priv.veth_attr.pair, value);
 }
 
 static int set_config_network_macvlan_mode(const char *key, const char *value,
index 31e705f9df9f052bebe729af36b69091a301865a..8b7b3a5b471d924811eb580a5a092e8c0d0e38c9 100644 (file)
@@ -305,3 +305,14 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
                        TRACE("downscript: %s", netdev->downscript);
        }
 }
+
+int network_ifname(char *valuep, const char *value)
+{
+       if (strlen(value) >= IFNAMSIZ) {
+               ERROR("Network devie name \"%s\" is too long (>= %zu)", value,
+                     (size_t)IFNAMSIZ);
+       }
+
+       strcpy(valuep, value);
+       return 0;
+}
index 01cd0510b4bd666d2153d4bc0b20c4ceba97f7df..53665b914724c9420d7e2ff49da8ff478b028931 100644 (file)
@@ -33,5 +33,6 @@ extern struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf,
 extern struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
                                                unsigned int idx);
 extern void lxc_log_configured_netdevs(const struct lxc_conf *conf);
+extern int network_ifname(char *valuep, const char *value);
 
 #endif /* __LXC_CONFILE_UTILS_H */
index fc7d086c8198663d3932ce04f949bbba1f3b90ce..b7354a33b97afdc4f584c30e8ef87c01daf72078 100644 (file)
@@ -523,7 +523,7 @@ static void exec_criu(struct criu_opts *opts)
                        case LXC_NET_VETH:
                                veth = n->priv.veth_attr.pair;
 
-                               if (n->link) {
+                               if (n->link[0] != '\0') {
                                        if (external_not_veth)
                                                fmt = "veth[%s]:%s@%s";
                                        else
@@ -542,7 +542,7 @@ static void exec_criu(struct criu_opts *opts)
                                        goto err;
                                break;
                        case LXC_NET_MACVLAN:
-                               if (!n->link) {
+                               if (n->link[0] == '\0') {
                                        ERROR("no host interface for macvlan %s", n->name);
                                        goto err;
                                }
@@ -764,11 +764,17 @@ static bool restore_net_info(struct lxc_container *c)
 
                snprintf(template, sizeof(template), "vethXXXXXX");
 
-               if (!netdev->priv.veth_attr.pair)
-                       netdev->priv.veth_attr.pair = lxc_mkifname(template);
+               if (netdev->priv.veth_attr.pair[0] == '\0' &&
+                   netdev->priv.veth_attr.veth1[0] == '\0') {
+                       char *tmp;
 
-               if (!netdev->priv.veth_attr.pair)
-                       goto out_unlock;
+                       tmp = lxc_mkifname(template);
+                       if (!tmp)
+                               goto out_unlock;
+
+                       strcpy(netdev->priv.veth_attr.veth1, tmp);
+                       free(tmp);
+               }
        }
 
        has_error = false;
index 6a3c36dcc80285a2a8dfc06a11516fc687dcc403..a4476ccb2aac628a3c8c7bf2926f6c12389a69d2 100644 (file)
@@ -101,7 +101,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
        char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ];
        unsigned int mtu = 0;
 
-       if (netdev->priv.veth_attr.pair) {
+       if (netdev->priv.veth_attr.pair[0] != '\0') {
                veth1 = netdev->priv.veth_attr.pair;
                if (handler->conf->reboot)
                        lxc_netdev_delete_by_name(veth1);
@@ -163,7 +163,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
                        WARN("Failed to parse mtu");
                else
                        INFO("Retrieved mtu %d", mtu);
-       } else if (netdev->link) {
+       } else if (netdev->link[0] != '\0') {
                bridge_index = if_nametoindex(netdev->link);
                if (bridge_index) {
                        mtu = netdev_get_mtu(bridge_index);
@@ -186,7 +186,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
                }
        }
 
-       if (netdev->link) {
+       if (netdev->link[0] != '\0') {
                err = lxc_bridge_attach(netdev->link, veth1);
                if (err) {
                        ERROR("Failed to attach \"%s\" to bridge \"%s\": %s",
@@ -217,7 +217,7 @@ static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
 out_delete:
        if (netdev->ifindex != 0)
                lxc_netdev_delete_by_name(veth1);
-       if (!netdev->priv.veth_attr.pair)
+       if (netdev->priv.veth_attr.pair != veth1)
                free(veth1);
        free(veth2);
        return -1;
@@ -228,7 +228,7 @@ static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
        char peerbuf[IFNAMSIZ], *peer;
        int err;
 
-       if (!netdev->link) {
+       if (netdev->link[0] == '\0') {
                ERROR("No link for macvlan network device specified");
                return -1;
        }
@@ -279,7 +279,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
        static uint16_t vlan_cntr = 0;
        unsigned int mtu = 0;
 
-       if (!netdev->link) {
+       if (netdev->link[0] == '\0') {
                ERROR("No link for vlan network device specified");
                return -1;
        }
@@ -308,7 +308,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
                if (lxc_safe_uint(netdev->mtu, &mtu) < 0) {
                        ERROR("Failed to retrieve mtu from \"%d\"/\"%s\".",
                              netdev->ifindex,
-                             netdev->name ? netdev->name : "(null)");
+                             netdev->name[0] != '\0' ? netdev->name : "(null)");
                        return -1;
                }
                err = lxc_netdev_set_mtu(peer, mtu);
@@ -325,7 +325,7 @@ static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
 
 static int instantiate_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
 {
-       if (!netdev->link) {
+       if (netdev->link[0] == '\0') {
                ERROR("No link for physical interface specified");
                return -1;
        }
@@ -380,7 +380,7 @@ static int shutdown_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
        char *veth1;
        int err;
 
-       if (netdev->priv.veth_attr.pair)
+       if (netdev->priv.veth_attr.pair[0] != '\0')
                veth1 = netdev->priv.veth_attr.pair;
        else
                veth1 = netdev->priv.veth_attr.veth1;
@@ -1986,7 +1986,7 @@ int lxc_find_gateway_addresses(struct lxc_handler *handler)
                        return -1;
                }
 
-               if (!netdev->link) {
+               if (netdev->link[0] == '\0') {
                        ERROR("Automatic gateway detection needs a link interface");
                        return -1;
                }
@@ -2060,7 +2060,7 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
                        exit(EXIT_FAILURE);
                }
 
-               if (netdev->link)
+               if (netdev->link[0] != '\0')
                        strncpy(netdev_link, netdev->link, IFNAMSIZ);
                else
                        strncpy(netdev_link, "none", IFNAMSIZ);
@@ -2072,8 +2072,8 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
 
                INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath,
                     lxcname, pidstr, netdev_link,
-                    netdev->name ? netdev->name : "(null)");
-               if (netdev->name)
+                    netdev->name[0] != '\0' ? netdev->name : "(null)");
+               if (netdev->name[0] != '\0')
                        execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create",
                               lxcpath, lxcname, pidstr, "veth", netdev_link,
                               netdev->name, (char *)NULL);
@@ -2112,11 +2112,6 @@ static int lxc_create_network_unpriv_exec(const char *lxcpath, char *lxcname,
                return -1;
        }
 
-       netdev->name = malloc(IFNAMSIZ + 1);
-       if (!netdev->name) {
-               SYSERROR("Failed to allocate memory");
-               return -1;
-       }
        memset(netdev->name, 0, IFNAMSIZ + 1);
        strncpy(netdev->name, token, IFNAMSIZ);
 
@@ -2212,7 +2207,7 @@ static int lxc_delete_network_unpriv_exec(const char *lxcpath, char *lxcname,
                        exit(EXIT_FAILURE);
                }
 
-               if (!netdev->link) {
+               if (netdev->link[0] == '\0') {
                        SYSERROR("Network link for network device \"%s\" is "
                                 "missing", netdev->priv.veth_attr.veth1);
                        exit(EXIT_FAILURE);
@@ -2395,7 +2390,7 @@ int lxc_network_move_created_netdev_priv(const char *lxcpath, char *lxcname,
                }
 
                DEBUG("Moved network device \"%s\"/\"%s\" to network namespace "
-                     "of %d:", ifname, netdev->name ? netdev->name : "(null)",
+                     "of %d:", ifname, netdev->name[0] != '\0' ? netdev->name : "(null)",
                      pid);
        }
 
@@ -2482,18 +2477,18 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
                        INFO("Interface \"%s\" with index %d already "
                                        "deleted or existing in different network "
                                        "namespace",
-                                       netdev->name ? netdev->name : "(null)",
+                                       netdev->name[0] != '\0' ? netdev->name : "(null)",
                                        netdev->ifindex);
                } else if (ret < 0) {
                        deleted_all = false;
                        WARN("Failed to remove interface \"%s\" with "
                                        "index %d: %s",
-                                       netdev->name ? netdev->name : "(null)",
+                                       netdev->name[0] != '\0' ? netdev->name : "(null)",
                                        netdev->ifindex, strerror(-ret));
                        continue;
                }
                INFO("Removed interface \"%s\" with index %d",
-                               netdev->name ? netdev->name : "(null)",
+                               netdev->name[0] != '\0' ? netdev->name : "(null)",
                                netdev->ifindex);
 
                if (netdev->type != LXC_NET_VETH)
@@ -2502,11 +2497,11 @@ bool lxc_delete_network_priv(struct lxc_handler *handler)
                /* Explicitly delete host veth device to prevent lingering
                 * devices. We had issues in LXD around this.
                 */
-               if (netdev->priv.veth_attr.pair)
+               if (netdev->priv.veth_attr.pair[0] != '\0')
                        hostveth = netdev->priv.veth_attr.pair;
                else
                        hostveth = netdev->priv.veth_attr.veth1;
-               if (*hostveth == '\0')
+               if (hostveth[0] == '\0')
                        continue;
 
                ret = lxc_netdev_delete_by_name(hostveth);
@@ -2739,9 +2734,12 @@ static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev)
         * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
         * netlink will replace the format specifier with an appropriate index.
         */
-       if (!netdev->name)
-               netdev->name = netdev->type == LXC_NET_PHYS ?
-                       netdev->link : "eth%d";
+       if (netdev->name[0] == '\0') {
+               if (netdev->type == LXC_NET_PHYS)
+                       strcpy(netdev->name, netdev->link);
+               else
+                       strcpy(netdev->name, "eth%d");
+       }
 
        /* rename the interface name */
        if (strcmp(ifname, netdev->name) != 0) {
index 3ef016642cbe358c2d60d7efa87cf325a9049600..331d982080aab1acb74715cac754633ca5cc2645 100644 (file)
@@ -91,7 +91,7 @@ struct lxc_route6 {
  * @ifindex : Ifindex of the network device.
  */
 struct ifla_veth {
-       char *pair;
+       char pair[IFNAMSIZ];
        char veth1[IFNAMSIZ];
        int ifindex;
 };
@@ -151,8 +151,8 @@ struct lxc_netdev {
        int ifindex;
        int type;
        int flags;
-       char *link;
-       char *name;
+       char link[IFNAMSIZ];
+       char name[IFNAMSIZ];
        char *hwaddr;
        char *mtu;
        union netdev_p priv;
index 6232a78590848c998bd96744b829adcdd0fc0c83..327c7cb950168eead187337ebb40cb6e4f622335 100644 (file)
@@ -882,13 +882,6 @@ static int read_unpriv_netifindex(struct lxc_list *network)
                if (netdev->type != LXC_NET_VETH)
                        continue;
 
-               netdev->name = malloc(IFNAMSIZ);
-               if (!netdev->name) {
-                       ERROR("Out of memory.");
-                       close(netpipe);
-                       return -1;
-               }
-
                if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
                        close(netpipe);
                        return -1;