From 306be392ba17e8879cdc5966f3666b7b23ccc4a9 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 02:10:38 +0000 Subject: [PATCH] fix(core): Mark dlopen/dlsym wrappers unsafe in core::mods --- src/core/mods/module.rs | 32 +++++++++++++++++++++++++------- src/core/mods/new.rs | 18 +++++++++++++----- src/main/mods.rs | 19 +++++++++++++++---- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/core/mods/module.rs b/src/core/mods/module.rs index 3c8183962..0cd49cb28 100644 --- a/src/core/mods/module.rs +++ b/src/core/mods/module.rs @@ -13,11 +13,21 @@ pub struct Module { } impl Module { - pub fn from_name(name: &str) -> Result { Self::from_path(path::from_name(name)?) } + /// # Safety + /// See [`new::from_name`]: loads a shared library via `dlopen(3)` and runs + /// its initialization routines. + pub unsafe fn from_name(name: &str) -> Result { + // SAFETY: Upheld by caller. + unsafe { Self::from_path(path::from_name(name)?) } + } - pub fn from_path(path: OsString) -> Result { + /// # Safety + /// See [`new::from_path`]: loads a shared library via `dlopen(3)` and runs + /// its initialization routines. + pub unsafe fn from_path(path: OsString) -> Result { Ok(Self { - handle: Some(new::from_path(&path)?), + // SAFETY: Upheld by caller. + handle: Some(unsafe { new::from_path(&path)? }), loaded: SystemTime::now(), path, }) @@ -38,16 +48,24 @@ impl Module { } } - pub fn get(&self, name: &str) -> Result> { + /// Look up a symbol in this module via `dlsym(3)`. + /// + /// # Safety + /// `Prototype` is not checked against the symbol's actual type or ABI in + /// the loaded shared library. The caller must ensure `name` refers to a + /// symbol whose type, calling convention, and ABI exactly match + /// `Prototype`. + pub unsafe fn get(&self, name: &str) -> Result> { let cname = CString::new(name.to_owned()).expect("terminated string from provided name"); let handle = self .handle .as_ref() .expect("backing library loaded by this instance"); - // SAFETY: Calls dlsym(3) on unix platforms. This might not have to be - // unsafe if wrapped in libloading with_dlerror(). - let sym = unsafe { handle.get::(cname.as_bytes()) }; + // SAFETY: Caller guarantees `Prototype` matches the symbol's actual + // type and ABI. Passing `as_bytes_with_nul()` avoids a redundant copy + // inside `libloading`. + let sym = unsafe { handle.get::(cname.as_bytes_with_nul()) }; let sym = sym.expect("symbol found; binding successful"); Ok(sym) diff --git a/src/core/mods/new.rs b/src/core/mods/new.rs index 41eec89ef..e138b2046 100644 --- a/src/core/mods/new.rs +++ b/src/core/mods/new.rs @@ -5,14 +5,22 @@ use crate::{Err, Result}; const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL; -pub fn from_name(name: &str) -> Result { +/// # Safety +/// Loads a shared library via `dlopen(3)`, which executes the library's +/// initialization routines (`.init_array` / constructors). The caller must +/// ensure the resolved module is trusted and ABI-compatible with this binary. +pub unsafe fn from_name(name: &str) -> Result { let path = path::from_name(name)?; - from_path(&path) + // SAFETY: Upheld by caller. + unsafe { from_path(&path) } } -pub fn from_path(path: &OsStr) -> Result { - //SAFETY: Calls dlopen(3) on unix platforms. This might not have to be - // unsafe if wrapped in with_dlerror. +/// # Safety +/// Loads a shared library via `dlopen(3)`, which executes the library's +/// initialization routines (`.init_array` / constructors). The caller must +/// ensure `path` points to a trusted, ABI-compatible shared library. +pub unsafe fn from_path(path: &OsStr) -> Result { + // SAFETY: Caller guarantees `path` is a trusted, ABI-compatible library. let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) }; if let Err(e) = lib { let name = path::to_name(path)?; diff --git a/src/main/mods.rs b/src/main/mods.rs index c0a945497..c00c30f70 100644 --- a/src/main/mods.rs +++ b/src/main/mods.rs @@ -42,7 +42,10 @@ pub(crate) async fn run(server: &Arc, starts: bool) -> Result<(bool, boo let main_lock = server.mods.read().await; let main_mod = (*main_lock).last().expect("main module loaded"); if starts { - let start = main_mod.get::("start")?; + // SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern + // "Rust" fn start` with signature matching `StartFuncProto`, compiled + // with the same toolchain and flags. + let start = unsafe { main_mod.get::("start")? }; match start(&server.server).await { | Ok(services) => server.services.lock().await.insert(services), | Err(error) => { @@ -54,7 +57,10 @@ pub(crate) async fn run(server: &Arc, starts: bool) -> Result<(bool, boo server.server.stopping.store(false, Ordering::Release); - let run = main_mod.get::("run")?; + // SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern + // "Rust" fn run` with signature matching `RunFuncProto`, compiled with the + // same toolchain and flags. + let run = unsafe { main_mod.get::("run")? }; if let Err(error) = run(server .services .lock() @@ -72,7 +78,10 @@ pub(crate) async fn run(server: &Arc, starts: bool) -> Result<(bool, boo let stops = !reloads || stale_from_mods(&main_lock)? <= restart_thresh(); let starts = reloads && stops; if stops { - let stop = main_mod.get::("stop")?; + // SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern + // "Rust" fn stop` with signature matching `StopFuncProto`, compiled + // with the same toolchain and flags. + let stop = unsafe { main_mod.get::("stop")? }; if let Err(error) = stop( server .services @@ -102,7 +111,9 @@ pub(crate) async fn open(server: &Arc) -> Result { for (i, name) in MODULE_NAMES.iter().enumerate() { if mods.get(i).is_none() { - mods.push(mods::Module::from_name(name)?); + // SAFETY: `MODULE_NAMES` are this workspace's own hot-reloadable + // shared libraries built with the same compiler/ABI. + mods.push(unsafe { mods::Module::from_name(name)? }); } }