From patchwork Fri Oct 6 03:37:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 9988327 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 ABF4B6020F for ; Fri, 6 Oct 2017 03:38:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F436286F3 for ; Fri, 6 Oct 2017 03:38:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 944D828CD3; Fri, 6 Oct 2017 03:38:44 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 495EE286F3 for ; Fri, 6 Oct 2017 03:38:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751932AbdJFDia (ORCPT ); Thu, 5 Oct 2017 23:38:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42884 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751755AbdJFDi3 (ORCPT ); Thu, 5 Oct 2017 23:38:29 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1A7096B223; Fri, 6 Oct 2017 03:38:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1A7096B223 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=bhe@redhat.com Received: from x1.redhat.com (ovpn-12-19.pek2.redhat.com [10.72.12.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4174965EA6; Fri, 6 Oct 2017 03:38:19 +0000 (UTC) From: Baoquan He To: linux-kernel@vger.kernel.org Cc: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, oleg@redhat.com, mhocko@kerne.org, keescook@chromium.org, jkosina@suse.cz, mingo@elte.hu, torvalds@linux-foundation.org, Baoquan He Subject: [PATCH 3/3] binfmt_elf: Search an unmapped area with total_size but not map the whole image Date: Fri, 6 Oct 2017 11:37:58 +0800 Message-Id: <1507261078-23615-4-git-send-email-bhe@redhat.com> In-Reply-To: <1507261078-23615-1-git-send-email-bhe@redhat.com> References: <1507261078-23615-1-git-send-email-bhe@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 06 Oct 2017 03:38:29 +0000 (UTC) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In elf_map(), we can search an unmapped area for the whole image of dynamic loader with total_size, then map the first PT_LOAD segment with its own size. But not map the while image, then unmap the remainder at the end. And also update the code comment in elf_map() accordingly. Signed-off-by: Baoquan He --- fs/binfmt_elf.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index d7a8a53a6f18..bebb9f2c934e 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -357,20 +357,23 @@ static unsigned long elf_map(struct file *filep, unsigned long addr, return addr; /* - * total_size is the size of the ELF (interpreter) image. - * The _first_ mmap needs to know the full size, otherwise - * randomization might put this image into an overlapping - * position with the ELF binary image. (since size < total_size) - * So we first map the 'big' image - and unmap the remainder at - * the end. (which unmap is needed for ELF images with holes.) + * total_size is the size of the ELF (interpreter) image if non-zero. + * It gets value either in load_elf_interp(), or in load_elf_binary() + * for "./ld.so someprog" testing. The _first_ mmap needs to know the + * full size, otherwise the first PT_LOAD segment is mapped below + * mm->mmap_base, the subsequent PT_LOAD segment(s) end up being mapped + * above mm->mmap_base into the area that is supposed to be the "gap" + * between the stack and the binary. So we search an unmapped area of + * total_size big, then use the found address as hint to do mmap. */ if (total_size) { total_size = ELF_PAGEALIGN(total_size); - map_addr = vm_mmap(filep, addr, total_size, prot, flags, off); - if (!BAD_ADDR(map_addr)) - vm_munmap(map_addr+size, total_size-size); - } else - map_addr = vm_mmap(filep, addr, size, prot, flags, off); + addr = get_unmapped_area(file, addr, total_size, off, flags); + if (offset_in_page(addr)) + return addr; + } + + map_addr = vm_mmap(filep, addr, size, prot, flags, off); return(map_addr); }