c1ae809
commit 97ac2654b2d831acaa18a2b018b0736245903fd2
c1ae809
Author: Ulrich Drepper <drepper@gmail.com>
c1ae809
Date:   Sat Dec 17 20:18:42 2011 -0500
c1ae809
c1ae809
    Check values from TZ file header
c1ae809
c1ae809
c1ae809
	[BZ #13506]
c1ae809
	* time/tzfile.c (__tzfile_read): Check values from file header.
c1ae809
c1ae809
diff --git a/time/tzfile.c b/time/tzfile.c
c1ae809
index 144e20b..402389c 100644
c1ae809
--- a/time/tzfile.c
c1ae809
+++ b/time/tzfile.c
c1ae809
@@ -234,23 +234,58 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
c1ae809
       goto read_again;
c1ae809
     }
c1ae809
 
c1ae809
+  if (__builtin_expect (num_transitions
c1ae809
+			> ((SIZE_MAX - (__alignof__ (struct ttinfo) - 1))
c1ae809
+			   / (sizeof (time_t) + 1)), 0))
c1ae809
+    goto lose;
c1ae809
   total_size = num_transitions * (sizeof (time_t) + 1);
c1ae809
   total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
c1ae809
 		& ~(__alignof__ (struct ttinfo) - 1));
c1ae809
   types_idx = total_size;
c1ae809
-  total_size += num_types * sizeof (struct ttinfo) + chars;
c1ae809
+  if (__builtin_expect (num_types
c1ae809
+			> (SIZE_MAX - total_size) / sizeof (struct ttinfo), 0))
c1ae809
+    goto lose;
c1ae809
+  total_size += num_types * sizeof (struct ttinfo);
c1ae809
+  if (__builtin_expect (chars > SIZE_MAX - total_size, 0))
c1ae809
+    goto lose;
c1ae809
+  total_size += chars;
c1ae809
+  if (__builtin_expect (__alignof__ (struct leap) - 1
c1ae809
+			> SIZE_MAX - total_size, 0))
c1ae809
+    goto lose;
c1ae809
   total_size = ((total_size + __alignof__ (struct leap) - 1)
c1ae809
 		& ~(__alignof__ (struct leap) - 1));
c1ae809
   leaps_idx = total_size;
c1ae809
+  if (__builtin_expect (num_leaps
c1ae809
+			> (SIZE_MAX - total_size) / sizeof (struct leap), 0))
c1ae809
+    goto lose;
c1ae809
   total_size += num_leaps * sizeof (struct leap);
c1ae809
-  tzspec_len = (sizeof (time_t) == 8 && trans_width == 8
c1ae809
-		? st.st_size - (ftello (f)
c1ae809
-				+ num_transitions * (8 + 1)
c1ae809
-				+ num_types * 6
c1ae809
-				+ chars
c1ae809
-				+ num_leaps * 12
c1ae809
-				+ num_isstd
c1ae809
-				+ num_isgmt) - 1 : 0);
c1ae809
+  tzspec_len = 0;
c1ae809
+  if (sizeof (time_t) == 8 && trans_width == 8)
c1ae809
+    {
c1ae809
+      off_t rem = st.st_size - ftello (f);
c1ae809
+      if (__builtin_expect (rem < 0
c1ae809
+			    || (size_t) rem < (num_transitions * (8 + 1)
c1ae809
+					       + num_types * 6
c1ae809
+					       + chars), 0))
c1ae809
+	goto lose;
c1ae809
+      tzspec_len = (size_t) rem - (num_transitions * (8 + 1)
c1ae809
+				   + num_types * 6
c1ae809
+				   + chars);
c1ae809
+      if (__builtin_expect (num_leaps > SIZE_MAX / 12
c1ae809
+			    || tzspec_len < num_leaps * 12, 0))
c1ae809
+	goto lose;
c1ae809
+      tzspec_len -= num_leaps * 12;
c1ae809
+      if (__builtin_expect (tzspec_len < num_isstd, 0))
c1ae809
+	goto lose;
c1ae809
+      tzspec_len -= num_isstd;
e13f8ca
+      if (__builtin_expect (tzspec_len == 0 || tzspec_len - 1 < num_isgmt, 0))
c1ae809
+	goto lose;
c1ae809
+      tzspec_len -= num_isgmt + 1;
c1ae809
+      if (__builtin_expect (SIZE_MAX - total_size < tzspec_len, 0))
c1ae809
+	goto lose;
c1ae809
+    }
c1ae809
+  if (__builtin_expect (SIZE_MAX - total_size - tzspec_len < extra, 0))
c1ae809
+    goto lose;
c1ae809
 
c1ae809
   /* Allocate enough memory including the extra block requested by the
c1ae809
      caller.  */