From patchwork Wed Jul 14 03:19:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 12375857 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8B24C07E95 for ; Wed, 14 Jul 2021 03:20:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 695CF60720 for ; Wed, 14 Jul 2021 03:20:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 695CF60720 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6575B6B0088; Tue, 13 Jul 2021 23:20:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 62E526B008C; Tue, 13 Jul 2021 23:20:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CE2C6B0092; Tue, 13 Jul 2021 23:20:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0214.hostedemail.com [216.40.44.214]) by kanga.kvack.org (Postfix) with ESMTP id 279C36B0088 for ; Tue, 13 Jul 2021 23:20:05 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 17B7D184B83C4 for ; Wed, 14 Jul 2021 03:20:04 +0000 (UTC) X-FDA: 78359739528.05.3BBD53A Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf26.hostedemail.com (Postfix) with ESMTP id A6CDA20019E8 for ; Wed, 14 Jul 2021 03:20:03 +0000 (UTC) Received: by mail-pg1-f172.google.com with SMTP id s18so911940pgg.8 for ; Tue, 13 Jul 2021 20:20:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dpsH7mIcXqvQoo7xmLF/2aa6XMVIAHRRWDLOhYJdccM=; b=exIQaQBU3X0JT6wmWUXd6kAxACmjf23dI3nG1rlDlnC/fI0EDqHEc2xPy8NnUUec9z XSCO5lzQ8eGi8OypTjRCcD4OBBcLiJNtmYC0fXO6UEYmdHwx9rWlB+szKkeIJ0Jt/Arz 7F1+20Xuz+EMaH8T32w26MtkbzseGg9t71/fk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dpsH7mIcXqvQoo7xmLF/2aa6XMVIAHRRWDLOhYJdccM=; b=q4niWwN5mvP9/xSDPqofqtZKFdnBwsluozX7NmlkC10UdCYVTErZFBtIdEn1dxeZls pVa0prSZYrn50ZJRTLVckgcaPwJxycaSkQjmT+prMiUEGpzwDemo8B+cbHsoEsdRr6hf WCgWzof7+rvbUv1hzNswIa5EMwDXAUXij/mMAOQftVJr6JSlORdhcZwGuCo8ig4qeUNg ra8s7tOS9Z1j5Jc0Tn0ZooTPTwlomY6bE6gAwo/h4t9opOgVL5o8YKaxV73u1HHifSS0 XVFnkXXiwYQRQhTSpwOv9VctnzlpxVD1m1KWb1D0C7OSeL4hpnVv0xM3CfHC6OpVOcM7 55yQ== X-Gm-Message-State: AOAM530Sk8rkI++kYHYne0KF+P4bmqEqykcY1OUjMcLDMpWYVCnSfY4Q 3I6igRWwZu7EMXSCCi1aE+JkfQ== X-Google-Smtp-Source: ABdhPJzOVvGLyGiNzawM6Viy1d2MdKlXSmP4iW8i14wLOD5oabfZ0EUyG/CZfoceK0ZK9nKoEgR90w== X-Received: by 2002:a05:6a00:170b:b029:32a:3950:f51b with SMTP id h11-20020a056a00170bb029032a3950f51bmr7770393pfc.64.1626232802699; Tue, 13 Jul 2021 20:20:02 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id t26sm569424pgu.35.2021.07.13.20.20.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jul 2021 20:20:02 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Sergei Trofimovich , Alexander Potapenko , Thomas Gleixner , Vlastimil Babka , linux-mm@kvack.org, bowsingbetee@pm.me, Mikhail Morfikov , stable@vger.kernelo.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2] mm: page_alloc: Fix page_poison=1 / INIT_ON_ALLOC_DEFAULT_ON interaction Date: Tue, 13 Jul 2021 20:19:35 -0700 Message-Id: <20210714031935.4094114-1-keescook@chromium.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3808; i=keescook@chromium.org; h=from:subject; bh=U6MyozWPzcduXf+KzHAMxmKTs+aNnI1dvJ/+PKKtCNA=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBg7lfG9y1F5wstRYTqbG4LzbstCIJIACpar5PRh85M YgemSkCJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYO5XxgAKCRCJcvTf3G3AJivxD/ wLKbiaTdFdcJ2auwyD8LrRpRRvxdIm0vySRmwi4OKlsjhRX3L+HY1c4TPIaKAYV2t99KdFl/LPHXWA Q/d1MTwBauC/OqqTYORwkso383gxzC8WcHCcOZcQep6fexIdCANDkcR2JQDH/LEZRMeO+wfoQbXmKw JvqW5X2NxuaXChhAhmsWGSx6WDXmYattgmf+mkl0rldwG5P3acTM3BKLh0XdL7gXDup49IiHZ/Krrd 3KkaY4qq8FagiFqHzyI4cPqhDz2v+sCJbNXisc/bW/Kt3CJNSGYIWZoLGp6tE1mv79z6WeZjegbPnt GMgupTmGumSYZ7Tmvc7dWWB0yRcWF1Q0H2F9hoAyRNHO1jYebpsJyWMO9g8M5HUxOzaSzqKo6zsBB1 /TuB9HanA5bSvcJyDv5PfOwPeZo6QXaRPV6Uo5LU9lE2tljsjKfKh7NXe5/PvReGQr1YSfY5RC7O9B G+ZokHmmRGOcAx/L8AYo9fU3ppmNO0LTXBtavDFYkVA3T0lS7bQwAKLwhNJ5qjaXzBw9G4snXL05bj NTs+RaQ/ZBzZnz+WvPNgWPRt58Ui2E7IQt6CgGMer7Gu2EPoYTbDK3KtPdFczX2cokfrLp6GXZrIaA UkMN25RqPTDoVWYwscqcVSMeggk2t+5jDpcHQxll206XX44c2AY+A3Va wisg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=exIQaQBU; spf=pass (imf26.hostedemail.com: domain of keescook@chromium.org designates 209.85.215.172 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Stat-Signature: zke5xcqnynke89ekch6b64mpr5mkccrg X-Rspamd-Queue-Id: A6CDA20019E8 X-Rspamd-Server: rspam01 X-HE-Tag: 1626232803-521814 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: From: Sergei Trofimovich To reproduce the failure we need the following system: - kernel command: page_poison=1 init_on_free=0 init_on_alloc=0 - kernel config: * CONFIG_INIT_ON_ALLOC_DEFAULT_ON=y * CONFIG_INIT_ON_FREE_DEFAULT_ON=y * CONFIG_PAGE_POISONING=y 0000000085629bdd: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0000000022861832: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00000000c597f5b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ CPU: 11 PID: 15195 Comm: bash Kdump: loaded Tainted: G U O 5.13.1-gentoo-x86_64 #1 Hardware name: System manufacturer System Product Name/PRIME Z370-A, BIOS 2801 01/13/2021 Call Trace: dump_stack+0x64/0x7c __kernel_unpoison_pages.cold+0x48/0x84 post_alloc_hook+0x60/0xa0 get_page_from_freelist+0xdb8/0x1000 __alloc_pages+0x163/0x2b0 __get_free_pages+0xc/0x30 pgd_alloc+0x2e/0x1a0 ? dup_mm+0x37/0x4f0 mm_init+0x185/0x270 dup_mm+0x6b/0x4f0 ? __lock_task_sighand+0x35/0x70 copy_process+0x190d/0x1b10 kernel_clone+0xba/0x3b0 __do_sys_clone+0x8f/0xb0 do_syscall_64+0x68/0x80 ? do_syscall_64+0x11/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xae Before the 51cba1eb ("init_on_alloc: Optimize static branches") init_on_alloc never enabled static branch by default. It could only be enabed explicitly by init_mem_debugging_and_hardening(). But after the 51cba1eb static branch could already be enabled by default. There was no code to ever disable it. That caused page_poison=1 / init_on_free=1 conflict. This change extends init_mem_debugging_and_hardening() to also disable static branch disabling. CC: Alexander Potapenko CC: Thomas Gleixner CC: Vlastimil Babka CC: linux-mm@kvack.org Reported-by: bowsingbetee@pm.me Reported-by: Mikhail Morfikov Fixes: 51cba1ebc60d ("init_on_alloc: Optimize static branches") Cc: stable@vger.kernelo.org Signed-off-by: Sergei Trofimovich Link: https://lore.kernel.org/r/20210712215816.1512739-1-slyfox@gentoo.org Co-developed-by: Kees Cook Signed-off-by: Kees Cook Reviewed-by: David Hildenbrand --- mm/page_alloc.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 3b97e17806be..1f19365bc158 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -840,21 +840,24 @@ void init_mem_debugging_and_hardening(void) } #endif - if (_init_on_alloc_enabled_early) { - if (page_poisoning_requested) - pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, " - "will take precedence over init_on_alloc\n"); - else - static_branch_enable(&init_on_alloc); - } - if (_init_on_free_enabled_early) { - if (page_poisoning_requested) - pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, " - "will take precedence over init_on_free\n"); - else - static_branch_enable(&init_on_free); + if ((_init_on_alloc_enabled_early || _init_on_free_enabled_early) && + page_poisoning_requested) { + pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, " + "will take precedence over init_on_alloc and init_on_free\n"); + _init_on_alloc_enabled_early = false; + _init_on_free_enabled_early = false; } + if (_init_on_alloc_enabled_early) + static_branch_enable(&init_on_alloc); + else + static_branch_disable(&init_on_alloc); + + if (_init_on_free_enabled_early) + static_branch_enable(&init_on_free); + else + static_branch_disable(&init_on_free); + #ifdef CONFIG_DEBUG_PAGEALLOC if (!debug_pagealloc_enabled()) return;