From patchwork Thu Dec 8 14:18:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 9466459 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6CEFF6071E for ; Thu, 8 Dec 2016 14:21:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5B5D028589 for ; Thu, 8 Dec 2016 14:21:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5004228595; Thu, 8 Dec 2016 14:21:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B972228589 for ; Thu, 8 Dec 2016 14:21:48 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEzXT-00008A-0z; Thu, 08 Dec 2016 14:19:11 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEzXR-000076-Aq for xen-devel@lists.xen.org; Thu, 08 Dec 2016 14:19:09 +0000 Received: from [85.158.137.68] by server-4.bemta-3.messagelabs.com id 4C/62-20885-CDB69485; Thu, 08 Dec 2016 14:19:08 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmplkeJIrShJLcpLzFFi42JxWrohUvdOtme EwflPbBZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bD22fYCq4IVvTsvsDWwNjL18XIySEh4C9x 89FeFhCbTUBfYveLT0wgtoiAusTpjousXYxcHMwC+5gk3v67AFTEwSEs4Czx/E4pSA2LgIrE3 dV9bCA2r4CnxIETbWwQM+Ukzh//yQxiCwmoSVzrv8QOUSMocXLmE7BdzAISEgdfvGCewMg9C0 lqFpLUAkamVYwaxalFZalFukameklFmekZJbmJmTm6hgbGermpxcWJ6ak5iUnFesn5uZsYgcF Qz8DAuIOx9YTfIUZJDiYlUd5dE9wjhPiS8lMqMxKLM+KLSnNSiw8xanBwCGxeu/oCoxRLXn5e qpIEr1uWZ4SQYFFqempFWmYOMFxhSiU4eJREeItB0rzFBYm5xZnpEKlTjLoc604tecokBDZDS pzXCaRIAKQoozQPbgQsdi4xykoJ8zIyMDAI8RSkFuVmlqDKv2IU52BUEuatBJnCk5lXArfpFd ARTEBHzLvhDnJESSJCSqqBseCF2pZ0oe9y9jOZss72qMRXBPaZTIg7nL/9wPFVVv+//w067vF z3/7Ftttufnl/fqVsZ9OjG9UXdz1iSNmhtvS70M5H2yMfBW9rmVtn1+vtJSi+xkJa7x7T/Gd1 rtbp+9Ys+f1n7XNtKafoq20xLvIGvObbPWO1Nm/Kuc08Nf6FpsWShuCiq0osxRmJhlrMRcWJA JGc0VSYAgAA X-Env-Sender: prvs=143b5a7df=Andrew.Cooper3@citrix.com X-Msg-Ref: server-5.tower-31.messagelabs.com!1481206746!71921488!1 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.0.16; banners=-,-,- X-VirusChecked: Checked Received: (qmail 25149 invoked from network); 8 Dec 2016 14:19:07 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-5.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 8 Dec 2016 14:19:07 -0000 X-IronPort-AV: E=Sophos;i="5.33,319,1477958400"; d="scan'208";a="393738316" From: Andrew Cooper To: Xen-devel Date: Thu, 8 Dec 2016 14:18:58 +0000 Message-ID: <1481206738-10702-1-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 MIME-Version: 1.0 Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Jan Beulich Subject: [Xen-devel] [PATCH] libelf: Fix div0 issues in elf_{shdr, phdr}_count() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP elf_uval() can return zero either because the field itself is zero, or because the access is out of bounds. c/s a01b6d4 "libelf: treat phdr and shdr similarly" introduced two div0 issues as e_{ph,sh}entsize are not checked for sanity before being used to divide elf->size. Spotted by Coverity. Signed-off-by: Andrew Cooper --- CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Tim Deegan CC: Wei Liu I experimented with making elf_access_unsigned() __must_check, but this didn't cause a compiler error. I am not quite sure why. --- xen/common/libelf/libelf-tools.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/xen/common/libelf/libelf-tools.c b/xen/common/libelf/libelf-tools.c index bf661e7..f62d9c3 100644 --- a/xen/common/libelf/libelf-tools.c +++ b/xen/common/libelf/libelf-tools.c @@ -130,11 +130,17 @@ uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr) unsigned elf_shdr_count(struct elf_binary *elf) { unsigned count = elf_uval(elf, elf->ehdr, e_shnum); + unsigned entsize = elf_uval(elf, elf->ehdr, e_shentsize); uint64_t max; if ( !count ) return 0; - max = elf->size / elf_uval(elf, elf->ehdr, e_shentsize); + if ( !entsize ) + { + elf_mark_broken(elf, "e_shentsize is zero"); + return 0; + } + max = elf->size / entsize; if ( max > UINT_MAX ) max = UINT_MAX; if ( count > max ) @@ -148,11 +154,17 @@ unsigned elf_shdr_count(struct elf_binary *elf) unsigned elf_phdr_count(struct elf_binary *elf) { unsigned count = elf_uval(elf, elf->ehdr, e_phnum); + unsigned entsize = elf_uval(elf, elf->ehdr, e_phentsize); uint64_t max; if ( !count ) return 0; - max = elf->size / elf_uval(elf, elf->ehdr, e_phentsize); + if ( !entsize ) + { + elf_mark_broken(elf, "e_phentsize is zero"); + return 0; + } + max = elf->size / entsize; if ( max > UINT_MAX ) max = UINT_MAX; if ( count > max )