aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Stoeckmann <tobias@stoeckmann.org>2019-04-09 11:59:20 -0400
committerNick Mathewson <nickm@torproject.org>2019-04-09 11:59:20 -0400
commit74b2bc43fbe61e3a04fe3f5cc9f817be307e13e1 (patch)
treecd777fac1c17802f2319fea0c639659bf1f67c31
parenta0db5ade3e1e2b81ed1196eb98aeca53583fba64 (diff)
downloadtor-74b2bc43fbe61e3a04fe3f5cc9f817be307e13e1.tar.gz
tor-74b2bc43fbe61e3a04fe3f5cc9f817be307e13e1.zip
Protect buffers against INT_MAX datalen overflows.
Many buffer functions have a hard limit of INT_MAX for datalen, but this limitation is not enforced in all functions: - buf_move_all may exceed that limit with too many chunks - buf_move_to_buf exceeds that limit with invalid buf_flushlen argument - buf_new_with_data may exceed that limit (unit tests only) This patch adds some annotations in some buf_pos_t functions to guarantee that no out of boundary access could occur even if another function lacks safe guards against datalen overflows. [This is a backport of the submitted patch to 0.2.9, where the buf_move_to_buf and buf_new_with_data functions did not exist.]
-rw-r--r--src/or/buffers.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 89382d1d8e..394ba0ccb8 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -394,6 +394,10 @@ buf_free(buf_t *buf)
{
if (!buf)
return;
+ if (BUG(buf_out->datalen >= INT_MAX || buf_in->datalen >= INT_MAX))
+ return;
+ if (BUG(buf_out->datalen >= INT_MAX - buf_in->datalen))
+ return;
buf_clear(buf);
buf->magic = 0xdeadbeef;
@@ -1034,6 +1038,7 @@ buf_find_pos_of_char(char ch, buf_pos_t *out)
static inline int
buf_pos_inc(buf_pos_t *pos)
{
+ tor_assert(pos->pos < INT_MAX - 1);
++pos->pos;
if (pos->pos == (off_t)pos->chunk->datalen) {
if (!pos->chunk->next)
@@ -1925,6 +1930,7 @@ buf_find_offset_of_char(buf_t *buf, char ch)
{
chunk_t *chunk;
off_t offset = 0;
+ tor_assert(buf->datalen < INT_MAX);
for (chunk = buf->head; chunk; chunk = chunk->next) {
char *cp = memchr(chunk->data, ch, chunk->datalen);
if (cp)
@@ -2044,6 +2050,7 @@ assert_buf_ok(buf_t *buf)
for (ch = buf->head; ch; ch = ch->next) {
total += ch->datalen;
tor_assert(ch->datalen <= ch->memlen);
+ tor_assert(ch->datalen < INT_MAX);
tor_assert(ch->data >= &ch->mem[0]);
tor_assert(ch->data <= &ch->mem[0]+ch->memlen);
if (ch->data == &ch->mem[0]+ch->memlen) {