Blame SOURCES/0002-ell-avoid-using-inet_ntoa.patch

b94532
From e2fa5ae4d6eec61d7ca80e07269f86235cef8d60 Mon Sep 17 00:00:00 2001
b94532
Message-Id: <e2fa5ae4d6eec61d7ca80e07269f86235cef8d60.1624267112.git.davide.caratti@gmail.com>
b94532
In-Reply-To: <4097c1b862eb59f7110d097df7f719d00869191d.1624267112.git.davide.caratti@gmail.com>
b94532
References: <4097c1b862eb59f7110d097df7f719d00869191d.1624267112.git.davide.caratti@gmail.com>
b94532
From: Davide Caratti <davide.caratti@gmail.com>
b94532
Date: Wed, 16 Jun 2021 21:22:14 +0200
b94532
Subject: [PATCH 2/2] ell: avoid using inet_ntoa()
b94532
b94532
static checkers (like rpminspect) complain about use of inet_ntoa(), as
b94532
it relies on a static buffer. Fix this as follows:
b94532
b94532
 - use inet_ntop() with a buffer of size INET_ADDRSTRLEN allocated in
b94532
   the stack, similarly to what it is already done for IPv6 addresses
b94532
 - convert IP_STR() to use NIPQUAD() / NIPQUAD_FMT, similarly to what
b94532
   is done with MAC addresses
b94532
---
b94532
 ell/acd.c         | 28 +++++++++++++---------------
b94532
 ell/dhcp-lease.c  |  3 ++-
b94532
 ell/dhcp-server.c | 37 ++++++++++++++++++-------------------
b94532
 ell/dhcp.c        |  6 ++++--
b94532
 ell/rtnl.c        |  7 +++++--
b94532
 5 files changed, 42 insertions(+), 39 deletions(-)
b94532
b94532
diff --git a/ell/acd.c b/ell/acd.c
b94532
index 6989f82..4216898 100644
b94532
--- a/ell/acd.c
b94532
+++ b/ell/acd.c
b94532
@@ -55,14 +55,11 @@
b94532
 #define MAC "%02x:%02x:%02x:%02x:%02x:%02x"
b94532
 #define MAC_STR(a) a[0], a[1], a[2], a[3], a[4], a[5]
b94532
 
b94532
-#define IP_STR(uint_ip) \
b94532
-({ \
b94532
-	struct in_addr _in; \
b94532
-	char *_out; \
b94532
-	_in.s_addr = uint_ip; \
b94532
-	_out = inet_ntoa(_in); \
b94532
-	_out; \
b94532
-})
b94532
+#define NIPQUAD_FMT "%u.%u.%u.%u"
b94532
+#define NIPQUAD(u32_ip)	((unsigned char *) &u32_ip)[0], \
b94532
+			((unsigned char *) &u32_ip)[1], \
b94532
+			((unsigned char *) &u32_ip)[2], \
b94532
+			((unsigned char *) &u32_ip)[3]
b94532
 
b94532
 #define ACD_DEBUG(fmt, args...)					\
b94532
 	l_util_debug(acd->debug_handler, acd->debug_data,		\
b94532
@@ -146,7 +143,8 @@ static int acd_send_packet(struct l_acd *acd, uint32_t source_ip)
b94532
 	p.arp_pln = 4;
b94532
 	p.arp_op = htons(ARPOP_REQUEST);
b94532
 
b94532
-	ACD_DEBUG("sending packet with target IP %s", IP_STR(acd->ip));
b94532
+	ACD_DEBUG("sending packet with target IP "NIPQUAD_FMT,
b94532
+			NIPQUAD(acd->ip));
b94532
 
b94532
 	memcpy(&p.arp_sha, acd->mac, ETH_ALEN);
b94532
 	memcpy(&p.arp_spa, &source_ip, sizeof(p.arp_spa));
b94532
@@ -165,8 +163,8 @@ static void announce_wait_timeout(struct l_timeout *timeout, void *user_data)
b94532
 	struct l_acd *acd = user_data;
b94532
 
b94532
 	if (acd->state == ACD_STATE_PROBE) {
b94532
-		ACD_DEBUG("No conflicts found for %s, announcing address",
b94532
-				IP_STR(acd->ip));
b94532
+		ACD_DEBUG("No conflicts found for "NIPQUAD_FMT ", announcing address",
b94532
+				NIPQUAD(acd->ip));
b94532
 
b94532
 		acd->state = ACD_STATE_ANNOUNCED;
b94532
 
b94532
@@ -284,17 +282,17 @@ static bool acd_read_handler(struct l_io *io, void *user_data)
b94532
 		!memcmp(arp.arp_tpa, &acd->ip, sizeof(uint32_t));
b94532
 
b94532
 	if (!source_conflict && !target_conflict) {
b94532
-		ACD_DEBUG("No target or source conflict detected for %s",
b94532
-				IP_STR(acd->ip));
b94532
+		ACD_DEBUG("No target or source conflict detected for "NIPQUAD_FMT,
b94532
+				NIPQUAD(acd->ip));
b94532
 		return true;
b94532
 	}
b94532
 
b94532
 	switch (acd->state) {
b94532
 	case ACD_STATE_PROBE:
b94532
 		/* No reason to continue probing */
b94532
-		ACD_DEBUG("%s conflict detected for %s",
b94532
+		ACD_DEBUG("%s conflict detected for "NIPQUAD_FMT,
b94532
 				target_conflict ? "Target" : "Source",
b94532
-				IP_STR(acd->ip));
b94532
+				NIPQUAD(acd->ip));
b94532
 
b94532
 		if (acd->event_func)
b94532
 			acd->event_func(L_ACD_EVENT_CONFLICT, acd->user_data);
b94532
diff --git a/ell/dhcp-lease.c b/ell/dhcp-lease.c
b94532
index 44c815f..94b67b4 100644
b94532
--- a/ell/dhcp-lease.c
b94532
+++ b/ell/dhcp-lease.c
b94532
@@ -178,12 +178,13 @@ error:
b94532
 static inline char *get_ip(uint32_t ip)
b94532
 {
b94532
 	struct in_addr addr;
b94532
+	char buf[INET_ADDRSTRLEN];
b94532
 
b94532
 	if (ip == 0)
b94532
 		return NULL;
b94532
 
b94532
 	addr.s_addr = ip;
b94532
-	return l_strdup(inet_ntoa(addr));
b94532
+	return l_strdup(inet_ntop(AF_INET, &addr, buf, INET_ADDRSTRLEN));
b94532
 }
b94532
 
b94532
 LIB_EXPORT char *l_dhcp_lease_get_address(const struct l_dhcp_lease *lease)
b94532
diff --git a/ell/dhcp-server.c b/ell/dhcp-server.c
b94532
index 34512ae..4a6b3f6 100644
b94532
--- a/ell/dhcp-server.c
b94532
+++ b/ell/dhcp-server.c
b94532
@@ -92,14 +92,11 @@ struct l_dhcp_server {
b94532
 #define MAC "%02x:%02x:%02x:%02x:%02x:%02x"
b94532
 #define MAC_STR(a) a[0], a[1], a[2], a[3], a[4], a[5]
b94532
 
b94532
-#define IP_STR(uint_ip) \
b94532
-({ \
b94532
-	struct in_addr _in; \
b94532
-	char *_out; \
b94532
-	_in.s_addr = uint_ip; \
b94532
-	_out = inet_ntoa(_in); \
b94532
-	_out; \
b94532
-})
b94532
+#define NIPQUAD_FMT "%u.%u.%u.%u"
b94532
+#define NIPQUAD(u32_ip)	((unsigned char *) &u32_ip)[0], \
b94532
+			((unsigned char *) &u32_ip)[1], \
b94532
+			((unsigned char *) &u32_ip)[2], \
b94532
+			((unsigned char *) &u32_ip)[3]
b94532
 
b94532
 #define SERVER_DEBUG(fmt, args...)					\
b94532
 	l_util_debug(server->debug_handler, server->debug_data,		\
b94532
@@ -286,8 +283,8 @@ static struct l_dhcp_lease *add_lease(struct l_dhcp_server *server,
b94532
 		l_queue_push_head(server->lease_list, lease);
b94532
 	}
b94532
 
b94532
-	SERVER_DEBUG("added lease IP %s for "MAC " lifetime=%u",
b94532
-			IP_STR(yiaddr), MAC_STR(chaddr),
b94532
+	SERVER_DEBUG("added lease IP "NIPQUAD_FMT " for "MAC " lifetime=%u",
b94532
+			NIPQUAD(yiaddr), MAC_STR(chaddr),
b94532
 			server->lease_seconds);
b94532
 
b94532
 	return lease;
b94532
@@ -477,8 +474,8 @@ static void send_offer(struct l_dhcp_server *server,
b94532
 
b94532
 	_dhcp_message_builder_finalize(&builder, &len;;
b94532
 
b94532
-	SERVER_DEBUG("Sending OFFER of %s to "MAC, IP_STR(reply->yiaddr),
b94532
-			MAC_STR(reply->chaddr));
b94532
+	SERVER_DEBUG("Sending OFFER of "NIPQUAD_FMT " to "MAC,
b94532
+			NIPQUAD(reply->yiaddr),	MAC_STR(reply->chaddr));
b94532
 
b94532
 	if (server->transport->l2_send(server->transport, server->address,
b94532
 					DHCP_PORT_SERVER,
b94532
@@ -561,7 +558,7 @@ static void send_ack(struct l_dhcp_server *server,
b94532
 
b94532
 	_dhcp_message_builder_finalize(&builder, &len;;
b94532
 
b94532
-	SERVER_DEBUG("Sending ACK to %s", IP_STR(reply->yiaddr));
b94532
+	SERVER_DEBUG("Sending ACK to "NIPQUAD_FMT, NIPQUAD(reply->yiaddr));
b94532
 
b94532
 	if (server->transport->l2_send(server->transport, server->address,
b94532
 					DHCP_PORT_SERVER, reply->ciaddr,
b94532
@@ -628,15 +625,15 @@ static void listener_event(const void *data, size_t len, void *user_data)
b94532
 
b94532
 	switch (type) {
b94532
 	case DHCP_MESSAGE_TYPE_DISCOVER:
b94532
-		SERVER_DEBUG("Received DISCOVER, requested IP %s",
b94532
-					IP_STR(requested_ip_opt));
b94532
+		SERVER_DEBUG("Received DISCOVER, requested IP "NIPQUAD_FMT,
b94532
+					NIPQUAD(requested_ip_opt));
b94532
 
b94532
 		send_offer(server, message, lease, requested_ip_opt);
b94532
 
b94532
 		break;
b94532
 	case DHCP_MESSAGE_TYPE_REQUEST:
b94532
-		SERVER_DEBUG("Received REQUEST, requested IP %s",
b94532
-				IP_STR(requested_ip_opt));
b94532
+		SERVER_DEBUG("Received REQUEST, requested IP "NIPQUAD_FMT,
b94532
+				NIPQUAD(requested_ip_opt));
b94532
 
b94532
 		if (requested_ip_opt == 0) {
b94532
 			requested_ip_opt = message->ciaddr;
b94532
@@ -760,6 +757,7 @@ LIB_EXPORT void l_dhcp_server_destroy(struct l_dhcp_server *server)
b94532
 
b94532
 LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server)
b94532
 {
b94532
+	char buf[INET_ADDRSTRLEN];
b94532
 	struct in_addr ia;
b94532
 
b94532
 	if (unlikely(!server))
b94532
@@ -846,11 +844,12 @@ LIB_EXPORT bool l_dhcp_server_start(struct l_dhcp_server *server)
b94532
 	l_acd_set_defend_policy(server->acd, L_ACD_DEFEND_POLICY_INFINITE);
b94532
 
b94532
 	ia.s_addr = server->address;
b94532
+	inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
b94532
 
b94532
 	/* In case of unit testing we don't want this to be a fatal error */
b94532
-	if (!l_acd_start(server->acd, inet_ntoa(ia))) {
b94532
+	if (!l_acd_start(server->acd, buf)) {
b94532
 		SERVER_DEBUG("Failed to start ACD on %s, continuing without",
b94532
-				IP_STR(server->address));
b94532
+				buf);
b94532
 
b94532
 		l_acd_destroy(server->acd);
b94532
 		server->acd = NULL;
b94532
diff --git a/ell/dhcp.c b/ell/dhcp.c
b94532
index fff1645..bd346cc 100644
b94532
--- a/ell/dhcp.c
b94532
+++ b/ell/dhcp.c
b94532
@@ -778,6 +778,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata)
b94532
 	struct l_dhcp_client *client = userdata;
b94532
 	const struct dhcp_message *message = data;
b94532
 	struct dhcp_message_iter iter;
b94532
+	char buf[INET_ADDRSTRLEN];
b94532
 	uint8_t msg_type = 0;
b94532
 	uint8_t t, l;
b94532
 	const void *v;
b94532
@@ -911,11 +912,12 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata)
b94532
 		l_acd_set_skip_probes(client->acd, true);
b94532
 
b94532
 		ia.s_addr = client->lease->address;
b94532
+		inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
b94532
 
b94532
 		/* For unit testing we don't want this to be a fatal error */
b94532
-		if (!l_acd_start(client->acd, inet_ntoa(ia))) {
b94532
+		if (!l_acd_start(client->acd, buf)) {
b94532
 			CLIENT_DEBUG("Failed to start ACD on %s, continuing",
b94532
-						inet_ntoa(ia));
b94532
+						buf);
b94532
 			l_acd_destroy(client->acd);
b94532
 			client->acd = NULL;
b94532
 		}
b94532
diff --git a/ell/rtnl.c b/ell/rtnl.c
b94532
index 957e749..2983013 100644
b94532
--- a/ell/rtnl.c
b94532
+++ b/ell/rtnl.c
b94532
@@ -752,6 +752,7 @@ LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex,
b94532
 LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
b94532
 				char **label, char **ip, char **broadcast)
b94532
 {
b94532
+	char buf[INET_ADDRSTRLEN];
b94532
 	struct in_addr in_addr;
b94532
 	struct rtattr *attr;
b94532
 
b94532
@@ -763,7 +764,8 @@ LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
b94532
 				break;
b94532
 
b94532
 			in_addr = *((struct in_addr *) RTA_DATA(attr));
b94532
-			*ip = l_strdup(inet_ntoa(in_addr));
b94532
+			*ip = l_strdup(inet_ntop(AF_INET, &in_addr, buf,
b94532
+							INET_ADDRSTRLEN));
b94532
 
b94532
 			break;
b94532
 		case IFA_BROADCAST:
b94532
@@ -771,7 +773,8 @@ LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
b94532
 				break;
b94532
 
b94532
 			in_addr = *((struct in_addr *) RTA_DATA(attr));
b94532
-			*broadcast = l_strdup(inet_ntoa(in_addr));
b94532
+			*broadcast = l_strdup(inet_ntop(AF_INET, &in_addr, buf,
b94532
+							INET_ADDRSTRLEN));
b94532
 
b94532
 			break;
b94532
 		case IFA_LABEL:
b94532
-- 
b94532
2.31.1
b94532