]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
basic_ncsa_auth: Fix NULL-dereference crash
authorAmos Jeffries <squid3@treenet.co.nz>
Mon, 26 Nov 2012 08:32:53 +0000 (01:32 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Mon, 26 Nov 2012 08:32:53 +0000 (01:32 -0700)
When reading corrupt or broken user passwd files with missing username
data this helper can crash.

 Detected by Coverity Scan. Issue 740398

helpers/basic_auth/NCSA/basic_ncsa_auth.cc

index f5ef97e643319652de8468fa4f808a393fd7a649..7ce42f8b002be6308b28673ec48f6ef8633ed6ee 100644 (file)
@@ -65,7 +65,7 @@ static void
 read_passwd_file(const char *passwdfile)
 {
     FILE *f;
-    char buf[8192];
+    char buf[HELPER_INPUT_BUFFER];
     user_data *u;
     char *user;
     char *passwd;
@@ -84,11 +84,18 @@ read_passwd_file(const char *passwdfile)
         fprintf(stderr, "FATAL: %s: %s\n", passwdfile, xstrerror());
         exit(1);
     }
-    while (fgets(buf, 8192, f) != NULL) {
+    unsigned int lineCount = 0;
+    buf[HELPER_INPUT_BUFFER-1] = '\0';
+    while (fgets(buf, sizeof(buf)-1, f) != NULL) {
+        ++lineCount;
         if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
                 (buf[0] == '\n'))
             continue;
         user = strtok(buf, ":\n\r");
+        if (user == NULL) {
+            fprintf(stderr, "ERROR: Missing user name at %s line %d\n", passwdfile, lineCount);
+            continue;
+        }
         passwd = strtok(NULL, ":\n\r");
         if ((strlen(user) > 0) && passwd) {
             u = static_cast<user_data*>(xmalloc(sizeof(*u)));