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

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