From mboxrd@z Thu Jan 1 00:00:00 1970 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 A1AE1C282EC for ; Fri, 14 Mar 2025 16:48:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CA41280002; Fri, 14 Mar 2025 12:48:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 15285280001; Fri, 14 Mar 2025 12:48:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F367A280002; Fri, 14 Mar 2025 12:48:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D14B4280001 for ; Fri, 14 Mar 2025 12:48:08 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 94C59B95FB for ; Fri, 14 Mar 2025 16:48:10 +0000 (UTC) X-FDA: 83220739140.30.2046A70 Received: from pegase2.c-s.fr (pegase2.c-s.fr [93.17.235.10]) by imf17.hostedemail.com (Postfix) with ESMTP id 55E3140009 for ; Fri, 14 Mar 2025 16:48:08 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=csgroup.eu; spf=pass (imf17.hostedemail.com: domain of christophe.leroy@csgroup.eu designates 93.17.235.10 as permitted sender) smtp.mailfrom=christophe.leroy@csgroup.eu ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741970888; a=rsa-sha256; cv=none; b=T0dg1A/sBXvjt4UVEwmC31e496KviF2QzYb8HXW/GQcnFPXtpYswFv260cxrmPd5njRJic GQx1Ea3mnAFTbxH0lPfGXVc+G3sgtb6gfJqdsEwertPRl4l7SQ1hjoiSNbXbwbajDWjh6E lYdHfKSHhy0lDRYiBQILagpohw6xJEQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=csgroup.eu; spf=pass (imf17.hostedemail.com: domain of christophe.leroy@csgroup.eu designates 93.17.235.10 as permitted sender) smtp.mailfrom=christophe.leroy@csgroup.eu ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1741970888; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PSjYVdRTw5pO7YOKfWx3+OyS5hjGGG1NqhpYtXBzuCU=; b=JQ8YW3MQvB7zu8JNl8gSHaOOYwbfx3BOyXCAFSomFtDqJtoNHd0XFGQ4CuwvOiyqRuG0lF LuPzYpoYHiH+B+Rf1r8LvdUqMrCnEb/t4+YDZc8gQy9yi7wD8hToRTchsGyVXPOZFnGny6 pMtoqKqFOKU0Tu5fUIHiU3BOe9O0IaM= Received: from localhost (mailhub3.si.c-s.fr [172.26.127.67]) by localhost (Postfix) with ESMTP id 4ZDr062qKrz9sPd; Fri, 14 Mar 2025 17:48:06 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase2.c-s.fr ([172.26.127.65]) by localhost (pegase2.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nRgjHaRRMUv8; Fri, 14 Mar 2025 17:48:06 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase2.c-s.fr (Postfix) with ESMTP id 4ZDr061fTCz9rvV; Fri, 14 Mar 2025 17:48:06 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 231678B77B; Fri, 14 Mar 2025 17:48:06 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id Jej96Wu7PDch; Fri, 14 Mar 2025 17:48:06 +0100 (CET) Received: from [192.168.235.99] (unknown [192.168.235.99]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 443888B770; Fri, 14 Mar 2025 17:48:03 +0100 (CET) Message-ID: <6e3ce71a-da5a-4d69-a5ea-4caca761d00f@csgroup.eu> Date: Fri, 14 Mar 2025 17:48:00 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] module: Taint the kernel when write-protecting ro_after_init fails To: Kees Cook , Vlastimil Babka Cc: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Petr Mladek , Jani Nikula , Andrew Morton , John Ogness , Greg Kroah-Hartman , linux-mm , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org References: <20250306103712.29549-1-petr.pavlu@suse.com> <202503120923.199D458CB@keescook> Content-Language: fr-FR From: Christophe Leroy In-Reply-To: <202503120923.199D458CB@keescook> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Stat-Signature: x5ii1sxiie1jzw13wazsdxgsw9i1m3d3 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 55E3140009 X-Rspam-User: X-HE-Tag: 1741970888-840154 X-HE-Meta: U2FsdGVkX1/foOtNzOSX76mdrUTw0O7eXdgZWC3XNJEkMVaLQWULH6oWLi5fu68RG445gBHQiGUuYaDi49YgCYQpm8GaASgOe93AFMtr3qzfS2qI9LhakqQd3snPoI2jXbsD6aFptWb+NT49tyoqcV+B2d/pGMMewooQTTL5AKH5H038Rw+MuMFDvXKxjT23M+6uyn+8dafOi0HvHUjjzB0DvXujNSBxXEzEp3ifSoMY4gkjgcf8/oUiVEHR4iPTReX5ty0IaeOMfDeQf5X5ZapjVOgGhtSk/QS9tdYjbGVgklcn69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.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Le 12/03/2025 à 17:30, Kees Cook a écrit : > On Wed, Mar 12, 2025 at 04:45:24PM +0100, Vlastimil Babka wrote: >> On 3/6/25 17:57, Luis Chamberlain wrote: >>> + linux-mm since we're adding TAINT_BAD_PAGE >>> >>> On Thu, Mar 06, 2025 at 11:36:55AM +0100, Petr Pavlu wrote: >>>> In the unlikely case that setting ro_after_init data to read-only fails, it >>>> is too late to cancel loading of the module. The loader then issues only >>>> a warning about the situation. Given that this reduces the kernel's >>>> protection, it was suggested to make the failure more visible by tainting >>>> the kernel. >>>> >>>> Allow TAINT_BAD_PAGE to be set per-module and use it in this case. The flag >>>> is set in similar situations and has the following description in >>>> Documentation/admin-guide/tainted-kernels.rst: "bad page referenced or some >>>> unexpected page flags". >>>> >>>> Adjust the warning that reports the failure to avoid references to internal >>>> functions and to add information about the kernel being tainted, both to >>>> match the style of other messages in the file. Additionally, merge the >>>> message on a single line because checkpatch.pl recommends that for the >>>> ability to grep for the string. >>>> >>>> Suggested-by: Kees Cook >>>> Signed-off-by: Petr Pavlu >>>> --- >>>> I opted to use TAINT_BAD_PAGE for now because it seemed unnecessary to me >>>> to introduce a new flag only for this specific case. However, if we end up >>>> similarly checking set_memory_*() in the boot context, a separate flag >>>> would be probably better. >>>> --- >>>> kernel/module/main.c | 7 ++++--- >>>> kernel/panic.c | 2 +- >>>> 2 files changed, 5 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/kernel/module/main.c b/kernel/module/main.c >>>> index 1fb9ad289a6f..8f424a107b92 100644 >>>> --- a/kernel/module/main.c >>>> +++ b/kernel/module/main.c >>>> @@ -3030,10 +3030,11 @@ static noinline int do_init_module(struct module *mod) >>>> rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms); >>>> #endif >>>> ret = module_enable_rodata_ro_after_init(mod); >>>> - if (ret) >>>> - pr_warn("%s: module_enable_rodata_ro_after_init() returned %d, " >>>> - "ro_after_init data might still be writable\n", >>>> + if (ret) { >>>> + pr_warn("%s: write-protecting ro_after_init data failed with %d, the data might still be writable - tainting kernel\n", >>>> mod->name, ret); >>>> + add_taint_module(mod, TAINT_BAD_PAGE, LOCKDEP_STILL_OK); >>>> + } >>>> >>>> mod_tree_remove_init(mod); >>>> module_arch_freeing_init(mod); >>>> diff --git a/kernel/panic.c b/kernel/panic.c >>>> index d8635d5cecb2..794c443bfb5c 100644 >>>> --- a/kernel/panic.c >>>> +++ b/kernel/panic.c >>>> @@ -497,7 +497,7 @@ const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { >>>> TAINT_FLAG(CPU_OUT_OF_SPEC, 'S', ' ', false), >>>> TAINT_FLAG(FORCED_RMMOD, 'R', ' ', false), >>>> TAINT_FLAG(MACHINE_CHECK, 'M', ' ', false), >>>> - TAINT_FLAG(BAD_PAGE, 'B', ' ', false), >>>> + TAINT_FLAG(BAD_PAGE, 'B', ' ', true), >>>> TAINT_FLAG(USER, 'U', ' ', false), >>>> TAINT_FLAG(DIE, 'D', ' ', false), >>>> TAINT_FLAG(OVERRIDDEN_ACPI_TABLE, 'A', ' ', false), >>> >>> Reviewed-by: Luis Chamberlain >>> >>> For our needs this makes sense, however I am curious if TAINT_BAD_PAGE >>> is too broadly generic, and also if we're going to add it, if there are >>> other mm uses for such a thing. >> >> I'm not sure BAD_PAGE is a good fit. If there was a new flag that meant "a >> hardening measure failed", would that have other possible uses? The >> semantics would be that the kernel self-protection was weakened wrt >> expectations, even if not yet a corruption due to attack would be detected. >> Some admins could opt-in to panic in such case anyway, etc. Any other >> hardening features where such "failure to harden" is possible and could use >> this too? Kees? > > Yeah, it could certainly be used. The direction the hardening stuff has > taken is to use WARN() (as Linus requires no direct BUG() usage), and to > recommend that end users tune their warn_limit sysctl as needed. > > Being able to TAINT might be useful, but I don't have any places that > immediately come to mind that seem appropriate for it (besides this > case). Hm, well, maybe in the case of a W^X test failure? (I note that > this is also a "safe memory permission" failure...) Can be anything that fails in function mark_readonly() ? : jump_label_init_ro(); mark_rodata_ro(); debug_checkwx(); rodata_test(); Christophe