Blame SOURCES/aspell-0.60.6.1-CVE-2019-25051.patch

3e0092
From 0718b375425aad8e54e1150313b862e4c6fd324a Mon Sep 17 00:00:00 2001
3e0092
From: Kevin Atkinson <kevina@gnu.org>
3e0092
Date: Sat, 21 Dec 2019 20:32:47 +0000
3e0092
Subject: [PATCH] objstack: assert that the alloc size will fit within a chunk
3e0092
 to prevent a buffer overflow
3e0092
3e0092
Bug found using OSS-Fuze.
3e0092
---
3e0092
 common/objstack.hpp | 18 ++++++++++++++----
3e0092
 1 file changed, 14 insertions(+), 4 deletions(-)
3e0092
3e0092
diff --git a/common/objstack.hpp b/common/objstack.hpp
3e0092
index 3997bf7..bd97ccd 100644
3e0092
--- a/common/objstack.hpp
3e0092
+++ b/common/objstack.hpp
3e0092
@@ -5,6 +5,7 @@
3e0092
 #include "parm_string.hpp"
3e0092
 #include <stdlib.h>
3e0092
 #include <assert.h>
3e0092
+#include <stddef.h>
3e0092
 
3e0092
 namespace acommon {
3e0092
 
3e0092
@@ -26,6 +27,12 @@ class ObjStack
3e0092
   byte * temp_end;
3e0092
   void setup_chunk();
3e0092
   void new_chunk();
3e0092
+  bool will_overflow(size_t sz) const {
3e0092
+    return offsetof(Node,data) + sz > chunk_size;
3e0092
+  }
3e0092
+  void check_size(size_t sz) {
3e0092
+    assert(!will_overflow(sz));
3e0092
+  }
3e0092
 
3e0092
   ObjStack(const ObjStack &);
3e0092
   void operator=(const ObjStack &);
3e0092
@@ -56,7 +63,7 @@ class ObjStack
3e0092
   void * alloc_bottom(size_t size)  {
3e0092
     byte * tmp = bottom;
3e0092
     bottom += size;
3e0092
-    if (bottom > top) {new_chunk(); tmp = bottom; bottom += size;}
3e0092
+    if (bottom > top) {check_size(size); new_chunk(); tmp = bottom; bottom += size;}
3e0092
     return tmp;
3e0092
   }
3e0092
   // This alloc_bottom will insure that the object is aligned based on the
3e0092
@@ -66,7 +73,7 @@ class ObjStack
3e0092
     align_bottom(align);
3e0092
     byte * tmp = bottom;
3e0092
     bottom += size;
3e0092
-    if (bottom > top) {new_chunk(); goto loop;}
3e0092
+    if (bottom > top) {check_size(size); new_chunk(); goto loop;}
3e0092
     return tmp;
3e0092
   }
3e0092
   char * dup_bottom(ParmString str) {
3e0092
@@ -79,7 +86,7 @@ class ObjStack
3e0092
   // always be aligned as such.
3e0092
   void * alloc_top(size_t size) {
3e0092
     top -= size;
3e0092
-    if (top < bottom) {new_chunk(); top -= size;}
3e0092
+    if (top < bottom) {check_size(size); new_chunk(); top -= size;}
3e0092
     return top;
3e0092
   }
3e0092
   // This alloc_top will insure that the object is aligned based on
3e0092
@@ -88,7 +95,7 @@ class ObjStack
3e0092
   {loop:
3e0092
     top -= size;
3e0092
     align_top(align);
3e0092
-    if (top < bottom) {new_chunk(); goto loop;}
3e0092
+    if (top < bottom) {check_size(size); new_chunk(); goto loop;}
3e0092
     return top;
3e0092
   }
3e0092
   char * dup_top(ParmString str) {
3e0092
@@ -117,6 +124,7 @@ class ObjStack
3e0092
   void * alloc_temp(size_t size) {
3e0092
     temp_end = bottom + size;
3e0092
     if (temp_end > top) {
3e0092
+      check_size(size);
3e0092
       new_chunk();
3e0092
       temp_end = bottom + size;
3e0092
     }
3e0092
@@ -131,6 +139,7 @@ class ObjStack
3e0092
     } else {
3e0092
       size_t s = temp_end - bottom;
3e0092
       byte * p = bottom;
3e0092
+      check_size(size);
3e0092
       new_chunk();
3e0092
       memcpy(bottom, p, s);
3e0092
       temp_end = bottom + size;
3e0092
@@ -150,6 +159,7 @@ class ObjStack
3e0092
     } else {
3e0092
       size_t s = temp_end - bottom;
3e0092
       byte * p = bottom;
3e0092
+      check_size(size);
3e0092
       new_chunk();
3e0092
       memcpy(bottom, p, s);
3e0092
       temp_end = bottom + size;