1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use crate::ipc::{IpcRequest, IpcResponse, deserialize, fs_name, serialize};
use crate::settings::settings;
use crate::{Result, env};
use interprocess::local_socket::ListenerOptions;
use interprocess::local_socket::tokio::{RecvHalf, SendHalf};
use interprocess::local_socket::traits::tokio::Listener;
use interprocess::local_socket::traits::tokio::Stream;
use miette::{IntoDiagnostic, bail, miette};
use std::time::Instant;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::oneshot;
/// Rate limiter for IPC connections to prevent local DoS attacks.
/// Uses a sliding window algorithm to limit requests per second.
struct RateLimiter {
/// Timestamps of recent requests within the window
requests: Vec<Instant>,
/// Maximum requests allowed per window
max_requests: usize,
/// Window duration in milliseconds
window_ms: u64,
}
impl RateLimiter {
fn new(max_requests: usize, window_ms: u64) -> Self {
Self {
requests: Vec::with_capacity(max_requests),
max_requests,
window_ms,
}
}
/// Check if a request is allowed. Returns true if allowed, false if rate limited.
fn check(&mut self) -> bool {
let now = Instant::now();
let window = std::time::Duration::from_millis(self.window_ms);
// Remove expired timestamps
self.requests.retain(|&t| now.duration_since(t) < window);
if self.requests.len() >= self.max_requests {
false
} else {
self.requests.push(now);
true
}
}
}
pub struct IpcServer {
// clients: Mutex<HashMap<String, interprocess::local_socket::tokio::Stream>>,
rx: Receiver<(IpcRequest, Sender<IpcResponse>)>,
}
/// Handle for triggering graceful shutdown of the IPC server
pub struct IpcServerHandle {
shutdown_tx: Option<oneshot::Sender<()>>,
}
impl IpcServerHandle {
/// Signal the IPC server to shut down gracefully
pub fn shutdown(&mut self) {
if let Some(tx) = self.shutdown_tx.take() {
let _ = tx.send(());
}
}
}
impl IpcServer {
pub fn new() -> Result<(Self, IpcServerHandle)> {
xx::file::mkdirp(&*env::IPC_SOCK_DIR)?;
let _ = xx::file::remove_file(&*env::IPC_SOCK_MAIN);
let opts = ListenerOptions::new().name(fs_name("main")?);
debug!("Listening on {}", env::IPC_SOCK_MAIN.display());
let (tx, rx) = tokio::sync::mpsc::channel(1);
let (shutdown_tx, mut shutdown_rx) = oneshot::channel();
// Set restrictive umask before creating socket to avoid TOCTOU race condition.
// This ensures the socket is created with 0600 permissions from the start.
// Note: IpcServer::new() is called during supervisor startup before other async
// tasks are spawned, so the brief umask change won't affect concurrent operations.
#[cfg(unix)]
let old_umask = unsafe { libc::umask(0o077) };
let listener_result = opts.create_tokio();
// Always restore original umask, even if socket creation failed
#[cfg(unix)]
unsafe {
libc::umask(old_umask);
}
let listener = listener_result.into_diagnostic()?;
// When the supervisor is started with `sudo`, the socket file and directory
// are owned by root with restrictive permissions (0600/0700). Non-root CLI
// clients need to connect to this socket.
//
// If SUDO_UID/SUDO_GID are available, chown the socket back to the
// original user so permissions stay tight (0700/0600) but the real user
// owns the files.
//
// If not running via sudo, the socket is already owned by the current
// user with restrictive permissions (set by the umask above), so no
// permission change is needed.
//
// Guard: only act when euid==0 to avoid stale SUDO_UID/SUDO_GID values
// inherited into non-sudo environments.
#[cfg(unix)]
{
if nix::unistd::Uid::effective().is_root() {
if let (Ok(uid_s), Ok(gid_s)) =
(std::env::var("SUDO_UID"), std::env::var("SUDO_GID"))
{
if let (Ok(uid), Ok(gid)) = (uid_s.parse::<u32>(), gid_s.parse::<u32>()) {
let _ = chown_path(&env::IPC_SOCK_DIR, uid, gid);
let _ = chown_path(&env::IPC_SOCK_MAIN, uid, gid);
debug!("chowned IPC socket to uid={uid} gid={gid}");
}
}
}
}
tokio::spawn(async move {
loop {
tokio::select! {
biased;
_ = &mut shutdown_rx => {
debug!("IPC server received shutdown signal");
break;
}
result = listener.accept() => {
match result {
Ok(stream) => {
trace!("Client accepted");
let (recv, send) = stream.split();
let mut incoming_chan = Self::read_messages_chan(recv);
let outgoing_chan = Self::send_messages_chan(send);
let tx = tx.clone();
tokio::spawn(async move {
while let Some(req) = incoming_chan.recv().await {
if let Err(err) = tx.send((req, outgoing_chan.clone())).await {
debug!("Failed to send message: {err:?}");
break;
}
}
trace!("IPC connection handler task terminated cleanly");
});
}
Err(err) => {
error!("ipc server accept error: {err:?}");
}
}
}
}
}
// Clean up socket file on graceful shutdown
let _ = std::fs::remove_file(&*env::IPC_SOCK_MAIN);
debug!("IPC server shut down cleanly");
});
let server = Self { rx };
let handle = IpcServerHandle {
shutdown_tx: Some(shutdown_tx),
};
Ok((server, handle))
}
async fn send(send: &mut SendHalf, msg: IpcResponse) -> Result<()> {
let mut msg = serialize(&msg)?;
if msg.contains(&0) {
bail!("IPC message contains null byte");
}
msg.push(0);
send.write_all(&msg).await.into_diagnostic()?;
Ok(())
}
/// Read raw bytes from socket until null terminator (without deserializing)
async fn read_raw_message(recv: &mut BufReader<RecvHalf>) -> Result<Option<Vec<u8>>> {
let mut bytes = Vec::new();
recv.read_until(0, &mut bytes).await.into_diagnostic()?;
if bytes.is_empty() {
return Ok(None);
}
Ok(Some(bytes))
}
fn read_messages_chan(recv: RecvHalf) -> Receiver<IpcRequest> {
let mut recv = BufReader::new(recv);
let (tx, rx) = tokio::sync::mpsc::channel(1);
tokio::spawn(async move {
// Rate limit: use configured requests per configured window
// This is generous for normal CLI usage but prevents flooding
let s = settings();
let window_ms = u64::try_from(s.ipc_rate_limit_window().as_millis()).unwrap_or(1000);
let window_ms = if window_ms < 100 {
warn!(
"ipc.rate_limit_window is {window_ms}ms which is too small (< 100ms), \
clamping to 100ms to avoid effectively disabling rate limiting"
);
100
} else {
window_ms
};
let max_requests = match usize::try_from(s.ipc.rate_limit) {
Ok(0) => {
warn!("ipc.rate_limit is 0, which would block all IPC requests; clamping to 1");
1
}
Ok(n) => n,
Err(_) => {
warn!(
"ipc.rate_limit value {} is out of range, clamping to 100",
s.ipc.rate_limit
);
100
}
};
let mut rate_limiter = RateLimiter::new(max_requests, window_ms);
loop {
// Check rate limit BEFORE reading to avoid wasting CPU on deserialization
// when rate limited. We still need to drain the socket to prevent buffer
// buildup, but we skip the costly deserialization step.
let is_rate_limited = !rate_limiter.check();
// Read raw bytes from socket
let bytes = match Self::read_raw_message(&mut recv).await {
Ok(Some(bytes)) => bytes,
Ok(None) => {
trace!("Client disconnected");
break;
}
Err(err) => {
// I/O errors are not rate-limited (they indicate connection issues)
debug!("Failed to read from socket: {err:?}");
break;
}
};
// If rate limited, drop the message without deserializing
if is_rate_limited {
warn!("IPC client rate limited, dropping message");
continue;
}
// Deserialize the message
let msg = match deserialize(&bytes) {
Ok(msg) => {
trace!("Received message: {msg:?}");
msg
}
Err(err) => {
// Send an Invalid request so the handler can respond with an error
warn!("Failed to deserialize message: {err:?}");
IpcRequest::Invalid {
error: format!("{err:#}"),
}
}
};
if let Err(err) = tx.send(msg).await {
warn!("Failed to emit message: {err:?}");
break;
}
}
trace!("IPC read task terminated cleanly");
});
rx
}
fn send_messages_chan(mut send: SendHalf) -> Sender<IpcResponse> {
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
tokio::spawn(async move {
loop {
let msg = match rx.recv().await {
Some(msg) => {
trace!("Sending message: {msg:?}");
msg
}
None => {
trace!("IPC channel closed");
break;
}
};
if let Err(err) = Self::send(&mut send, msg).await {
// Broken-pipe / reset is expected when a client disconnects normally
// Traverse the error source chain to find the original io::Error
// since miette wraps it in a DiagnosticError
use std::error::Error as StdError;
let is_disconnect = {
let mut cur: Option<&dyn StdError> = Some(err.as_ref() as &dyn StdError);
let mut found = false;
while let Some(e) = cur {
if let Some(io) = e.downcast_ref::<std::io::Error>() {
found = matches!(
io.kind(),
std::io::ErrorKind::BrokenPipe
| std::io::ErrorKind::ConnectionReset
);
break;
}
cur = e.source();
}
found
};
if is_disconnect {
debug!("IPC client disconnected: {err:?}");
} else {
warn!("Failed to send message: {err:?}");
}
break;
}
}
trace!("IPC send task terminated cleanly");
});
tx
}
pub async fn read(&mut self) -> Result<(IpcRequest, Sender<IpcResponse>)> {
self.rx
.recv()
.await
.ok_or_else(|| miette!("IPC channel closed"))
}
pub fn close(&self) {
debug!("Closing IPC server");
let _ = std::fs::remove_file(&*env::IPC_SOCK_MAIN);
}
}
impl Drop for IpcServer {
fn drop(&mut self) {
self.close();
}
}
/// `chown` a single path using libc. Returns Ok(()) on success.
#[cfg(unix)]
fn chown_path(path: &std::path::Path, uid: u32, gid: u32) -> std::io::Result<()> {
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
let c_path = CString::new(path.as_os_str().as_bytes())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
let ret = unsafe { libc::chown(c_path.as_ptr(), uid, gid) };
if ret == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}