From patchwork Fri Oct 6 03:37:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoquan He X-Patchwork-Id: 9988323 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 708B36020F for ; Fri, 6 Oct 2017 03:38:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6380D286F3 for ; Fri, 6 Oct 2017 03:38:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57F6E28CD3; Fri, 6 Oct 2017 03:38:32 +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 19A9F286F3 for ; Fri, 6 Oct 2017 03:38:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751907AbdJFDiV (ORCPT ); Thu, 5 Oct 2017 23:38:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38060 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751755AbdJFDiU (ORCPT ); Thu, 5 Oct 2017 23:38:20 -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 A693F25787; Fri, 6 Oct 2017 03:38:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A693F25787 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.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 D522765EA6; Fri, 6 Oct 2017 03:38:11 +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 2/3] binfmt_elf: Get the total_size only for dynamic loader in load_elf_binary() Date: Fri, 6 Oct 2017 11:37:57 +0800 Message-Id: <1507261078-23615-3-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.39]); Fri, 06 Oct 2017 03:38:19 +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 commit: eab09532d4 ("binfmt_elf: use ELF_ET_DYN_BASE only for PIE") ... PIE programs are loaded offset from ELF_ET_DYN_BASE, and its map has been set as MAP_FIXED. Only dynamic loader will be mapped from below mm->mmap_base (E.g "./ld.so someprog"), and need take account of the need to allocate sufficient space for the entire loader image to avoid the case that 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 mm->mmap_base. Whether it's harmless or not, we should not allow program to map above mm->mmap_base. So here change to get the total_size only for dynamic loader in load_elf_binary(). Signed-off-by: Baoquan He --- fs/binfmt_elf.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 72b7ecba7ead..d7a8a53a6f18 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -967,9 +967,17 @@ static int load_elf_binary(struct linux_binprm *bprm) if (current->flags & PF_RANDOMIZE) load_bias += arch_mmap_rnd(); elf_flags |= MAP_FIXED; - } else + } else { load_bias = 0; + total_size = total_mapping_size(elf_phdata, + loc->elf_ex.e_phnum); + if (!total_size) { + retval = -EINVAL; + goto out_free_dentry; + } + } + /* * Since load_bias is used for all subsequent loading * calculations, we must lower it by the first vaddr @@ -978,13 +986,6 @@ static int load_elf_binary(struct linux_binprm *bprm) * is then page aligned. */ load_bias = ELF_PAGESTART(load_bias - vaddr); - - total_size = total_mapping_size(elf_phdata, - loc->elf_ex.e_phnum); - if (!total_size) { - retval = -EINVAL; - goto out_free_dentry; - } } error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,