Skip to content

An open-source remote desktop application designed for self-hosting, as an alternative to TeamViewer.

Initialize libsodium before anything uses it (#16974)

RustDeskb63336037291
diff --git a/src/common.rs b/src/common.rs
index 4bc5bc2d0..12916ab4b 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -120,7 +120,16 @@ impl Drop for SimpleCallOnReturn {
}
}
+/// `sodium_init()` chooses libsodium's implementations, so it runs before anything else uses the
+/// library: a later call swaps Poly1305 under a computation another thread is in the middle of.
+pub fn init_sodium() {
+ if hbb_common::sodiumoxide::init().is_err() {
+ log::error!("Failed to initialize libsodium");
+ }
+}
+
pub fn global_init() -> bool {
+ init_sodium();
#[cfg(all(target_os = "linux", feature = "drm"))]
crate::platform::linux::dispatch_wayland_display_probe();
#[cfg(target_os = "linux")]
diff --git a/src/flutter_ffi.rs b/src/flutter_ffi.rs
index 4ff89becd..d366d39e1 100644
--- a/src/flutter_ffi.rs
+++ b/src/flutter_ffi.rs
@@ -41,6 +41,8 @@ lazy_static::lazy_static! {
}
fn initialize(app_dir: &str, custom_client_config: &str) {
+ // On mobile `global_init` below comes after the custom client's signature is verified.
+ crate::common::init_sodium();
flutter::async_tasks::start_flutter_async_runner();
// `APP_DIR` is set in `main_get_data_dir_ios()` on iOS.
#[cfg(not(target_os = "ios"))]
@@ -3087,6 +3089,8 @@ pub mod server_side {
custom_client_config: JString,
) {
log::debug!("startServer from jvm");
+ // Started at boot, the service runs without the Flutter side and its `initialize`.
+ crate::common::init_sodium();
let mut env = env;
if let Ok(app_dir) = env.get_string(&app_dir) {
*config::APP_DIR.write().unwrap() = app_dir.into();