Commit c958bb1f

c958bb1fcd4fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(runtime): heap-use-after-free in sp_String_new(s) when s is a heap string

sp_String_new(s) called sp_gc_alloc first, then read strlen(s) and memcpy(...,s,...). If s was a heap-allocated string (sp_str_alloc'd — e.g. the result of sp_str_repeat) whose only liveness anchor was the C stack parameter, sp_gc_alloc could trigger sp_gc_collect → sp_str_sweep, freeing s mid-call. The post-alloc reads then dereferenced freed memory.

ASAN catches this immediately on Linux when GC fires during the call. The crash was non-deterministic depending on the allocator's free-region reuse policy: glibc's malloc tended to leave the bytes intact long enough for the read to return stale-but-correct data, so existing tests passed. Windows MinGW's allocator reuses freed regions faster, surfacing the bug as a hard crash — that's the trace observed under PR #215's bound_method_gc.rb on the windows- mingw runner.

Reorder: snapshot strlen + raw-malloc + memcpy from s BEFORE sp_gc_alloc. The malloc'd buffer is on neither the GC heap nor the string heap, so it survives any GC the subsequent sp_gc_alloc may trigger; we then attach it to the freshly allocated sp_String.

Verified: ASAN reproducer (10000.times { Filler.new; String.new ("garbage" * 100) }) crashes pre-fix at sp_String_new:550, runs clean post-fix. Full suite + bootstrap stable.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
c958bb1fcd4f
Parent
a9b2d9118f8b

lib/sp_runtime.hmodifiedView file

@@ -547,7 +547,25 @@
547547
548548 typedef struct{char*data;int64_t len;int64_t cap;}sp_String;
549549 static void sp_String_fin(void*p){free(((sp_String*)p)->data);}
550-static sp_String*sp_String_new(const char*s){sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL);r->len=(int64_t)strlen(s);r->cap=r->len*2+16;r->data=(char*)malloc(r->cap+1);memcpy(r->data,s,r->len+1);{sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+1;sp_gc_bytes+=r->cap+1;}return r;}
550+static sp_String*sp_String_new(const char*s){
551+ /* Copy s's payload into a raw-malloc'd buffer BEFORE sp_gc_alloc.
552+ If s is a heap string (sp_str_alloc) whose only liveness anchor
553+ is this C stack frame, sp_gc_alloc can trigger sp_gc_collect →
554+ sp_str_sweep, which would free s mid-call; a post-alloc strlen +
555+ memcpy on s would then read freed memory (heap-use-after-free
556+ under ASAN, non-deterministic crash on Windows MinGW where the
557+ allocator reuses freed regions faster). The malloc'd buffer is
558+ not on the GC heap and not on the string heap, so it survives
559+ any GC inside sp_gc_alloc. */
560+ int64_t len=(int64_t)strlen(s);
561+ int64_t cap=len*2+16;
562+ char*data=(char*)malloc(cap+1);
563+ memcpy(data,s,len+1);
564+ sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL);
565+ r->len=len;r->cap=cap;r->data=data;
566+ {sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+1;sp_gc_bytes+=r->cap+1;}
567+ return r;
568+}
551569 static inline void sp_String_append(sp_String*s,const char*t){int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));sp_gc_bytes-=s->cap+1;h->size-=s->cap+1;s->cap=(s->len+tl)*2+16;s->data=(char*)realloc(s->data,s->cap+1);h->size+=s->cap+1;sp_gc_bytes+=s->cap+1;}memcpy(s->data+s->len,t,tl+1);s->len+=tl;}
552570 static inline void sp_String_prepend(sp_String*s,const char*t){int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));sp_gc_bytes-=s->cap+1;h->size-=s->cap+1;s->cap=(s->len+tl)*2+16;s->data=(char*)realloc(s->data,s->cap+1);h->size+=s->cap+1;sp_gc_bytes+=s->cap+1;}memmove(s->data+tl,s->data,s->len+1);memcpy(s->data,t,tl);s->len+=tl;}
553571 static inline const char*sp_String_cstr(sp_String*s){return s->data;}