From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 438561A0AF2; Tue, 10 Sep 2024 10:12:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725963151; cv=none; b=suUvXOZlY99jJd5Y3zB2MeJdIti/LwBjx8zZkTrau7Edte88EUy2tbK8aifz90TRHGykEiPl9+Y8kj37+SK8YZ7Uuz+1RpaTa0sRF9nyY8TBi6C0aHV+VP3pK3k8gOLp/4e/R92MZMVzVa9NYRinQaGlxIlsXZSz+4qchlmw+y8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725963151; c=relaxed/simple; bh=nBCKfxzmVpMcwpbJhlMMqiymkOV24P2z9GayWHeeDmw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ebQna5l1lAGxghsGr/6/T6fzJXr/HFVpcPe0KWgNN6m5/9nKFMYvmKKMiWCCNyPLH8k+y6hLtRs7E7nc7NOHqw/WCQrvWl35JAA8zhwzeKvyoV4KzxXWai36jVnwSn0rPy9LrKF+WMYjU6XnDGt2KSPBPMriFVW8aRopAIQqfmo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=oPDRKxq8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="oPDRKxq8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E637C4CECD; Tue, 10 Sep 2024 10:12:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1725963150; bh=nBCKfxzmVpMcwpbJhlMMqiymkOV24P2z9GayWHeeDmw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oPDRKxq8x+sOmXejxvgZy37A7FDCujHQoIYRa51/HqhipZxwX6ToORov6fllBd3rk rtqGF6kEL7T3lzpqqqIfp7tyCk3lgd5Il1qAVlhetF6FB3n3E32eq8Ky+0oNVbIsth 51HF3oTYrGNt2WlK0k2u6x3obxbpNSwX6AgGJxqA= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alice Ryhl , Boqun Feng , Trevor Gross , Benno Lossin , Gary Guo , Miguel Ojeda , Sasha Levin Subject: [PATCH 6.1 153/192] rust: macros: provide correct provenance when constructing THIS_MODULE Date: Tue, 10 Sep 2024 11:32:57 +0200 Message-ID: <20240910092604.257546283@linuxfoundation.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240910092557.876094467@linuxfoundation.org> References: <20240910092557.876094467@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Boqun Feng [ Upstream commit a5a3c952e82c1ada12bf8c55b73af26f1a454bd2 ] Currently while defining `THIS_MODULE` symbol in `module!()`, the pointer used to construct `ThisModule` is derived from an immutable reference of `__this_module`, which means the pointer doesn't have the provenance for writing, and that means any write to that pointer is UB regardless of data races or not. However, the usage of `THIS_MODULE` includes passing this pointer to functions that may write to it (probably in unsafe code), and this will create soundness issues. One way to fix this is using `addr_of_mut!()` but that requires the unstable feature "const_mut_refs". So instead of `addr_of_mut()!`, an extern static `Opaque` is used here: since `Opaque` is transparent to `T`, an extern static `Opaque` will just wrap the C symbol (defined in a C compile unit) in an `Opaque`, which provides a pointer with writable provenance via `Opaque::get()`. This fix the potential UBs because of pointer provenance unmatched. Reported-by: Alice Ryhl Signed-off-by: Boqun Feng Reviewed-by: Alice Ryhl Reviewed-by: Trevor Gross Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Closes: https://rust-for-linux.zulipchat.com/#narrow/stream/x/topic/x/near/465412664 Fixes: 1fbde52bde73 ("rust: add `macros` crate") Cc: stable@vger.kernel.org # 6.6.x: be2ca1e03965: ("rust: types: Make Opaque::get const") Link: https://lore.kernel.org/r/20240828180129.4046355-1-boqun.feng@gmail.com [ Fixed two typos, reworded title. - Miguel ] Signed-off-by: Miguel Ojeda Signed-off-by: Sasha Levin --- rust/macros/module.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 031028b3dc41..071b96639a2e 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -183,7 +183,11 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { // freed until the module is unloaded. #[cfg(MODULE)] static THIS_MODULE: kernel::ThisModule = unsafe {{ - kernel::ThisModule::from_ptr(&kernel::bindings::__this_module as *const _ as *mut _) + extern \"C\" {{ + static __this_module: kernel::types::Opaque; + }} + + kernel::ThisModule::from_ptr(__this_module.get()) }}; #[cfg(not(MODULE))] static THIS_MODULE: kernel::ThisModule = unsafe {{ -- 2.43.0