077c9d
commit 745664bd798ec8fd50438605948eea594179fba1
077c9d
Author: Florian Weimer <fweimer@redhat.com>
077c9d
Date:   Tue Aug 28 13:19:27 2018 +0200
077c9d
077c9d
    nscd: Fix use-after-free in addgetnetgrentX [BZ #23520]
077c9d
    
077c9d
    addinnetgrX may use the heap-allocated buffer, so free the buffer
077c9d
    in this function.
077c9d
077c9d
diff --git a/nscd/netgroupcache.c b/nscd/netgroupcache.c
077c9d
index 2b35389cc816c3c8..87059fb28042f0a5 100644
077c9d
--- a/nscd/netgroupcache.c
077c9d
+++ b/nscd/netgroupcache.c
077c9d
@@ -113,7 +113,8 @@ do_notfound (struct database_dyn *db, int fd, request_header *req,
077c9d
 static time_t
077c9d
 addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
077c9d
 		 const char *key, uid_t uid, struct hashentry *he,
077c9d
-		 struct datahead *dh, struct dataset **resultp)
077c9d
+		 struct datahead *dh, struct dataset **resultp,
077c9d
+		 void **tofreep)
077c9d
 {
077c9d
   if (__glibc_unlikely (debug_level > 0))
077c9d
     {
077c9d
@@ -139,6 +140,7 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
077c9d
   size_t group_len = strlen (key) + 1;
077c9d
   struct name_list *first_needed
077c9d
     = alloca (sizeof (struct name_list) + group_len);
077c9d
+  *tofreep = NULL;
077c9d
 
077c9d
   if (netgroup_database == NULL
077c9d
       && __nss_database_lookup ("netgroup", NULL, NULL, &netgroup_database))
077c9d
@@ -151,6 +153,7 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
077c9d
 
077c9d
   memset (&data, '\0', sizeof (data));
077c9d
   buffer = xmalloc (buflen);
077c9d
+  *tofreep = buffer;
077c9d
   first_needed->next = first_needed;
077c9d
   memcpy (first_needed->name, key, group_len);
077c9d
   data.needed_groups = first_needed;
077c9d
@@ -439,8 +442,6 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
077c9d
     }
077c9d
 
077c9d
  out:
077c9d
-  free (buffer);
077c9d
-
077c9d
   *resultp = dataset;
077c9d
 
077c9d
   return timeout;
077c9d
@@ -477,8 +478,12 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
077c9d
 							    group, group_len,
077c9d
 							    db, uid);
077c9d
   time_t timeout;
077c9d
+  void *tofree;
077c9d
   if (result != NULL)
077c9d
-    timeout = result->head.timeout;
077c9d
+    {
077c9d
+      timeout = result->head.timeout;
077c9d
+      tofree = NULL;
077c9d
+    }
077c9d
   else
077c9d
     {
077c9d
       request_header req_get =
077c9d
@@ -487,7 +492,7 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
077c9d
 	  .key_len = group_len
077c9d
 	};
077c9d
       timeout = addgetnetgrentX (db, -1, &req_get, group, uid, NULL, NULL,
077c9d
-				 &result);
077c9d
+				 &result, &tofree);
077c9d
     }
077c9d
 
077c9d
   struct indataset
077c9d
@@ -560,7 +565,7 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
077c9d
       ++dh->nreloads;
077c9d
       if (cacheable)
077c9d
         pthread_rwlock_unlock (&db->lock);
077c9d
-      return timeout;
077c9d
+      goto out;
077c9d
     }
077c9d
 
077c9d
   if (he == NULL)
077c9d
@@ -596,17 +601,30 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
077c9d
 	dh->usable = false;
077c9d
     }
077c9d
 
077c9d
+ out:
077c9d
+  free (tofree);
077c9d
   return timeout;
077c9d
 }
077c9d
 
077c9d
 
077c9d
+static time_t
077c9d
+addgetnetgrentX_ignore (struct database_dyn *db, int fd, request_header *req,
077c9d
+			const char *key, uid_t uid, struct hashentry *he,
077c9d
+			struct datahead *dh)
077c9d
+{
077c9d
+  struct dataset *ignore;
077c9d
+  void *tofree;
077c9d
+  time_t timeout = addgetnetgrentX (db, fd, req, key, uid, he, dh,
077c9d
+				    &ignore, &tofree);
077c9d
+  free (tofree);
077c9d
+  return timeout;
077c9d
+}
077c9d
+
077c9d
 void
077c9d
 addgetnetgrent (struct database_dyn *db, int fd, request_header *req,
077c9d
 		void *key, uid_t uid)
077c9d
 {
077c9d
-  struct dataset *ignore;
077c9d
-
077c9d
-  addgetnetgrentX (db, fd, req, key, uid, NULL, NULL, &ignore);
077c9d
+  addgetnetgrentX_ignore (db, fd, req, key, uid, NULL, NULL);
077c9d
 }
077c9d
 
077c9d
 
077c9d
@@ -619,10 +637,8 @@ readdgetnetgrent (struct database_dyn *db, struct hashentry *he,
077c9d
       .type = GETNETGRENT,
077c9d
       .key_len = he->len
077c9d
     };
077c9d
-  struct dataset *ignore;
077c9d
-
077c9d
-  return addgetnetgrentX (db, -1, &req, db->data + he->key, he->owner, he, dh,
077c9d
-			  &ignore);
077c9d
+  return addgetnetgrentX_ignore
077c9d
+    (db, -1, &req, db->data + he->key, he->owner, he, dh);
077c9d
 }
077c9d
 
077c9d