-
Notifications
You must be signed in to change notification settings - Fork 31k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: fix stack-buffer overflow for long exception lines #2404
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1317,8 +1317,6 @@ void AppendExceptionLine(Environment* env, | |
err_obj->SetHiddenValue(env->processed_string(), True(env->isolate())); | ||
} | ||
|
||
char arrow[1024]; | ||
|
||
// Print (filename):(line number): (message). | ||
node::Utf8Value filename(env->isolate(), message->GetScriptResourceName()); | ||
const char* filename_string = *filename; | ||
|
@@ -1351,34 +1349,38 @@ void AppendExceptionLine(Environment* env, | |
int start = message->GetStartColumn(); | ||
int end = message->GetEndColumn(); | ||
|
||
char arrow[1024]; | ||
int max_off = sizeof(arrow) - 2; | ||
|
||
int off = snprintf(arrow, | ||
sizeof(arrow), | ||
"%s:%i\n%s\n", | ||
filename_string, | ||
linenum, | ||
sourceline_string); | ||
CHECK_GE(off, 0); | ||
if (off > max_off) { | ||
off = max_off; | ||
} | ||
|
||
// Print wavy underline (GetUnderline is deprecated). | ||
for (int i = 0; i < start; i++) { | ||
if (sourceline_string[i] == '\0' || | ||
static_cast<size_t>(off) >= sizeof(arrow)) { | ||
if (sourceline_string[i] == '\0' || off >= max_off) { | ||
break; | ||
} | ||
CHECK_LT(static_cast<size_t>(off), sizeof(arrow)); | ||
CHECK_LT(off, max_off); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this redundant, as you have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only replaced asserts from before |
||
arrow[off++] = (sourceline_string[i] == '\t') ? '\t' : ' '; | ||
} | ||
for (int i = start; i < end; i++) { | ||
if (sourceline_string[i] == '\0' || | ||
static_cast<size_t>(off) >= sizeof(arrow)) { | ||
if (sourceline_string[i] == '\0' || off >= max_off) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous loop is not always executed |
||
break; | ||
} | ||
CHECK_LT(static_cast<size_t>(off), sizeof(arrow)); | ||
CHECK_LT(off, max_off); | ||
arrow[off++] = '^'; | ||
} | ||
CHECK_LE(static_cast<size_t>(off - 1), sizeof(arrow) - 1); | ||
arrow[off++] = '\n'; | ||
arrow[off] = '\0'; | ||
CHECK_LE(off, max_off); | ||
arrow[off] = '\n'; | ||
arrow[off + 1] = '\0'; | ||
|
||
Local<String> arrow_str = String::NewFromUtf8(env->isolate(), arrow); | ||
Local<Value> msg; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can still cause an assertion to be raised when
sourceline_string
is really long andsnprintf()
tries to write pastsizeof(arrow)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the total length of all of the strings being printed to
arrow
exceedssizeof(arrow)
,snprintf()
will truncate the output and return-1
to indicate that truncation has happened. When that happens (like in #2581),CHECK_GE(off, 0)
will trigger and cause a C++ assertion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snprintf returns
-1
only on an encoding error else it will return the number of characters that would have been written if if n had been sufficiently large. In #2581 it triggers at https://github.com/nodejs/node/blob/master/src/node.cc#L1379Based on https://groups.google.com/d/msg/comp.std.c/lIvkxXr5_wE/aVuIpm52ToQJ for an encoding error you have to use
mbrtowc()
orwcrtomb()
, directly or indirectly which you only do with "%lc" or "%ls" format specifiers. I think in that case an assert is enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm testing on Windows where a custom
snprintf()
is used which internally uses_vsprintf_p()
which MSDN says:So on Windows, exceeding
sizeof(arrow)
must constitute an "output error" and that's why I'm seeingoff == -1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the custom snprintf should be replaced with something like this:
The _TRUNCATE negates the non-standard -1 behaviour and VS 2015 has
snprintf.