From patchwork Fri Dec 9 15:44:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Jackson X-Patchwork-Id: 9468493 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 71AB160586 for ; Fri, 9 Dec 2016 15:47:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 61EC828448 for ; Fri, 9 Dec 2016 15:47:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 56CEE2861D; Fri, 9 Dec 2016 15:47:51 +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 D71BE28448 for ; Fri, 9 Dec 2016 15:47:50 +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 1cFNMI-0003Kl-2A; Fri, 09 Dec 2016 15:45:14 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cFNMG-0003Im-Nx for xen-devel@lists.xensource.com; Fri, 09 Dec 2016 15:45:12 +0000 Received: from [85.158.137.68] by server-3.bemta-3.messagelabs.com id 9A/60-24885-881DA485; Fri, 09 Dec 2016 15:45:12 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprDIsWRWlGSWpSXmKPExsXitHSDvW77Ra8 Igxf7FC3uTXnP7sDosb1vF3sAYxRrZl5SfkUCa8aFtlksBVNMK95tvsTUwLhWrYuRg0NCwF/i Wot7FyMnkOkhcf3sCSYQm01AV6Jpy182EFtEQFnieNMX1i5GLg5mgedMEtt6VoAVCQu4Shxon gFmswioSEx5fBvM5hXwlPh4toMJYqicxPnjP5lBbE4BL4mZi26wgthCAvUSnxc3sYPcICSgJj F3fTxEq6DEyZlPWEBsZgEJiYMvXjBDjLGU+Lb+KfMERv5ZSMpmISlbwMi0ilGjOLWoLLVI19B CL6koMz2jJDcxM0fX0MBYLze1uDgxPTUnMalYLzk/dxMjMNTqGRgYdzD+Pu15iFGSg0lJlLeY yStCiC8pP6UyI7E4I76oNCe1+BCjDAeHkgTv+/NAOcGi1PTUirTMHGDQw6QlOHiURHjFLgCle YsLEnOLM9MhUqcYFaXEeRNAEgIgiYzSPLg2WKRdYpSVEuZlZGBgEOIpSC3KzSxBlX/FKM7BqC TMqwgyhSczrwRu+iugxUxAi+fdcAdZXJKIkJJqYDwgpqiqtnCL5uqCaonoLcsVrgjt+2oQJ8c julZVIX3hQe1DLZofPtotvbBReenX+WuerNlwUeqkgEXMB/Nvd/7zM+g8XfL/W+QRf9tXd+UY hdQnsXpwTVl+XMFove2OypVvP/OLcopz1oRcnX7oNdf0zU/Un14ovcHqIWizOz6197Gp9rznH cpKLMUZiYZazEXFiQBTKOVfrwIAAA== X-Env-Sender: prvs=1440ad286=Ian.Jackson@citrix.com X-Msg-Ref: server-3.tower-31.messagelabs.com!1481298309!75348525!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.0.16; banners=-,-,- X-VirusChecked: Checked Received: (qmail 3278 invoked from network); 9 Dec 2016 15:45:11 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-3.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 9 Dec 2016 15:45:11 -0000 X-IronPort-AV: E=Sophos;i="5.33,324,1477958400"; d="scan'208";a="402787258" X-CrossPremisesHeadersFilteredBySendConnector: FTLPEX02AMS01.citrite.net From: Ian Jackson To: Date: Fri, 9 Dec 2016 15:44:46 +0000 Message-ID: <1481298289-13546-6-git-send-email-ian.jackson@eu.citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1481298289-13546-1-git-send-email-ian.jackson@eu.citrix.com> References: <22602.39819.815607.194639@mariner.uk.xensource.com> <1481298289-13546-1-git-send-email-ian.jackson@eu.citrix.com> MIME-Version: 1.0 X-OrganizationHeadersPreserved: FTLPEX02AMS01.citrite.net Cc: Stefano Stabellini , Wei Liu , George Dunlap , Andrew Cooper , Tim Deegan , Jan Beulich , Ian Jackson Subject: [Xen-devel] [PATCH 5/8] libelf: loop safety: Replace all calls to strcmp 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 strcmp can do singificant work, and is found in some inner loops where we search for the meaning of things we find in the image. We need to avoid doing too much work. So replace all calls to strcmp with elf_strcmp_safe. Signed-off-by: Ian Jackson --- xen/common/libelf/libelf-dominfo.c | 37 +++++++++++++++++++------------------ xen/common/libelf/libelf-private.h | 7 ++++--- xen/common/libelf/libelf-tools.c | 4 ++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/xen/common/libelf/libelf-dominfo.c b/xen/common/libelf/libelf-dominfo.c index 87a47d9..b037d10 100644 --- a/xen/common/libelf/libelf-dominfo.c +++ b/xen/common/libelf/libelf-dominfo.c @@ -70,7 +70,8 @@ elf_errorstatus elf_xen_parse_features(struct elf_binary *elf, if ( feature[0] == '!' ) { /* required */ - if ( !strcmp(feature + 1, elf_xen_feature_names[i]) ) + if ( !elf_strcmp_safe(elf, feature + 1, + elf_xen_feature_names[i]) ) { elf_xen_feature_set(i, supported); if ( required ) @@ -81,7 +82,7 @@ elf_errorstatus elf_xen_parse_features(struct elf_binary *elf, else { /* supported */ - if ( !strcmp(feature, elf_xen_feature_names[i]) ) + if ( !elf_strcmp_safe(elf, feature, elf_xen_feature_names[i]) ) { elf_xen_feature_set(i, supported); break; @@ -173,13 +174,13 @@ elf_errorstatus elf_xen_parse_note(struct elf_binary *elf, safe_strcpy(parms->xen_ver, str); break; case XEN_ELFNOTE_PAE_MODE: - if ( !strcmp(str, "yes") ) + if ( !elf_strcmp_safe(elf, str, "yes") ) parms->pae = XEN_PAE_EXTCR3; if ( strstr(str, "bimodal") ) parms->pae = XEN_PAE_BIMODAL; break; case XEN_ELFNOTE_BSD_SYMTAB: - if ( !strcmp(str, "yes") ) + if ( !elf_strcmp_safe(elf, str, "yes") ) parms->bsd_symtab = 1; break; @@ -255,7 +256,7 @@ static unsigned elf_xen_parse_notes(struct elf_binary *elf, note_name = elf_note_name(elf, note); if ( note_name == NULL ) continue; - if ( strcmp(note_name, "Xen") ) + if ( elf_strcmp_safe(elf, note_name, "Xen") ) continue; if ( elf_xen_parse_note(elf, parms, note) ) return ELF_NOTE_INVALID; @@ -315,38 +316,38 @@ elf_errorstatus elf_xen_parse_guest_info(struct elf_binary *elf, elf_msg(elf, "ELF: %s=\"%s\"\n", name, value); /* strings */ - if ( !strcmp(name, "LOADER") ) + if ( !elf_strcmp_safe(elf, name, "LOADER") ) safe_strcpy(parms->loader, value); - if ( !strcmp(name, "GUEST_OS") ) + if ( !elf_strcmp_safe(elf, name, "GUEST_OS") ) safe_strcpy(parms->guest_os, value); - if ( !strcmp(name, "GUEST_VER") ) + if ( !elf_strcmp_safe(elf, name, "GUEST_VER") ) safe_strcpy(parms->guest_ver, value); - if ( !strcmp(name, "XEN_VER") ) + if ( !elf_strcmp_safe(elf, name, "XEN_VER") ) safe_strcpy(parms->xen_ver, value); - if ( !strcmp(name, "PAE") ) + if ( !elf_strcmp_safe(elf, name, "PAE") ) { - if ( !strcmp(value, "yes[extended-cr3]") ) + if ( !elf_strcmp_safe(elf, value, "yes[extended-cr3]") ) parms->pae = XEN_PAE_EXTCR3; else if ( !strncmp(value, "yes", 3) ) parms->pae = XEN_PAE_YES; } - if ( !strcmp(name, "BSD_SYMTAB") ) + if ( !elf_strcmp_safe(elf, name, "BSD_SYMTAB") ) parms->bsd_symtab = 1; /* longs */ - if ( !strcmp(name, "VIRT_BASE") ) + if ( !elf_strcmp_safe(elf, name, "VIRT_BASE") ) parms->virt_base = strtoull(value, NULL, 0); - if ( !strcmp(name, "VIRT_ENTRY") ) + if ( !elf_strcmp_safe(elf, name, "VIRT_ENTRY") ) parms->virt_entry = strtoull(value, NULL, 0); - if ( !strcmp(name, "ELF_PADDR_OFFSET") ) + if ( !elf_strcmp_safe(elf, name, "ELF_PADDR_OFFSET") ) parms->elf_paddr_offset = strtoull(value, NULL, 0); - if ( !strcmp(name, "HYPERCALL_PAGE") ) + if ( !elf_strcmp_safe(elf, name, "HYPERCALL_PAGE") ) parms->virt_hypercall = (strtoull(value, NULL, 0) << 12) + parms->virt_base; /* other */ - if ( !strcmp(name, "FEATURES") ) - if ( elf_xen_parse_features(value, parms->f_supported, + if ( !elf_strcmp_safe(elf, name, "FEATURES") ) + if ( elf_xen_parse_features(elf, value, parms->f_supported, parms->f_required) ) return -1; } diff --git a/xen/common/libelf/libelf-private.h b/xen/common/libelf/libelf-private.h index 388c3da..082c572 100644 --- a/xen/common/libelf/libelf-private.h +++ b/xen/common/libelf/libelf-private.h @@ -98,9 +98,10 @@ do { strncpy((d),(s),sizeof((d))-1); \ #define memset MISTAKE_unspecified_memset #define memmove MISTAKE_unspecified_memmove #define strcpy MISTAKE_unspecified_strcpy - /* This prevents libelf from using these undecorated versions - * of memcpy, memset, memmove and strcpy. Every call site - * must either use elf_mem*_unchecked, or elf_mem*_safe. */ +#define strcmp MISTAKE_unspecified_strcmp + /* This prevents libelf from using these undecorated versions + * of memcpy, memset, memmove, strcpy and strcmp. Every call site + * must either use elf_mem*_unchecked, or elf_*_safe. */ #endif /* __LIBELF_PRIVATE_H__ */ diff --git a/xen/common/libelf/libelf-tools.c b/xen/common/libelf/libelf-tools.c index ab83150..7fa5963 100644 --- a/xen/common/libelf/libelf-tools.c +++ b/xen/common/libelf/libelf-tools.c @@ -162,7 +162,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *n /* input has an insane section header count field */ break; sname = elf_section_name(elf, shdr); - if ( sname && !strcmp(sname, name) ) + if ( sname && !elf_strcmp_safe(elf, sname, name) ) return shdr; } return ELF_INVALID_HANDLE(elf_shdr); @@ -274,7 +274,7 @@ ELF_HANDLE_DECL(elf_sym) elf_sym_by_name(struct elf_binary *elf, const char *sym sym_name = elf_strval(elf, elf->sym_strtab + name); if ( sym_name == NULL ) /* out of range, oops */ return ELF_INVALID_HANDLE(elf_sym); - if ( strcmp(sym_name, symbol) ) + if ( elf_strcmp_safe(elf, sym_name, symbol) ) continue; return sym; }