[{"id":5049,"web_url":"https://patchwork.libcamera.org/comment/5049/","msgid":"<20200605164110.GA1175@pendragon.ideasonboard.com>","date":"2020-06-05T16:41:10","subject":"Re: [libcamera-devel] [PATCH v2 2/2] libcamera: ipa_module: Fix\n\timplicit sign-extension in elfSection","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Umang,\n\nThank you for the patch.\n\nOn Fri, Jun 05, 2020 at 03:09:16PM +0000, Umang Jain wrote:\n> Given how the elfSection() is used, the sub-expression\n>        (idx * eHdr->e_shentsize)\n> has effectively two (16 bits, unsigned) operands.\n> The sub-expression is promoted to type int (32 bits, signed) for\n> multiplication and then added to eHdr->e_shoff, which is uint32_t\n> on 32-bit platforms and uint64_t on 64-bit platforms. Since eHdr->e_shoff\n> is unsigned, the integer conversion rules dictates that the other signed\n> operand(i.e. the resultant of aforementioned sub-expression) will be\n> converted to unsigned type too. This causes sign-extension for both of\n> the above operands to match eHdr->e_shoff's type and should be avoided.\n> \n> The solution is to explicitly cast one of the operands of the\n> sub-expression with unsigned int type. Hence, the other operand will be\n> integer promoted and the resultant will also be of unsigned int type,\n> not requiring to bother about a sign-extension.\n> \n> Reported-by: Coverity CID=280008\n> Reported-by: Coverity CID=280009\n> Reported-by: Coverity CID=280010\n> Signed-off-by: Umang Jain <email@uajain.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/libcamera/ipa_module.cpp | 3 ++-\n>  1 file changed, 2 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\n> index f54dd8b..d79151d 100644\n> --- a/src/libcamera/ipa_module.cpp\n> +++ b/src/libcamera/ipa_module.cpp\n> @@ -93,7 +93,8 @@ ElfW(Shdr) *elfSection(Span<uint8_t> elf, ElfW(Ehdr) *eHdr, unsigned int idx)\n>  \tif (idx >= eHdr->e_shnum)\n>  \t\treturn nullptr;\n>  \n> -\toff_t offset = eHdr->e_shoff + idx * eHdr->e_shentsize;\n> +\toff_t offset = eHdr->e_shoff + idx *\n> +\t\t\t\t       static_cast<uint32_t>(eHdr->e_shentsize);\n\nIs this needed now that idx is an unsigned int and not an uint16_t\nanymore ?\n\n>  \treturn elfPointer<ElfW(Shdr)>(elf, offset);\n>  }","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3EECE603C6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  5 Jun 2020 18:41:29 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B3ED127C;\n\tFri,  5 Jun 2020 18:41:28 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"YBBjWA0e\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591375288;\n\tbh=sp+UybsevoZL7jMlm6umG5zIelfAAM9oOPfjliNNiv8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=YBBjWA0eLfKm+a8hLRhINV2iSkp9E8riWCDGXTveR0uLVKYITBO9PiZiDiYn8d8Ji\n\tyEQ7pmz0cvgon7pFfMhbUJvEKdQ0SaXv5G9uwf1u2pCee+ZLwfLkyjFeOABOirB77g\n\t1XSSFvmbuRfyz60sKjXGSI3RqeHAxgoQxfo2c4Ww=","Date":"Fri, 5 Jun 2020 19:41:10 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Umang Jain <email@uajain.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<20200605164110.GA1175@pendragon.ideasonboard.com>","References":"<20200605150858.116564-1-email@uajain.com>\n\t<20200605150858.116564-3-email@uajain.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200605150858.116564-3-email@uajain.com>","Subject":"Re: [libcamera-devel] [PATCH v2 2/2] libcamera: ipa_module: Fix\n\timplicit sign-extension in elfSection","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Fri, 05 Jun 2020 16:41:29 -0000"}},{"id":5092,"web_url":"https://patchwork.libcamera.org/comment/5092/","msgid":"<18784203-da05-746d-e13e-33e7c5a019a2@uajain.com>","date":"2020-06-06T16:32:28","subject":"Re: [libcamera-devel] [PATCH v2 2/2] libcamera: ipa_module: Fix\n\timplicit sign-extension in elfSection","submitter":{"id":1,"url":"https://patchwork.libcamera.org/api/people/1/","name":"Umang Jain","email":"email@uajain.com"},"content":"Hi Laurent,\n\nOn 6/5/20 10:11 PM, Laurent Pinchart wrote:\n> Hi Umang,\n>\n> Thank you for the patch.\n>\n> On Fri, Jun 05, 2020 at 03:09:16PM +0000, Umang Jain wrote:\n>> Given how the elfSection() is used, the sub-expression\n>>         (idx * eHdr->e_shentsize)\n>> has effectively two (16 bits, unsigned) operands.\n>> The sub-expression is promoted to type int (32 bits, signed) for\n>> multiplication and then added to eHdr->e_shoff, which is uint32_t\n>> on 32-bit platforms and uint64_t on 64-bit platforms. Since eHdr->e_shoff\n>> is unsigned, the integer conversion rules dictates that the other signed\n>> operand(i.e. the resultant of aforementioned sub-expression) will be\n>> converted to unsigned type too. This causes sign-extension for both of\n>> the above operands to match eHdr->e_shoff's type and should be avoided.\n>>\n>> The solution is to explicitly cast one of the operands of the\n>> sub-expression with unsigned int type. Hence, the other operand will be\n>> integer promoted and the resultant will also be of unsigned int type,\n>> not requiring to bother about a sign-extension.\n>>\n>> Reported-by: Coverity CID=280008\n>> Reported-by: Coverity CID=280009\n>> Reported-by: Coverity CID=280010\n>> Signed-off-by: Umang Jain <email@uajain.com>\n>> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n>> ---\n>>   src/libcamera/ipa_module.cpp | 3 ++-\n>>   1 file changed, 2 insertions(+), 1 deletion(-)\n>>\n>> diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\n>> index f54dd8b..d79151d 100644\n>> --- a/src/libcamera/ipa_module.cpp\n>> +++ b/src/libcamera/ipa_module.cpp\n>> @@ -93,7 +93,8 @@ ElfW(Shdr) *elfSection(Span<uint8_t> elf, ElfW(Ehdr) *eHdr, unsigned int idx)\n>>   \tif (idx >= eHdr->e_shnum)\n>>   \t\treturn nullptr;\n>>   \n>> -\toff_t offset = eHdr->e_shoff + idx * eHdr->e_shentsize;\n>> +\toff_t offset = eHdr->e_shoff + idx *\n>> +\t\t\t\t       static_cast<uint32_t>(eHdr->e_shentsize);\n> Is this needed now that idx is an unsigned int and not an uint16_t\n> anymore ?\n\nShould I assume `unsigned int` will be 32-bit minimum? If yes, then you \nare right.\nWell, I have not heard \"16-bit platforms\" in recent years but I am not \nsure if there is\n\nany obscurity out there in wild.\n\n>\n>>   \treturn elfPointer<ElfW(Shdr)>(elf, offset);\n>>   }","headers":{"Return-Path":"<bounces+15657259-5c31-libcamera-devel=lists.libcamera.org@em7280.uajain.com>","Received":["from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7707361167\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  6 Jun 2020 18:32:30 +0200 (CEST)","by filterdrecv-p3mdw1-6f5df8956d-rzqmc with SMTP id\n\tfilterdrecv-p3mdw1-6f5df8956d-rzqmc-19-5EDBC51C-42\n\t2020-06-06 16:32:28.8472141 +0000 UTC m=+245917.465911074","from mail.uajain.com (unknown)\n\tby ismtpd0001p1maa1.sendgrid.net (SG) with ESMTP\n\tid kuDBTl95Q7usiTzfuvsKiQ Sat, 06 Jun 2020 16:32:28.207 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=uajain.com\n\theader.i=@uajain.com header.b=\"yo9uyxdx\"; \n\tdkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com;\n\th=subject:references:from:mime-version:in-reply-to:to:cc:content-type:\n\tcontent-transfer-encoding;\n\ts=s1; bh=r6Zuyx67Sryejbv2CxqIV3ThXa8WPmC/JL4roUZojrQ=;\n\tb=yo9uyxdxyqW4VYyVhyCnKbQa+jGfNZHA29zhJGST1ZyLZLXTdbRrFQPheLN64AlwRRyW\n\tNFPg3Pd83LYQxPDsBdc6RwQsHTjTqS8S8eZ9hjtshKrdgR2FeqWYAsw0XXNnWOL4bQatgf\n\tk8AQ/aQIDEffHw0p/xuE8N2q0ACZymI4M=","References":"<20200605150858.116564-1-email@uajain.com>\n\t<20200605150858.116564-3-email@uajain.com>\n\t<20200605164110.GA1175@pendragon.ideasonboard.com>","From":"Umang Jain <email@uajain.com>","Message-ID":"<18784203-da05-746d-e13e-33e7c5a019a2@uajain.com>","Date":"Sat, 06 Jun 2020 16:32:28 +0000 (UTC)","Mime-Version":"1.0","In-Reply-To":"<20200605164110.GA1175@pendragon.ideasonboard.com>","X-SG-EID":"1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcp6CL6ozs3IeZvk96OtB41CPGBGOcJjUuyi/uO125dGtiuCjcRkXV3rJO+DX/qNfAKWn9pKNnXuJ4TLM/f3c7sIzJTgK5x1bCKn8lFdJNkbG9s9OSE9AeuIM25xqEV8NyDuB3FNRhldWJR+zo/PkA6MTSbNz4XtveUClYhkACdc7NLcSPqQsgHZ8tJtwpl9b5sKTHxVYMPFO/AMnix1BpEw==","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, Kieran Bingham\n\t<kieran.bingham@ideasonboard.com>","Content-Type":"text/plain; charset=us-ascii; format=flowed","Content-Transfer-Encoding":"7bit","Content-Language":"en-US","Subject":"Re: [libcamera-devel] [PATCH v2 2/2] libcamera: ipa_module: Fix\n\timplicit sign-extension in elfSection","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Sat, 06 Jun 2020 16:32:31 -0000"}},{"id":5103,"web_url":"https://patchwork.libcamera.org/comment/5103/","msgid":"<20200606224957.GL7339@pendragon.ideasonboard.com>","date":"2020-06-06T22:49:57","subject":"Re: [libcamera-devel] [PATCH v2 2/2] libcamera: ipa_module: Fix\n\timplicit sign-extension in elfSection","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Umang,\n\nOn Sat, Jun 06, 2020 at 04:32:28PM +0000, Umang Jain wrote:\n> On 6/5/20 10:11 PM, Laurent Pinchart wrote:\n> > On Fri, Jun 05, 2020 at 03:09:16PM +0000, Umang Jain wrote:\n> >> Given how the elfSection() is used, the sub-expression\n> >>         (idx * eHdr->e_shentsize)\n> >> has effectively two (16 bits, unsigned) operands.\n> >> The sub-expression is promoted to type int (32 bits, signed) for\n> >> multiplication and then added to eHdr->e_shoff, which is uint32_t\n> >> on 32-bit platforms and uint64_t on 64-bit platforms. Since eHdr->e_shoff\n> >> is unsigned, the integer conversion rules dictates that the other signed\n> >> operand(i.e. the resultant of aforementioned sub-expression) will be\n> >> converted to unsigned type too. This causes sign-extension for both of\n> >> the above operands to match eHdr->e_shoff's type and should be avoided.\n> >>\n> >> The solution is to explicitly cast one of the operands of the\n> >> sub-expression with unsigned int type. Hence, the other operand will be\n> >> integer promoted and the resultant will also be of unsigned int type,\n> >> not requiring to bother about a sign-extension.\n> >>\n> >> Reported-by: Coverity CID=280008\n> >> Reported-by: Coverity CID=280009\n> >> Reported-by: Coverity CID=280010\n> >> Signed-off-by: Umang Jain <email@uajain.com>\n> >> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> >> ---\n> >>   src/libcamera/ipa_module.cpp | 3 ++-\n> >>   1 file changed, 2 insertions(+), 1 deletion(-)\n> >>\n> >> diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\n> >> index f54dd8b..d79151d 100644\n> >> --- a/src/libcamera/ipa_module.cpp\n> >> +++ b/src/libcamera/ipa_module.cpp\n> >> @@ -93,7 +93,8 @@ ElfW(Shdr) *elfSection(Span<uint8_t> elf, ElfW(Ehdr) *eHdr, unsigned int idx)\n> >>   \tif (idx >= eHdr->e_shnum)\n> >>   \t\treturn nullptr;\n> >>   \n> >> -\toff_t offset = eHdr->e_shoff + idx * eHdr->e_shentsize;\n> >> +\toff_t offset = eHdr->e_shoff + idx *\n> >> +\t\t\t\t       static_cast<uint32_t>(eHdr->e_shentsize);\n> > Is this needed now that idx is an unsigned int and not an uint16_t\n> > anymore ?\n> \n> Should I assume `unsigned int` will be 32-bit minimum? If yes, then you \n> are right.\n> Well, I have not heard \"16-bit platforms\" in recent years but I am not \n> sure if there is any obscurity out there in wild.\n\nI think we can guarantee that all platforms we support will be 32-bit or\nmore, but if you want to be on the safe side, you can make idx a\nuin32_t. Or make it a ElfW(Half) in patch 1/2 (as it can't be larger\nthan that), and keep this patch to cast it to uint32_t. That make be\ncleaner.\n\n> >>   \treturn elfPointer<ElfW(Shdr)>(elf, offset);\n> >>   }","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DD994603CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  7 Jun 2020 00:50:16 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5D96330D;\n\tSun,  7 Jun 2020 00:50:16 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Ko8gx5rI\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591483816;\n\tbh=8ekfUDOpol33EOyZgG5CILeNFXgLzasJ8ZV2gqzrwVE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Ko8gx5rI8WtXU3YuTdvOcIsR6OI5WeYLNXcFVe7iEVJ3Gy7t9bTbiwKdLpsGnAlc3\n\tul2NOdSSqvGl01z0EbThC1IR67QzIsPOF8cHqKtg0+mSP3X2WwYtZktVPJvlMliKvY\n\tJeF0WbH1sAVStJ5RgTtkf0rN24UCIy+hsqVMuvis=","Date":"Sun, 7 Jun 2020 01:49:57 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Umang Jain <email@uajain.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<20200606224957.GL7339@pendragon.ideasonboard.com>","References":"<20200605150858.116564-1-email@uajain.com>\n\t<20200605150858.116564-3-email@uajain.com>\n\t<20200605164110.GA1175@pendragon.ideasonboard.com>\n\t<18784203-da05-746d-e13e-33e7c5a019a2@uajain.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<18784203-da05-746d-e13e-33e7c5a019a2@uajain.com>","Subject":"Re: [libcamera-devel] [PATCH v2 2/2] libcamera: ipa_module: Fix\n\timplicit sign-extension in elfSection","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Sat, 06 Jun 2020 22:50:17 -0000"}}]