Skip to content
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

Fix use-after-free in error message construction #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,15 +1487,17 @@ static const char *auxL_pusherror(lua_State *L, int error, const char *fun) {
file = path;
}

ERR_clear_error();

ERR_error_string_n(code, txt, sizeof txt);

if (fun) {
return lua_pushfstring(L, "%s: %s:%d:%s", fun, file, line, txt);
lua_pushfstring(L, "%s: %s:%d:%s", fun, file, line, txt);
} else {
return lua_pushfstring(L, "%s:%d:%s", file, line, txt);
lua_pushfstring(L, "%s:%d:%s", file, line, txt);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lua_pushfstring can fail (i.e. longjmp out). We need to clear the OpenSSL error before calling it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather take a potential longjmp (due to memory allocation failure) over consistently using released memory (the current situation).

Nevertheless, I'm happy to amend the patch. But what do you suggest? Allocating a temporary buffer to hold the string also has the potential to fail if lua_pushfstring() may fail. Then what?

I haven't dug deeply into the OpenSSL error handling API docs, but as far as I can tell, if we don't clear the error it just remains on the stack, it doesn't actually "leak".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the temporary (stack-allocated) buffer txt. ERR_error_string_n copies into that.
So the patch really only needs to swap the order of ERR_clear_error and ERR_error_string_n

}

ERR_clear_error();

return 1;
#if HAVE_DLADDR
} else if (error == auxL_EDYLD) {
const char *const fmt = (fun)? "%s: %s" : "%.0s%s";
Expand Down