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