CVE-2019-14495: Out-of-bounds Write (CWE-787)
value: webadmin.c in 3proxy before 0.8.13 has an out-of-bounds write in the admin interface.
CWE description: The product writes data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 9.8
Published: 2019-08-01T17:15Z
Repository: https://github.com/z3APA3A/3proxy
Language: C
Files changed: src/webadmin.c
Commit date: 2019-08-01 16:49:26 +0300
Commit message: Fix: out-of-bounds write and few more bugs in 'admin' configuration upload

Diff: diff --git a/src/webadmin.c b/src/webadmin.c
index 70b47c4..04915b2 100644
--- a/src/webadmin.c
+++ b/src/webadmin.c
@@ -8,11 +8,11 @@
 
 #include "proxy.h"
 
 #define RETURN(xxx) { param->res = xxx; goto CLEANRET; }
 
-#define LINESIZE 2048
+#define LINESIZE 65536
 
 extern FILE *writable;
 FILE * confopen();
 extern void decodeurl(unsigned char *s, int filter);
 
@@ -339,11 +339,11 @@ void * adminchild(struct clientparam* param) {
  char * buf;
  char username[256];
  char *sb;
  char *req = NULL;
  struct printparam pp;
- int contentlen = 0;
+ unsigned contentlen = 0;
  int isform = 0;
 
  pp.inbuf = 0;
  pp.cp = param;
 
@@ -388,11 +388,12 @@ void * adminchild(struct clientparam* param) {
 		continue;
 	}
 	else if(i > 15 && (!strncasecmp(buf, "content-length:", 15))){
 		sb = buf + 15;
 		while(isspace(*sb))sb++;
-		contentlen = atoi(sb);
+		sscanf(sb, "%u", &contentlen);
+		if(contentlen > LINESIZE*1024) contentlen = 0;
 	}
 	else if(i > 13 && (!strncasecmp(buf, "content-type:", 13))){
 		sb = buf + 13;
 		while(isspace(*sb))sb++;
 		if(!strncasecmp(sb, "x-www-form-urlencoded", 21)) isform = 1;
@@ -518,38 +519,37 @@ void * adminchild(struct clientparam* param) {
 			if(!fp){
 				printstr(&pp, "<h3><font color=\"red\">Failed to open config file</font></h3>");
 				break;
 			}
 				printstr(&pp, "<h3>Please be careful editing config file remotely</h3>");
-				printstr(&pp, "<form method=\"POST\" action=\"/U\"><textarea cols=\"80\" rows=\"30\" name=\"conffile\">");
+				printstr(&pp, "<form method=\"POST\" action=\"/U\" enctype=\"application/x-www-form-urlencoded\"><textarea cols=\"80\" rows=\"30\" name=\"conffile\">");
 				while(fgets(buf, 256, fp)){
 					printstr(&pp, buf);
 				}
 				if(!writable) fclose(fp);
 				printstr(&pp, "</textarea><br><input type=\"Submit\"></form>");
 			break;
 		}
 	case 'U':
 		{
-			int l=0;
+			unsigned l=0;
 			int error = 0;
 
-			if(!writable || fseek(writable, 0, 0)){
+			if(!writable || !contentlen || fseek(writable, 0, 0)){
 				error = 1;
 			}
-			while((i = sockgetlinebuf(param, CLIENT, (unsigned char *)buf, LINESIZE - 1, '+', conf.timeouts[STRING_S])) > 0){
+			while(l < contentlen && (i = sockgetlinebuf(param, CLIENT, (unsigned char *)buf, (contentlen - l) > LINESIZE - 1?LINESIZE - 1:contentlen - l, '+', conf.timeouts[STRING_S])) > 0){
 				if(i > (contentlen - l)) i = (contentlen - l);
-				buf[i] = 0;
 				if(!l){
-					if(strncasecmp(buf, "conffile=", 9)) error = 1;
+					if(i<9 || strncasecmp(buf, "conffile=", 9)) error = 1;
 				}
 				if(!error){
+					buf[i] = 0;
 					decodeurl((unsigned char *)buf, 1);
 					fprintf(writable, "%s", l? buf : buf + 9);
 				}
 				l += i;
-				if(l >= contentlen) break;
 			}
 			if(writable && !error){
 				fflush(writable);
 #ifndef _WINCE
 				ftruncate(fileno(writable), ftell(writable));

Vulnerable code: #define LINESIZE 2048
 int contentlen = 0;
		contentlen = atoi(sb);
				printstr(&pp, "<form method=\"POST\" action=\"/U\"><textarea cols=\"80\" rows=\"30\" name=\"conffile\">");
			int l=0;
			if(!writable || fseek(writable, 0, 0)){
			while((i = sockgetlinebuf(param, CLIENT, (unsigned char *)buf, LINESIZE - 1, '+', conf.timeouts[STRING_S])) > 0){
				buf[i] = 0;
					if(strncasecmp(buf, "conffile=", 9)) error = 1;
				if(l >= contentlen) break;
Fixed code: #define LINESIZE 65536
 unsigned contentlen = 0;
		sscanf(sb, "%u", &contentlen);
		if(contentlen > LINESIZE*1024) contentlen = 0;
				printstr(&pp, "<form method=\"POST\" action=\"/U\" enctype=\"application/x-www-form-urlencoded\"><textarea cols=\"80\" rows=\"30\" name=\"conffile\">");
			unsigned l=0;
			if(!writable || !contentlen || fseek(writable, 0, 0)){
			while(l < contentlen && (i = sockgetlinebuf(param, CLIENT, (unsigned char *)buf, (contentlen - l) > LINESIZE - 1?LINESIZE - 1:contentlen - l, '+', conf.timeouts[STRING_S])) > 0){
					if(i<9 || strncasecmp(buf, "conffile=", 9)) error = 1;
					buf[i] = 0;