From patchwork Tue Aug 28 15:22:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivaprasad G Bhat X-Patchwork-Id: 10578719 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C1F64920 for ; Tue, 28 Aug 2018 15:36:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0DBE2A5BE for ; Tue, 28 Aug 2018 15:36:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A4D062A5C9; Tue, 28 Aug 2018 15:36:37 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 280E32A5BE for ; Tue, 28 Aug 2018 15:36:37 +0000 (UTC) Received: from localhost ([::1]:38722 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fug2m-0000sk-A7 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 28 Aug 2018 11:36:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43993) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fug12-0007Kl-IV for qemu-devel@nongnu.org; Tue, 28 Aug 2018 11:34:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fufou-0002fC-HI for qemu-devel@nongnu.org; Tue, 28 Aug 2018 11:22:20 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:36084) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fufou-0002eb-87 for qemu-devel@nongnu.org; Tue, 28 Aug 2018 11:22:16 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w7SFKfq8070952 for ; Tue, 28 Aug 2018 11:22:14 -0400 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0a-001b2d01.pphosted.com with ESMTP id 2m585daftc-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 Aug 2018 11:22:14 -0400 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 Aug 2018 16:22:12 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 Aug 2018 16:22:09 +0100 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w7SFM8wY30408710 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 28 Aug 2018 15:22:08 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 64C2352050; Tue, 28 Aug 2018 18:22:05 +0100 (BST) Received: from lep8c.aus.stglabs.ibm.com (unknown [9.40.192.207]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id BA0995205A; Tue, 28 Aug 2018 18:22:04 +0100 (BST) From: Shivaprasad G Bhat To: riku.voipio@iki.fi, laurent@vivier.eu Date: Tue, 28 Aug 2018 10:22:07 -0500 User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 18082815-0028-0000-0000-000002F0E68B X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18082815-0029-0000-0000-000023AA3F00 Message-Id: <153546966871.75539.10980006517656536139.stgit@lep8c.aus.stglabs.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-08-28_06:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1808280156 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v2] linux-user: elf: mmap all the target-pages of hostpage for data segment X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-ppc@nongnu.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP If the hostpage size is greater than the TARGET_PAGESIZE, the target-pages of size TARGET_PAGESIZE are marked valid only till the length requested during the elfload. The glibc attempts to consume unused space in the last page of data segment(__libc_memalign() in elf/dl-minimal.c). If PT_LOAD p_align is greater than or equal to hostpage size, the GLRO(dl_pagesize) is actually the host pagesize as set in the auxillary vectors. So, there is no explicit mmap request for the remaining target-pages on the last hostpage. The glibc assumes that particular space as available and subsequent attempts to use those addresses lead to crash as the target_mmap has not marked them valid for target-pages as valid. The issue is seen when trying to chroot to 16.04-x86_64 ubuntu on a PPC64 host where the fork fails to access the thread_id as it is allocated on a page not marked valid. The recent glibc doesnt have checks for thread-id in fork, but the issue can manifest somewhere else, none the less. The fix here is to map all the target-pages of the hostpage during the elfload if the p_align is greater than or equal to hostpage size, for data segment to allow the glibc for proper consumption. Signed-off-by: Shivaprasad G Bhat --- v1: https://lists.gnu.org/archive/html/qemu-devel/2018-08/msg05730.html Changes from v1: - Made the conditionals consistent with the commit "33143c446e" and changed the commit message accordingly. linux-user/elfload.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 8638612aec..cced43f45c 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1438,9 +1438,23 @@ struct exec /* Necessary parameters */ #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE -#define TARGET_ELF_PAGESTART(_v) ((_v) & \ - ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1)) -#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1)) +#define TARGET_ELF_PAGESTART(_v, _a, _s, _m) \ + (((_a & ~_m) != 0) ? \ + (_v) & ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE - 1) : \ + ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \ + (_v) & ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE - 1) : \ + (_v) & ~(abi_ulong)(_s - 1))); +#define TARGET_ELF_PAGEOFFSET(_v, _a, _s, _m) \ + (((_a & ~_m) != 0) ? \ + (_v) & (TARGET_ELF_EXEC_PAGESIZE - 1) : \ + ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \ + (_v) & (TARGET_ELF_EXEC_PAGESIZE - 1) : \ + (_v) & (_s - 1))); +#define TARGET_ELF_PAGELENGTH(_v, _a, _s, _m) \ + (((_a & ~_m) != 0) ? \ + TARGET_PAGE_ALIGN(_v) : \ + ((TARGET_ELF_EXEC_PAGESIZE > _s) ? \ + TARGET_PAGE_ALIGN(_v) : HOST_PAGE_ALIGN(_v))); #define DLINFO_ITEMS 15 @@ -2279,7 +2293,7 @@ static void load_elf_image(const char *image_name, int image_fd, for (i = 0; i < ehdr->e_phnum; i++) { struct elf_phdr *eppnt = phdr + i; if (eppnt->p_type == PT_LOAD) { - abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; + abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len; int elf_prot = 0; if (eppnt->p_flags & PF_R) elf_prot = PROT_READ; @@ -2287,10 +2301,17 @@ static void load_elf_image(const char *image_name, int image_fd, if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC; vaddr = load_bias + eppnt->p_vaddr; - vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr); - vaddr_ps = TARGET_ELF_PAGESTART(vaddr); - - error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, + vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr, info->alignment, + qemu_host_page_size, + qemu_host_page_mask); + vaddr_ps = TARGET_ELF_PAGESTART(vaddr, info->alignment, + qemu_host_page_size, + qemu_host_page_mask); + vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po, + info->alignment, + qemu_host_page_size, + qemu_host_page_mask); + error = target_mmap(vaddr_ps, vaddr_len, elf_prot, MAP_PRIVATE | MAP_FIXED, image_fd, eppnt->p_offset - vaddr_po); if (error == -1) {