945d3e
From 0b83636e914a894b324836e3fb2f20a2f7599fc4 Mon Sep 17 00:00:00 2001
945d3e
From: Jindrich Novy <jnovy@redhat.com>
945d3e
Date: Wed, 27 May 2020 11:01:02 +0200
945d3e
Subject: [PATCH] Fix possible infinite loops and use-after-free
945d3e
MIME-Version: 1.0
945d3e
Content-Type: text/plain; charset=UTF-8
945d3e
Content-Transfer-Encoding: 8bit
945d3e
945d3e
Error: USE_AFTER_FREE (CWE-416): [#def1]
945d3e
libslirp-4.3.0/src/ip_icmp.c:79: freed_arg: "icmp_detach" frees "slirp->icmp.so_next".
945d3e
libslirp-4.3.0/src/ip_icmp.c:79: deref_arg: Calling "icmp_detach" dereferences freed pointer "slirp->icmp.so_next".
945d3e
   77|   {
945d3e
   78|       while (slirp->icmp.so_next != &slirp->icmp) {
945d3e
   79|->         icmp_detach(slirp->icmp.so_next);
945d3e
   80|       }
945d3e
   81|   }
945d3e
945d3e
Error: USE_AFTER_FREE (CWE-416): [#def27]
945d3e
libslirp-4.3.0/src/udp.c:56: freed_arg: "udp_detach" frees "slirp->udb.so_next".
945d3e
libslirp-4.3.0/src/udp.c:56: deref_arg: Calling "udp_detach" dereferences freed pointer "slirp->udb.so_next".
945d3e
   54|   {
945d3e
   55|       while (slirp->udb.so_next != &slirp->udb) {
945d3e
   56|->         udp_detach(slirp->udb.so_next);
945d3e
   57|       }
945d3e
   58|   }
945d3e
945d3e
Signed-off-by: Jindrich Novy <jnovy@redhat.com>
945d3e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
945d3e
---
945d3e
 src/ip_icmp.c | 7 +++++--
945d3e
 src/udp.c     | 5 ++++-
945d3e
 2 files changed, 9 insertions(+), 3 deletions(-)
945d3e
945d3e
diff --git a/src/ip_icmp.c b/src/ip_icmp.c
945d3e
index fe0add4..7533595 100644
945d3e
--- libslirp-4.3.0/src/ip_icmp.c
945d3e
+++ libslirp-4.3.0/src/ip_icmp.c
945d3e
@@ -75,8 +75,11 @@ void icmp_init(Slirp *slirp)
945d3e
 
945d3e
 void icmp_cleanup(Slirp *slirp)
945d3e
 {
945d3e
-    while (slirp->icmp.so_next != &slirp->icmp) {
945d3e
-        icmp_detach(slirp->icmp.so_next);
945d3e
+    struct socket *so, *so_next;
945d3e
+
945d3e
+    for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so_next) {
945d3e
+        so_next = so->so_next;
945d3e
+        icmp_detach(so);
945d3e
     }
945d3e
 }
945d3e
 
945d3e
diff --git a/src/udp.c b/src/udp.c
945d3e
index 6bde20f..9ed1e74 100644
945d3e
--- libslirp-4.3.0/src/udp.c
945d3e
+++ libslirp-4.3.0/src/udp.c
945d3e
@@ -52,7 +52,10 @@ void udp_init(Slirp *slirp)
945d3e
 
945d3e
 void udp_cleanup(Slirp *slirp)
945d3e
 {
945d3e
-    while (slirp->udb.so_next != &slirp->udb) {
945d3e
+    struct socket *so, *so_next;
945d3e
+
945d3e
+    for (so = slirp->udb.so_next; so != &slirp->udb; so = so_next) {
945d3e
+        so_next = so->so_next;
945d3e
         udp_detach(slirp->udb.so_next);
945d3e
     }
945d3e
 }
945d3e
-- 
945d3e
2.26.2
945d3e
945d3e
From 2d79c0b7d78e55624790a102fbd924a4259eef16 Mon Sep 17 00:00:00 2001
945d3e
From: Jindrich Novy <jnovy@redhat.com>
945d3e
Date: Wed, 27 May 2020 11:07:19 +0200
945d3e
Subject: [PATCH] Use secure string copy to avoid overflow
945d3e
MIME-Version: 1.0
945d3e
Content-Type: text/plain; charset=UTF-8
945d3e
Content-Transfer-Encoding: 8bit
945d3e
945d3e
Error: STRING_OVERFLOW (CWE-120): [#def2]
945d3e
libslirp-4.3.0/src/ip_icmp.c:277: fixed_size_dest: You might overrun the 20-character fixed-size string "bufa" by copying the return value of "inet_ntoa" without checking the length.
945d3e
  275|       if (slirp_debug & DBG_MISC) {
945d3e
  276|           char bufa[20], bufb[20];
945d3e
  277|->         strcpy(bufa, inet_ntoa(ip->ip_src));
945d3e
  278|           strcpy(bufb, inet_ntoa(ip->ip_dst));
945d3e
  279|           DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
945d3e
945d3e
Error: STRING_OVERFLOW (CWE-120): [#def3]
945d3e
libslirp-4.3.0/src/ip_icmp.c:278: fixed_size_dest: You might overrun the 20-character fixed-size string "bufb" by copying the return value of "inet_ntoa" without checking the length.
945d3e
  276|           char bufa[20], bufb[20];
945d3e
  277|           strcpy(bufa, inet_ntoa(ip->ip_src));
945d3e
  278|->         strcpy(bufb, inet_ntoa(ip->ip_dst));
945d3e
  279|           DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
945d3e
  280|       }
945d3e
945d3e
Signed-off-by: Jindrich Novy <jnovy@redhat.com>
945d3e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
945d3e
---
945d3e
 src/ip_icmp.c | 4 ++--
945d3e
 1 file changed, 2 insertions(+), 2 deletions(-)
945d3e
945d3e
diff --git a/src/ip_icmp.c b/src/ip_icmp.c
945d3e
index 7533595..13a0e55 100644
945d3e
--- libslirp-4.3.0/src/ip_icmp.c
945d3e
+++ libslirp-4.3.0/src/ip_icmp.c
945d3e
@@ -277,8 +277,8 @@ void icmp_send_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,
945d3e
     ip = mtod(msrc, struct ip *);
945d3e
     if (slirp_debug & DBG_MISC) {
945d3e
         char bufa[20], bufb[20];
945d3e
-        strcpy(bufa, inet_ntoa(ip->ip_src));
945d3e
-        strcpy(bufb, inet_ntoa(ip->ip_dst));
945d3e
+        slirp_pstrcpy(bufa, sizeof(bufa), inet_ntoa(ip->ip_src));
945d3e
+        slirp_pstrcpy(bufb, sizeof(bufb), inet_ntoa(ip->ip_dst));
945d3e
         DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
945d3e
     }
945d3e
     if (ip->ip_off & IP_OFFMASK)
945d3e
-- 
945d3e
2.26.2
945d3e
945d3e
From 961a676e93fe7d599d3856e63bd132fe0d2decb2 Mon Sep 17 00:00:00 2001
945d3e
From: Jindrich Novy <jnovy@redhat.com>
945d3e
Date: Wed, 27 May 2020 11:16:57 +0200
945d3e
Subject: [PATCH] Check lseek() for failure
945d3e
MIME-Version: 1.0
945d3e
Content-Type: text/plain; charset=UTF-8
945d3e
Content-Transfer-Encoding: 8bit
945d3e
945d3e
Error: CHECKED_RETURN (CWE-252): [#def26]
945d3e
libslirp-4.3.0/src/tftp.c:121: check_return: Calling "lseek(spt->fd, block_nr * spt->block_size, 0)" without checking return value. This library function may fail and return an error code.
945d3e
  119|
945d3e
  120|       if (len) {
945d3e
  121|->         lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
945d3e
  122|
945d3e
  123|           bytes_read = read(spt->fd, buf, len);
945d3e
945d3e
Signed-off-by: Jindrich Novy <jnovy@redhat.com>
945d3e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
945d3e
---
945d3e
 src/tftp.c | 4 +++-
945d3e
 1 file changed, 3 insertions(+), 1 deletion(-)
945d3e
945d3e
diff --git a/src/tftp.c b/src/tftp.c
945d3e
index c209145..c6950ee 100644
945d3e
--- libslirp-4.3.0/src/tftp.c
945d3e
+++ libslirp-4.3.0/src/tftp.c
945d3e
@@ -118,7 +118,9 @@ static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
945d3e
     }
945d3e
 
945d3e
     if (len) {
945d3e
-        lseek(spt->fd, block_nr * spt->block_size, SEEK_SET);
945d3e
+        if (lseek(spt->fd, block_nr * spt->block_size, SEEK_SET) == (off_t)-1) {
945d3e
+            return -1;
945d3e
+        }
945d3e
 
945d3e
         bytes_read = read(spt->fd, buf, len);
945d3e
     }
945d3e
-- 
945d3e
2.26.2
945d3e
945d3e
From b0fc01a6b8cf6a50a1af69845cca692cc42dd970 Mon Sep 17 00:00:00 2001
945d3e
From: Jindrich Novy <jnovy@redhat.com>
945d3e
Date: Wed, 27 May 2020 11:18:36 +0200
945d3e
Subject: [PATCH] Be sure to initialize sockaddr structure
945d3e
MIME-Version: 1.0
945d3e
Content-Type: text/plain; charset=UTF-8
945d3e
Content-Transfer-Encoding: 8bit
945d3e
945d3e
Error: UNINIT (CWE-457): [#def30]
945d3e
libslirp-4.3.0/src/udp.c:325: var_decl: Declaring variable "addr" without initializer.
945d3e
libslirp-4.3.0/src/udp.c:342: uninit_use_in_call: Using uninitialized value "addr". Field "addr.sin_zero" is uninitialized when calling "bind".
945d3e
945d3e
Signed-off-by: Jindrich Novy <jnovy@redhat.com>
945d3e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
945d3e
---
945d3e
 src/udp.c | 1 +
945d3e
 1 file changed, 1 insertion(+)
945d3e
945d3e
diff --git a/src/udp.c b/src/udp.c
945d3e
index 9ed1e74..0ad44d7 100644
945d3e
--- libslirp-4.3.0/src/udp.c
945d3e
+++ libslirp-4.3.0/src/udp.c
945d3e
@@ -329,6 +329,7 @@ struct socket *udp_listen(Slirp *slirp, uint32_t haddr, unsigned hport,
945d3e
     struct socket *so;
945d3e
     socklen_t addrlen = sizeof(struct sockaddr_in);
945d3e
 
945d3e
+    memset(&addr, 0, sizeof(addr));
945d3e
     so = socreate(slirp);
945d3e
     so->s = slirp_socket(AF_INET, SOCK_DGRAM, 0);
945d3e
     if (so->s < 0) {
945d3e
-- 
945d3e
2.26.2
945d3e