Blame SOURCES/telnet-0.17-overflow-exploit.patch

776859
diff -up netkit-telnet-0.17/telnetd/utility.c.orig netkit-telnet-0.17/telnetd/utility.c
776859
--- netkit-telnet-0.17/telnetd/utility.c.orig	2020-03-25 11:53:56.772624325 +0100
776859
+++ netkit-telnet-0.17/telnetd/utility.c	2020-03-25 11:54:01.966601415 +0100
776859
@@ -221,31 +221,38 @@ void 	ptyflush(void)
776859
  */
776859
 static
776859
 char *
776859
-nextitem(char *current)
776859
+nextitem(char *current, const char *endp)
776859
 {
776859
+    if (current >= endp) {
776859
+        return NULL;
776859
+    }
776859
     if ((*current&0xff) != IAC) {
776859
 	return current+1;
776859
     }
776859
+    if (current+1 >= endp) {
776859
+        return NULL;
776859
+    }
776859
     switch (*(current+1)&0xff) {
776859
     case DO:
776859
     case DONT:
776859
     case WILL:
776859
     case WONT:
776859
-	return current+3;
776859
+	return current+3 <= endp ? current+3 : NULL;
776859
     case SB:		/* loop forever looking for the SE */
776859
 	{
776859
 	    register char *look = current+2;
776859
 
776859
-	    for (;;) {
776859
+	    while (look < endp) {
776859
 		if ((*look++&0xff) == IAC) {
776859
-		    if ((*look++&0xff) == SE) {
776859
+		    if (look < endp && (*look++&0xff) == SE) {
776859
 			return look;
776859
 		    }
776859
 		}
776859
 	    }
776859
+	    return NULL;
776859
 	}
776859
     default:
776859
-	return current+2;
776859
+	return current+2 <= endp ? current+2 : NULL;
776859
     }
776859
 }  /* end of nextitem */
776859
 
776859
@@ -271,7 +278,7 @@ void netclear(void)
776859
     register char *thisitem, *next;
776859
     char *good;
776859
 #define	wewant(p)	((nfrontp > p) && ((*p&0xff) == IAC) && \
776859
-				((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
776859
+				(nfrontp > p+1 && (((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))))
776859
 
776859
 #if	defined(ENCRYPT)
776859
     thisitem = nclearto > netobuf ? nclearto : netobuf;
776859
@@ -279,7 +286,7 @@ void netclear(void)
776859
     thisitem = netobuf;
776859
 #endif
776859
 
776859
-    while ((next = nextitem(thisitem)) <= nbackp) {
776859
+    while ((next = nextitem(thisitem, nbackp)) != NULL && next <= nbackp) {
776859
 	thisitem = next;
776859
     }
776859
 
776859
@@ -291,20 +298,23 @@ void netclear(void)
776859
     good = netobuf;	/* where the good bytes go */
776859
 #endif
776859
 
776859
-    while (nfrontp > thisitem) {
776859
+    while (thisitem != NULL && nfrontp > thisitem) {
776859
 	if (wewant(thisitem)) {
776859
 	    int length;
776859
 
776859
 	    next = thisitem;
776859
 	    do {
776859
-		next = nextitem(next);
776859
-	    } while (wewant(next) && (nfrontp > next));
776859
+		next = nextitem(next, nfrontp);
776859
+	    } while (next != NULL && wewant(next) && (nfrontp > next));
776859
+	    if (next == NULL) {
776859
+		next = nfrontp;
776859
+	    }
776859
 	    length = next-thisitem;
776859
 	    bcopy(thisitem, good, length);
776859
 	    good += length;
776859
 	    thisitem = next;
776859
 	} else {
776859
-	    thisitem = nextitem(thisitem);
776859
+	    thisitem = nextitem(thisitem, nfrontp);
776859
 	}
776859
     }