From patchwork Tue Jul 11 16:53:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Rzeszutek Wilk X-Patchwork-Id: 9835257 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 94D06602A0 for ; Tue, 11 Jul 2017 16:55:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8587C28573 for ; Tue, 11 Jul 2017 16:55:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7993C28550; Tue, 11 Jul 2017 16:55: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=-3.7 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM,UNPARSEABLE_RELAY 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 09A4528573 for ; Tue, 11 Jul 2017 16:55:49 +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 1dUyPi-0006uz-Bb; Tue, 11 Jul 2017 16:53:30 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dUyPg-0006uC-JV for xen-devel@lists.xenproject.org; Tue, 11 Jul 2017 16:53:28 +0000 Received: from [85.158.139.211] by server-5.bemta-5.messagelabs.com id B1/43-02177-78205695; Tue, 11 Jul 2017 16:53:27 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprMIsWRWlGSWpSXmKPExsXSO6nOVbedKTX SYMpaaYvvWyYzOTB6HP5whSWAMYo1My8pvyKBNWPmu9fMBdMkKub8u8newLhNqIuRk0NIYDKT xKp7Wl2MXED2b0aJ3rnd7BDORkaJrlm7mSGcbkaJs4/us0K0FEk8/fyOqYuRg4NNwETizSpHk LCIgK1E+6IVrCD1zALzGCX+n3nBApIQFnCQuHy2nRHEZhFQlTjz5QYbSC+vgLXE8ke1IGEJAT mJS9u+MIPYnAI2En8XtjFCrLKW+Nk1ixWixlii/e1FtgmM/AsYGVYxahSnFpWlFukaWeglFWW mZ5TkJmbm6BoamOrlphYXJ6an5iQmFesl5+duYgQGUD0DA+MOxr5VfocYJTmYlER5g2JTIoX4 kvJTKjMSizPii0pzUosPMcpwcChJ8EowpkYKCRalpqdWpGXmAEMZJi3BwaMkwhv/F6iVt7ggM bc4Mx0idYpRl+PVhP/fmIRY8vLzUqXEeS1AZgiAFGWU5sGNgMXVJUZZKWFeRgYGBiGegtSi3M wSVPlXjOIcjErCvIYgU3gy80rgNr0COoIJ6Ig12WBHlCQipKQaGCcnxGc+aImc155dwsOx7FL FtbTFcw/lNbjet5M91mzTPfP6uVesimxTY5jZBC858Hk2v+T/vCj44lr386p9K86v4j23rUVk L290l0bdz7UKMmYai+Q07jguFHHducHkq5ZdToiAHbt++Y9N/z3mSC1JCHiweusVw32i+Qsrp a++PbbsBz+LmRJLcUaioRZzUXEiAMOBKh+mAgAA X-Env-Sender: konrad@char.us.oracle.com X-Msg-Ref: server-6.tower-206.messagelabs.com!1499792005!102229069!1 X-Originating-IP: [141.146.126.69] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTQxLjE0Ni4xMjYuNjkgPT4gMjc3MjE4\n X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 34909 invoked from network); 11 Jul 2017 16:53:27 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-6.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 11 Jul 2017 16:53:27 -0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v6BGrHaJ025809 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Jul 2017 16:53:17 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v6BGrGGb007571 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Jul 2017 16:53:17 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v6BGrG7Z019337; Tue, 11 Jul 2017 16:53:16 GMT Received: from char.us.oracle.com (/10.137.176.158) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 11 Jul 2017 09:53:15 -0700 Received: by char.us.oracle.com (Postfix, from userid 1000) id C73776A0126; Tue, 11 Jul 2017 12:53:14 -0400 (EDT) From: Konrad Rzeszutek Wilk To: xen-devel@lists.xenproject.org, ross.lagerwall@citrix.com Date: Tue, 11 Jul 2017 12:53:11 -0400 Message-Id: <20170711165313.26497-2-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170711165313.26497-1-konrad.wilk@oracle.com> References: <20170711165313.26497-1-konrad.wilk@oracle.com> X-Source-IP: aserv0022.oracle.com [141.146.126.234] Cc: andrew.cooper3@citrix.com, julien.grall@arm.com, sstabellini@kernel.org, jbeulich@suse.com, Konrad Rzeszutek Wilk Subject: [Xen-devel] [PATCH v1 1/3] xen/livepatch: Tighten alignment checks. 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: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP The ELF specification mentions nothing about the sh_size being modulo the sh_addralign. Only that sh_addr MUST be aligned on sh_addralign if sh_addralign is not zero or one. We on loading did not take this in-to account so this patch adds two checks: One on the ELF file itself as it is being parsed, and then when we copy the payload contents in memory - and check the aligmnent needs there. Signed-off-by: Konrad Rzeszutek Wilk --- xen/common/livepatch.c | 9 +++++++++ xen/common/livepatch_elf.c | 7 +++++++ xen/include/xen/elfstructs.h | 2 ++ 3 files changed, 18 insertions(+) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index ca36161..5d53096 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -406,6 +406,15 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) ASSERT(offset[i] != UINT_MAX); elf->sec[i].load_addr = buf + offset[i]; + if ( elf->sec[i].sec->sh_addralign > 1 && + ((Elf_Addr)elf->sec[i].load_addr % elf->sec[i].sec->sh_addralign) ) + { + dprintk(XENLOG_ERR, LIVEPATCH "%s: %s @ %p is not aligned (%"PRIuElfWord")\n", + elf->name, elf->sec[i].name, elf->sec[i].load_addr, + elf->sec[i].sec->sh_addralign); + rc = -EINVAL; + goto out; + } /* Don't copy NOBITS - such as BSS. */ if ( elf->sec[i].sec->sh_type != SHT_NOBITS ) diff --git a/xen/common/livepatch_elf.c b/xen/common/livepatch_elf.c index b69e271..852e9c4 100644 --- a/xen/common/livepatch_elf.c +++ b/xen/common/livepatch_elf.c @@ -86,6 +86,13 @@ static int elf_resolve_sections(struct livepatch_elf *elf, const void *data) delta < sizeof(Elf_Ehdr) ? "at ELF header" : "is past end"); return -EINVAL; } + else if ( sec[i].sec->sh_addralign > 1 && + sec[i].sec->sh_addr % sec[i].sec->sh_addralign ) + { + dprintk(XENLOG_ERR, LIVEPATCH "%s: Section [%u] size (%"PRIuElfWord") is not aligned properly (%"PRIuElfWord")\n", + elf->name, i, sec[i].sec->sh_size, sec[i].sec->sh_addralign); + return -EINVAL; + } else if ( (sec[i].sec->sh_flags & (SHF_WRITE | SHF_ALLOC)) && sec[i].sec->sh_type == SHT_NOBITS && sec[i].sec->sh_size > LIVEPATCH_MAX_SIZE ) diff --git a/xen/include/xen/elfstructs.h b/xen/include/xen/elfstructs.h index 950e149..edc8862 100644 --- a/xen/include/xen/elfstructs.h +++ b/xen/include/xen/elfstructs.h @@ -555,6 +555,7 @@ typedef struct { #if defined(ELFSIZE) && (ELFSIZE == 32) #define PRIxElfAddr "08x" +#define PRIuElfWord "08u" #define Elf_Ehdr Elf32_Ehdr #define Elf_Phdr Elf32_Phdr @@ -582,6 +583,7 @@ typedef struct { #define AuxInfo Aux32Info #elif defined(ELFSIZE) && (ELFSIZE == 64) #define PRIxElfAddr PRIx64 +#define PRIuElfWord PRIu64 #define Elf_Ehdr Elf64_Ehdr #define Elf_Phdr Elf64_Phdr