5de29b
commit bc8f194c8c29e46e8ee4034f06e46988dfff38f7
5de29b
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
5de29b
Date:   Wed Apr 30 12:00:39 2014 +0530
5de29b
5de29b
    Initialize all of datahead structure in nscd (BZ #16791)
5de29b
    
5de29b
    The datahead structure has an unused padding field that remains
5de29b
    uninitialized.  Valgrind prints out a warning for it on querying a
5de29b
    netgroups entry.  This is harmless, but is a potential data leak since
5de29b
    it would result in writing out an uninitialized byte to the cache
5de29b
    file.  Besides, this happens only when there is a cache miss, so we're
5de29b
    not adding computation to any fast path.
5de29b
5de29b
commit 1cdeb2372ddecac0dfe0c132a033e9590ffa07d2
5de29b
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
5de29b
Date:   Wed Apr 30 11:57:09 2014 +0530
5de29b
5de29b
    Consolidate code to initialize nscd dataset header
5de29b
    
5de29b
    This patch consolidates the code to initialize the header of a dataset
5de29b
    into a single set of functions (one for positive and another for
5de29b
    negative datasets) primarily to reduce repetition of code.  The
5de29b
    secondary reason is to simplify Patch 2/2 which fixes the problem of
5de29b
    an uninitialized byte in the header by initializing an unused field in
5de29b
    the structure and hence preventing a possible data leak into the cache
5de29b
    file.
5de29b
12745e
diff --git glibc-2.17-c758a686/nscd/aicache.c glibc-2.17-c758a686/nscd/aicache.c
5de29b
index 98d40a1..d7966bd 100644
12745e
--- glibc-2.17-c758a686/nscd/aicache.c
12745e
+++ glibc-2.17-c758a686/nscd/aicache.c
5de29b
@@ -383,17 +383,12 @@ addhstaiX (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  cp = family;
5de29b
 	}
5de29b
 
5de29b
-      /* Fill in the rest of the dataset.  */
5de29b
-      dataset->head.allocsize = total + req->key_len;
5de29b
-      dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-      dataset->head.notfound = false;
5de29b
-      dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      dataset->head.ttl = ttl == INT32_MAX ? db->postimeout : ttl;
5de29b
-      timeout = dataset->head.timeout = time (NULL) + dataset->head.ttl;
5de29b
+      timeout = datahead_init_pos (&dataset->head, total + req->key_len,
5de29b
+				   total - offsetof (struct dataset, resp),
5de29b
+				   he == NULL ? 0 : dh->nreloads + 1,
5de29b
+				   ttl == INT32_MAX ? db->postimeout : ttl);
5de29b
 
5de29b
+      /* Fill in the rest of the dataset.  */
5de29b
       dataset->resp.version = NSCD_VERSION;
5de29b
       dataset->resp.found = 1;
5de29b
       dataset->resp.naddrs = naddrs;
5de29b
@@ -528,15 +523,9 @@ next_nip:
5de29b
       else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
5de29b
 					      + req->key_len), 1)) != NULL)
5de29b
 	{
5de29b
-	  dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-	  dataset->head.recsize = total;
5de29b
-	  dataset->head.notfound = true;
5de29b
-	  dataset->head.nreloads = 0;
5de29b
-	  dataset->head.usable = true;
5de29b
-
5de29b
-	  /* Compute the timeout time.  */
5de29b
-	  timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
5de29b
-	  dataset->head.ttl = db->negtimeout;
5de29b
+	  timeout = datahead_init_neg (&dataset->head,
5de29b
+				       sizeof (struct dataset) + req->key_len,
5de29b
+				       total, db->negtimeout);
5de29b
 
5de29b
 	  /* This is the reply.  */
5de29b
 	  memcpy (&dataset->resp, &notfound, total);
12745e
diff --git glibc-2.17-c758a686/nscd/grpcache.c glibc-2.17-c758a686/nscd/grpcache.c
5de29b
index b5a33eb..df59fa7 100644
12745e
--- glibc-2.17-c758a686/nscd/grpcache.c
12745e
+++ glibc-2.17-c758a686/nscd/grpcache.c
5de29b
@@ -128,14 +128,10 @@ cache_addgr (struct database_dyn *db, int fd, request_header *req,
5de29b
 	    }
5de29b
 	  else if ((dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1)) != NULL)
5de29b
 	    {
5de29b
-	      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-	      dataset->head.recsize = total;
5de29b
-	      dataset->head.notfound = true;
5de29b
-	      dataset->head.nreloads = 0;
5de29b
-	      dataset->head.usable = true;
5de29b
-
5de29b
-	      /* Compute the timeout time.  */
5de29b
-	      timeout = dataset->head.timeout = t + db->negtimeout;
5de29b
+	      timeout = datahead_init_neg (&dataset->head,
5de29b
+					   (sizeof (struct dataset)
5de29b
+					    + req->key_len), total,
5de29b
+					   db->negtimeout);
5de29b
 
5de29b
 	      /* This is the reply.  */
5de29b
 	      memcpy (&dataset->resp, &notfound, total);
5de29b
@@ -232,14 +228,10 @@ cache_addgr (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  dataset_temporary = true;
5de29b
 	}
5de29b
 
5de29b
-      dataset->head.allocsize = total + n;
5de29b
-      dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-      dataset->head.notfound = false;
5de29b
-      dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      timeout = dataset->head.timeout = t + db->postimeout;
5de29b
+      timeout = datahead_init_pos (&dataset->head, total + n,
5de29b
+				   total - offsetof (struct dataset, resp),
5de29b
+				   he == NULL ? 0 : dh->nreloads + 1,
5de29b
+				   db->postimeout);
5de29b
 
5de29b
       dataset->resp.version = NSCD_VERSION;
5de29b
       dataset->resp.found = 1;
12745e
diff --git glibc-2.17-c758a686/nscd/hstcache.c glibc-2.17-c758a686/nscd/hstcache.c
5de29b
index a79b67a..d4f1ad2 100644
12745e
--- glibc-2.17-c758a686/nscd/hstcache.c
12745e
+++ glibc-2.17-c758a686/nscd/hstcache.c
5de29b
@@ -152,15 +152,11 @@ cache_addhst (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
5de29b
 						  + req->key_len), 1)) != NULL)
5de29b
 	    {
5de29b
-	      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-	      dataset->head.recsize = total;
5de29b
-	      dataset->head.notfound = true;
5de29b
-	      dataset->head.nreloads = 0;
5de29b
-	      dataset->head.usable = true;
5de29b
-
5de29b
-	      /* Compute the timeout time.  */
5de29b
-	      dataset->head.ttl = ttl == INT32_MAX ? db->negtimeout : ttl;
5de29b
-	      timeout = dataset->head.timeout = t + dataset->head.ttl;
5de29b
+	      timeout = datahead_init_neg (&dataset->head,
5de29b
+					   (sizeof (struct dataset)
5de29b
+					    + req->key_len), total,
5de29b
+					   (ttl == INT32_MAX
5de29b
+					    ? db->negtimeout : ttl));
5de29b
 
5de29b
 	      /* This is the reply.  */
5de29b
 	      memcpy (&dataset->resp, resp, total);
5de29b
@@ -257,15 +253,10 @@ cache_addhst (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  alloca_used = true;
5de29b
 	}
5de29b
 
5de29b
-      dataset->head.allocsize = total + req->key_len;
5de29b
-      dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-      dataset->head.notfound = false;
5de29b
-      dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      dataset->head.ttl = ttl == INT32_MAX ? db->postimeout : ttl;
5de29b
-      timeout = dataset->head.timeout = t + dataset->head.ttl;
5de29b
+      timeout = datahead_init_pos (&dataset->head, total + req->key_len,
5de29b
+				   total - offsetof (struct dataset, resp),
5de29b
+				   he == NULL ? 0 : dh->nreloads + 1,
5de29b
+				   ttl == INT32_MAX ? db->postimeout : ttl);
5de29b
 
5de29b
       dataset->resp.version = NSCD_VERSION;
5de29b
       dataset->resp.found = 1;
12745e
diff --git glibc-2.17-c758a686/nscd/initgrcache.c glibc-2.17-c758a686/nscd/initgrcache.c
5de29b
index 1bf9f0d..361319f 100644
12745e
--- glibc-2.17-c758a686/nscd/initgrcache.c
12745e
+++ glibc-2.17-c758a686/nscd/initgrcache.c
5de29b
@@ -213,14 +213,10 @@ addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
5de29b
 						  + req->key_len), 1)) != NULL)
5de29b
 	    {
5de29b
-	      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-	      dataset->head.recsize = total;
5de29b
-	      dataset->head.notfound = true;
5de29b
-	      dataset->head.nreloads = 0;
5de29b
-	      dataset->head.usable = true;
5de29b
-
5de29b
-	      /* Compute the timeout time.  */
5de29b
-	      timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
5de29b
+	      timeout = datahead_init_neg (&dataset->head,
5de29b
+					   (sizeof (struct dataset)
5de29b
+					    + req->key_len), total,
5de29b
+					   db->negtimeout);
5de29b
 
5de29b
 	      /* This is the reply.  */
5de29b
 	      memcpy (&dataset->resp, &notfound, total);
5de29b
@@ -276,14 +272,10 @@ addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  alloca_used = true;
5de29b
 	}
5de29b
 
5de29b
-      dataset->head.allocsize = total + req->key_len;
5de29b
-      dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-      dataset->head.notfound = false;
5de29b
-      dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      timeout = dataset->head.timeout = time (NULL) + db->postimeout;
5de29b
+      timeout = datahead_init_pos (&dataset->head, total + req->key_len,
5de29b
+				   total - offsetof (struct dataset, resp),
5de29b
+				   he == NULL ? 0 : dh->nreloads + 1,
5de29b
+				   db->postimeout);
5de29b
 
5de29b
       dataset->resp.version = NSCD_VERSION;
5de29b
       dataset->resp.found = 1;
12745e
diff --git glibc-2.17-c758a686/nscd/netgroupcache.c glibc-2.17-c758a686/nscd/netgroupcache.c
5de29b
index 820d823..b3d40e9 100644
12745e
--- glibc-2.17-c758a686/nscd/netgroupcache.c
12745e
+++ glibc-2.17-c758a686/nscd/netgroupcache.c
5de29b
@@ -90,15 +90,9 @@ do_notfound (struct database_dyn *db, int fd, request_header *req,
5de29b
   /* If we cannot permanently store the result, so be it.  */
5de29b
   if (dataset != NULL)
5de29b
     {
5de29b
-      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-      dataset->head.recsize = total;
5de29b
-      dataset->head.notfound = true;
5de29b
-      dataset->head.nreloads = 0;
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
5de29b
-      dataset->head.ttl = db->negtimeout;
5de29b
+      timeout = datahead_init_neg (&dataset->head,
5de29b
+				   sizeof (struct dataset) + req->key_len,
5de29b
+				   total, db->negtimeout);
5de29b
 
5de29b
       /* This is the reply.  */
5de29b
       memcpy (&dataset->resp, &notfound, total);
5de29b
@@ -359,13 +353,10 @@ addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
5de29b
 
5de29b
   /* Fill in the dataset.  */
5de29b
   dataset = (struct dataset *) buffer;
5de29b
-  dataset->head.allocsize = total + req->key_len;
5de29b
-  dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-  dataset->head.notfound = false;
5de29b
-  dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-  dataset->head.usable = true;
5de29b
-  dataset->head.ttl = db->postimeout;
5de29b
-  timeout = dataset->head.timeout = time (NULL) + dataset->head.ttl;
5de29b
+  timeout = datahead_init_pos (&dataset->head, total + req->key_len,
5de29b
+			       total - offsetof (struct dataset, resp),
5de29b
+			       he == NULL ? 0 : dh->nreloads + 1,
5de29b
+			       db->postimeout);
5de29b
 
5de29b
   dataset->resp.version = NSCD_VERSION;
5de29b
   dataset->resp.found = 1;
5de29b
@@ -541,12 +532,12 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
5de29b
       dataset = &dataset_mem;
5de29b
     }
5de29b
 
5de29b
-  dataset->head.allocsize = sizeof (*dataset) + req->key_len;
5de29b
-  dataset->head.recsize = sizeof (innetgroup_response_header);
5de29b
+  datahead_init_pos (&dataset->head, sizeof (*dataset) + req->key_len,
5de29b
+		     sizeof (innetgroup_response_header),
5de29b
+		     he == NULL ? 0 : dh->nreloads + 1, result->head.ttl);
5de29b
+  /* Set the notfound status and timeout based on the result from
5de29b
+     getnetgrent.  */
5de29b
   dataset->head.notfound = result->head.notfound;
5de29b
-  dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-  dataset->head.usable = true;
5de29b
-  dataset->head.ttl = result->head.ttl;
5de29b
   dataset->head.timeout = timeout;
5de29b
 
5de29b
   dataset->resp.version = NSCD_VERSION;
12745e
diff --git glibc-2.17-c758a686/nscd/nscd-client.h glibc-2.17-c758a686/nscd/nscd-client.h
5de29b
index 98f77e7..ee16df6 100644
12745e
--- glibc-2.17-c758a686/nscd/nscd-client.h
12745e
+++ glibc-2.17-c758a686/nscd/nscd-client.h
5de29b
@@ -236,6 +236,48 @@ struct datahead
5de29b
   } data[0];
5de29b
 };
5de29b
 
5de29b
+static inline time_t
5de29b
+datahead_init_common (struct datahead *head, nscd_ssize_t allocsize,
5de29b
+		      nscd_ssize_t recsize, uint32_t ttl)
5de29b
+{
5de29b
+  /* Initialize so that we don't write out junk in uninitialized data to the
5de29b
+     cache.  */
5de29b
+  memset (head, 0, sizeof (*head));
5de29b
+
5de29b
+  head->allocsize = allocsize;
5de29b
+  head->recsize = recsize;
5de29b
+  head->usable = true;
5de29b
+
5de29b
+  head->ttl = ttl;
5de29b
+
5de29b
+  /* Compute and return the timeout time.  */
5de29b
+  return head->timeout = time (NULL) + ttl;
5de29b
+}
5de29b
+
5de29b
+static inline time_t
5de29b
+datahead_init_pos (struct datahead *head, nscd_ssize_t allocsize,
5de29b
+		   nscd_ssize_t recsize, uint8_t nreloads, uint32_t ttl)
5de29b
+{
5de29b
+  time_t ret = datahead_init_common (head, allocsize, recsize, ttl);
5de29b
+
5de29b
+  head->notfound = false;
5de29b
+  head->nreloads = nreloads;
5de29b
+
5de29b
+  return ret;
5de29b
+}
5de29b
+
5de29b
+static inline time_t
5de29b
+datahead_init_neg (struct datahead *head, nscd_ssize_t allocsize,
5de29b
+		   nscd_ssize_t recsize, uint32_t ttl)
5de29b
+{
5de29b
+  time_t ret = datahead_init_common (head, allocsize, recsize, ttl);
5de29b
+
5de29b
+  /* We don't need to touch nreloads here since it is set to our desired value
5de29b
+     (0) when we clear the structure.  */
5de29b
+  head->notfound = true;
5de29b
+
5de29b
+  return ret;
5de29b
+}
5de29b
 
5de29b
 /* Structure for one hash table entry.  */
5de29b
 struct hashentry
12745e
diff --git glibc-2.17-c758a686/nscd/pwdcache.c glibc-2.17-c758a686/nscd/pwdcache.c
5de29b
index fa355c3..41c245b 100644
12745e
--- glibc-2.17-c758a686/nscd/pwdcache.c
12745e
+++ glibc-2.17-c758a686/nscd/pwdcache.c
5de29b
@@ -135,14 +135,10 @@ cache_addpw (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
5de29b
 						  + req->key_len), 1)) != NULL)
5de29b
 	    {
5de29b
-	      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-	      dataset->head.recsize = total;
5de29b
-	      dataset->head.notfound = true;
5de29b
-	      dataset->head.nreloads = 0;
5de29b
-	      dataset->head.usable = true;
5de29b
-
5de29b
-	      /* Compute the timeout time.  */
5de29b
-	      timeout = dataset->head.timeout = t + db->negtimeout;
5de29b
+	      timeout = datahead_init_neg (&dataset->head,
5de29b
+					   (sizeof (struct dataset)
5de29b
+					    + req->key_len), total,
5de29b
+					   db->negtimeout);
5de29b
 
5de29b
 	      /* This is the reply.  */
5de29b
 	      memcpy (&dataset->resp, &notfound, total);
5de29b
@@ -215,14 +211,10 @@ cache_addpw (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  alloca_used = true;
5de29b
 	}
5de29b
 
5de29b
-      dataset->head.allocsize = total + n;
5de29b
-      dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-      dataset->head.notfound = false;
5de29b
-      dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      timeout = dataset->head.timeout = t + db->postimeout;
5de29b
+      timeout = datahead_init_pos (&dataset->head, total + n,
5de29b
+				   total - offsetof (struct dataset, resp),
5de29b
+				   he == NULL ? 0 : dh->nreloads + 1,
5de29b
+				   db->postimeout);
5de29b
 
5de29b
       dataset->resp.version = NSCD_VERSION;
5de29b
       dataset->resp.found = 1;
12745e
diff --git glibc-2.17-c758a686/nscd/servicescache.c glibc-2.17-c758a686/nscd/servicescache.c
5de29b
index 12ce9b2..95bdcfe 100644
12745e
--- glibc-2.17-c758a686/nscd/servicescache.c
12745e
+++ glibc-2.17-c758a686/nscd/servicescache.c
5de29b
@@ -120,14 +120,10 @@ cache_addserv (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
5de29b
 						  + req->key_len), 1)) != NULL)
5de29b
 	    {
5de29b
-	      dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
5de29b
-	      dataset->head.recsize = total;
5de29b
-	      dataset->head.notfound = true;
5de29b
-	      dataset->head.nreloads = 0;
5de29b
-	      dataset->head.usable = true;
5de29b
-
5de29b
-	      /* Compute the timeout time.  */
5de29b
-	      timeout = dataset->head.timeout = t + db->negtimeout;
5de29b
+	      timeout = datahead_init_neg (&dataset->head,
5de29b
+					   (sizeof (struct dataset)
5de29b
+					    + req->key_len), total,
5de29b
+					   db->negtimeout);
5de29b
 
5de29b
 	      /* This is the reply.  */
5de29b
 	      memcpy (&dataset->resp, &notfound, total);
5de29b
@@ -207,14 +203,10 @@ cache_addserv (struct database_dyn *db, int fd, request_header *req,
5de29b
 	  alloca_used = true;
5de29b
 	}
5de29b
 
5de29b
-      dataset->head.allocsize = total + req->key_len;
5de29b
-      dataset->head.recsize = total - offsetof (struct dataset, resp);
5de29b
-      dataset->head.notfound = false;
5de29b
-      dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
5de29b
-      dataset->head.usable = true;
5de29b
-
5de29b
-      /* Compute the timeout time.  */
5de29b
-      timeout = dataset->head.timeout = t + db->postimeout;
5de29b
+      timeout = datahead_init_pos (&dataset->head, total + req->key_len,
5de29b
+				   total - offsetof (struct dataset, resp),
5de29b
+				   he == NULL ? 0 : dh->nreloads + 1,
5de29b
+				   db->postimeout);
5de29b
 
5de29b
       dataset->resp.version = NSCD_VERSION;
5de29b
       dataset->resp.found = 1;