Skip to content
Permalink
Browse files Browse the repository at this point in the history
objstack: assert that the alloc size will fit within a chunk
to prevent a buffer overflow

Bug found using OSS-Fuze.
  • Loading branch information
kevina committed Dec 21, 2019
1 parent 7a40501 commit 0718b37
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions common/objstack.hpp
Expand Up @@ -5,6 +5,7 @@
#include "parm_string.hpp"
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>

namespace acommon {

Expand All @@ -26,6 +27,12 @@ class ObjStack
byte * temp_end;
void setup_chunk();
void new_chunk();
bool will_overflow(size_t sz) const {
return offsetof(Node,data) + sz > chunk_size;
}
void check_size(size_t sz) {
assert(!will_overflow(sz));

This comment has been minimized.

Copy link
@johnmcfarlane

johnmcfarlane Jul 27, 2021

What happens if NDEBUG is defined before <assert.h> is included?

}

ObjStack(const ObjStack &);
void operator=(const ObjStack &);
Expand Down Expand Up @@ -56,7 +63,7 @@ class ObjStack
void * alloc_bottom(size_t size) {
byte * tmp = bottom;
bottom += size;
if (bottom > top) {new_chunk(); tmp = bottom; bottom += size;}
if (bottom > top) {check_size(size); new_chunk(); tmp = bottom; bottom += size;}
return tmp;
}
// This alloc_bottom will insure that the object is aligned based on the
Expand All @@ -66,7 +73,7 @@ class ObjStack
align_bottom(align);
byte * tmp = bottom;
bottom += size;
if (bottom > top) {new_chunk(); goto loop;}
if (bottom > top) {check_size(size); new_chunk(); goto loop;}
return tmp;
}
char * dup_bottom(ParmString str) {
Expand All @@ -79,7 +86,7 @@ class ObjStack
// always be aligned as such.
void * alloc_top(size_t size) {
top -= size;
if (top < bottom) {new_chunk(); top -= size;}
if (top < bottom) {check_size(size); new_chunk(); top -= size;}
return top;
}
// This alloc_top will insure that the object is aligned based on
Expand All @@ -88,7 +95,7 @@ class ObjStack
{loop:
top -= size;
align_top(align);
if (top < bottom) {new_chunk(); goto loop;}
if (top < bottom) {check_size(size); new_chunk(); goto loop;}
return top;
}
char * dup_top(ParmString str) {
Expand Down Expand Up @@ -117,6 +124,7 @@ class ObjStack
void * alloc_temp(size_t size) {
temp_end = bottom + size;
if (temp_end > top) {
check_size(size);
new_chunk();
temp_end = bottom + size;
}
Expand All @@ -131,6 +139,7 @@ class ObjStack
} else {
size_t s = temp_end - bottom;
byte * p = bottom;
check_size(size);
new_chunk();
memcpy(bottom, p, s);
temp_end = bottom + size;
Expand All @@ -150,6 +159,7 @@ class ObjStack
} else {
size_t s = temp_end - bottom;
byte * p = bottom;
check_size(size);
new_chunk();
memcpy(bottom, p, s);
temp_end = bottom + size;
Expand Down

0 comments on commit 0718b37

Please sign in to comment.