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