Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3333427imu; Sun, 11 Nov 2018 12:35:20 -0800 (PST) X-Google-Smtp-Source: AJdET5e/Hfnlam8WhbA5ePOVsRRYC8PVaiVREQiSA0sUvYLrzYwwTwWQPR9XkojY6gpzKCXLbg3Y X-Received: by 2002:a17:902:1e3:: with SMTP id b90-v6mr14419795plb.117.1541968520075; Sun, 11 Nov 2018 12:35:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541968520; cv=none; d=google.com; s=arc-20160816; b=G6sc2f70rcKdEWFfuW0pnIYNdBFRrDK8D2wgZaF8JcnL7FOvcYbJl9cjqey46DBf8J fwyjYGjtv1zz0mHXrHqfDCp9Kbih3MJ9/a6TD06DuyAAxFdbjLyNOzAUWZ5QugIMzT7m y7icPeeBEbuqv9xbXu7nijFS5SL13QNnMHLhxQiOrbamsLIAeqCLR+w7GkMOHcAn0L7z L//Ib2p9kG3RU7YnidSByV3s/TlIPuo/502Na3/7lZXj3PT2QWzMy0SriEPYkgxMG5Cc z4fyz1cF1aPR5dIWsuT5F/7yDyzzvp456V8VxDuAc+ZpBHb+yEcBhDsJUZwW8hmn7hnU xa/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=FYxboRasOVUrk2ysvb4GKEhvW9AKwBK0M9oiAIaN8UU=; b=V4hqae1LOGs+R7v++yW/yZbGoRxtNp/KyhUDu9cGhRrJ0zPcxcH4behu8T8njtg+zL LYmqhvCui6zmkNdYCMvbtAOtkDPOLnSrXkv356Gn2GO6/5dDHV291THI9cRnOS87DUTx +/Ek+nqww25t95NY7xgsOZc4X6VNIkKf4IsKpQiF2SAasedJtr8kYhQTIUVounuDpJqh ccXPHH6Ft18YrdzIVHDCGyMHrZQCe/ACYSmxDr0tHw2SLdCNNHI30tZgV1hDcj20mXtL UeuiWf8NCBS6BAY32C/Ows5h7iQ8CaXxH9+FclTY49DYkyHmHyx6AAPyJL/VYpFTOBIT epoQ== ARC-Authentication-Results: i=1; mx.google.com; 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 35-v6si7907703ple.289.2018.11.11.12.35.05; Sun, 11 Nov 2018 12:35:20 -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; 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 S1730439AbeKLGYP (ORCPT + 99 others); Mon, 12 Nov 2018 01:24:15 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:49974 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730206AbeKLFsN (ORCPT ); Mon, 12 Nov 2018 00:48:13 -0500 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLvsZ-0000oN-46; Sun, 11 Nov 2018 19:58:43 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gLvsW-0001jI-If; Sun, 11 Nov 2018 19:58:40 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Huang, Ying" , "Michael S. Tsirkin" , "Dan Williams" , "syzbot" , "Linus Torvalds" , "Aneesh Kumar K.V" , "Oscar Salvador" , "Zi Yan" , "Al Viro" , "Michal Hocko" , "Kirill A. Shutemov" , "Tetsuo Handa" Date: Sun, 11 Nov 2018 19:49:05 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 254/366] mm: do not bug_on on incorrect length in __mm_populate() In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.61-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Michal Hocko 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 3.16: - There is no do_brk_flags() function; update do_brk() - do_brk(), vm_brk() return the address on success - Adjust filename, context] Signed-off-by: Ben Hutchings --- --- a/mm/mlock.c +++ b/mm/mlock.c @@ -669,8 +669,6 @@ int __mm_populate(unsigned long start, u 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) { --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2751,21 +2751,15 @@ static inline void verify_mm_writelocked * 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); @@ -2834,12 +2828,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);