diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-08 13:23:58 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-08 13:23:58 -0300 |
| commit | 9b854e6dbcf569113f68e63d87644b69eb00a228 (patch) | |
| tree | 11eb6b8f2bc3fe0e9138a7eea97ac84c343280ff | |
| parent | 2b2d8ecd7aba6cc7604532c53372db01a30618d3 (diff) | |
| download | lua-9b854e6dbcf569113f68e63d87644b69eb00a228.tar.gz lua-9b854e6dbcf569113f68e63d87644b69eb00a228.tar.bz2 lua-9b854e6dbcf569113f68e63d87644b69eb00a228.zip | |
BUG: string concatenation may cause arithmetic overflow, leading
to a buffer overflow.
| -rw-r--r-- | bugs | 36 | ||||
| -rw-r--r-- | lvm.c | 7 |
2 files changed, 40 insertions, 3 deletions
| @@ -633,3 +633,39 @@ patch = [[ | |||
| 633 | ]], | 633 | ]], |
| 634 | 634 | ||
| 635 | } | 635 | } |
| 636 | |||
| 637 | |||
| 638 | |||
| 639 | ----------------------------------------------------------------- | ||
| 640 | -- Lua 5.0.2 | ||
| 641 | |||
| 642 | Bug{ | ||
| 643 | what = [[string concatenation may cause arithmetic overflow, leading | ||
| 644 | to a buffer overflow]], | ||
| 645 | |||
| 646 | report = [[Rici Lake, 20/05/2004]], | ||
| 647 | |||
| 648 | example = [[ | ||
| 649 | longs = string.rep("\0", 2^25) | ||
| 650 | function catter(i) | ||
| 651 | return assert(loadstring( | ||
| 652 | string.format("return function(a) return a%s end", | ||
| 653 | string.rep("..a", i-1))))() | ||
| 654 | end | ||
| 655 | rep129 = catter(129) | ||
| 656 | rep129(longs) | ||
| 657 | ]], | ||
| 658 | |||
| 659 | patch = [[ | ||
| 660 | * lvm.c: | ||
| 661 | 329c329,331 | ||
| 662 | < tl += tsvalue(top-n-1)->tsv.len; | ||
| 663 | --- | ||
| 664 | > size_t l = tsvalue(top-n-1)->tsv.len; | ||
| 665 | > if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); | ||
| 666 | > tl += l; | ||
| 667 | 332d333 | ||
| 668 | < if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow"); | ||
| 669 | ]] | ||
| 670 | } | ||
| 671 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.7 2004/05/31 18:51:50 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.8 2004/06/02 19:07:55 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -308,10 +308,11 @@ void luaV_concat (lua_State *L, int total, int last) { | |||
| 308 | char *buffer; | 308 | char *buffer; |
| 309 | int i; | 309 | int i; |
| 310 | while (n < total && tostring(L, top-n-1)) { /* collect total length */ | 310 | while (n < total && tostring(L, top-n-1)) { /* collect total length */ |
| 311 | tl += tsvalue(top-n-1)->len; | 311 | size_t l = tsvalue(top-n-1)->len; |
| 312 | if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); | ||
| 313 | tl += l; | ||
| 312 | n++; | 314 | n++; |
| 313 | } | 315 | } |
| 314 | if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow"); | ||
| 315 | buffer = luaZ_openspace(L, &G(L)->buff, tl); | 316 | buffer = luaZ_openspace(L, &G(L)->buff, tl); |
| 316 | tl = 0; | 317 | tl = 0; |
| 317 | for (i=n; i>0; i--) { /* concat all strings */ | 318 | for (i=n; i>0; i--) { /* concat all strings */ |
