From 522456b9f3ab07a78de17bf693abead4a296b028 Mon Sep 17 00:00:00 2001 From: Hans Dedecker Date: Sun, 12 Aug 2018 22:08:22 +0200 Subject: 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 --- vlan.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'vlan.c') diff --git a/vlan.c b/vlan.c index c3ab2d2..8748b30 100644 --- a/vlan.c +++ b/vlan.c @@ -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) -- cgit v1.2.3