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

Conversation

mwild1
Copy link
Contributor

@mwild1 mwild1 commented Jul 8, 2024

Calling ERR_clear_error() releases the buffers that hold the path/filename, so we need to push those to Lua (which will copy them) before they are released.

The problem can be verified with valgrind, or indeed just by running it on my machine which shows random memory contents prefixed to the error message string.

Calling ERR_clear_error() releases the buffers that hold the path/filename, so
we need to push those to Lua (which will copy them) before they are released.

The problem can be verified with valgrind, or indeed just by running it on my
machine which shows random memory contents prefixed to the error message
string.
} 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants