From 4361dd5e89347e60b412331da27615e92b1fcfcd Mon Sep 17 00:00:00 2001 From: Factorial <27factorial@gmail.com> Date: Wed, 14 Feb 2024 12:06:51 -0800 Subject: [PATCH] fix UB in Daemonize::privileged_action --- daemonize/src/lib.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/daemonize/src/lib.rs b/daemonize/src/lib.rs index 4f0f758c..7b283f1a 100644 --- a/daemonize/src/lib.rs +++ b/daemonize/src/lib.rs @@ -51,7 +51,6 @@ use std::env::set_current_dir; use std::ffi::CString; use std::fmt; use std::fs::File; -use std::mem::transmute; use std::os::unix::ffi::OsStringExt; use std::os::unix::io::AsRawFd; use std::path::{Path, PathBuf}; @@ -320,9 +319,19 @@ impl Daemonize { /// Execute `action` just before dropping privileges. Most common use case is to open /// listening socket. Result of `action` execution will be returned by `start` method. pub fn privileged_action N + 'static>(self, action: F) -> Daemonize { - let mut new: Daemonize = unsafe { transmute(self) }; - new.privileged_action = Box::new(action); - new + Daemonize { + directory: self.directory, + pid_file: self.pid_file, + chown_pid_file: self.chown_pid_file, + user: self.user, + group: self.group, + umask: self.umask, + root: self.root, + privileged_action: Box::new(action), + stdin: self.stdin, + stdout: self.stdout, + stderr: self.stderr, + } } /// Configuration for the child process's standard output stream.