diff options
author | Hans Dedecker <dedeckeh@gmail.com> | 2018-08-12 22:08:22 +0200 |
---|---|---|
committer | Hans Dedecker <dedeckeh@gmail.com> | 2018-08-14 17:06:38 +0200 |
commit | 522456b9f3ab07a78de17bf693abead4a296b028 (patch) | |
tree | 5e749e5e2aa3c4a3e77a0546d5148961398bccf6 | |
parent | 115a694ab9a44cd52a8ab18e07909c753de9fb94 (diff) |
device: gracefully handle device names exceeding IFNAMESIZ
Instead of truncating the device name when it exceeds IFNAMSIZ length;
let device_set_ifname return an error code and do not add the device
to the device list.
This avoids possible issues with device names becoming identical due the
truncation and as a result unexpected behavior.
Further let the different device types gracefully handle the error code
returned by device_init
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
-rw-r--r-- | alias.c | 6 | ||||
-rw-r--r-- | bridge.c | 8 | ||||
-rw-r--r-- | device.c | 30 | ||||
-rw-r--r-- | device.h | 2 | ||||
-rw-r--r-- | macvlan.c | 7 | ||||
-rw-r--r-- | tunnel.c | 8 | ||||
-rw-r--r-- | veth.c | 7 | ||||
-rw-r--r-- | vlan.c | 22 | ||||
-rw-r--r-- | vlandev.c | 8 |
9 files changed, 77 insertions, 21 deletions
@@ -124,7 +124,11 @@ alias_device_create(const char *name, struct device_type *devtype, strcpy(alias->name, name); alias->dev.set_state = alias_device_set_state; alias->dev.hidden = true; - device_init_virtual(&alias->dev, devtype, NULL); + if (device_init_virtual(&alias->dev, devtype, NULL) < 0) { + free(alias); + return NULL; + } + alias->avl.key = alias->name; avl_insert(&aliases, &alias->avl); alias->dep.alias = true; @@ -720,7 +720,13 @@ bridge_create(const char *name, struct device_type *devtype, return NULL; dev = &bst->dev; - device_init(dev, devtype, name); + + if (device_init(dev, devtype, name) < 0) { + device_cleanup(dev); + free(bst); + return NULL; + } + dev->config_pending = true; bst->retry.cb = bridge_retry_members; @@ -470,7 +470,7 @@ int device_check_state(struct device *dev) return dev->type->check_state(dev); } -void device_init_virtual(struct device *dev, struct device_type *type, const char *name) +int device_init_virtual(struct device *dev, struct device_type *type, const char *name) { assert(dev); assert(type); @@ -480,18 +480,27 @@ void device_init_virtual(struct device *dev, struct device_type *type, const cha INIT_SAFE_LIST(&dev->aliases); dev->type = type; - if (name) - device_set_ifname(dev, name); + if (name) { + int ret; + + ret = device_set_ifname(dev, name); + if (ret < 0) + return ret; + } if (!dev->set_state) dev->set_state = set_device_state; + + return 0; } int device_init(struct device *dev, struct device_type *type, const char *ifname) { int ret; - device_init_virtual(dev, type, ifname); + ret = device_init_virtual(dev, type, ifname); + if (ret < 0) + return ret; dev->avl.key = dev->ifname; @@ -520,7 +529,13 @@ device_create_default(const char *name, bool external) dev->external = external; dev->set_state = simple_device_set_state; - device_init(dev, &simple_device_type, name); + + if (device_init(dev, &simple_device_type, name) < 0) { + device_cleanup(dev); + free(dev); + return NULL; + } + dev->default_config = true; if (external) system_if_apply_settings(dev, &dev->settings, dev->settings.flags); @@ -648,10 +663,13 @@ int device_set_ifname(struct device *dev, const char *name) if (!strcmp(dev->ifname, name)) return 0; + if (strlen(name) > sizeof(dev->ifname) - 1) + return -1; + if (dev->avl.key) avl_delete(&devices, &dev->avl); - strncpy(dev->ifname, name, IFNAMSIZ); + strcpy(dev->ifname, name); if (dev->avl.key) ret = avl_insert(&devices, &dev->avl); @@ -250,7 +250,7 @@ device_apply_config(struct device *dev, struct device_type *type, void device_reset_config(void); void device_reset_old(void); -void device_init_virtual(struct device *dev, struct device_type *type, const char *name); +int device_init_virtual(struct device *dev, struct device_type *type, const char *name); int device_init(struct device *dev, struct device_type *type, const char *ifname); void device_cleanup(struct device *dev); struct device *device_find(const char *name); @@ -239,7 +239,12 @@ macvlan_create(const char *name, struct device_type *devtype, return NULL; dev = &mvdev->dev; - device_init(dev, devtype, name); + if (device_init(dev, devtype, name) < 0) { + device_cleanup(dev); + free(mvdev); + return NULL; + } + dev->config_pending = true; mvdev->set_state = dev->set_state; @@ -72,7 +72,13 @@ tunnel_create(const char *name, struct device_type *devtype, return NULL; dev = &tun->dev; - device_init(dev, devtype, name); + + if (device_init(dev, devtype, name) < 0) { + device_cleanup(dev); + free(tun); + return NULL; + } + tun->set_state = dev->set_state; dev->set_state = tunnel_set_state; device_apply_config(dev, devtype, attr); @@ -218,7 +218,12 @@ veth_create(const char *name, struct device_type *devtype, return NULL; dev = &veth->dev; - device_init(dev, devtype, name); + if (device_init(dev, devtype, name) < 0) { + device_cleanup(dev); + free(veth); + return NULL; + } + dev->config_pending = true; veth->set_state = dev->set_state; @@ -61,7 +61,7 @@ static int vlan_set_device_state(struct device *dev, bool up) return ret; } -static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) +static int vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) { char *name; @@ -69,7 +69,7 @@ static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev) vldev->dev.hidden = dev->hidden; sprintf(name, "%s.%d", dev->ifname, vldev->id); - device_set_ifname(&vldev->dev, name); + return device_set_ifname(&vldev->dev, name); } static void vlan_dev_cb(struct device_user *dep, enum device_event ev) @@ -85,7 +85,8 @@ static void vlan_dev_cb(struct device_user *dep, enum device_event ev) device_set_present(&vldev->dev, false); break; case DEV_EVENT_UPDATE_IFNAME: - vlan_dev_set_name(vldev, dep->dev); + if (vlan_dev_set_name(vldev, dep->dev) < 0) + free_vlan_if(&vldev->dev); break; case DEV_EVENT_TOPO_CHANGE: /* Propagate topo changes */ @@ -106,9 +107,6 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) struct vlan_device *vldev; struct device_user *dep; - if (strlen(dev->ifname) > (IFNAMSIZ - 6)) - return NULL; - /* look for an existing interface before creating a new one */ list_for_each_entry(dep, &dev->users.list, list.list) { if (dep->cb != vlan_dev_cb) @@ -132,9 +130,12 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) vldev->id = id; - device_init(&vldev->dev, &vlan_type, NULL); + if (device_init(&vldev->dev, &vlan_type, NULL) < 0) + goto error; + + if (vlan_dev_set_name(vldev, dev) < 0) + goto error; - vlan_dev_set_name(vldev, dev); vldev->dev.default_config = true; vldev->set_state = vldev->dev.set_state; @@ -144,6 +145,11 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create) device_add_user(&vldev->dep, dev); return &vldev->dev; + +error: + device_cleanup(&vldev->dev); + free(vldev); + return NULL; } static char *split_vlan(char *s) @@ -222,7 +222,13 @@ vlandev_create(const char *name, struct device_type *devtype, return NULL; dev = &mvdev->dev; - device_init(dev, devtype, name); + + if (device_init(dev, devtype, name) < 0) { + device_cleanup(dev); + free(mvdev); + return NULL; + } + dev->config_pending = true; mvdev->set_state = dev->set_state; |