Skip to content

Commit 49e746e

Browse files
committed
Fix use-after-free in error message construction
Calling `ERR_clear_error()` releases the buffers that hold the path/filename, so we need to copy those to a stack allocated buffer before they are released. Closes #217
1 parent ac59988 commit 49e746e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/openssl.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,7 @@ static const char *auxL_pusherror(lua_State *L, int error, const char *fun) {
14771477
const char *path, *file;
14781478
int line;
14791479
char txt[256];
1480+
char prefix[256];
14801481

14811482
if (!ERR_peek_error())
14821483
return lua_pushliteral(L, "oops: no OpenSSL errors set");
@@ -1493,15 +1494,16 @@ static const char *auxL_pusherror(lua_State *L, int error, const char *fun) {
14931494
file = path;
14941495
}
14951496

1496-
ERR_clear_error();
1497-
1498-
ERR_error_string_n(code, txt, sizeof txt);
1499-
15001497
if (fun) {
1501-
return lua_pushfstring(L, "%s: %s:%d:%s", fun, file, line, txt);
1498+
snprintf(prefix, sizeof prefix, "%s: %s:%d", fun, file, line);
15021499
} else {
1503-
return lua_pushfstring(L, "%s:%d:%s", file, line, txt);
1500+
snprintf(prefix, sizeof prefix, "%s:%d", file, line);
15041501
}
1502+
1503+
ERR_error_string_n(code, txt, sizeof txt);
1504+
ERR_clear_error();
1505+
1506+
return lua_pushfstring(L, "%s:%s", prefix, txt);
15051507
#if HAVE_DLADDR
15061508
} else if (error == auxL_EDYLD) {
15071509
const char *const fmt = (fun)? "%s: %s" : "%.0s%s";

0 commit comments

Comments
 (0)