From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 06CD36F08C1; Wed, 29 Nov 2023 18:41:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 06CD36F08C1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1701272492; bh=XkZQ8rcyl4jNJCfNvPyYZ+DqGM7xXbveM9orSukKWG0=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Z3059G+x6rxpI/1WrmzHzff10GxQhOV5H0bYanUbPxQK/C2VExjS6Q23kaoohozik xQp7Q+GBAMJfYRmd6t/wuq7CX9SNLdNvHHJDrRSZLxxRBz+tELnMPa8dU+gDrNhwx9 pcwSV8umLOpg0EJVnDU1DaxDuIsWiaOCzS/ojMjg= Received: from smtp31.i.mail.ru (smtp31.i.mail.ru [95.163.41.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id EDFBB6F08C1 for ; Wed, 29 Nov 2023 18:41:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EDFBB6F08C1 Received: by smtp31.i.mail.ru with esmtpa (envelope-from ) id 1r8Mgr-001xt6-13; Wed, 29 Nov 2023 18:41:29 +0300 Message-ID: <9faa1bde-fd48-4dd2-8760-25c308b743a8@tarantool.org> Date: Wed, 29 Nov 2023 18:41:29 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: Sergey Kaplun , Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9D2A6479154BC7F411F120FC51D15E0A97FA15DA56006F231182A05F538085040C6FF843DA05D943A50395ED53E719E19153868B5FF10DCC86FF70728E2B4E786 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE71EEA4C46C73542F4EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637663000D94094A5D68638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80C848B685FF66DADDC01549523645F7F117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE26055571C92BF10F7B089FF177BE8049D8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE32D01283D1ACF37BAAD7EC71F1DB88427C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB861051D4BA689FC2E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F890A246B268E114ECE5475246E174218B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A57A7744F6A5A2E7CD588C79D73590123EC76A1C6C3344E159F87CCE6106E1FC07E67D4AC08A07B9B06A1CB4668A9CA5FACB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF0C20360BC99F3B36D7D5C009B648874D2CA270711791400B959E3417EB7F343AA08A323FE4E4FE84153AE024B6FD08448CB288EE196B8EF740BB226BC690C328E48CAC7CA610320002C26D483E81D6BE0DBAE6F56676BC7117BB6831D7356A2DEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojxXIty/pLbagbo8Wr6H419g== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A7695DFF3C9175749A3E50395ED53E719E19F7F5837242FA16B2EBA65886582A37BD66FEC6BF5C9C28D98A98C1125256619760D574B6FC815AB872D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit 5/6] FFI: Fix dangling reference to CType. Improve checks. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hello, Sergey thanks for the patch! See comments below On 10/23/23 12:22, Sergey Kaplun wrote: > From: Mike Pall > > Reported by elmknot. > > (cherry-picked from commit cc96ab9d513582703f8663a8775a935b56db32b7) > > During the recording of an arithmetic operation with a cdata function > object and some cdata value in `recff_cdata_arith()`, reallocation of > `cts->tab` in `lj_ctype_intern()` may occur. In that case, the reference > to the first `CType` object (`s[0]`) becomes invalid. This patch saves > the `CTypeID` of this object and gets its `CType` again after possible > reallocation. > > Also, this commit fills `cts->tab` memory with zeros before being freed > when `-DLUAJIT_CTYPE_CHECK_ANCHOR` is defined. It helps to observe > assertion failures in case this memory is used after free. failed to reproduce a bug with reverted fix and LuaJIT built with a flag: $ CMAKE_C_FLAGS=-DLUAJIT_CTYPE_CHECK_ANCHOR CFLAGS=-DLUAJIT_CTYPE_CHECK_ANCHOR cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DLUA_USE_ASSERT=ON -DLUAJIT_USE_ASAN=ON $ ./build/src/luajit test/tarantool-tests/lj-920-fix-dangling-reference-to-ctype.test.lua TAP version 13 1..1 ok - no heap-use-after-free in recff_cdata_arith > > Sergey Kaplun: > * added the description and the test for the problem > > Part of tarantool/tarantool#9145 > --- > src/lj_crecord.c | 4 + > src/lj_ctype.c | 12 +++ > ...0-fix-dangling-reference-to-ctype.test.lua | 77 +++++++++++++++++++ > 3 files changed, 93 insertions(+) > create mode 100644 test/tarantool-tests/lj-920-fix-dangling-reference-to-ctype.test.lua > > diff --git a/src/lj_crecord.c b/src/lj_crecord.c > index 10d1dc70..e17e512f 100644 > --- a/src/lj_crecord.c > +++ b/src/lj_crecord.c > @@ -1502,9 +1502,13 @@ void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd) > if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); > goto ok; > } else if (ctype_isfunc(ct->info)) { > + CTypeID id0 = i ? ctype_typeid(cts, s[0]) : 0; > tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR); > ct = ctype_get(cts, > lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); > + if (i) { > + s[0] = ctype_get(cts, id0); /* cts->tab may have been reallocated. */ > + } > goto ok; > } else { > tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata))); > diff --git a/src/lj_ctype.c b/src/lj_ctype.c > index a42e3d60..0874fa61 100644 > --- a/src/lj_ctype.c > +++ b/src/lj_ctype.c > @@ -191,8 +191,20 @@ CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size) > } > id = cts->top; > if (LJ_UNLIKELY(id >= cts->sizetab)) { > +#ifdef LUAJIT_CTYPE_CHECK_ANCHOR > + CType *ct; > +#endif > if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); > +#ifdef LUAJIT_CTYPE_CHECK_ANCHOR > + ct = lj_mem_newvec(cts->L, id+1, CType); > + memcpy(ct, cts->tab, id*sizeof(CType)); > + memset(cts->tab, 0, id*sizeof(CType)); > + lj_mem_freevec(cts->g, cts->tab, cts->sizetab, CType); > + cts->tab = ct; > + cts->sizetab = id+1; > +#else > lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); > +#endif > } > cts->top = id+1; > cts->tab[id].info = info; > diff --git a/test/tarantool-tests/lj-920-fix-dangling-reference-to-ctype.test.lua b/test/tarantool-tests/lj-920-fix-dangling-reference-to-ctype.test.lua > new file mode 100644 > index 00000000..a7e35888 > --- /dev/null > +++ b/test/tarantool-tests/lj-920-fix-dangling-reference-to-ctype.test.lua > @@ -0,0 +1,77 @@ > +local tap = require('tap') > +local ffi = require('ffi') > +local test = tap.test('lj-920-fix-dangling-reference-to-ctype'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > + -- luacheck: no global > + ['Impossible to predict the value of cts->top'] = _TARANTOOL, Let's declare _TARANTOOL as global in .luacheckrc instead of suppressing warning inline every time. About 7 tests suppress it. > +}) > + > +test:plan(1) > + > +-- This test demonstrates LuaJIT's incorrect behaviour when the > +-- reallocation of `cts->tab` strikes during the recording of the > +-- cdata metamethod arithmetic. > +-- The test fails under ASAN. > + > +-- XXX: Just some C functions to be casted. There is no need to > +-- declare their prototypes correctly. > +ffi.cdef[[ > + int malloc(void); > + int fprintf(void); > + int printf(void); > + int memset(void); > + int memcpy(void); > + int memmove(void); > + int getppid(void); > +]] > + > +local cfunc_type = ffi.metatype(ffi.typeof('struct {int a;}'), { > + -- Just some metatable with reloaded arithmetic operator. > + __add = function(o1, _) return o1 end > +}) > +-- Just some cdata with metamethod. > +local test_value = cfunc_type(1) > + > +-- XXX: structure to set `cts->top` to 112. > +local _ = ffi.new('struct {int a; long b; float c; double d;}', 0) > + > +-- Anchor table to prevent cdata objects from being collected. > +local anchor = {} > +-- Each call to this function grows `cts->top` by 3. > +local function save_new_func(func) > + anchor[#anchor + 1] = ffi.cast('void (*)(void)', func) > +end > + > +save_new_func(ffi.C.fprintf) -- `cts->top` = 112 > +save_new_func(ffi.C.printf) -- `cts->top` = 115 > +save_new_func(ffi.C.memset) -- `cts->top` = 118 > +save_new_func(ffi.C.memcpy) -- `cts->top` = 121 > +save_new_func(ffi.C.malloc) -- `cts->top` = 124 > + > +-- Assertions to check the `cts->top` value and step between > +-- calls. > +assert(ffi.typeinfo(124), 'cts->top >= 124') > +assert(not ffi.typeinfo(125), 'cts->top < 125') > + > +save_new_func(ffi.C.memmove) -- `cts->top` = 127 > + > +jit.opt.start('hotloop=1') > + > +-- Just some function to record trace and cause reallocation. > +local function recorded_func(func_arg) > + local res = test_value + func_arg > + return res > +end > +recorded_func(ffi.C.malloc) > + > +assert(ffi.typeinfo(127), 'cts->top >= 127') > +assert(not ffi.typeinfo(128), 'cts->top < 128') > + > +-- Last call to grow `cts->top` up to 129, so this causes > +-- `cts->tab` reallocation. Need to use different functions as > +-- an argument. > +recorded_func(ffi.C.getppid) > + > +test:ok(true, 'no heap-use-after-free in recff_cdata_arith') > + > +test:done(true)