Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp150215lqo; Wed, 27 Mar 2024 09:11:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVc86iF6PHhF5JtV0mD7sn2EQxvrW0POSRIX5TUQ+tbRk++ueDnFmigArykRIk1Hfo0Sm356yePXyUeHw7+VlhmuTdNxSdomjI5Fze0bQ== X-Google-Smtp-Source: AGHT+IFsmrlKPwC0Wj81pxN50TCtWz05+YV7MxGLrme9uvn7RpMXKl+R2MwK6p6Gh0oIaIYKHYmI X-Received: by 2002:a05:6214:192a:b0:691:67f9:16e7 with SMTP id es10-20020a056214192a00b0069167f916e7mr52815qvb.24.1711555871487; Wed, 27 Mar 2024 09:11:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711555871; cv=pass; d=google.com; s=arc-20160816; b=a3RD/B3zgVD+cyDh3RcqQP4jn3vHREVygn3czBHbKE59HNGQqWmajUyhVvl0Qr8Tum 8Lgrc5xUhX1VMWOIBJPN8V89ygo5LpxqUxdEzpzphIg7mDIzCD+3CYu7r/Zbkn1a0wDM 6W9HA4/4da1zvKkvx1W4Rj+2mXeO/sCsFtG1AfyPVucaaakzWWCL21EDq6gPt5LqxRXg 3OeKsbrpkoAp5uDdnA3TK6jPcW1pLKxnpafeLc0cMp8sHajoB/oLv9tPLkxXlLdJ+3ac VIJMT+SfaZWiYGypb0TCGao+CCmlAEa03WmeGbTTBXaP9jCjLmjWICcvHNNVs4CsrFqC hslQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:message-id:subject:cc :from:to:date:dkim-signature; bh=Ka2xa47AQ5Q73+6JzpnFAcof+BAgDh2tkVUlnUr2JVE=; fh=EQCfLmN0BpSsTLi2Qfkr8ZXN1zgEAu19hSueglGScy0=; b=tF9YhVzenkL+tTM7mhpIr3XKZYJ0RqMQVvaVtYxCE8xJf88kMneUTrty9ENddJ32A0 eIu07IWWunsF3IS2j+uFRguqBwTiRQ8ddC57o5x3la1xaQgjArt0q3htqfpcGTsSe/O1 OcDt8QqWvcotFWnN8oebDYAjeHCisIAqBJ5c018Jfcgi9dYe0M971QscYtWr2+cm74un 0aOhrFFSjXOzEcTQbfcgSu3Lir/Sw0IH+WJGglCYAsllyPUDbQ2bfFFqnPfPhjzzpjzS s4fGEEdRbU7Vr6LtoRRYewljniPmvPvec1G5ogO+rEkDAhabzAURDJIn61Xvsf+1I0kl nzXQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="NZF/48Dz"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-121600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121600-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q7-20020a056214194700b00690a7e9cd15si3788003qvk.14.2024.03.27.09.11.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 09:11:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="NZF/48Dz"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-121600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121600-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 2C1B41C31B55 for ; Wed, 27 Mar 2024 16:11:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A5BDB1442FE; Wed, 27 Mar 2024 16:04:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="NZF/48Dz" Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3D6113C9B9 for ; Wed, 27 Mar 2024 16:04:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711555454; cv=none; b=O2JzqA/GQPxkJ2w+lgwqpKlerDjjSQIY7w5FSTPgKF36xVzAt1EBOrpKK39l/lZ+mKunpG4ZvaMqljSFb8QTofwUjW0CrptFkDnx5Y6EFSCzAY8GlZog9xlZYtE37h+8zPpq76WeJxTqqdKH0DIdxLr01HtwEOyWNwsH53sdlCs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711555454; c=relaxed/simple; bh=EdLNzX8uuiq2CjNBfiXznOjIDyfPc5MSkqemwj47Fxc=; h=Date:To:From:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=Uvli65DF/5OomxNkr27+CHo1sQbR+d/GBTzXAn5iOha30/5VdthzyOwW2Vwi4lT+88yzydnXK5lw+kYafKXKtlPJtc8wmmDEmjXPSxe89bX+cnzsP/0XTo2WEXyTL+MlmM+Jbk4OYUtCXYLCpYa+sbYfVNR7kv2IpaJM0jQLejU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=NZF/48Dz; arc=none smtp.client-ip=185.70.40.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1711555449; x=1711814649; bh=Ka2xa47AQ5Q73+6JzpnFAcof+BAgDh2tkVUlnUr2JVE=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=NZF/48Dzz7IZFkHGpV08pk9cJ7GI15CSkz6G7ljNb8L0GFu0DXubC+dd4sdTcXM3H Xj1rhsJcVFBzolFWgok7ZIe0B05tC3TPi0SKfNy6W3AAqjJ5ygJg7e9TLl+y/gUGoB fcOo4Blj81axwFF1KJ1btZKNtSy+DycpIeyFa5YlYpwu3NZKYqHV7aHFMTJESDbjDG aLYPXQSnoT1//TjkkGBa7i43YDx3/yCsspZU62HNLzS7xhxXlFbs4YipRArvC7QJfd R7pGm6y9hFfmem6wli+vHzpyKKqo0mz/s7tc4UwJPkV6s83FKmo2/SlogR/sX1Wkg2 2Ux5GFZ+Q3pww== Date: Wed, 27 Mar 2024 16:04:03 +0000 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Martin Rodriguez Reboredo , Asahi Lina , Eric Curtin , Neal Gompa , Thomas Bertschinger , Andrea Righi , Sumera Priyadarsini , Finn Behrens , Adam Bratschi-Kaye From: Benno Lossin Cc: stable@vger.kernel.org, Daniel Xu , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rust: macros: fix soundness issue in `module!` macro Message-ID: <20240327160346.22442-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable The `module!` macro creates glue code that are called by C to initialize the Rust modules using the `Module::init` function. Part of this glue code are the local functions `__init` and `__exit` that are used to initialize/destroy the Rust module. These functions are safe and also visible to the Rust mod in which the `module!` macro is invoked. This means that they can be called by other safe Rust code. But since they contain `unsafe` blocks that rely on only being called at the right time, this is a soundness issue. Wrap these generated functions inside of two private modules, this guarantees that the public functions cannot be called from the outside. Make the safe functions `unsafe` and add SAFETY comments. Cc: stable@vger.kernel.org Closes: https://github.com/Rust-for-Linux/linux/issues/629 Fixes: 1fbde52bde73 ("rust: add `macros` crate") Signed-off-by: Benno Lossin --- This patch is best viewed with `git show --ignore-space-change`, since I also adjusted the indentation. rust/macros/module.rs | 198 ++++++++++++++++++++++++------------------ 1 file changed, 112 insertions(+), 86 deletions(-) diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 27979e582e4b..16c4921a08f2 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -199,103 +199,129 @@ pub(crate) fn module(ts: TokenStream) -> TokenStrea= m { /// Used by the printing macros, e.g. [`info!`]. const __LOG_PREFIX: &[u8] =3D b\"{name}\\0\"; =20 - /// The \"Rust loadable module\" mark. - // - // This may be best done another way later on, e.g. as a new m= odinfo - // key or a new section. For the moment, keep it simple. - #[cfg(MODULE)] - #[doc(hidden)] - #[used] - static __IS_RUST_MODULE: () =3D (); - - static mut __MOD: Option<{type_}> =3D None; - - // SAFETY: `__this_module` is constructed by the kernel at loa= d time and will not be - // freed until the module is unloaded. - #[cfg(MODULE)] - static THIS_MODULE: kernel::ThisModule =3D unsafe {{ - kernel::ThisModule::from_ptr(&kernel::bindings::__this_mod= ule as *const _ as *mut _) - }}; - #[cfg(not(MODULE))] - static THIS_MODULE: kernel::ThisModule =3D unsafe {{ - kernel::ThisModule::from_ptr(core::ptr::null_mut()) - }}; - - // Loadable modules need to export the `{{init,cleanup}}_modul= e` identifiers. - /// # Safety - /// - /// This function must not be called after module initializati= on, because it may be - /// freed after that completes. - #[cfg(MODULE)] - #[doc(hidden)] - #[no_mangle] - #[link_section =3D \".init.text\"] - pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {= { - __init() - }} + // Double nested modules, since then nobody can access the pub= lic items inside. + mod __module_init {{ + mod __module_init {{ + use super::super::{type_}; + + /// The \"Rust loadable module\" mark. + // + // This may be best done another way later on, e.g. as= a new modinfo + // key or a new section. For the moment, keep it simpl= e. + #[cfg(MODULE)] + #[doc(hidden)] + #[used] + static __IS_RUST_MODULE: () =3D (); + + static mut __MOD: Option<{type_}> =3D None; + + // SAFETY: `__this_module` is constructed by the kerne= l at load time and will not be + // freed until the module is unloaded. + #[cfg(MODULE)] + static THIS_MODULE: kernel::ThisModule =3D unsafe {{ + kernel::ThisModule::from_ptr(&kernel::bindings::__= this_module as *const _ as *mut _) + }}; + #[cfg(not(MODULE))] + static THIS_MODULE: kernel::ThisModule =3D unsafe {{ + kernel::ThisModule::from_ptr(core::ptr::null_mut()= ) + }}; + + // Loadable modules need to export the `{{init,cleanup= }}_module` identifiers. + /// # Safety + /// + /// This function must not be called after module init= ialization, because it may be + /// freed after that completes. + #[cfg(MODULE)] + #[doc(hidden)] + #[no_mangle] + #[link_section =3D \".init.text\"] + pub unsafe extern \"C\" fn init_module() -> core::ffi:= :c_int {{ + __init() + }} =20 - #[cfg(MODULE)] - #[doc(hidden)] - #[no_mangle] - pub extern \"C\" fn cleanup_module() {{ - __exit() - }} + #[cfg(MODULE)] + #[doc(hidden)] + #[no_mangle] + pub extern \"C\" fn cleanup_module() {{ + __exit() + }} =20 - // Built-in modules are initialized through an initcall pointe= r - // and the identifiers need to be unique. - #[cfg(not(MODULE))] - #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))] - #[doc(hidden)] - #[link_section =3D \"{initcall_section}\"] - #[used] - pub static __{name}_initcall: extern \"C\" fn() -> core::ffi::= c_int =3D __{name}_init; - - #[cfg(not(MODULE))] - #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)] - core::arch::global_asm!( - r#\".section \"{initcall_section}\", \"a\" - __{name}_initcall: - .long __{name}_init - . - .previous - \"# - ); + // Built-in modules are initialized through an initcal= l pointer + // and the identifiers need to be unique. + #[cfg(not(MODULE))] + #[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))] + #[doc(hidden)] + #[link_section =3D \"{initcall_section}\"] + #[used] + pub static __{name}_initcall: extern \"C\" fn() -> cor= e::ffi::c_int =3D __{name}_init; + + #[cfg(not(MODULE))] + #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)] + core::arch::global_asm!( + r#\".section \"{initcall_section}\", \"a\" + __{name}_initcall: + .long __{name}_init - . + .previous + \"# + ); + + #[cfg(not(MODULE))] + #[doc(hidden)] + #[no_mangle] + pub extern \"C\" fn __{name}_init() -> core::ffi::c_in= t {{ + __init() + }} =20 - #[cfg(not(MODULE))] - #[doc(hidden)] - #[no_mangle] - pub extern \"C\" fn __{name}_init() -> core::ffi::c_int {{ - __init() - }} + #[cfg(not(MODULE))] + #[doc(hidden)] + #[no_mangle] + pub extern \"C\" fn __{name}_exit() {{ + __exit() + }} =20 - #[cfg(not(MODULE))] - #[doc(hidden)] - #[no_mangle] - pub extern \"C\" fn __{name}_exit() {{ - __exit() - }} + /// # Safety + /// + /// This function must + /// - only be called once, + /// - not be called concurrently with `__exit`. + unsafe fn __init() -> core::ffi::c_int {{ + match <{type_} as kernel::Module>::init(&THIS_MODU= LE) {{ + Ok(m) =3D> {{ + // SAFETY: + // no data race, since `__MOD` can only be= accessed by this module and + // there only `__init` and `__exit` access= it. These functions are only + // called once and `__exit` cannot be call= ed before or during `__init`. + unsafe {{ + __MOD =3D Some(m); + }} + return 0; + }} + Err(e) =3D> {{ + return e.to_errno(); + }} + }} + }} =20 - fn __init() -> core::ffi::c_int {{ - match <{type_} as kernel::Module>::init(&THIS_MODULE) {{ - Ok(m) =3D> {{ + /// # Safety + /// + /// This function must + /// - only be called once, + /// - be called after `__init`, + /// - not be called concurrently with `__init`. + unsafe fn __exit() {{ + // SAFETY: + // no data race, since `__MOD` can only be accesse= d by this module and there + // only `__init` and `__exit` access it. These fun= ctions are only called once + // and `__init` was already called. unsafe {{ - __MOD =3D Some(m); + // Invokes `drop()` on `__MOD`, which should b= e used for cleanup. + __MOD =3D None; }} - return 0; }} - Err(e) =3D> {{ - return e.to_errno(); - }} - }} - }} =20 - fn __exit() {{ - unsafe {{ - // Invokes `drop()` on `__MOD`, which should be used f= or cleanup. - __MOD =3D None; + {modinfo} }} }} - - {modinfo} ", type_ =3D info.type_, name =3D info.name, base-commit: 4cece764965020c22cff7665b18a012006359095 --=20 2.44.0