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 30D89CA4939; Tue, 3 Sep 2024 17:36:06 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 30D89CA4939 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1725374166; bh=Hoh2ArL9Gupf/exKfR4XdaqOYWSLP+5DxXMEE107as8=; 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=wzF4/e1suHvmP6YKolL7TXooHpuf/SC6jm95zwiWBr6Qp9TjXw4EzsDUE+c/U3nMg Tgw+9ryA3xlk779v/G4/ZoalPjeMpw6YLxp0q5z4tpj3b35/iqJfA3txWP7/nNxkcK MyikS5zdlfq9sASfxguLK2PsqLgbXJESnMswz0Hk= Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [95.163.41.95]) (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 D7138CA4900 for ; Tue, 3 Sep 2024 17:36:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D7138CA4900 Received: by smtp57.i.mail.ru with esmtpa (envelope-from ) id 1slUdW-0000000GVqp-420R; Tue, 03 Sep 2024 17:36:03 +0300 Content-Type: multipart/alternative; boundary="------------0gRe9Wjo0A0oSFpuyzFXD1EK" Message-ID: <9fe3baf7-69df-44b1-9208-ebb90e28f1b4@tarantool.org> Date: Tue, 3 Sep 2024 17:36:02 +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: <20240819114133.2083-1-skaplun@tarantool.org> In-Reply-To: <20240819114133.2083-1-skaplun@tarantool.org> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD90E1F90BB1B8DBC181EDB1B2F6AAB338083C83F6C484A5D6E182A05F538085040C531D82A1E90EEAF479CDAE959BF64241BB22438A0497E04F76546DE32D60A58B939BEC6D9612482 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE781307CBDB76B677BEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006371E92E38DA2D50EB18638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D89E98ADF637C5353A3D40A8D47D58FD394D806A0C62F4E10FCC7F00164DA146DAFE8445B8C89999728AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8D166953D3EA3826BF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C4CB6874B0BCFF0B8BA3038C0950A5D36C8A9BA7A39EFB766D91E3A1F190DE8FDBA3038C0950A5D36D5E8D9A59859A8B6FF43116A1C2F940D3AA81AA40904B5D99C9F4D5AE37F343AD1F44FA8B9022EA23BBE47FD9DD3FB595F5C1EE8F4F765FCF1175FABE1C0F9B6E2021AF6380DFAD18AA50765F7900637F09814068C508CC822CA9DD8327EE4930A3850AC1BE2E735473DB2713789FF2EC4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A58C3DA50A74DAEA245002B1117B3ED6960788F693D1A6364DBFF4097FFC9E796F823CB91A9FED034534781492E4B8EEADF1165EFABA1780A6BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF455AD3C705A1F0E4B54931D1144FDB7151CC5617CD924F1219EB2D7E5220F1147E72D8710CBEBF0E3EE79E6E40FF6950B2C6AA91F71E7A60BC5DBF7C9EA156913A1E9F0E12F0BE735F4332CA8FE04980913E6812662D5F2AB9AF64DB4688768036DF5FE9C0001AF333F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojpn6nlaCBY3SsrkOdTk27og== X-Mailru-Sender: 520A125C2F17F0B1E52FEF5D219D6140C170C54ECA85E7E32BF052E6EBAB4B64CA95342AEBCDFE7F0152A3D17938EB451EB5A0BCEC6A560B3DDE9B364B0DF289BE2DA36745F2EEB5CEBA01FB949A1F1EEAB4BC95F72C04283CDA0F3B3F5B9367 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] FFI: Fix __tostring metamethod access to enum cdata value. 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" This is a multi-part message in MIME format. --------------0gRe9Wjo0A0oSFpuyzFXD1EK Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, Sergey thanks for the patch! LGTM Sergey On 19.08.2024 14:41, Sergey Kaplun wrote: > From: Mike Pall > > Thanks to Sergey Kaplun. > > (cherry picked from commit f2a1cd43281361035149b6eedbd267b5e71d64d0) > > On a 64-bit host, `*(uint32_t **)p` (in the > `lj_cf_ffi_meta___tostring()`) is the read of 8 bytes, while the size of > the cdata tail for the enum is only 4. This leads to heap-buffer-overflow > during the call of `tostring()` on the corresponding cdata. > > This patch fixes the pointer cast to `(uint32_t *)p`, which is correct. > > Sergey Kaplun: > * added the description and the test for the problem > > Part of tarantool/tarantool#10199 > --- > > Branch:https://github.com/tarantool/luajit/tree/skaplun/lj-1232-fix-enum-tostring > Related issues: > *https://github.com/LuaJIT/LuaJIT/issues/1232 > *https://github.com/tarantool/tarantool/issues/10199 > > src/lib_ffi.c | 2 +- > .../lj-1232-fix-enum-tostring.test.lua | 21 +++++++++++++++++++ > 2 files changed, 22 insertions(+), 1 deletion(-) > create mode 100644 test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua > > diff --git a/src/lib_ffi.c b/src/lib_ffi.c > index 3c8dd77f..7988dab8 100644 > --- a/src/lib_ffi.c > +++ b/src/lib_ffi.c > @@ -305,7 +305,7 @@ LJLIB_CF(ffi_meta___tostring) > p = *(void **)p; > } else if (ctype_isenum(ct->info)) { > msg = "cdata<%s>: %d"; > - p = (void *)(uintptr_t)*(uint32_t **)p; > + p = (void *)(uintptr_t)*(uint32_t *)p; > } else { > if (ctype_isptr(ct->info)) { > p = cdata_getptr(p, ct->size); > diff --git a/test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua b/test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua > new file mode 100644 > index 00000000..073bfcb6 > --- /dev/null > +++ b/test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua > @@ -0,0 +1,21 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate heap-buffer-overflow in the > +-- `tostring()` call for the enum cdata. > +-- See also:https://github.com/LuaJIT/LuaJIT/issues/1232. > + > +local test = tap.test('lj-1232-fix-enum-tostring') > + > +local ffi = require('ffi') > +local ENUM_VAL = 1 > +local EXPECTED = 'cdata: ' .. ENUM_VAL > + > +test:plan(1) > + > +local cdata_enum = ffi.new(('enum {foo = %d}'):format(ENUM_VAL), ENUM_VAL) > + > +-- XXX: The test shows heap-buffer-overflow only under ASAN. > + > +test:like(tostring(cdata_enum), EXPECTED, 'correct tostring result') > + > +test:done(true) --------------0gRe9Wjo0A0oSFpuyzFXD1EK Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

Hi, Sergey

thanks for the patch! LGTM

Sergey

On 19.08.2024 14:41, Sergey Kaplun wrote:
From: Mike Pall <mike>

Thanks to Sergey Kaplun.

(cherry picked from commit f2a1cd43281361035149b6eedbd267b5e71d64d0)

On a 64-bit host, `*(uint32_t **)p` (in the
`lj_cf_ffi_meta___tostring()`) is the read of 8 bytes, while the size of
the cdata tail for the enum is only 4. This leads to heap-buffer-overflow
during the call of `tostring()` on the corresponding cdata.

This patch fixes the pointer cast to `(uint32_t *)p`, which is correct.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#10199
---

Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1232-fix-enum-tostring
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1232
* https://github.com/tarantool/tarantool/issues/10199

 src/lib_ffi.c                                 |  2 +-
 .../lj-1232-fix-enum-tostring.test.lua        | 21 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua

diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 3c8dd77f..7988dab8 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -305,7 +305,7 @@ LJLIB_CF(ffi_meta___tostring)
       p = *(void **)p;
     } else if (ctype_isenum(ct->info)) {
       msg = "cdata<%s>: %d";
-      p = (void *)(uintptr_t)*(uint32_t **)p;
+      p = (void *)(uintptr_t)*(uint32_t *)p;
     } else {
       if (ctype_isptr(ct->info)) {
 	p = cdata_getptr(p, ct->size);
diff --git a/test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua b/test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua
new file mode 100644
index 00000000..073bfcb6
--- /dev/null
+++ b/test/tarantool-tests/lj-1232-fix-enum-tostring.test.lua
@@ -0,0 +1,21 @@
+local tap = require('tap')
+
+-- Test file to demonstrate heap-buffer-overflow in the
+-- `tostring()` call for the enum cdata.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1232.
+
+local test = tap.test('lj-1232-fix-enum-tostring')
+
+local ffi = require('ffi')
+local ENUM_VAL = 1
+local EXPECTED = 'cdata<enum %d+>: ' .. ENUM_VAL
+
+test:plan(1)
+
+local cdata_enum = ffi.new(('enum {foo = %d}'):format(ENUM_VAL), ENUM_VAL)
+
+-- XXX: The test shows heap-buffer-overflow only under ASAN.
+
+test:like(tostring(cdata_enum), EXPECTED, 'correct tostring result')
+
+test:done(true)
--------------0gRe9Wjo0A0oSFpuyzFXD1EK--