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 A59696EC40; Sun, 27 Jun 2021 17:12:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A59696EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1624803125; bh=qne2Oanvh8ZKNtcSLRgjrI5PBrs5hQvOshhOkNCa7bI=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=vMMpGWeyjpEKbaXzvhRJ7dJSjPWY+DlJeH0y9ZhfsilcYcXMHtowy3bfIVu/Lp2b9 T98UeEj4BTd7rsfsNCc0l1CLonVYtwhvuJCL9FCKVUt7GpW3uCHTRL3hsOH3mTnvQL Bzdq3GZeJmYUiblEgC7cYeQC63cQQ2F+yj+BqjYk= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 73D966EC40 for ; Sun, 27 Jun 2021 17:12:03 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 73D966EC40 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1lxVVy-0001d3-Dt; Sun, 27 Jun 2021 17:12:02 +0300 To: Cyrill Gorcunov Cc: tml References: <20210625100707.87807-1-gorcunov@gmail.com> <9652278e-570e-40e5-b2d1-856fe58179fc@tarantool.org> Message-ID: <4b0f4d8f-4e0c-00d0-38ad-0b6abad3aafe@tarantool.org> Date: Sun, 27 Jun 2021 16:12:01 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD954DFF1DC42D673FB4F75AC5594ACDC16869A51A860A12816182A05F538085040EFCA7C491078787109CC02491F07F83CFD0F84521B9E1271CF6A4B72838BF86A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F0ABDA2F087648F5EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637ED2BA022FBF94AB68638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8554F0855D4EBAB9B18E672F438DD280B117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCECADA55FE5B58BB7A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751F28451B159A507268D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6AC294AFEFA671E80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A541EDB079EF86283AE6EF1FFDB76EA1CF580F7AEF949FEC54D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA752546FE575EB473F1410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3453037B563866572121ED65C52BC159FCC43ED5D61C785A04BABDFEAB0F3E9F6F28DDDDAA3EB4FCDE1D7E09C32AA3244CDCF657EF27FD4A71D16F28F08CF067DEBBA718C7E6A9E042729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojBC0PCXY5SAXcyEtv7ZkVVA== X-Mailru-Sender: 689FA8AB762F73936BC43F508A06382277C7CD1C67A7B20D4CEC61FA3EB83C013841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH] raft: more precise verification of incoming request state 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: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" On 26.06.2021 15:34, Cyrill Gorcunov wrote: > On Fri, Jun 25, 2021 at 11:49:10PM +0200, Vladislav Shpilevoy wrote: >>> Closes #6067 >> >> Unfortunately, the patch does not fix much. Firstly, >> the state is decoded from MessagePack as mp_decode_uint(). >> It can't be negative originally by design. > > No, Vlad, this is not how signed\unsigned numbers are processed. Yeah, I know how the numbers are processed. But right here it does not matter. The state initially appears from mp_decode_uint() which returns uint64_t. Hence it is positive initially. Always. You can't make mp_decode_uint() < 0 true without any casts. It becomes truncated later. >> Secondly, and most importantly, the state is decoded as >> uint64_t raft_request.state, but it is saved without checks >> into enum struct raft_msg.state in box_raft_request_to_msg(). > > I suppose this is done because box_raft_request_to_msg a kind of > general wrapper which doesn't care much about contents validation > but simply represent the data in a new structure? Yes. > When stage > of processing starts (ie raft_process_msg helper) we validate > each member, so I think this is a good architecture to investigate > contents in raft_process_msg, You can't validate it during decoding properly, because the decoder does not and should not depend on the raft library. It does not know which states are valid. It only decodes the fields into numbers. > but if you prefer to use > box_raft_request_to_msg instead I would make it so. It would be good to keep box_raft_request_to_msg() simple. But it is not a requirement. >> So the current patch does not change almost anything I >> suppose? Because if I send ((UINT32_MAX << 31) | 0x01), it >> will work. Because the upper 32 bits would be truncated >> (assuming the enum is 32 bits), and your checks are done too >> late to notice it. >> >> Correct? If so, then I would propose to fix the issue entirely, >> if you want to work on that. > > I think I didnt put more technical details in commit message, > let me try to explain in a more verbose way: the enum is stored > as integer type but standart doesn't require it to be signed > or unsigned, so it is implementation defined. Currently on my > gcc-11 series the if statement consider @state member as > _unsigned_ integer thus basically my patch does absolutely > nothing, the code remains the same on asm level. Still a compiler > has all rights to use a signed "state" and in this case our > `if` won't hit leading to undefined behaviour in case if the > "state" is in form (0x80000000 | x) and what is worse we won't > even able to figure out what has happened. > > We can emulate this behaviour by using > > raft_process_msg > ... > if ((int)req->state < 0) { > panic("RAFT: req->state > 0 -> %d", (int)req->state >= (int)raft_state_MAX); > assert(req->state > 0); > } > > in result if we pass -1u as state in the test we will have > [001] +2021-06-26 15:52:58.610 [57492] main/101/main F> RAFT: req->state > 0 -> 0 > > thus if compiler use signed int type as internal representation then > (int)req->state >= (int)raft_state_MAX won't take the branch because > it is a negative value. The very fact you needed to put so much effort into the explanation of a simple negative number check means it is a huge overkill. Please, compare with < 0 explicitly. If the compiler will store the enum as uint, it will drop your condition as unreachable anyway. > Surely the patch won't catch integer overflows since we trim the > data as you noted above because, gimme some time to think about, > I really like how our box_raft_request_to_msg helpers are done > and if it possible would love to not change them much: they are > simple and short now. You can try to fix it by changing state to uint64_t in raft_request (now it is uint32_t), then you need to check for the valid values in box_raft_request_to_msg(). Or you can patch raft_msg to store the state as uint64_t. So the value wouldn't be truncated anywhere and raft_process_msg() could check its correctness properly. >>> diff --git a/src/lib/raft/raft.c b/src/lib/raft/raft.c >>> index eacdddb7e..409e983f0 100644 >>> --- a/src/lib/raft/raft.c >>> +++ b/src/lib/raft/raft.c >>> @@ -309,7 +309,9 @@ raft_process_msg(struct raft *raft, const struct raft_msg *req, uint32_t source) >>> say_info("RAFT: message %s from %u", raft_msg_to_string(req), source); >>> assert(source > 0); >>> assert(source != raft->self); >>> - if (req->term == 0 || req->state == 0 || req->state >= raft_state_MAX) { >>> + >>> + if (req->term == 0 || req->state == 0 || >>> + (unsigned)req->state >= raft_state_MAX) { >> >> Please, perform a normal < 0 comparison. No need for unsigned cast >> cast. > > We need an explicit cast here because as I said the internal > implementation is compiler defined. It has nothing to do with the compiler-specific implementation. It is simply over-engineering. You cast value to unsigned to check if it is negative. WTF? Lets do a normal comparison. > On the other hands I played with a number of gcc and llvm compilers > and for this code they all does correct comparision (as unsigned > integers) so maybe we could just leave it as is, simply close > the bug as won't implement and spend time on more important things? > I don't mind actually, so up to you just say me what you think. I don't mind fixing the issue. But now your patch does not fix it, unfortunately. If you want to fix it - go on. But it must be truly fixed then.