e354a5
commit 2954daf00bb4dc27c69a48e6798d5960ea320741
e354a5
Author: Andreas Schwab <schwab@suse.de>
e354a5
Date:   Tue Oct 23 09:40:14 2018 +0200
e354a5
e354a5
    Add more checks for valid ld.so.cache file (bug 18093)
e354a5
e354a5
diff --git a/elf/cache.c b/elf/cache.c
e354a5
index e63979da7d25560c..c4cd825c30e00e8e 100644
e354a5
--- a/elf/cache.c
e354a5
+++ b/elf/cache.c
e354a5
@@ -199,6 +199,11 @@ print_cache (const char *cache_name)
e354a5
     }
e354a5
   else
e354a5
     {
e354a5
+      /* Check for corruption, avoiding overflow.  */
e354a5
+      if ((cache_size - sizeof (struct cache_file)) / sizeof (struct file_entry)
e354a5
+	  < cache->nlibs)
e354a5
+	error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
e354a5
+
e354a5
       size_t offset = ALIGN_CACHE (sizeof (struct cache_file)
e354a5
 				   + (cache->nlibs
e354a5
 				      * sizeof (struct file_entry)));
e354a5
diff --git a/elf/dl-cache.c b/elf/dl-cache.c
e354a5
index 6ee5153ff9514872..6dd99a35b9f97cfb 100644
e354a5
--- a/elf/dl-cache.c
e354a5
+++ b/elf/dl-cache.c
e354a5
@@ -204,7 +204,10 @@ _dl_load_cache_lookup (const char *name)
e354a5
 	 - only the new format
e354a5
 	 The following checks if the cache contains any of these formats.  */
e354a5
       if (file != MAP_FAILED && cachesize > sizeof *cache
e354a5
-	  && memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1) == 0)
e354a5
+	  && memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1) == 0
e354a5
+	  /* Check for corruption, avoiding overflow.  */
e354a5
+	  && ((cachesize - sizeof *cache) / sizeof (struct file_entry)
e354a5
+	      >= ((struct cache_file *) file)->nlibs))
e354a5
 	{
e354a5
 	  size_t offset;
e354a5
 	  /* Looks ok.  */