Skip to content

Commit 0718b37

Browse files
committed
objstack: assert that the alloc size will fit within a chunk
to prevent a buffer overflow Bug found using OSS-Fuze.
1 parent 7a40501 commit 0718b37

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Diff for: common/objstack.hpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "parm_string.hpp"
66
#include <stdlib.h>
77
#include <assert.h>
8+
#include <stddef.h>
89

910
namespace acommon {
1011

@@ -26,6 +27,12 @@ class ObjStack
2627
byte * temp_end;
2728
void setup_chunk();
2829
void new_chunk();
30+
bool will_overflow(size_t sz) const {
31+
return offsetof(Node,data) + sz > chunk_size;
32+
}
33+
void check_size(size_t sz) {
34+
assert(!will_overflow(sz));
35+
}
2936

3037
ObjStack(const ObjStack &);
3138
void operator=(const ObjStack &);
@@ -56,7 +63,7 @@ class ObjStack
5663
void * alloc_bottom(size_t size) {
5764
byte * tmp = bottom;
5865
bottom += size;
59-
if (bottom > top) {new_chunk(); tmp = bottom; bottom += size;}
66+
if (bottom > top) {check_size(size); new_chunk(); tmp = bottom; bottom += size;}
6067
return tmp;
6168
}
6269
// This alloc_bottom will insure that the object is aligned based on the
@@ -66,7 +73,7 @@ class ObjStack
6673
align_bottom(align);
6774
byte * tmp = bottom;
6875
bottom += size;
69-
if (bottom > top) {new_chunk(); goto loop;}
76+
if (bottom > top) {check_size(size); new_chunk(); goto loop;}
7077
return tmp;
7178
}
7279
char * dup_bottom(ParmString str) {
@@ -79,7 +86,7 @@ class ObjStack
7986
// always be aligned as such.
8087
void * alloc_top(size_t size) {
8188
top -= size;
82-
if (top < bottom) {new_chunk(); top -= size;}
89+
if (top < bottom) {check_size(size); new_chunk(); top -= size;}
8390
return top;
8491
}
8592
// This alloc_top will insure that the object is aligned based on
@@ -88,7 +95,7 @@ class ObjStack
8895
{loop:
8996
top -= size;
9097
align_top(align);
91-
if (top < bottom) {new_chunk(); goto loop;}
98+
if (top < bottom) {check_size(size); new_chunk(); goto loop;}
9299
return top;
93100
}
94101
char * dup_top(ParmString str) {
@@ -117,6 +124,7 @@ class ObjStack
117124
void * alloc_temp(size_t size) {
118125
temp_end = bottom + size;
119126
if (temp_end > top) {
127+
check_size(size);
120128
new_chunk();
121129
temp_end = bottom + size;
122130
}
@@ -131,6 +139,7 @@ class ObjStack
131139
} else {
132140
size_t s = temp_end - bottom;
133141
byte * p = bottom;
142+
check_size(size);
134143
new_chunk();
135144
memcpy(bottom, p, s);
136145
temp_end = bottom + size;
@@ -150,6 +159,7 @@ class ObjStack
150159
} else {
151160
size_t s = temp_end - bottom;
152161
byte * p = bottom;
162+
check_size(size);
153163
new_chunk();
154164
memcpy(bottom, p, s);
155165
temp_end = bottom + size;

0 commit comments

Comments
 (0)