https://sourceware.org/bugzilla/show_bug.cgi?id=18913

There are multiple integer overflow and out of bounds access issues
in __open_catalog. I highly doubt that they could be used to attack
a system properly, because NLSPATH is removed for setuid binaries.
Still, these problems should be fixed.

Please see my attached diff and this explanation. For experimentation,
please also look at the attached proof of concept message file, which
leads to a segmentation fault due to these issues:


First block:

Verify that file is not larger than size_t limitations, because the code
expects to load the whole file at once -- either through mmap or malloc.
4 GB should be way enough for all systems, anyway...

Second block:

Fixed a typo, "a reasonable" instead of "an reasonable"

Third block:

If the file is invalid, later code parts jump to invalid_file, but just
as the previous comment stated (see second block), errno is not properly
set. Without setting it to EINVAL, it could happen that -1 is returned,
but errno states "Success".

Fourth block:

The values for plane_size and plane_depth are read without validation
from file. They basically specify the size of the pointer table inside
the file (see tab_size calculation). At this stage, make sure that
"3 * plane_size * plane_depth * 2 * sizeof(u_int32_t)" won't result in
an integer overflow. Explanation: We have 2 pointer tables, each
table holds 3 u_int32_t values for plane_size * plane_depth entries.

Also, make sure that plane_size is not 0. It is used in a modulo (%)
expression in catgets().

Fifth block:

Actually check if the file is large enough to hold these values. The
previous check only verified that the result can be kept in size_t.

Sixth block:

The current check is incomplete, because it does not properly take
sizeof(u_int32_t) into account. "2 * tab_size" is supposed to specify
the size of the two pointer tables, but in fact it only specifies the
amount of indices. While at it, make sure that addition of max_offset
won't result in an integer overflow.

Seventh block:

The calculation of max_offset is wrong. It does not take sizeof(u_int32_t)
into account (see sixth block), but even adds tab size and max offset,
instead of subtracting them from st.st_size. Therefore, the check for the
last '\0' termination of the contained strings can easily result in
a read overflow.


PS: Is there a good way to check for overflows in arithmetic expressions
inside glibc infrastructure? I doubt that gcc-specific macros are
acceptable.

--- glibc-2.22/catgets/open_catalog.c~	2015-08-05 08:42:21.000000000 +0200
+++ gblic-2.22/catgets/open_catalog.c	2015-09-02 19:26:12.332338284 +0200
@@ -194,10 +194,11 @@
     goto close_unlock_return;
 
   if (__builtin_expect (!S_ISREG (st.st_mode), 0)
-      || (size_t) st.st_size < sizeof (struct catalog_obj))
+      || (size_t) st.st_size < sizeof (struct catalog_obj)
+      || st.st_size > ((size_t) ~0))
     {
       /* `errno' is not set correctly but the file is not usable.
-	 Use an reasonable error value.  */
+	 Use a reasonable error value.  */
       __set_errno (EINVAL);
       goto close_unlock_return;
     }
@@ -260,6 +261,9 @@
   else
     {
     invalid_file:
+      /* `errno' is not set correctly but the file is not usable.
+	 Use a reasonable error value.  */
+      __set_errno (EINVAL);
       /* Invalid file.  Free the resources and mark catalog as not
 	 usable.  */
 #ifdef _POSIX_MAPPED_FILES
@@ -277,6 +281,11 @@
   catalog->plane_size = SWAP (catalog->file_ptr->plane_size);
   catalog->plane_depth = SWAP (catalog->file_ptr->plane_depth);
 
+  /* Check whether dimensions are in valid ranges.  */
+  if (catalog->plane_size == 0 || ((size_t) ~0) / 3 / 2 / sizeof(u_int32_t)
+      / catalog->plane_size < catalog->plane_depth)
+    goto invalid_file;
+
   /* The file contains two versions of the pointer tables.  Pick the
      right one for the local byte order.  */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
@@ -298,20 +307,28 @@
   /* Determine the largest string offset mentioned in the table.  */
   max_offset = 0;
   tab_size = 3 * catalog->plane_size * catalog->plane_depth;
+
+  /* Check whether file is large enough to hold the pointer tables.  */
+  if (((size_t) st.st_size - sizeof (struct catalog_obj))
+       / sizeof (u_int32_t) / 2 < tab_size)
+    goto invalid_file;
+
   for (cnt = 2; cnt < tab_size; cnt += 3)
     if (catalog->name_ptr[cnt] > max_offset)
       max_offset = catalog->name_ptr[cnt];
 
   /* Now we can check whether the file is large enough to contain the
      tables it says it contains.  */
-  if ((size_t) st.st_size
-      <= (sizeof (struct catalog_obj) + 2 * tab_size + max_offset))
+  if (((size_t) ~0) - sizeof (struct catalog_obj)
+       - 2 * tab_size * sizeof (u_int32_t) < max_offset
+      || (size_t) st.st_size <= (sizeof (struct catalog_obj)
+       + 2 * tab_size * sizeof (u_int32_t) + max_offset))
     /* The last string is not contained in the file.  */
     goto invalid_file;
 
   lastp = catalog->strings + max_offset;
-  max_offset = (st.st_size
-		- sizeof (struct catalog_obj) + 2 * tab_size + max_offset);
+  max_offset = (st.st_size - sizeof (struct catalog_obj)
+		- 2 * tab_size * sizeof (u_int32_t) - max_offset);
   while (*lastp != '\0')
     {
       if (--max_offset == 0)
