From patchwork Sun Jun 7 14:30:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 3978 Return-Path: Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A33AC600F7 for ; Sun, 7 Jun 2020 16:30:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="F+7phBrX"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=2jabfmk5QR7Db/ZzwTlL0K8EIHd6SdbXBkLwGfoEG7s=; b=F+7phBrXBYkwTX8PE9syy2FDZhtQNL9LFU0/e8Z/O5lhFty/S6Z0wSy8EJKZTEy4j7i3 9C5LZFCAQEgZ2BJUgxt1oS7/GGWuunu6T1FUmlOIXyCVcpE67V3p+Gh5064rHDM34ZASSo ZoLcGmBD8jN1wU1r2QTIFepKgDUZTiCn4= Received: by filterdrecv-p3mdw1-6f5df8956d-8rppr with SMTP id filterdrecv-p3mdw1-6f5df8956d-8rppr-20-5EDCF9FA-46 2020-06-07 14:30:18.473731592 +0000 UTC m=+324990.134891972 Received: from mail.uajain.com (unknown) by ismtpd0008p1maa1.sendgrid.net (SG) with ESMTP id JS1To19sQaaSs9PR6LSkpQ Sun, 07 Jun 2020 14:30:17.787 +0000 (UTC) From: Umang Jain Date: Sun, 07 Jun 2020 14:30:18 +0000 (UTC) Message-Id: <20200607143012.17752-2-email@uajain.com> In-Reply-To: <20200607143012.17752-1-email@uajain.com> References: <20200607143012.17752-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcGsFXrTC3nkaVUz7y/MXE3aIrlXxsCQ5m4ALiEPD7h7ovoYuI8rMAdRC0ZWithFTpYl7lK6P+7uq966qmJ9nDdeZ9aZ7jWmIbf9goYdnYdqiyyV8o9ESSKrU3mkJc4w3ibxSRTXOED1Je4hnpHyLxaybSH+01mZTLZOqZN9IEIFMDY1kGl442ZQQzkkfmEl9lai45zAzGZGjzD3itEisnYg== To: laurent.pinchart@ideasonboard.com, libcamera-devel@lists.libcamera.org, kieran.bingham@ideasonboard.com Subject: [libcamera-devel] [PATCH v3 1/2] libcamera: ipa_module: Share code to find section header of ELF header X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 07 Jun 2020 14:30:20 -0000 Refactor the code to find section into a common helper function. This commit introduces no functional changes. Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart --- src/libcamera/ipa_module.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index 045c8fc..60aaa34 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -88,6 +88,15 @@ int elfVerifyIdent(Span elf) return 0; } +ElfW(Shdr) *elfSection(Span elf, ElfW(Ehdr) *eHdr, ElfW(Half) idx) +{ + if (idx >= eHdr->e_shnum) + return nullptr; + + off_t offset = eHdr->e_shoff + idx * eHdr->e_shentsize; + return elfPointer(elf, offset); +} + /** * \brief Retrieve address and size of a symbol from an mmap'ed ELF file * \param[in] elf Address and size of mmap'ed ELF file @@ -102,8 +111,7 @@ Span elfLoadSymbol(Span elf, const char *symbol) if (!eHdr) return {}; - off_t offset = eHdr->e_shoff + eHdr->e_shentsize * eHdr->e_shstrndx; - ElfW(Shdr) *sHdr = elfPointer(elf, offset); + ElfW(Shdr) *sHdr = elfSection(elf, eHdr, eHdr->e_shstrndx); if (!sHdr) return {}; off_t shnameoff = sHdr->sh_offset; @@ -111,12 +119,11 @@ Span elfLoadSymbol(Span elf, const char *symbol) /* Locate .dynsym section header. */ ElfW(Shdr) *dynsym = nullptr; for (unsigned int i = 0; i < eHdr->e_shnum; i++) { - offset = eHdr->e_shoff + eHdr->e_shentsize * i; - sHdr = elfPointer(elf, offset); + sHdr = elfSection(elf, eHdr, i); if (!sHdr) return {}; - offset = shnameoff + sHdr->sh_name; + off_t offset = shnameoff + sHdr->sh_name; char *name = elfPointer(elf, offset); if (!name) return {}; @@ -132,8 +139,7 @@ Span elfLoadSymbol(Span elf, const char *symbol) return {}; } - offset = eHdr->e_shoff + eHdr->e_shentsize * dynsym->sh_link; - sHdr = elfPointer(elf, offset); + sHdr = elfSection(elf, eHdr, dynsym->sh_link); if (!sHdr) return {}; off_t dynsym_nameoff = sHdr->sh_offset; @@ -142,7 +148,7 @@ Span elfLoadSymbol(Span elf, const char *symbol) ElfW(Sym) *targetSymbol = nullptr; unsigned int dynsym_num = dynsym->sh_size / dynsym->sh_entsize; for (unsigned int i = 0; i < dynsym_num; i++) { - offset = dynsym->sh_offset + dynsym->sh_entsize * i; + off_t offset = dynsym->sh_offset + dynsym->sh_entsize * i; ElfW(Sym) *sym = elfPointer(elf, offset); if (!sym) return {}; @@ -165,13 +171,10 @@ Span elfLoadSymbol(Span elf, const char *symbol) } /* Locate and return data of symbol. */ - if (targetSymbol->st_shndx >= eHdr->e_shnum) - return {}; - offset = eHdr->e_shoff + targetSymbol->st_shndx * eHdr->e_shentsize; - sHdr = elfPointer(elf, offset); + sHdr = elfSection(elf, eHdr, targetSymbol->st_shndx); if (!sHdr) return {}; - offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr); + off_t offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr); uint8_t *data = elfPointer(elf, offset, targetSymbol->st_size); if (!data) return {}; From patchwork Sun Jun 7 14:30:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 3979 Return-Path: Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DB45F61626 for ; Sun, 7 Jun 2020 16:30:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="ilUeU8Yt"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=TBjKum+DY72wRVegSwltHxRqXC6J1KGz1NRSRIej3YA=; b=ilUeU8YtnH9Hm/d93w7cqE3Ub6v05B/h962j3OLBHket7cQ+c26OPakAl6YHH9yXJ2gx AoGe2JU9ZBMUIpZZjVYcC70oct4vYkKaajRNb6Nu9ibwk2FhUL6xumhoiBzLYVAIpb+yES M+u7+LM4bNcFOzxDNXEc82oFuteyzNxVY= Received: by filter0135p3las1.sendgrid.net with SMTP id filter0135p3las1-9010-5EDCF9FA-1E7 2020-06-07 14:30:18.733685738 +0000 UTC m=+400000.314394105 Received: from mail.uajain.com (unknown) by ismtpd0004p1maa1.sendgrid.net (SG) with ESMTP id hZX9YxZ2QLakbeVOk4GZ1A Sun, 07 Jun 2020 14:30:18.366 +0000 (UTC) From: Umang Jain Date: Sun, 07 Jun 2020 14:30:18 +0000 (UTC) Message-Id: <20200607143012.17752-3-email@uajain.com> In-Reply-To: <20200607143012.17752-1-email@uajain.com> References: <20200607143012.17752-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcAl6MTtKrYqN2BGXS9ZDVMf59817yL7+3nHWtUAHR16ne0RQ/mUc/NqTkjCEkd4+DGgj+PD3r5a3rDZOXWMtKRFLnYILa6UC3bnQ8P6hJpXmdvr3TSOLz3jLmvSOR51R92r8OXVUboaIHWmksjTjZXoG/OpGi1c8GL7EwSbFK4Ytdb3/oHKCNGMXxSRsnOHOR To: laurent.pinchart@ideasonboard.com, libcamera-devel@lists.libcamera.org, kieran.bingham@ideasonboard.com Subject: [libcamera-devel] [PATCH v3 2/2] libcamera: ipa_module: Fix implicit sign-extension in elfSection X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 07 Jun 2020 14:30:22 -0000 Given how the elfSection() is used the sub-expression (idx * eHdr->e_shentsize) it has effectively two (16 bits, unsigned) operands. The sub-expression is promoted to type int (32 bits, signed) for multiplication and then added to eHdr->e_shoff, which is uint32_t on 32-bit platforms and uint64_t on 64-bit platforms. Since eHdr->e_shoff is unsigned, the integer conversion rules dictates that the other signed operand(i.e. the resultant of aforementioned sub-expression) will be converted to unsigned type too. This causes sign-extension for both of the above operands to match eHdr->e_shoff's type and should be avoided. The solution is to explicitly cast one of the operands of the sub-expression with unsigned int type. Hence, the other operand will be integer promoted and the resultant will also be of unsigned int type, not requiring to bother about a sign-extension. Reported-by: Coverity CID=280008 Reported-by: Coverity CID=280009 Reported-by: Coverity CID=280010 Signed-off-by: Umang Jain Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/libcamera/ipa_module.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index 60aaa34..72e357e 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -93,7 +93,8 @@ ElfW(Shdr) *elfSection(Span elf, ElfW(Ehdr) *eHdr, ElfW(Half) idx) if (idx >= eHdr->e_shnum) return nullptr; - off_t offset = eHdr->e_shoff + idx * eHdr->e_shentsize; + off_t offset = eHdr->e_shoff + idx * + static_cast(eHdr->e_shentsize); return elfPointer(elf, offset); }