a82cae
From c34e1dd29983e5d36d367462b9b4b4b8fcd5a0f8 Mon Sep 17 00:00:00 2001
a82cae
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
a82cae
Date: Mon, 6 Feb 2017 15:13:41 +0100
a82cae
Subject: [PATCH] Fix stack buffer overflow in deserialization of hooks.
a82cae
MIME-Version: 1.0
a82cae
Content-Type: text/plain; charset=UTF-8
a82cae
Content-Transfer-Encoding: 8bit
a82cae
a82cae
Ported from perl:
a82cae
a82cae
commit 3e998ddfb597cfae7bdb460b22e6c50440b1de92
a82cae
Author: John Lightsey <jd@cpanel.net>
a82cae
Date:   Tue Jan 24 10:30:18 2017 -0600
a82cae
a82cae
    Fix stack buffer overflow in deserialization of hooks.
a82cae
a82cae
    The use of signed lengths resulted in a stack overflow in retrieve_hook()
a82cae
    when a negative length was provided in the storable data.
a82cae
a82cae
    The retrieve_blessed() codepath had a similar problem with the placement
a82cae
    of the trailing null byte when negative lengths were provided.
a82cae
a82cae
Signed-off-by: Petr Písař <ppisar@redhat.com>
a82cae
---
a82cae
 Storable.xs | 11 +++++++++--
a82cae
 t/store.t   | 12 +++++++++++-
a82cae
 2 files changed, 20 insertions(+), 3 deletions(-)
a82cae
a82cae
diff --git a/Storable.xs b/Storable.xs
a82cae
index bc15d1d..3cce3ed 100644
a82cae
--- a/Storable.xs
a82cae
+++ b/Storable.xs
a82cae
@@ -4016,7 +4016,7 @@ static SV *retrieve_idx_blessed(pTHX_ stcxt_t *cxt, const char *cname)
a82cae
  */
a82cae
 static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname)
a82cae
 {
a82cae
-	I32 len;
a82cae
+	U32 len;
a82cae
 	SV *sv;
a82cae
 	char buf[LG_BLESS + 1];		/* Avoid malloc() if possible */
a82cae
 	char *classname = buf;
a82cae
@@ -4037,6 +4037,9 @@ static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname)
a82cae
 	if (len & 0x80) {
a82cae
 		RLEN(len);
a82cae
 		TRACEME(("** allocating %d bytes for class name", len+1));
a82cae
+		if (len > I32_MAX) {
a82cae
+			CROAK(("Corrupted classname length"));
a82cae
+		}
a82cae
 		New(10003, classname, len+1, char);
a82cae
 		malloced_classname = classname;
a82cae
 	}
a82cae
@@ -4087,7 +4090,7 @@ static SV *retrieve_blessed(pTHX_ stcxt_t *cxt, const char *cname)
a82cae
  */
a82cae
 static SV *retrieve_hook(pTHX_ stcxt_t *cxt, const char *cname)
a82cae
 {
a82cae
-	I32 len;
a82cae
+	U32 len;
a82cae
 	char buf[LG_BLESS + 1];		/* Avoid malloc() if possible */
a82cae
 	char *classname = buf;
a82cae
 	unsigned int flags;
a82cae
@@ -4221,6 +4224,10 @@ static SV *retrieve_hook(pTHX_ stcxt_t *cxt, const char *cname)
a82cae
 		else
a82cae
 			GETMARK(len);
a82cae
 
a82cae
+		if (len > I32_MAX) {
a82cae
+			CROAK(("Corrupted classname length"));
a82cae
+		}
a82cae
+
a82cae
 		if (len > LG_BLESS) {
a82cae
 			TRACEME(("** allocating %d bytes for class name", len+1));
a82cae
 			New(10003, classname, len+1, char);
a82cae
diff --git a/t/store.t b/t/store.t
a82cae
index be43299..1cbf021 100644
a82cae
--- a/t/store.t
a82cae
+++ b/t/store.t
a82cae
@@ -19,7 +19,7 @@ sub BEGIN {
a82cae
 
a82cae
 use Storable qw(store retrieve store_fd nstore_fd fd_retrieve);
a82cae
 
a82cae
-use Test::More tests => 21;
a82cae
+use Test::More tests => 22;
a82cae
 
a82cae
 $a = 'toto';
a82cae
 $b = \$a;
a82cae
@@ -87,5 +87,15 @@ is(&dump($r), &dump(\%a));
a82cae
 eval { $r = fd_retrieve(::OUT); };
a82cae
 isnt($@, '');
a82cae
 
a82cae
+{
a82cae
+
a82cae
+    my $frozen =
a82cae
+      "\x70\x73\x74\x30\x04\x0a\x08\x31\x32\x33\x34\x35\x36\x37\x38\x04\x08\x08\x08\x03\xff\x00\x00\x00\x19\x08\xff\x00\x00\x00\x08\x08\xf9\x16\x16\x13\x16\x10\x10\x10\xff\x15\x16\x16\x16\x1e\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x13\xf0\x16\x16\x16\xfe\x16\x41\x41\x41\x41\xe8\x03\x41\x41\x41\x41\x41\x41\x41\x41\x51\x41\xa9\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xb8\xac\xac\xac\xac\xac\xac\xac\xac\x9a\xac\xac\xac\xac\xac\xac\xac\xac\xac\x93\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\x00\x64\xac\xa8\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\xac\x2c\xac\x41\x41\x41\x41\x41\x41\x41\x41\x41\x00\x80\x41\x80\x41\x41\x41\x41\x41\x41\x51\x41\xac\xac\xac";
a82cae
+    open my $fh, '<', \$frozen;
a82cae
+    eval { Storable::fd_retrieve($fh); };
a82cae
+    pass('RT 130635:  no stack smashing error when retrieving hook');
a82cae
+
a82cae
+}
a82cae
+
a82cae
 close OUT or die "Could not close: $!";
a82cae
 END { 1 while unlink 'store' }
a82cae
-- 
a82cae
2.7.4
a82cae