aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-12-21 13:37:51 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-12-21 13:37:51 -0300
commit5853c37a83ec66ccb45094f9aeac23dfdbcde671 (patch)
tree403a884f463dc02a64b85a8b454a6e5386109376
parent842a83f09caa2ebd4bc03e0076420148ac07c808 (diff)
downloadlua-5853c37a83ec66ccb45094f9aeac23dfdbcde671.tar.gz
lua-5853c37a83ec66ccb45094f9aeac23dfdbcde671.tar.bz2
lua-5853c37a83ec66ccb45094f9aeac23dfdbcde671.zip
Bug: Buffer overflow in string concatenation
Even if the string fits in size_t, the whole size of the TString object can overflow when we add the header.
-rw-r--r--lstring.c2
-rw-r--r--lvm.c2
2 files changed, 2 insertions, 2 deletions
diff --git a/lstring.c b/lstring.c
index e921dd0f..97757355 100644
--- a/lstring.c
+++ b/lstring.c
@@ -224,7 +224,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
224 return internshrstr(L, str, l); 224 return internshrstr(L, str, l);
225 else { 225 else {
226 TString *ts; 226 TString *ts;
227 if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) 227 if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
228 luaM_toobig(L); 228 luaM_toobig(L);
229 ts = luaS_createlngstrobj(L, l); 229 ts = luaS_createlngstrobj(L, l);
230 memcpy(getlngstr(ts), str, l * sizeof(char)); 230 memcpy(getlngstr(ts), str, l * sizeof(char));
diff --git a/lvm.c b/lvm.c
index 4d71cfff..918ae64c 100644
--- a/lvm.c
+++ b/lvm.c
@@ -661,7 +661,7 @@ void luaV_concat (lua_State *L, int total) {
661 /* collect total length and number of strings */ 661 /* collect total length and number of strings */
662 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { 662 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
663 size_t l = tsslen(tsvalue(s2v(top - n - 1))); 663 size_t l = tsslen(tsvalue(s2v(top - n - 1)));
664 if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { 664 if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) {
665 L->top.p = top - total; /* pop strings to avoid wasting stack */ 665 L->top.p = top - total; /* pop strings to avoid wasting stack */
666 luaG_runerror(L, "string length overflow"); 666 luaG_runerror(L, "string length overflow");
667 } 667 }