From: Dmitriy Vyukov Date: Mon, 27 Jan 2014 16:29:21 +0000 (+0400) Subject: runtime: fix buffer overflow in stringtoslicerune X-Git-Tag: go1.3beta1~869 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e1a91c5b8963e3e02c897f96218d4eae17bcb740;p=gostls13.git runtime: fix buffer overflow in stringtoslicerune On 32-bits n*sizeof(r[0]) can overflow. Or it can become 1<<32-eps, and mallocgc will "successfully" allocate 0 pages for it, there are no checks downstream and MHeap_Grow just does: npage = (npage+15)&~15; ask = npage<> PageShift; if((size & PageMask) != 0) npages++; diff --git a/src/pkg/runtime/string.goc b/src/pkg/runtime/string.goc index 407188cfe6..a46fa5d8d2 100644 --- a/src/pkg/runtime/string.goc +++ b/src/pkg/runtime/string.goc @@ -334,6 +334,8 @@ func stringtoslicerune(s String) (b Slice) { n++; } + if(n > MaxMem/sizeof(r[0])) + runtime·throw("out of memory"); mem = runtime·roundupsize(n*sizeof(r[0])); b.array = runtime·mallocgc(mem, 0, FlagNoScan|FlagNoZero); b.len = n;