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