From 94c3869941b0b92b9fe8c30a45848a64f78e6409 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Tue, 12 Aug 2025 19:47:53 +0200 Subject: [PATCH] interface: avoid memleaks on invalid interfaces Free strings that were allocated with strdup in interface_alloc Signed-off-by: Felix Fietkau --- config.c | 2 +- interface.c | 19 ++++++++++++------- interface.h | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/config.c b/config.c index f752da0..56943a3 100644 --- a/config.c +++ b/config.c @@ -193,7 +193,7 @@ config_parse_interface(struct uci_section *s, bool alias) error_free_config: free(config); error: - free(iface); + interface_free(iface); } static void diff --git a/interface.c b/interface.c index 2ae94ae..3c9556a 100644 --- a/interface.c +++ b/interface.c @@ -702,14 +702,9 @@ interface_cleanup(struct interface *iface) interface_cleanup_state(iface); } -static void -interface_do_free(struct interface *iface) +void interface_free(struct interface *iface) { - interface_event(iface, IFEV_FREE); - interface_cleanup(iface); free(iface->config); - netifd_ubus_remove_interface(iface); - avl_delete(&interfaces.avl, &iface->node.avl); free(iface->zone); free(iface->jail); free(iface->jail_device); @@ -717,6 +712,16 @@ interface_do_free(struct interface *iface) free(iface); } +static void +interface_do_remove(struct interface *iface) +{ + interface_event(iface, IFEV_FREE); + interface_cleanup(iface); + netifd_ubus_remove_interface(iface); + avl_delete(&interfaces.avl, &iface->node.avl); + interface_free(iface); +} + static void interface_do_reload(struct interface *iface) { @@ -739,7 +744,7 @@ interface_handle_config_change(struct interface *iface) interface_do_reload(iface); break; case IFC_REMOVE: - interface_do_free(iface); + interface_do_remove(iface); return; } if (iface->autostart) diff --git a/interface.h b/interface.h index c12a266..eef620c 100644 --- a/interface.h +++ b/interface.h @@ -184,6 +184,7 @@ extern struct vlist_tree interfaces; extern const struct uci_blob_param_list interface_attr_list; struct interface *interface_alloc(const char *name, struct blob_attr *config, bool dynamic); +void interface_free(struct interface *iface); bool interface_add(struct interface *iface, struct blob_attr *config); bool interface_add_alias(struct interface *iface, struct blob_attr *config); -- 2.30.2