From patchwork Wed Aug 24 02:22:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Rzeszutek Wilk X-Patchwork-Id: 9296735 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 1A64E607D0 for ; Wed, 24 Aug 2016 02:25:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0CBB628DBF for ; Wed, 24 Aug 2016 02:25:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 01A0228DD8; Wed, 24 Aug 2016 02:25:14 +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, 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 2C31E28DBF for ; Wed, 24 Aug 2016 02:25:14 +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 1bcNq3-000304-Rg; Wed, 24 Aug 2016 02:22:47 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bcNq2-0002zf-KP for xen-devel@lists.xenproject.org; Wed, 24 Aug 2016 02:22:46 +0000 Received: from [193.109.254.147] by server-10.bemta-6.messagelabs.com id 83/D6-27438-5F40DB75; Wed, 24 Aug 2016 02:22:45 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpjkeJIrShJLcpLzFFi42LpnVTnqvuFZW+ 4wdLvRhbft0xmcmD0OPzhCksAYxRrZl5SfkUCa8a6i11MBcd1Kl51L2BtYPyl0MXIxSEk0MEk sX7BF2YI5xujxI8NU1ggnI2MEh8XLQVyOIGcCYwSl0/wdDFycLAJmEi8WeUIEhYRiJDofXkKr IRZIFpiUl8bG4gtLOAm8fnyMWYQm0VAVeLdvqusIDYvUPzT9jMsIGMkBOQlnt2uBwlzCrhLnD k8C2qTm8Siw2/YQWwJAWOJvll9LBMY+RYwMqxi1ChOLSpLLdI1NNdLKspMzyjJTczM0TU0MNP LTS0uTkxPzUlMKtZLzs/dxAgMEwYg2MF4e2PAIUZJDiYlUV6zt3vChfiS8lMqMxKLM+KLSnNS iw8xynBwKEnwpjPvDRcSLEpNT61Iy8wBBixMWoKDR0mEVwMkzVtckJhbnJkOkTrFqMvR1fn8H JMQS15+XqqUOO9akCIBkKKM0jy4EbDoucQoKyXMywh0lBBPQWpRbmYJqvwrRnEORiVh3mKQKT yZeSVwm14BHcEEdETL/d0gR5QkIqSkGhjTZAQaX+t/D5/zOm3OswUvZrI2eem9T3yh/PSizbE 5E794epxj1X+stsfzNaPH5ouT6ib8dr364OuVU+J3ntnGLV+v57Zc3di7a9u1S657P52eefZk Rsphloa0wzM+K6h72ckm3TVPP6vSLi4XVHDIcnPmk4P8T39UODPm1gkqxMvnXJzIft1WiaU4I 9FQi7moOBEALFodtpkCAAA= X-Env-Sender: konrad.wilk@oracle.com X-Msg-Ref: server-2.tower-27.messagelabs.com!1472005363!2939798!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: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 49990 invoked from network); 24 Aug 2016 02:22:44 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-2.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 24 Aug 2016 02:22:44 -0000 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u7O2Mc2v000716 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 24 Aug 2016 02:22:38 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.13.8/8.13.8) with ESMTP id u7O2McLr007415 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 24 Aug 2016 02:22:38 GMT Received: from abhmp0014.oracle.com (abhmp0014.oracle.com [141.146.116.20]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id u7O2MbE4000701; Wed, 24 Aug 2016 02:22:37 GMT Received: from localhost.localdomain.com (/209.6.196.81) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 23 Aug 2016 19:22:36 -0700 From: Konrad Rzeszutek Wilk To: xen-devel@lists.xenproject.org, konrad@kernel.org, ross.lagerwall@citrix.com Date: Tue, 23 Aug 2016 22:22:04 -0400 Message-Id: <1472005332-32207-2-git-send-email-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <1472005332-32207-1-git-send-email-konrad.wilk@oracle.com> References: <1472005332-32207-1-git-send-email-konrad.wilk@oracle.com> X-Source-IP: aserv0021.oracle.com [141.146.126.233] Cc: Jan Beulich , Konrad Rzeszutek Wilk Subject: [Xen-devel] [PATCH v4 1/9] livepatch: Clear .bss when payload is reverted 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 So that when we apply the patch again the .bss is cleared. Otherwise we may find some variables containing old values. The payloads may contain various .bss - especially if -fdata-sections is used which can create .bss. sections. Signed-off-by: Konrad Rzeszutek Wilk --- Cc: Ross Lagerwall Cc: Jan Beulich v3: Initial submission v4: s/EINVAL/EOPNOTSUPP/ Do memset in a single place Support multiple BSS sections. --- xen/common/livepatch.c | 60 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 5da28a3..b5aef57 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -70,6 +70,9 @@ struct payload { unsigned int nsyms; /* Nr of entries in .strtab and symbols. */ struct livepatch_build_id id; /* ELFNOTE_DESC(.note.gnu.build-id) of the payload. */ struct livepatch_build_id dep; /* ELFNOTE_DESC(.livepatch.depends). */ + void **bss; /* .bss's of the payload. */ + size_t *bss_size; /* and their sizes. */ + size_t n_bss; /* Size of the array. */ char name[XEN_LIVEPATCH_NAME_SIZE]; /* Name of it. */ }; @@ -255,12 +258,18 @@ static struct payload *find_payload(const char *name) static void free_payload_data(struct payload *payload) { /* Set to zero until "move_payload". */ - if ( !payload->pages ) - return; - - vfree((void *)payload->text_addr); + if ( payload->pages ) + { + vfree((void *)payload->text_addr); + payload->pages = 0; + } - payload->pages = 0; + if ( payload->n_bss ) + { + xfree(payload->bss); + xfree(payload->bss_size); + payload->n_bss = 0; + } } /* @@ -287,6 +296,7 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) unsigned int i; size_t size = 0; unsigned int *offset; + unsigned int n_bss = 0; int rc = 0; offset = xmalloc_array(unsigned int, elf->hdr->e_shnum); @@ -309,7 +319,11 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) calc_section(&elf->sec[i], &payload->text_size, &offset[i]); else if ( !(elf->sec[i].sec->sh_flags & SHF_EXECINSTR) && (elf->sec[i].sec->sh_flags & SHF_WRITE) ) + { calc_section(&elf->sec[i], &payload->rw_size, &offset[i]); + if ( elf->sec[i].sec->sh_type == SHT_NOBITS ) + n_bss++; + } else if ( !(elf->sec[i].sec->sh_flags & SHF_EXECINSTR) && !(elf->sec[i].sec->sh_flags & SHF_WRITE) ) calc_section(&elf->sec[i], &payload->ro_size, &offset[i]); @@ -334,12 +348,8 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) size = PFN_UP(size); /* Nr of pages. */ text_buf = vmalloc_xen(size * PAGE_SIZE); if ( !text_buf ) - { - dprintk(XENLOG_ERR, LIVEPATCH "%s: Could not allocate memory for payload!\n", - elf->name); - rc = -ENOMEM; - goto out; - } + goto out_mem; + rw_buf = text_buf + PAGE_ALIGN(payload->text_size); ro_buf = rw_buf + PAGE_ALIGN(payload->rw_size); @@ -348,6 +358,14 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) payload->rw_addr = rw_buf; payload->ro_addr = ro_buf; + payload->bss = xmalloc_array(void *, n_bss); + payload->bss_size = xmalloc_array(size_t, n_bss); + if ( !payload->bss || !payload->bss_size ) + goto out_mem; + + payload->n_bss = n_bss; + n_bss = 0; /* Reusing as counter. */ + for ( i = 1; i < elf->hdr->e_shnum; i++ ) { if ( elf->sec[i].sec->sh_flags & SHF_ALLOC ) @@ -374,14 +392,24 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf) elf->name, elf->sec[i].name, elf->sec[i].load_addr); } else - memset(elf->sec[i].load_addr, 0, elf->sec[i].sec->sh_size); + { + payload->bss[n_bss] = elf->sec[i].load_addr; + payload->bss_size[n_bss++] = elf->sec[i].sec->sh_size; + } } } + ASSERT(n_bss == payload->n_bss); out: xfree(offset); return rc; + + out_mem: + dprintk(XENLOG_ERR, LIVEPATCH "%s: Could not allocate memory for payload!\n", + elf->name); + rc = -ENOMEM; + goto out; } static int secure_payload(struct payload *payload, struct livepatch_elf *elf) @@ -997,6 +1025,10 @@ static int apply_payload(struct payload *data) printk(XENLOG_INFO LIVEPATCH "%s: Applying %u functions\n", data->name, data->nfuncs); + /* And clear the BSS for subsequent operation. */ + for ( i = 0; i < data->n_bss; i++ ) + memset(data->bss[i], 0, data->bss_size[i]); + arch_livepatch_quiesce(); for ( i = 0; i < data->nfuncs; i++ ) @@ -1513,9 +1545,9 @@ static void livepatch_printall(unsigned char key) list_for_each_entry ( data, &payload_list, list ) { - printk(" name=%s state=%s(%d) %p (.data=%p, .rodata=%p) using %u pages.\n", + printk(" name=%s state=%s(%d) %p (.data=%p, .rodata=%p) using %u pages (%zu .bss).\n", data->name, state2str(data->state), data->state, data->text_addr, - data->rw_addr, data->ro_addr, data->pages); + data->rw_addr, data->ro_addr, data->pages, data->n_bss); for ( i = 0; i < data->nfuncs; i++ ) {