Fix buffer overread in ascii formatter

When a text with an (escaped) zero byte was passed to ParseValue, only
the part of the string up to the zero byte was copied, but the length of
the full string was passed to the input framework.

This leads to the input manager reading over the end of the buffer.

Fixes zeek/zeek#1398
This commit is contained in:
Johanna Amann 2021-02-12 14:16:25 +00:00
parent 021a31b29a
commit 61290fc19c
3 changed files with 7 additions and 2 deletions

View file

@ -225,7 +225,9 @@ Value* Ascii::ParseValue(const string& s, const string& name, TypeTag type, Type
{ {
string unescaped = util::get_unescaped_string(s); string unescaped = util::get_unescaped_string(s);
val->val.string_val.length = unescaped.size(); val->val.string_val.length = unescaped.size();
val->val.string_val.data = util::copy_string(unescaped.c_str()); val->val.string_val.data = new char[val->val.string_val.length];
// we do not need a zero-byte at the end - the input manager adds that explicitly
memcpy(val->val.string_val.data, unescaped.data(), unescaped.size());
break; break;
} }

View file

@ -5,3 +5,5 @@ abc|\xffdef
DATA2 DATA2
abc\xff|def abc\xff|def
DATA2 DATA2
abc\x00\x00\x00\xff|def
DATA3

View file

@ -21,6 +21,7 @@ redef InputAscii::unset_field = "-";
abc\x0a\xffdef|DATA2 abc\x0a\xffdef|DATA2
abc\x7c\xffdef|DATA2 abc\x7c\xffdef|DATA2
abc\xff\x7cdef|DATA2 abc\xff\x7cdef|DATA2
abc\x00\x00\x00\xff\x7cdef|DATA3
#end|2012-07-20-01-49-19 #end|2012-07-20-01-49-19
@TEST-END-FILE @TEST-END-FILE
@ -37,7 +38,7 @@ event line(description: Input::EventDescription, tpe: Input::Event, a: string, b
print outfile, a; print outfile, a;
print outfile, b; print outfile, b;
try = try + 1; try = try + 1;
if ( try == 3 ) if ( try == 4 )
{ {
Input::remove("input"); Input::remove("input");
close(outfile); close(outfile);