Correctly handle buffer overflow while reading line with fgets.
authordrepper <drepper>
Sun, 25 Jan 1998 16:35:04 +0000 (16:35 +0000)
committerdrepper <drepper>
Sun, 25 Jan 1998 16:35:04 +0000 (16:35 +0000)
nis/nss_compat/compat-grp.c
nis/nss_compat/compat-pwd.c
nis/nss_compat/compat-spwd.c

index 4b873d6..dab1b5e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
 
@@ -458,17 +458,15 @@ getgrent_next_file (struct group *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
+           return NSS_STATUS_NOTFOUND;
+         if (p == NULL || buffer[buflen - 1] != '\xff')
            {
-             if (feof (ent->stream))
-               return NSS_STATUS_NOTFOUND;
-             else
-               {
-                 fsetpos (ent->stream, &pos);
-                 *errnop = ERANGE;
-                 return NSS_STATUS_TRYAGAIN;
-               }
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
            }
 
          /* Terminate the line for any case.  */
@@ -600,17 +598,15 @@ internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
+           return NSS_STATUS_NOTFOUND;
+         if (p == NULL || buffer[buflen - 1] != '\xff')
            {
-             if (feof (ent->stream))
-               return NSS_STATUS_NOTFOUND;
-             else
-               {
-                 fsetpos (ent->stream, &pos);
-                 *errnop = ERANGE;
-                 return NSS_STATUS_TRYAGAIN;
-               }
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
            }
 
          /* Terminate the line for any case.  */
@@ -794,17 +790,15 @@ internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
+           return NSS_STATUS_NOTFOUND;
+         if (p == NULL || buffer[buflen - 1] != '\xff')
            {
-             if (feof (ent->stream))
-               return NSS_STATUS_NOTFOUND;
-             else
-               {
-                 fsetpos (ent->stream, &pos);
-                 *errnop = ERANGE;
-                 return NSS_STATUS_TRYAGAIN;
-               }
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
            }
 
          /* Terminate the line for any case.  */
index 8a1c424..5bfff17 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
 
@@ -810,17 +810,15 @@ getpwent_next_file (struct passwd *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
+           return NSS_STATUS_NOTFOUND;
+         if (p == NULL || buffer[buflen - 1] != '\xff')
            {
-             if (feof (ent->stream))
-               return NSS_STATUS_NOTFOUND;
-             else
-               {
-                 fsetpos (ent->stream, &pos);
-                 *errnop = ERANGE;
-                 return NSS_STATUS_TRYAGAIN;
-               }
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
            }
 
          /* Terminate the line for any case.  */
@@ -1023,17 +1021,15 @@ internal_getpwnam_r (const char *name, struct passwd *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
+           return NSS_STATUS_NOTFOUND;
+         if (p == NULL || buffer[buflen - 1] != '\xff')
            {
-             if (feof (ent->stream))
-               return NSS_STATUS_NOTFOUND;
-             else
-               {
-                 fsetpos (ent->stream, &pos);
-                 *errnop = ERANGE;
-                 return NSS_STATUS_TRYAGAIN;
-               }
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
            }
 
          /* Terminate the line for any case.  */
@@ -1306,17 +1302,15 @@ internal_getpwuid_r (uid_t uid, struct passwd *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
+           return NSS_STATUS_NOTFOUND;
+         if (p == NULL || buffer[buflen - 1] != '\xff')
            {
-             if (feof (ent->stream))
-               return NSS_STATUS_NOTFOUND;
-             else
-               {
-                 fsetpos (ent->stream, &pos);
-                 *errnop = ERANGE;
-                 return NSS_STATUS_TRYAGAIN;
-               }
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
            }
 
          /* Terminate the line for any case.  */
index 83db856..816e9c1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
 
@@ -761,12 +761,16 @@ getspent_next_file (struct spwd *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
            return NSS_STATUS_NOTFOUND;
-
-         /* Terminate the line for any case.  */
-         buffer[buflen - 1] = '\0';
+         if (p == NULL || buffer[buflen - 1] != '\xff')
+           {
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
+           }
 
          /* Skip leading blanks.  */
          while (isspace (*p))
@@ -965,12 +969,16 @@ internal_getspnam_r (const char *name, struct spwd *result, ent_t *ent,
       do
        {
          fgetpos (ent->stream, &pos);
+         buffer[buflen - 1] = '\xff';
          p = fgets (buffer, buflen, ent->stream);
-         if (p == NULL)
+         if (p == NULL && feof (ent->stream))
            return NSS_STATUS_NOTFOUND;
-
-         /* Terminate the line for any case.  */
-         buffer[buflen - 1] = '\0';
+         if (p == NULL || buffer[buflen - 1] != '\xff')
+           {
+             fsetpos (ent->stream, &pos);
+             *errnop = ERANGE;
+             return NSS_STATUS_TRYAGAIN;
+           }
 
          /* Skip leading blanks.  */
          while (isspace (*p))