Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3013927imu; Mon, 19 Nov 2018 09:21:46 -0800 (PST) X-Google-Smtp-Source: AJdET5dK5kQamQDK3xBRDZ62wtzrvU61UZIOFLEbQ8Pu2y2fcZNCcwRaVHerCuSxVRHAG3kaZ95c X-Received: by 2002:a62:1e83:: with SMTP id e125-v6mr23700674pfe.231.1542648106794; Mon, 19 Nov 2018 09:21:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542648106; cv=none; d=google.com; s=arc-20160816; b=PiM2un6mbt/5ROewCuvM+eNgf+YGkrZOB1tQewbg8rDLhMGhJTCxh2dgHfwvT5ZgLg vMSWeZ+ALLMkQYFqdvAeduwhbNGf7nBX3c+/+KV1p/zMmsl5/NOmCbvI99SxJVVtB93K wTBWEqH5H8iht1rPqwSUdHHjjKd2yxVBQX47V3vwEVv5zwzzCypMKmMq5R5p5W7KvpZU 31kKFqi7zJs1sazG8+zRJ5lne0svJL2ulEX6vx6mvG35zmgzesplN8r08wsFNqSDxknu M75H32MR6kzFUx+mXyPtiub9AuZe4XNv2q7cR6OPL5yKxRAyyj6qw6+J05bmpbNBlRru fOrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LfHM64ZSGNRUkN0T1bmOnlzLLgkCkw8Q/gyQlwaXhGw=; b=BJ+xu0YnrFoXV9Up418VlOBmOWLkZvOW3bsx9/bSqfme5A5WDJyz9cmzJBD4th7G/I JFrsOMPhheV+2m3VLck2XvxM50n/FTKMozn+CQPC1RS5PXn+bJ+50N/oH0OD78OQvK1Y pD+VZ714I63GDTI9enUxWTmadsSEuVxKrSOa6HMD3hZooEwqTQT6AW16MRuAf3jBNzqq rFkixn2T/Kkei8ObSfSuZB9vlDO8A9ybTgcBYDb33rZ44VDCrcMt94n1dFj1Rglsufwg Can/MgKm6e99kpovJIXMBzWocXNHvrOvyqaaWaHhk92iVFFMb4JLx02n76HtsFtQEZAL pyhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1OczWtjy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v189si38653069pgb.398.2018.11.19.09.21.31; Mon, 19 Nov 2018 09:21:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1OczWtjy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405500AbeKTDZK (ORCPT + 99 others); Mon, 19 Nov 2018 22:25:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:38212 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404694AbeKTDZK (ORCPT ); Mon, 19 Nov 2018 22:25:10 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 98F5A223C7; Mon, 19 Nov 2018 17:00:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542646853; bh=6OBVnQmEuxFkeR64b7OFg+kAk3XG3X8nf8lplQ49ixc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1OczWtjyPSxtM9gd9gq/oHKrRtG+rPXMQQtXMvwsjxXHbO0bolmjnzCn235mRl2jY BSJHQUEXp12sNaAzsrPpzjRpoZwhv71EO+lYwBcAkmflKeaDzeFrFtgVb9DzkCfTPB fZhgzBt+BrmhAGEZJhtBLWhMOFaqWiQ6xflphV6k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michal Hocko , syzbot , Tetsuo Handa , Oscar Salvador , Zi Yan , "Aneesh Kumar K.V" , Dan Williams , "Kirill A. Shutemov" , "Michael S. Tsirkin" , Al Viro , "Huang, Ying" , Andrew Morton , Linus Torvalds , Ben Hutchings , Sasha Levin Subject: [PATCH 4.4 116/160] mm: do not bug_on on incorrect length in __mm_populate() Date: Mon, 19 Nov 2018 17:29:15 +0100 Message-Id: <20181119162641.942118072@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181119162630.031306128@linuxfoundation.org> References: <20181119162630.031306128@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ commit bb177a732c4369bb58a1fe1df8f552b6f0f7db5f upstream. syzbot has noticed that a specially crafted library can easily hit VM_BUG_ON in __mm_populate kernel BUG at mm/gup.c:1242! invalid opcode: 0000 [#1] SMP CPU: 2 PID: 9667 Comm: a.out Not tainted 4.18.0-rc3 #644 Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/19/2017 RIP: 0010:__mm_populate+0x1e2/0x1f0 Code: 55 d0 65 48 33 14 25 28 00 00 00 89 d8 75 21 48 83 c4 20 5b 41 5c 41 5d 41 5e 41 5f 5d c3 e8 75 18 f1 ff 0f 0b e8 6e 18 f1 ff <0f> 0b 31 db eb c9 e8 93 06 e0 ff 0f 1f 00 55 48 89 e5 53 48 89 fb Call Trace: vm_brk_flags+0xc3/0x100 vm_brk+0x1f/0x30 load_elf_library+0x281/0x2e0 __ia32_sys_uselib+0x170/0x1e0 do_fast_syscall_32+0xca/0x420 entry_SYSENTER_compat+0x70/0x7f The reason is that the length of the new brk is not page aligned when we try to populate the it. There is no reason to bug on that though. do_brk_flags already aligns the length properly so the mapping is expanded as it should. All we need is to tell mm_populate about it. Besides that there is absolutely no reason to to bug_on in the first place. The worst thing that could happen is that the last page wouldn't get populated and that is far from putting system into an inconsistent state. Fix the issue by moving the length sanitization code from do_brk_flags up to vm_brk_flags. The only other caller of do_brk_flags is brk syscall entry and it makes sure to provide the proper length so t here is no need for sanitation and so we can use do_brk_flags without it. Also remove the bogus BUG_ONs. [osalvador@techadventures.net: fix up vm_brk_flags s@request@len@] Link: http://lkml.kernel.org/r/20180706090217.GI32658@dhcp22.suse.cz Signed-off-by: Michal Hocko Reported-by: syzbot Tested-by: Tetsuo Handa Reviewed-by: Oscar Salvador Cc: Zi Yan Cc: "Aneesh Kumar K.V" Cc: Dan Williams Cc: "Kirill A. Shutemov" Cc: Michael S. Tsirkin Cc: Al Viro Cc: "Huang, Ying" Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 4.4: - There is no do_brk_flags() function; update do_brk() - do_brk(), vm_brk() return the address on success - Adjust context] Signed-off-by: Ben Hutchings Signed-off-by: Sasha Levin --- mm/gup.c | 2 -- mm/mmap.c | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index b599526db9f7..018144c4b9ec 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -940,8 +940,6 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) int locked = 0; long ret = 0; - VM_BUG_ON(start & ~PAGE_MASK); - VM_BUG_ON(len != PAGE_ALIGN(len)); end = start + len; for (nstart = start; nstart < end; nstart = nend) { diff --git a/mm/mmap.c b/mm/mmap.c index dd9205542a86..3074dbcd9621 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2808,21 +2808,15 @@ static inline void verify_mm_writelocked(struct mm_struct *mm) * anonymous maps. eventually we may be able to do some * brk-specific accounting here. */ -static unsigned long do_brk(unsigned long addr, unsigned long request) +static unsigned long do_brk(unsigned long addr, unsigned long len) { struct mm_struct *mm = current->mm; struct vm_area_struct *vma, *prev; - unsigned long flags, len; + unsigned long flags; struct rb_node **rb_link, *rb_parent; pgoff_t pgoff = addr >> PAGE_SHIFT; int error; - len = PAGE_ALIGN(request); - if (len < request) - return -ENOMEM; - if (!len) - return addr; - flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); @@ -2890,12 +2884,19 @@ out: return addr; } -unsigned long vm_brk(unsigned long addr, unsigned long len) +unsigned long vm_brk(unsigned long addr, unsigned long request) { struct mm_struct *mm = current->mm; + unsigned long len; unsigned long ret; bool populate; + len = PAGE_ALIGN(request); + if (len < request) + return -ENOMEM; + if (!len) + return addr; + down_write(&mm->mmap_sem); ret = do_brk(addr, len); populate = ((mm->def_flags & VM_LOCKED) != 0); -- 2.17.1