From patchwork Fri Dec 2 16:27:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 13062915 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C672C47088 for ; Fri, 2 Dec 2022 16:27:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0DF6E6B0071; Fri, 2 Dec 2022 11:27:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 068F66B0073; Fri, 2 Dec 2022 11:27:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4B2C6B0074; Fri, 2 Dec 2022 11:27:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CF1906B0071 for ; Fri, 2 Dec 2022 11:27:40 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9D75114062F for ; Fri, 2 Dec 2022 16:27:40 +0000 (UTC) X-FDA: 80197897080.18.15A4032 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf23.hostedemail.com (Postfix) with ESMTP id CA7D3140004 for ; Fri, 2 Dec 2022 16:27:39 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=zx2c4.com header.s=20210105 header.b="K2mu/d0h"; dmarc=pass (policy=quarantine) header.from=zx2c4.com; spf=pass (imf23.hostedemail.com: domain of "SRS0=rIz1=4A=zx2c4.com=Jason@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=rIz1=4A=zx2c4.com=Jason@kernel.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669998460; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=efvuuNTBLR4HLAxHYiJEHInfh40yYXX9WEzTI0HmGLg=; b=VGUBO5GO7hpWRG45IAHXy/jx8wzR//BMCEzqwo92RxRUDTAJFZj4Pe2pI2uQsnkU5u/4ge /bd9MRyicNgSP7Wyi5d15QncsjRNsL3BL8Rawh+swu+s9E0Eb1oC9BcfDC760T+6QwSOvl xkkHNNjSQfqG3JmYXhPSl73PuWXIIbw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=zx2c4.com header.s=20210105 header.b="K2mu/d0h"; dmarc=pass (policy=quarantine) header.from=zx2c4.com; spf=pass (imf23.hostedemail.com: domain of "SRS0=rIz1=4A=zx2c4.com=Jason@kernel.org" designates 145.40.68.75 as permitted sender) smtp.mailfrom="SRS0=rIz1=4A=zx2c4.com=Jason@kernel.org" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669998460; a=rsa-sha256; cv=none; b=5pZqEiHHM3UbnVICcAfcya/McBx7iN/U5Ut0cxHv/pKcFIqwhnw/EkgGVH3n5QZuifoJmn Y4yysiIC8qBaCFgxqRPz72JDGnFIYwVjvTCLi5ioF0a11Xm6kwILi5+YpeiPdyuJlDRmSy Tq1BOsl6QuxzyaBPbQdVOQPOXnuKm7U= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id EBC06B821F3; Fri, 2 Dec 2022 16:27:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1367CC43470; Fri, 2 Dec 2022 16:27:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1669998453; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=efvuuNTBLR4HLAxHYiJEHInfh40yYXX9WEzTI0HmGLg=; b=K2mu/d0hu0fuzeoZKXgWBK7i9x1nguVwSTDZXFuEa3J7bt0HjPrQQ9z0Xwa1mFBqiNMNvZ dmtf6N02mK4Q9NAL9afNBybBlywue7fSRDHHInpB8fOVOct1LuB2lxQ5gzsu8zpbNFgJ/J JqygsfBkKumNEQHAEK4/CX3G3WXIWe8= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 228e8cf8 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 2 Dec 2022 16:27:32 +0000 (UTC) From: "Jason A. Donenfeld" To: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Cc: "Jason A. Donenfeld" , "Liam R . Howlett" , Yu Zhao , Catalin Marinas , David Hildenbrand , David Howells , Davidlohr Bueso , Matthew Wilcox , SeongJae Park , Sven Schnelle , Vlastimil Babka , Will Deacon Subject: [PATCH] mm: do not BUG_ON missing brk mapping, because userspace can unmap it Date: Fri, 2 Dec 2022 17:27:24 +0100 Message-Id: <20221202162724.2009-1-Jason@zx2c4.com> MIME-Version: 1.0 X-Spamd-Result: default: False [3.90 / 9.00]; BAYES_HAM(-3.00)[99.99%]; R_MISSING_CHARSET(2.50)[]; IRL_BL_25(2.00)[52.25.139.140:received]; SUBJECT_HAS_UNDERSCORES(1.00)[]; MID_CONTAINS_FROM(1.00)[]; FORGED_SENDER(0.30)[Jason@zx2c4.com,SRS0=rIz1=4A=zx2c4.com=Jason@kernel.org]; BAD_REP_POLICIES(0.10)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_TWELVE(0.00)[16]; MIME_TRACE(0.00)[0:+]; DMARC_POLICY_ALLOW(0.00)[zx2c4.com,quarantine]; DKIM_TRACE(0.00)[zx2c4.com:+]; RCVD_COUNT_THREE(0.00)[4]; FROM_NEQ_ENVFROM(0.00)[Jason@zx2c4.com,SRS0=rIz1=4A=zx2c4.com=Jason@kernel.org]; ARC_NA(0.00)[]; R_DKIM_ALLOW(0.00)[zx2c4.com:s=20210105]; TO_MATCH_ENVRCPT_SOME(0.00)[]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(0.00)[+a:ams.source.kernel.org]; TO_DN_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: CA7D3140004 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 7snk1zcfnb6sso4wzc5ujhwjffo68pkc X-HE-Tag: 1669998459-124764 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The following program will trigger the BUG_ON that this patch removes, because the user can munmap() mm->brk: #include #include #include #include static void *brk_now(void) { return (void *)syscall(SYS_brk, 0); } static void brk_set(void *b) { assert(syscall(SYS_brk, b) != -1); } int main(int argc, char *argv[]) { void *b = brk_now(); brk_set(b + 4096); assert(munmap(b - 4096, 4096 * 2) == 0); brk_set(b); return 0; } Compile that with musl, since glibc actually uses brk(), and then execute it, and it'll hit this splat: kernel BUG at mm/mmap.c:229! invalid opcode: 0000 [#1] PREEMPT SMP CPU: 12 PID: 1379 Comm: a.out Tainted: G S U 6.1.0-rc7+ #419 RIP: 0010:__do_sys_brk+0x2fc/0x340 Code: 00 00 4c 89 ef e8 04 d3 fe ff eb 9a be 01 00 00 00 4c 89 ff e8 35 e0 fe ff e9 6e ff ff ff 4d 89 a7 20> RSP: 0018:ffff888140bc7eb0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 00000000007e7000 RCX: ffff8881020fe000 RDX: ffff8881020fe001 RSI: ffff8881955c9b00 RDI: ffff8881955c9b08 RBP: 0000000000000000 R08: ffff8881955c9b00 R09: 00007ffc77844000 R10: 0000000000000000 R11: 0000000000000001 R12: 00000000007e8000 R13: 00000000007e8000 R14: 00000000007e7000 R15: ffff8881020fe000 FS: 0000000000604298(0000) GS:ffff88901f700000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000603fe0 CR3: 000000015ba9a005 CR4: 0000000000770ee0 PKRU: 55555554 Call Trace: do_syscall_64+0x2b/0x50 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x400678 Code: 10 4c 8d 41 08 4c 89 44 24 10 4c 8b 01 8b 4c 24 08 83 f9 2f 77 0a 4c 8d 4c 24 20 4c 01 c9 eb 05 48 8b> RSP: 002b:00007ffc77863890 EFLAGS: 00000212 ORIG_RAX: 000000000000000c RAX: ffffffffffffffda RBX: 000000000040031b RCX: 0000000000400678 RDX: 00000000004006a1 RSI: 00000000007e6000 RDI: 00000000007e7000 RBP: 00007ffc77863900 R08: 0000000000000000 R09: 00000000007e6000 R10: 00007ffc77863930 R11: 0000000000000212 R12: 00007ffc77863978 R13: 00007ffc77863988 R14: 0000000000000000 R15: 0000000000000000 Instead, just error out if the original mapping has been removed. Fixes: 2e7ce7d354f2 ("mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap()") Cc: Liam R. Howlett Cc: Yu Zhao Cc: Catalin Marinas Cc: David Hildenbrand Cc: David Howells Cc: Davidlohr Bueso Cc: Matthew Wilcox Cc: SeongJae Park Cc: Sven Schnelle Cc: Vlastimil Babka Cc: Will Deacon Cc: Andrew Morton Signed-off-by: Jason A. Donenfeld Acked-by: Vlastimil Babka Reviewed-by: Liam R. Howlett Reviewed-by: SeongJae Park --- mm/mmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index c3c5c1d6103d..f5a37fe9a19e 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -226,8 +226,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) /* Search one past newbrk */ mas_set(&mas, newbrk); brkvma = mas_find(&mas, oldbrk); - BUG_ON(brkvma == NULL); - if (brkvma->vm_start >= oldbrk) + if (!brkvma || brkvma->vm_start >= oldbrk) goto out; /* mapping intersects with an existing non-brk vma. */ /* * mm->brk must be protected by write mmap_lock.