[PATCH 09/11] Use hwaddr_len over sizeof hwaddr

Daniel M. Weeks weeksd2 at rpi.edu
Fri Apr 17 03:56:00 AEST 2020


The length of the hardware address stored in hwaddr may be shorter than
the size of the field. Only the actual length should be used in
comparisons and transformations of hwaddr.

Signed-off-by: Daniel M. Weeks <weeksd2 at rpi.edu>
---
 discover/network.c          | 25 +++++++++++++------------
 discover/platform-powerpc.c |  4 ++--
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/discover/network.c b/discover/network.c
index 0433c70..8637fc8 100644
--- a/discover/network.c
+++ b/discover/network.c
@@ -89,7 +89,7 @@ static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
 }
 
 static const struct interface_config *find_config_by_hwaddr(
-		uint8_t *hwaddr)
+		uint8_t *hwaddr, int hwaddr_len)
 {
 	const struct config *config;
 	unsigned int i;
@@ -140,7 +140,7 @@ static struct interface *find_interface_by_uuid(struct network *network,
 
 	list_for_each_entry(&network->interfaces, interface, list) {
 		mac = mac_bytes_to_string(interface, interface->hwaddr,
-					sizeof(interface->hwaddr));
+					interface->hwaddr_len);
 		if (!strcmp(mac, uuid)) {
 			talloc_free(mac);
 			return interface;
@@ -161,7 +161,7 @@ uint8_t *find_mac_by_name(void *ctx, struct network *network,
 		return NULL;
 
 	return talloc_memdup(ctx, &interface->hwaddr,
-			     sizeof(uint8_t) * HWADDR_SIZE);
+			     interface->hwaddr_len);
 }
 
 static int network_init_netlink(struct network *network)
@@ -221,7 +221,7 @@ static void create_interface_dev(struct network *network,
 		struct interface *interface)
 {
 	char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
-						sizeof(interface->hwaddr));
+						interface->hwaddr_len);
 
 	interface->dev = discover_device_create(network->handler, uuid,
 						interface->name);
@@ -253,7 +253,7 @@ void network_register_device(struct network *network,
 
 	iface->dev = dev;
 	dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
-			sizeof(iface->hwaddr));
+			iface->hwaddr_len);
 }
 
 void network_unregister_device(struct network *network,
@@ -390,7 +390,7 @@ static void configure_interface_static(struct network *network,
 		return;
 	}
 
-	system_info_set_interface_address(sizeof(interface->hwaddr),
+	system_info_set_interface_address(interface->hwaddr_len,
 				interface->hwaddr,
 				config->static_config.address);
 
@@ -417,7 +417,7 @@ static void configure_interface_static(struct network *network,
 				config->static_config.url,
 				mac_bytes_to_string(interface->dev,
 						interface->hwaddr,
-						sizeof(interface->hwaddr)),
+						interface->hwaddr_len),
 				config->static_config.address);
 		device_handler_start_requery_timeout(network->handler,
 				interface->dev, -1);
@@ -455,7 +455,8 @@ static void configure_interface(struct network *network,
 		return;
 	}
 
-	config = find_config_by_hwaddr(interface->hwaddr);
+	config = find_config_by_hwaddr(interface->hwaddr, interface->hwaddr_len);
+
 	if (config && config->ignore) {
 		pb_log("network: ignoring interface %s\n", interface->name);
 		interface->state = IFSTATE_IGNORED;
@@ -521,7 +522,7 @@ void network_requery_device(struct network *network,
 		process_release(interface->udhcpc_process);
 	}
 
-	config = find_config_by_hwaddr(interface->hwaddr);
+	config = find_config_by_hwaddr(interface->hwaddr, interface->hwaddr_len);
 
 	if (config && config->ignore)
 		return;
@@ -540,7 +541,7 @@ void network_requery_device(struct network *network,
 				config->static_config.url,
 				mac_bytes_to_string(interface->dev,
 						interface->hwaddr,
-						sizeof(interface->hwaddr)),
+						interface->hwaddr_len),
 				config->static_config.address);
 		device_handler_start_requery_timeout(network->handler,
 				dev, -1);
@@ -613,7 +614,7 @@ static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
 		interface = talloc_zero(network, struct interface);
 		interface->ifindex = info->ifi_index;
 		interface->state = IFSTATE_NEW;
-		memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
+		memcpy(interface->hwaddr, ifaddr, interface->hwaddr_len);
 		strncpy(interface->name, ifname, sizeof(interface->name) - 1);
 
 		list_for_each_entry(&network->interfaces, tmp, list)
@@ -641,7 +642,7 @@ static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
 	/* notify the sysinfo code about changes to this interface */
 	if (strcmp(interface->name, "lo"))
 		system_info_register_interface(
-				sizeof(interface->hwaddr),
+				interface->hwaddr_len,
 				interface->hwaddr, interface->name,
 				info->ifi_flags & IFF_LOWER_UP);
 
diff --git a/discover/platform-powerpc.c b/discover/platform-powerpc.c
index be2fbab..9bdba30 100644
--- a/discover/platform-powerpc.c
+++ b/discover/platform-powerpc.c
@@ -261,14 +261,14 @@ static void set_param(struct platform_powerpc *platform, const char *name,
 
 static int parse_hwaddr(struct interface_config *ifconf, char *str)
 {
-	int i;
+	unsigned int i;
 
 	if (strlen(str) != strlen("00:00:00:00:00:00"))
 		return -1;
 
 	ifconf->hwaddr_len = strlen(str) / 3 + 1;
 
-	for (i = 0; i < HWADDR_SIZE; i++) {
+	for (i = 0; i < ifconf->hwaddr_len; i++) {
 		char byte[3], *endp;
 		unsigned long x;
 
-- 
Daniel M. Weeks


-- 
Daniel M. Weeks
Lead HPC Developer
Center for Computational Innovations
Rensselaer Polytechnic Institute
Troy, NY 12180
518-276-4458


More information about the Petitboot mailing list