6e033a
From 849c972aa16a85c860f67d7e7f1fbe58e45187d2 Mon Sep 17 00:00:00 2001
6e033a
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
6e033a
Date: Wed, 9 Feb 2022 22:15:08 +0400
6e033a
Subject: [PATCH 2/2] Replace inet_ntoa() with safer inet_ntop()
6e033a
MIME-Version: 1.0
6e033a
Content-Type: text/plain; charset=UTF-8
6e033a
Content-Transfer-Encoding: 8bit
6e033a
6e033a
inet_ntoa() returns a static pointer which is subject to safety issues.
6e033a
Use the recommended alternative.
6e033a
6e033a
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e033a
---
6e033a
 src/arp_table.c |  8 ++++++--
6e033a
 src/ip_icmp.c   | 10 ++++++----
6e033a
 src/misc.c      | 22 +++++++++++++---------
6e033a
 src/socket.c    |  5 +++--
6e033a
 src/udp.c       |  5 +++--
6e033a
 5 files changed, 31 insertions(+), 19 deletions(-)
6e033a
6e033a
diff --git a/src/arp_table.c b/src/arp_table.c
6e033a
index ba8c8a4eee88..3cf2ecc238bc 100644
6e033a
--- a/src/arp_table.c
6e033a
+++ b/src/arp_table.c
6e033a
@@ -35,9 +35,11 @@ void arp_table_add(Slirp *slirp, uint32_t ip_addr,
6e033a
     ArpTable *arptbl = &slirp->arp_table;
6e033a
     int i;
6e033a
     char ethaddr_str[ETH_ADDRSTRLEN];
6e033a
+    char addr[INET_ADDRSTRLEN];
6e033a
 
6e033a
     DEBUG_CALL("arp_table_add");
6e033a
-    DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){ .s_addr = ip_addr }));
6e033a
+    DEBUG_ARG("ip = %s", inet_ntop(AF_INET, &(struct in_addr){ .s_addr = ip_addr },
6e033a
+                                   addr, sizeof(addr)));
6e033a
     DEBUG_ARG("hw addr = %s", slirp_ether_ntoa(ethaddr, ethaddr_str,
6e033a
                                                sizeof(ethaddr_str)));
6e033a
 
6e033a
@@ -69,9 +71,11 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
6e033a
     ArpTable *arptbl = &slirp->arp_table;
6e033a
     int i;
6e033a
     char ethaddr_str[ETH_ADDRSTRLEN];
6e033a
+    char addr[INET_ADDRSTRLEN];
6e033a
 
6e033a
     DEBUG_CALL("arp_table_search");
6e033a
-    DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){ .s_addr = ip_addr }));
6e033a
+    DEBUG_ARG("ip = %s", inet_ntop(AF_INET, &(struct in_addr){ .s_addr = ip_addr },
6e033a
+                                   addr, sizeof(addr)));
6e033a
 
6e033a
     /* If broadcast address */
6e033a
     if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
6e033a
diff --git a/src/ip_icmp.c b/src/ip_icmp.c
6e033a
index f4d686b0222d..26e44a3fd49c 100644
6e033a
--- a/src/ip_icmp.c
6e033a
+++ b/src/ip_icmp.c
6e033a
@@ -291,10 +291,12 @@ void icmp_forward_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsi
6e033a
         goto end_error;
6e033a
     ip = mtod(msrc, struct ip *);
6e033a
     if (slirp_debug & DBG_MISC) {
6e033a
-        char bufa[20], bufb[20];
6e033a
-        slirp_pstrcpy(bufa, sizeof(bufa), inet_ntoa(ip->ip_src));
6e033a
-        slirp_pstrcpy(bufb, sizeof(bufb), inet_ntoa(ip->ip_dst));
6e033a
-        DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
6e033a
+        char addr_src[INET_ADDRSTRLEN];
6e033a
+        char addr_dst[INET_ADDRSTRLEN];
6e033a
+
6e033a
+        inet_ntop(AF_INET, &ip->ip_src, addr_src, sizeof(addr_src));
6e033a
+        inet_ntop(AF_INET, &ip->ip_dst, addr_dst, sizeof(addr_dst));
6e033a
+        DEBUG_MISC(" %.16s to %.16s", addr_src, addr_dst);
6e033a
     }
6e033a
     if (ip->ip_off & IP_OFFMASK)
6e033a
         goto end_error; /* Only reply to fragment 0 */
6e033a
diff --git a/src/misc.c b/src/misc.c
6e033a
index e6bc0a207d0b..1306f68eb539 100644
6e033a
--- a/src/misc.c
6e033a
+++ b/src/misc.c
6e033a
@@ -293,6 +293,7 @@ char *slirp_connection_info(Slirp *slirp)
6e033a
     uint16_t dst_port;
6e033a
     struct socket *so;
6e033a
     const char *state;
6e033a
+    char addr[INET_ADDRSTRLEN];
6e033a
     char buf[20];
6e033a
 
6e033a
     g_string_append_printf(str,
6e033a
@@ -322,10 +323,11 @@ char *slirp_connection_info(Slirp *slirp)
6e033a
         }
6e033a
         slirp_fmt0(buf, sizeof(buf), "  TCP[%s]", state);
6e033a
         g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s,
6e033a
-                               src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) :
6e033a
-                                                     "*",
6e033a
+                               src.sin_addr.s_addr ?
6e033a
+                               inet_ntop(AF_INET, &src.sin_addr, addr, sizeof(addr)) : "*",
6e033a
                                ntohs(src.sin_port));
6e033a
-        g_string_append_printf(str, "%15s %5d %5d %5d\n", inet_ntoa(dst_addr),
6e033a
+        g_string_append_printf(str, "%15s %5d %5d %5d\n",
6e033a
+                               inet_ntop(AF_INET, &dst_addr, addr, sizeof(addr)),
6e033a
                                ntohs(dst_port), so->so_rcv.sb_cc,
6e033a
                                so->so_snd.sb_cc);
6e033a
     }
6e033a
@@ -346,10 +348,11 @@ char *slirp_connection_info(Slirp *slirp)
6e033a
             dst_port = so->so_fport;
6e033a
         }
6e033a
         g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s,
6e033a
-                               src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) :
6e033a
-                                                     "*",
6e033a
+                               src.sin_addr.s_addr ?
6e033a
+                               inet_ntop(AF_INET, &src.sin_addr, addr, sizeof(addr)) : "*",
6e033a
                                ntohs(src.sin_port));
6e033a
-        g_string_append_printf(str, "%15s %5d %5d %5d\n", inet_ntoa(dst_addr),
6e033a
+        g_string_append_printf(str, "%15s %5d %5d %5d\n",
6e033a
+                               inet_ntop(AF_INET, &dst_addr, addr, sizeof(addr)),
6e033a
                                ntohs(dst_port), so->so_rcv.sb_cc,
6e033a
                                so->so_snd.sb_cc);
6e033a
     }
6e033a
@@ -360,9 +363,10 @@ char *slirp_connection_info(Slirp *slirp)
6e033a
         src.sin_addr = so->so_laddr;
6e033a
         dst_addr = so->so_faddr;
6e033a
         g_string_append_printf(str, "%-19s %3d %15s  -    ", buf, so->s,
6e033a
-                               src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) :
6e033a
-                                                     "*");
6e033a
-        g_string_append_printf(str, "%15s  -    %5d %5d\n", inet_ntoa(dst_addr),
6e033a
+                               src.sin_addr.s_addr ?
6e033a
+                               inet_ntop(AF_INET, &src.sin_addr, addr, sizeof(addr)) : "*");
6e033a
+        g_string_append_printf(str, "%15s  -    %5d %5d\n",
6e033a
+                               inet_ntop(AF_INET, &dst_addr, addr, sizeof(addr)),
6e033a
                                so->so_rcv.sb_cc, so->so_snd.sb_cc);
6e033a
     }
6e033a
 
6e033a
diff --git a/src/socket.c b/src/socket.c
6e033a
index c0b02ad131f3..6607e319ad6c 100644
6e033a
--- a/src/socket.c
6e033a
+++ b/src/socket.c
6e033a
@@ -743,13 +743,14 @@ struct socket *tcp_listen(Slirp *slirp, uint32_t haddr, unsigned hport,
6e033a
     struct sockaddr_in addr;
6e033a
     struct socket *so;
6e033a
     int s, opt = 1;
6e033a
+    char inet_addr[INET_ADDRSTRLEN];
6e033a
     socklen_t addrlen = sizeof(addr);
6e033a
     memset(&addr, 0, addrlen);
6e033a
 
6e033a
     DEBUG_CALL("tcp_listen");
6e033a
-    DEBUG_ARG("haddr = %s", inet_ntoa((struct in_addr){ .s_addr = haddr }));
6e033a
+    DEBUG_ARG("haddr = %s", inet_ntop(AF_INET, &(struct in_addr){ .s_addr = haddr }, inet_addr, sizeof(inet_addr)));
6e033a
     DEBUG_ARG("hport = %d", ntohs(hport));
6e033a
-    DEBUG_ARG("laddr = %s", inet_ntoa((struct in_addr){ .s_addr = laddr }));
6e033a
+    DEBUG_ARG("laddr = %s", inet_ntop(AF_INET, &(struct in_addr){ .s_addr = laddr }, inet_addr, sizeof(inet_addr)));
6e033a
     DEBUG_ARG("lport = %d", ntohs(lport));
6e033a
     DEBUG_ARG("flags = %x", flags);
6e033a
 
6e033a
diff --git a/src/udp.c b/src/udp.c
6e033a
index e4578aa94ed5..0547cd6fc5c3 100644
6e033a
--- a/src/udp.c
6e033a
+++ b/src/udp.c
6e033a
@@ -248,14 +248,15 @@ bad:
6e033a
 int udp_output(struct socket *so, struct mbuf *m, struct sockaddr_in *saddr,
6e033a
                struct sockaddr_in *daddr, int iptos)
6e033a
 {
6e033a
+    char addr[INET_ADDRSTRLEN];
6e033a
     register struct udpiphdr *ui;
6e033a
     int error = 0;
6e033a
 
6e033a
     DEBUG_CALL("udp_output");
6e033a
     DEBUG_ARG("so = %p", so);
6e033a
     DEBUG_ARG("m = %p", m);
6e033a
-    DEBUG_ARG("saddr = %s", inet_ntoa(saddr->sin_addr));
6e033a
-    DEBUG_ARG("daddr = %s", inet_ntoa(daddr->sin_addr));
6e033a
+    DEBUG_ARG("saddr = %s", inet_ntop(AF_INET, &saddr->sin_addr, addr, sizeof(addr)));
6e033a
+    DEBUG_ARG("daddr = %s", inet_ntop(AF_INET, &daddr->sin_addr, addr, sizeof(addr)));
6e033a
 
6e033a
     /*
6e033a
      * Adjust for header
6e033a
-- 
6e033a
2.34.1.428.gdcc0cd074f0c
6e033a