Skip to content

Commit 65c9b28

Browse files
authored
Fix buffer overflow in os.tmpname (#1180)
At least on macOS, `strlen(getenv("TMPDIR"))` is 50. We now allow a /tmp that takes up to 120 or so bytes to spell. Instead of overflowing, we do a bounds check and the function fails successfully on even longer /tmps. Fixes #1108 (os.tmpname crashes redbean)
1 parent 4292348 commit 65c9b28

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

third_party/lua/README.cosmo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ LOCAL MODIFICATIONS
3636
Added Python-like printf modulus operator for strings.
3737

3838
Added Python-like printf multiply operator for strings.
39+
40+
Fixed a buffer overflow in os.tmpname

third_party/lua/loslib.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ __static_yoink("lua_notice");
133133

134134
#if defined(LUA_USE_POSIX) /* { */
135135

136-
#define LUA_TMPNAMBUFSIZE 32
136+
#define LUA_TMPNAMBUFSIZE 128
137137

138138
#define lua_tmpnam(b,e) { \
139-
strcpy(b, __get_tmpdir()); \
140-
strcat(b, "lua_XXXXXX"); \
141-
e = mkstemp(b); \
139+
strlcpy(b, __get_tmpdir(), LUA_TMPNAMBUFSIZE); \
140+
e = strlcat(b, "lua_XXXXXX", LUA_TMPNAMBUFSIZE) >= LUA_TMPNAMBUFSIZE; \
141+
e = e ? -1 : mkstemp(b); \
142142
if (e != -1) close(e); \
143143
e = (e == -1); }
144144

0 commit comments

Comments
 (0)