Skip to content

Commit

Permalink
fix: limit ipc socket filename length (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
viandoxdev authored Feb 15, 2022
1 parent 3fc24a9 commit fb0e57a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion crates/eww/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ tokio-util = "0.6"
sysinfo = "0.16.1"

dyn-clone = "1.0"
base64 = "0.13"
wait-timeout = "0.2"

notify = "5.0.0-pre.7"
Expand Down
24 changes: 19 additions & 5 deletions crates/eww/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use anyhow::{bail, Context, Result};
use daemon_response::{DaemonResponse, DaemonResponseReceiver};
use opts::ActionWithServer;
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
os::unix::net,
path::{Path, PathBuf},
time::Duration,
Expand Down Expand Up @@ -210,18 +212,30 @@ impl EwwPaths {
}

let config_dir = config_dir.canonicalize()?;
let daemon_id = base64::encode(format!("{}", config_dir.display()));

let mut hasher = DefaultHasher::new();
format!("{}", config_dir.display()).hash(&mut hasher);
// daemon_id is a hash of the config dir path to ensure that, given a normal XDG_RUNTIME_DIR,
// the absolute path to the socket stays under the 108 bytes limit. (see #387, man 7 unix)
let daemon_id = format!("{:x}", hasher.finish());

let ipc_socket_file = std::env::var("XDG_RUNTIME_DIR")
.map(std::path::PathBuf::from)
.unwrap_or_else(|_| std::path::PathBuf::from("/tmp"))
.join(format!("eww-server_{}", daemon_id));

// 100 as the limit isn't quite 108 everywhere (i.e 104 on BSD or mac)
if format!("{}", ipc_socket_file.display()).len() > 100 {
log::warn!("The IPC socket file's absolute path exceeds 100 bytes, the socket may fail to create.");
}

Ok(EwwPaths {
config_dir,
log_file: std::env::var("XDG_CACHE_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".cache"))
.join(format!("eww_{}.log", daemon_id)),
ipc_socket_file: std::env::var("XDG_RUNTIME_DIR")
.map(std::path::PathBuf::from)
.unwrap_or_else(|_| std::path::PathBuf::from("/tmp"))
.join(format!("eww-server_{}", daemon_id)),
ipc_socket_file,
})
}

Expand Down

0 comments on commit fb0e57a

Please sign in to comment.