From: Samuel Mendoza-Jonas Date: Mon, 5 Sep 2016 04:21:23 +0000 (+1000) Subject: discover: Pass UUID to discover_device_create() X-Git-Tag: v1.2.5~1 X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=3037e6c59656daf510e9ced820d149d74f8a499c discover: Pass UUID to discover_device_create() Currently discover_device_create() will search for existing discover devices by id to determine if a new device is required. However it is possible under some circumstances for distinct devices to have the same name. This is especially troublesome if the following network events are seen in network_handle_nlmsg(): - New interface, 'foo' with uuid x:x:x:x:x:x -> new discover device created with dev->device->id = 'foo' dev->uuid = x:x:x:x:x:x - New interface, 'foo' with uuid y:y:y:y:y:y -> existing device 'foo' found dev->uuid = y:y:y:y:y:y This can occur if an interface rename event arrives *after* an old name is reused, where temporarily Petitboot will see two distinct network interfaces with the same name. Now the two interfaces point to the same discover device, which can quickly result in a segfault if a 'remove' event occurs for one of the interfaces and the discover device is freed. To generally avoid this a 'uuid' parameter is added to discover_device_create(), which if present allows existing devices to be looked up by UUID rather than just their name. Signed-off-by: Samuel Mendoza-Jonas (cherry picked from commit 1def8f21aecc41ac22652e7b8bd1f5bf7a4dae98) --- diff --git a/discover/device-handler.c b/discover/device-handler.c index 8063ad4..e503b58 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -221,17 +221,22 @@ static int destroy_device(void *arg) } struct discover_device *discover_device_create(struct device_handler *handler, - const char *id) + const char *uuid, const char *id) { struct discover_device *dev; - dev = device_lookup_by_id(handler, id); + if (uuid) + dev = device_lookup_by_uuid(handler, uuid); + else + dev = device_lookup_by_id(handler, id); + if (dev) return dev; dev = talloc_zero(handler, struct discover_device); dev->device = talloc_zero(dev, struct device); dev->device->id = talloc_strdup(dev->device, id); + dev->uuid = talloc_strdup(dev, uuid); list_init(&dev->params); list_init(&dev->boot_options); @@ -1132,7 +1137,7 @@ void device_handler_process_url(struct device_handler *handler, goto msg; } - dev = discover_device_create(handler, event->device); + dev = discover_device_create(handler, mac, event->device); if (pb_url->scheme == pb_url_file) dev->device->type = DEVICE_TYPE_ANY; ctx = device_handler_discover_context_create(handler, dev); @@ -1165,7 +1170,7 @@ void device_handler_discover_context_commit(struct device_handler *handler, struct discover_device *dev = ctx->device; struct discover_boot_option *opt, *tmp; - if (!device_lookup_by_id(handler, dev->device->id)) + if (!device_lookup_by_uuid(handler, dev->uuid)) device_handler_add_device(handler, dev); /* move boot options from the context to the device */ diff --git a/discover/device-handler.h b/discover/device-handler.h index a456181..79b34d6 100644 --- a/discover/device-handler.h +++ b/discover/device-handler.h @@ -80,7 +80,7 @@ const struct discover_device *device_handler_get_device( const struct device_handler *handler, unsigned int index); struct discover_device *discover_device_create(struct device_handler *handler, - const char *id); + const char *uuid, const char *id); void device_handler_add_device(struct device_handler *handler, struct discover_device *device); void device_handler_add_ramdisk(struct device_handler *handler, diff --git a/discover/network.c b/discover/network.c index 2de96ec..0161c69 100644 --- a/discover/network.c +++ b/discover/network.c @@ -197,13 +197,15 @@ static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len) static void add_interface(struct network *network, struct interface *interface) { + char *uuid = mac_bytes_to_string(interface, interface->hwaddr, + sizeof(interface->hwaddr)); + list_add(&network->interfaces, &interface->list); - interface->dev = discover_device_create(network->handler, - interface->name); + interface->dev = discover_device_create(network->handler, uuid, + interface->name); interface->dev->device->type = DEVICE_TYPE_NETWORK; - interface->dev->uuid = mac_bytes_to_string(interface->dev, - interface->hwaddr, sizeof(interface->hwaddr)); device_handler_add_device(network->handler, interface->dev); + talloc_free(uuid); } static void remove_interface(struct network *network, diff --git a/discover/udev.c b/discover/udev.c index f4cefab..1e04313 100644 --- a/discover/udev.c +++ b/discover/udev.c @@ -158,7 +158,7 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev, } } - ddev = discover_device_create(udev->handler, name); + ddev = discover_device_create(udev->handler, uuid, name); ddev->device_path = talloc_strdup(ddev, node); diff --git a/discover/user-event.c b/discover/user-event.c index b201de8..b341005 100644 --- a/discover/user-event.c +++ b/discover/user-event.c @@ -375,7 +375,8 @@ static int user_event_dhcp(struct user_event *uev, struct event *event) struct device_handler *handler = uev->handler; struct discover_device *dev; - dev = discover_device_create(handler, event->device); + dev = discover_device_create(handler, event_get_param(event, "mac"), + event->device); device_handler_dhcp(handler, dev, event); @@ -388,7 +389,10 @@ static int user_event_add(struct user_event *uev, struct event *event) struct discover_context *ctx; struct discover_device *dev; - dev = discover_device_create(handler, event->device); + /* In case this is a network interface, try to refer to it by UUID */ + dev = discover_device_create(handler, event_get_param(event, "mac"), + event->device); + dev->device->id = talloc_strdup(dev, event->device); ctx = device_handler_discover_context_create(handler, dev); parse_user_event(ctx, event); @@ -404,8 +408,13 @@ static int user_event_remove(struct user_event *uev, struct event *event) { struct device_handler *handler = uev->handler; struct discover_device *dev; + const char *mac = event_get_param(event, "mac"); + + if (mac) + dev = device_lookup_by_uuid(handler, event_get_param(event, "mac")); + else + dev = device_lookup_by_id(handler, event->device); - dev = device_lookup_by_id(handler, event->device); if (!dev) return 0; diff --git a/test/parser/utils.c b/test/parser/utils.c index 5cebc99..f0796fd 100644 --- a/test/parser/utils.c +++ b/test/parser/utils.c @@ -73,7 +73,7 @@ struct discover_device *test_create_device(struct parser_test *test, { struct discover_device *dev; - dev = discover_device_create(test->handler, name); + dev = discover_device_create(test->handler, NULL, name); dev->device->id = talloc_strdup(dev, name); dev->device_path = talloc_asprintf(dev, "/dev/%s", name);