Skip to content

Commit

Permalink
fix: use libc::umask for socket permission
Browse files Browse the repository at this point in the history
The use of the `PermissionsExt` does not appear to work unfortunately
when adjusting the socket permissions.

Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Jul 5, 2024
1 parent ce09eb8 commit 995b1c3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pueue_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ chrono = { workspace = true }
command-group = { workspace = true }
dirs = "5.0"
handlebars = { workspace = true }
libc = "0.2.155"
log = { workspace = true }
rand = "0.8"
rcgen = "0.13"
Expand Down
16 changes: 9 additions & 7 deletions pueue_lib/src/network/socket/unix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::convert::TryFrom;
use std::fs;
use std::os::unix::fs::PermissionsExt;

use async_trait::async_trait;
use log::info;
Expand Down Expand Up @@ -144,14 +142,18 @@ pub async fn get_listener(settings: &Shared) -> Result<GenericListener, Error> {
})?;
}

// Adjust the umask prior to creating the socket.
let old_umask = settings
.unix_socket_permissions
.map(|permissions| unsafe { libc::umask(!permissions) });

let unix_listener = UnixListener::bind(&socket_path)
.map_err(|err| Error::IoPathError(socket_path.clone(), "creating unix socket", err))?;

// Adjust socket permissions
let mut permissions = fs::metadata(&socket_path)
.map_err(|err| Error::IoPathError(socket_path, "obtaining file metadata", err))?
.permissions();
permissions.set_mode(settings.unix_socket_permissions);
// Reset the umask now that the socket is created.
if let Some(permissions) = old_umask {
unsafe { libc::umask(permissions) };
}

return Ok(Box::new(unix_listener));
}
Expand Down
2 changes: 1 addition & 1 deletion pueue_lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct Shared {
/// socket. For a client to connect to the daemon, the client must have
/// read/write permissions.
#[cfg(not(target_os = "windows"))]
pub unix_socket_permissions: Option<u32>,
pub unix_socket_permissions: Option<libc::mode_t>,

/// The TCP hostname/ip address.
#[serde(default = "default_host")]
Expand Down

0 comments on commit 995b1c3

Please sign in to comment.