Blame SOURCES/libtar-1.2.11-CVE-2013-4397.patch

360332
From 8505fb844300f493b4e848d4461537a7bb0e8cc0 Mon Sep 17 00:00:00 2001
360332
From: Kamil Dudka <kdudka@redhat.com>
360332
Date: Fri, 4 Oct 2013 13:55:26 +0200
360332
Subject: [PATCH] libtar - fix CVE-2013-4397 libtar (upstream patch)
360332
360332
Heap-based buffer overflows by expanding a specially-crafted archive
360332
---
360332
 lib/block.c |   38 ++++++++++++++++++++++++--------------
360332
 1 files changed, 24 insertions(+), 14 deletions(-)
360332
360332
diff --git a/lib/block.c b/lib/block.c
360332
index 2917dc6..092bc28 100644
360332
--- a/lib/block.c
360332
+++ b/lib/block.c
360332
@@ -90,8 +90,8 @@ th_read_internal(TAR *t)
360332
 int
360332
 th_read(TAR *t)
360332
 {
360332
-	int i, j;
360332
-	size_t sz;
360332
+	int i;
360332
+	size_t sz, j, blocks;
360332
 	char *ptr;
360332
 
360332
 #ifdef DEBUG
360332
@@ -118,21 +118,26 @@ th_read(TAR *t)
360332
 	if (TH_ISLONGLINK(t))
360332
 	{
360332
 		sz = th_get_size(t);
360332
-		j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0);
360332
+		blocks = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0);
360332
+		if (blocks > ((size_t)-1 / T_BLOCKSIZE))
360332
+		{
360332
+			errno = E2BIG;
360332
+			return -1;
360332
+		}
360332
 #ifdef DEBUG
360332
 		printf("    th_read(): GNU long linkname detected "
360332
-		       "(%ld bytes, %d blocks)\n", sz, j);
360332
+		       "(%ld bytes, %d blocks)\n", sz, blocks);
360332
 #endif
360332
-		t->th_buf.gnu_longlink = (char *)malloc(j * T_BLOCKSIZE);
360332
+		t->th_buf.gnu_longlink = (char *)malloc(blocks * T_BLOCKSIZE);
360332
 		if (t->th_buf.gnu_longlink == NULL)
360332
 			return -1;
360332
 
360332
-		for (ptr = t->th_buf.gnu_longlink; j > 0;
360332
-		     j--, ptr += T_BLOCKSIZE)
360332
+		for (j = 0, ptr = t->th_buf.gnu_longlink; j < blocks;
360332
+		     j++, ptr += T_BLOCKSIZE)
360332
 		{
360332
 #ifdef DEBUG
360332
 			printf("    th_read(): reading long linkname "
360332
-			       "(%d blocks left, ptr == %ld)\n", j, ptr);
360332
+			       "(%d blocks left, ptr == %ld)\n", blocks-j, ptr);
360332
 #endif
360332
 			i = tar_block_read(t, ptr);
360332
 			if (i != T_BLOCKSIZE)
360332
@@ -163,21 +168,26 @@ th_read(TAR *t)
360332
 	if (TH_ISLONGNAME(t))
360332
 	{
360332
 		sz = th_get_size(t);
360332
-		j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0);
360332
+		blocks = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0);
360332
+		if (blocks > ((size_t)-1 / T_BLOCKSIZE))
360332
+		{
360332
+			errno = E2BIG;
360332
+			return -1;
360332
+		}
360332
 #ifdef DEBUG
360332
 		printf("    th_read(): GNU long filename detected "
360332
-		       "(%ld bytes, %d blocks)\n", sz, j);
360332
+		       "(%ld bytes, %d blocks)\n", sz, blocks);
360332
 #endif
360332
-		t->th_buf.gnu_longname = (char *)malloc(j * T_BLOCKSIZE);
360332
+		t->th_buf.gnu_longname = (char *)malloc(blocks * T_BLOCKSIZE);
360332
 		if (t->th_buf.gnu_longname == NULL)
360332
 			return -1;
360332
 
360332
-		for (ptr = t->th_buf.gnu_longname; j > 0;
360332
-		     j--, ptr += T_BLOCKSIZE)
360332
+		for (j = 0, ptr = t->th_buf.gnu_longname; j < blocks;
360332
+		     j++, ptr += T_BLOCKSIZE)
360332
 		{
360332
 #ifdef DEBUG
360332
 			printf("    th_read(): reading long filename "
360332
-			       "(%d blocks left, ptr == %ld)\n", j, ptr);
360332
+			       "(%d blocks left, ptr == %ld)\n", blocks-j, ptr);
360332
 #endif
360332
 			i = tar_block_read(t, ptr);
360332
 			if (i != T_BLOCKSIZE)
360332
-- 
360332
1.7.1
360332