git: 229863871f52 - stable/12 - Fix CVE-2020-10188

From: Cy Schubert <cy_at_FreeBSD.org>
Date: Fri, 30 Sep 2022 13:29:17 UTC
The branch stable/12 has been updated by cy:

URL: https://cgit.FreeBSD.org/src/commit/?id=229863871f52ee8f1a08f40c330eeb9e8bab9bb3

commit 229863871f52ee8f1a08f40c330eeb9e8bab9bb3
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2022-09-21 21:38:08 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2022-09-30 00:09:49 +0000

    Fix CVE-2020-10188
    
    Reviewed by:            emaste
    Obtained from:          NetBSD 6cc1539c8028b
    MFC after:              3 days
    Differential Revision:  https://reviews.freebsd.org/D36732
    
    (cherry picked from commit 5760cb266e0ab04c221c2acdb4b6c4c141130ecd)
---
 contrib/telnet/telnetd/utility.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/contrib/telnet/telnetd/utility.c b/contrib/telnet/telnetd/utility.c
index 1ad51c55b177..2e1f61fd5bd9 100644
--- a/contrib/telnet/telnetd/utility.c
+++ b/contrib/telnet/telnetd/utility.c
@@ -147,31 +147,38 @@ ptyflush(void)
  * character.
  */
 static char *
-nextitem(char *current)
+nextitem(char *current, const char *endp)
 {
+    if (current >= endp) {
+	return NULL;
+    }
     if ((*current&0xff) != IAC) {
 	return current+1;
     }
+    if (current+1 >= endp) {
+	return NULL;
+    }
     switch (*(current+1)&0xff) {
     case DO:
     case DONT:
     case WILL:
     case WONT:
-	return current+3;
+	return current+3 <= endp ? current+3 : NULL;
     case SB:		/* loop forever looking for the SE */
 	{
 	    char *look = current+2;
 
-	    for (;;) {
+	    while (look < endp) {
 		if ((*look++&0xff) == IAC) {
-		    if ((*look++&0xff) == SE) {
+		    if (look < endp && (*look++&0xff) == SE) {
 			return look;
 		    }
 		}
 	    }
+	    return NULL;
 	}
     default:
-	return current+2;
+	return current+2 <= endp ? current+2 : NULL;
     }
 }  /* end of nextitem */
 
@@ -197,7 +204,7 @@ netclear(void)
     char *thisitem, *next;
     char *good;
 #define	wewant(p)	((nfrontp > p) && ((*p&0xff) == IAC) && \
-				((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
+				(nfrontp > p+1) && ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
 
 #ifdef	ENCRYPTION
     thisitem = nclearto > netobuf ? nclearto : netobuf;
@@ -205,7 +212,7 @@ netclear(void)
     thisitem = netobuf;
 #endif	/* ENCRYPTION */
 
-    while ((next = nextitem(thisitem)) <= nbackp) {
+    while ((next = nextitem(thisitem, nbackp)) != NULL && (next <= nbackp)) {
 	thisitem = next;
     }
 
@@ -217,20 +224,23 @@ netclear(void)
     good = netobuf;	/* where the good bytes go */
 #endif	/* ENCRYPTION */
 
-    while (nfrontp > thisitem) {
+    while ((thisitem != NULL) && (nfrontp > thisitem)) {
 	if (wewant(thisitem)) {
 	    int length;
 
 	    next = thisitem;
 	    do {
-		next = nextitem(next);
-	    } while (wewant(next) && (nfrontp > next));
+		next = nextitem(next, nfrontp);
+	    } while ((next != NULL) && wewant(next) && (nfrontp > next));
+	    if (next == NULL) {
+		next = nfrontp;
+	    }
 	    length = next-thisitem;
 	    memmove(good, thisitem, length);
 	    good += length;
 	    thisitem = next;
 	} else {
-	    thisitem = nextitem(thisitem);
+	    thisitem = nextitem(thisitem, nfrontp);
 	}
     }