zlayer-overlayd 0.13.0

Standalone ZLayer overlay daemon: owns the WireGuard/Wintun adapter, HCN/bridge mechanics, IP allocation, DNS and NAT; driven by the main daemon over IPC
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! `zlayer-overlayd` — the standalone `ZLayer` overlay daemon binary.
//!
//! Binds the IPC endpoint ([`transport::serve`]), wraps an [`OverlaydServer`]
//! in an `Arc<Mutex<_>>`, and runs each accepted connection's request loop
//! against it. A `Shutdown` request drops the overlay adapter and exits the
//! process gracefully.
//!
//! On Windows, when launched by the Service Control Manager with `--service`,
//! control is handed to the SCM dispatcher in [`mod@service`] which registers a
//! control handler (Stop/Shutdown → graceful exit), reports `Running`/`Stopped`,
//! and drives [`run_overlayd`] on a runtime it builds itself. The plain
//! foreground path uses the same [`run_overlayd`] with a `Ctrl-C`-wired
//! shutdown so dev runs exit cleanly too.

use std::path::PathBuf;
use std::sync::Arc;

use clap::Parser;
use tokio::sync::Mutex;
use zlayer_overlayd::server::OverlaydServer;
use zlayer_overlayd::transport;
use zlayer_paths::ZLayerDirs;
use zlayer_types::overlayd::{OverlaydFrame, OverlaydRequest};

#[cfg(windows)]
mod service;

/// Standalone `ZLayer` overlay daemon.
#[derive(Debug, Parser)]
#[command(name = "zlayer-overlayd", about, version)]
struct Args {
    /// Root data directory (HCN markers, IPAM state, UAPI sockets live under it).
    #[arg(long, value_name = "PATH")]
    data_dir: Option<PathBuf>,

    /// IPC endpoint to listen on (Unix socket path, or `\\.\pipe\...` on
    /// Windows). Defaults to a data-dir-aware path.
    #[arg(long, value_name = "PATH")]
    socket: Option<PathBuf>,

    /// Run under the Windows Service Control Manager (SCM). Set automatically
    /// by `zlayer daemon install` when registering the overlayd service; not
    /// meant to be passed by hand. Windows-only; ignored elsewhere.
    #[arg(long, hide = true)]
    #[cfg_attr(not(windows), allow(dead_code))]
    service: bool,

    /// Print the deterministic source build-id (stamped at compile time from the
    /// overlayd source files) and exit. The install/sync change-gate runs this on
    /// the staged and installed binaries to decide whether the overlayd service
    /// needs re-registering — it's stable across non-reproducible rebuilds of
    /// byte-identical source, unlike the binary's content hash. Not meant to be
    /// passed by hand.
    #[arg(long, hide = true)]
    build_id: bool,
}

fn main() -> anyhow::Result<()> {
    let args = Args::parse();

    // Source build-id probe: print the compile-time-stamped id and exit. The
    // change-gate shells out to this, so it must stay side-effect-free and run
    // before any logging init or privileged setup.
    if args.build_id {
        println!("{}", env!("ZLAYER_OVERLAYD_BUILD_ID"));
        return Ok(());
    }

    // Shared logging: console + a rotated file sink at ~/.zlayer/logs/overlayd.log
    // (plus OTLP forwarding when the OTEL_* env is set). Hold the guards for the
    // whole process so the async file writer keeps flushing. A failed init must
    // not stop the daemon from coming up, so fall back to a bare stderr subscriber.
    let _log_guards = match zlayer_observability::init_common_logging(
        "overlayd",
        zlayer_observability::CommonLoggingOptions::default(),
    ) {
        Ok(guards) => Some(guards),
        Err(e) => {
            eprintln!("overlayd: shared logging init failed ({e}); falling back to stderr");
            tracing_subscriber::fmt()
                .with_env_filter(
                    tracing_subscriber::EnvFilter::try_from_default_env()
                        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
                )
                .init();
            None
        }
    };

    let data_dir = args.data_dir.unwrap_or_else(ZLayerDirs::default_data_dir);
    let socket = args
        .socket
        .unwrap_or_else(|| PathBuf::from(ZLayerDirs::default_overlayd_socket_path_for(&data_dir)));

    tracing::info!(
        data_dir = %data_dir.display(),
        socket = %socket.display(),
        "starting zlayer-overlayd"
    );

    // Windows SCM mode: hand control to the dispatcher, which owns its own
    // tokio runtime and drives `run_overlayd` for us. Blocks until SCM stops
    // the service.
    #[cfg(windows)]
    if args.service {
        return service::run_as_overlayd_service(data_dir, socket);
    }

    // Foreground / dev mode: build a runtime and drive `run_overlayd` directly,
    // wiring Ctrl-C (and SIGTERM on Unix) to the external shutdown so the
    // process exits cleanly when interrupted.
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .thread_name("zlayer-overlayd-worker")
        .build()?;

    runtime.block_on(async move {
        let (external_tx, external_rx) = tokio::sync::oneshot::channel::<()>();
        tokio::spawn(async move {
            // Wait for the FIRST termination signal — Ctrl-C (SIGINT) in dev, or
            // SIGTERM from `systemctl stop` — then fire the external shutdown so
            // `run_overlayd` can run teardown before exiting. Without the SIGTERM
            // arm, `systemctl stop` (default SIGTERM) would kill the process with
            // no teardown and leave host network state behind until a reboot.
            #[cfg(unix)]
            {
                use tokio::signal::unix::{signal, SignalKind};
                match signal(SignalKind::terminate()) {
                    Ok(mut sigterm) => {
                        tokio::select! {
                            r = tokio::signal::ctrl_c() => {
                                if r.is_ok() {
                                    tracing::info!(
                                        "overlayd: Ctrl-C (SIGINT) received; signaling shutdown"
                                    );
                                }
                            }
                            _ = sigterm.recv() => {
                                tracing::info!("overlayd: SIGTERM received; signaling shutdown");
                            }
                        }
                    }
                    Err(e) => {
                        // Couldn't install the SIGTERM handler; still honor Ctrl-C.
                        tracing::warn!(error = %e, "overlayd: failed to install SIGTERM handler");
                        if tokio::signal::ctrl_c().await.is_ok() {
                            tracing::info!("overlayd: Ctrl-C received; signaling shutdown");
                        }
                    }
                }
            }
            #[cfg(not(unix))]
            if tokio::signal::ctrl_c().await.is_ok() {
                tracing::info!("overlayd: Ctrl-C received; signaling shutdown");
            }
            let _ = external_tx.send(());
        });
        run_overlayd(data_dir, socket, external_rx).await
    })
}

/// Run the overlayd serve loop until either an in-band `Shutdown` IPC request
/// is observed, the `external_shutdown` oneshot fires, or the accept loop
/// returns. Shared by the foreground path and the Windows SCM dispatcher.
///
/// `external_shutdown` is fired by the caller's signal handler (Ctrl-C in
/// dev, an SCM Stop/Shutdown control under the service) and joins the same
/// `select!` as the existing in-band shutdown, so both routes tear the
/// adapter down gracefully.
///
/// # Errors
///
/// Returns an error if binding the IPC endpoint or the accept loop fails.
async fn run_overlayd(
    data_dir: PathBuf,
    socket: PathBuf,
    external_shutdown: tokio::sync::oneshot::Receiver<()>,
) -> anyhow::Result<()> {
    // Scope the overlay's UAPI sockets to this data dir so a test daemon's
    // sockets don't collide with a host-wide install.
    let dirs = ZLayerDirs::new(data_dir.clone());
    let uapi_dir = dirs.wireguard();

    // Self-heal the boringtun UAPI socket directory before any overlay setup.
    // boringtun hardcodes its control socket under this dir and, on a restricted
    // root overlayd (no CAP_DAC_OVERRIDE), cannot delete a stale `zl-*-g.sock`
    // left in it if the dir is owned by a non-root uid — which permanently wedges
    // the global overlay with `bind(): Address already in use`. See
    // `normalize_uapi_sock_dir` for the full rationale.
    #[cfg(not(windows))]
    normalize_uapi_sock_dir(&uapi_dir);

    let server = OverlaydServer::new(data_dir).with_uapi_sock_dir(uapi_dir);
    let server = Arc::new(Mutex::new(server));

    // A oneshot fired when a connection handler observes a Shutdown request, so
    // `serve`'s accept loop can be aborted and the process exit gracefully.
    let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();
    let shutdown_tx = Arc::new(Mutex::new(Some(shutdown_tx)));

    let serve = {
        let server = Arc::clone(&server);
        let shutdown_tx = Arc::clone(&shutdown_tx);
        transport::serve(&socket, move |mut conn| {
            let server = Arc::clone(&server);
            let shutdown_tx = Arc::clone(&shutdown_tx);
            async move {
                loop {
                    let frame = match conn.recv().await {
                        Ok(f) => f,
                        Err(zlayer_overlayd::OverlaydError::Closed) => break,
                        Err(e) => {
                            tracing::warn!(error = %e, "overlayd: recv failed; closing connection");
                            break;
                        }
                    };
                    let OverlaydFrame::Request { id, request } = frame else {
                        tracing::warn!("overlayd: received non-Request frame; ignoring");
                        continue;
                    };

                    let (response, shutdown) = {
                        let mut guard = server.lock().await;
                        let response = guard.handle(request).await;
                        (response, guard.shutdown_requested())
                    };

                    if let Err(e) = conn.send(&OverlaydFrame::Response { id, response }).await {
                        tracing::warn!(error = %e, "overlayd: send failed; closing connection");
                        break;
                    }

                    if shutdown {
                        tracing::info!("overlayd: shutdown requested; signaling exit");
                        if let Some(tx) = shutdown_tx.lock().await.take() {
                            let _ = tx.send(());
                        }
                        break;
                    }
                }
            }
        })
    };

    tokio::select! {
        res = serve => {
            res?;
        }
        _ = shutdown_rx => {
            // A connection handler observed an in-band `Shutdown` request. Its
            // `handle()` already ran `teardown_global_overlay()` (it set
            // `shutdown_requested`), so no additional teardown is needed here.
            tracing::info!("overlayd: exiting after graceful shutdown (in-band request)");
        }
        _ = external_shutdown => {
            // Ctrl-C / SIGTERM. Nothing has torn the overlay down yet, so run the
            // same graceful teardown the in-band `Shutdown` request performs by
            // routing a `Shutdown` through the existing dispatch path. This reverts
            // global overlay + host network state (forwarding sysctls, overlay
            // iptables chains) so a `systemctl stop` recovers connectivity without
            // a reboot. Guard on `shutdown_requested()` so teardown runs exactly
            // once even if an in-band `Shutdown` raced in just before the signal.
            let mut guard = server.lock().await;
            if guard.shutdown_requested() {
                tracing::info!(
                    "overlayd: exiting after graceful shutdown \
                     (external signal; teardown already performed)"
                );
            } else {
                tracing::info!(
                    "overlayd: external signal received; running global overlay teardown"
                );
                let _ = guard.handle(OverlaydRequest::Shutdown).await;
                tracing::info!("overlayd: exiting after graceful shutdown (external signal)");
            }
        }
    }

    Ok(())
}

/// Remove every stale boringtun UAPI control socket (`zl-*.sock`) from `dir`.
///
/// On a fresh overlayd boot every such socket is orphaned — the process that
/// created it is gone — so removing it is always safe and clears both the
/// stable-name global `-g` socket and any historical per-service leftovers. The
/// caller must already own `dir` (or hold `CAP_DAC_OVERRIDE`); see
/// [`normalize_uapi_sock_dir`], which guarantees that before calling this.
#[cfg(not(windows))]
fn sweep_stale_uapi_sockets(dir: &std::path::Path) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if name.starts_with("zl-") && name.ends_with(".sock") {
            let _ = std::fs::remove_file(entry.path());
        }
    }
}

/// Normalize the boringtun UAPI socket directory so overlayd always operates in
/// a **root-owned** directory free of stale sockets.
///
/// boringtun hardcodes its UAPI control socket under `/var/run/wireguard/`
/// (`SOCK_DIR`, see `boringtun::device::api`) and ignores any configured path.
/// If that directory ends up owned by a non-root uid (e.g. created by a rootless
/// run), a capability-restricted root overlayd — which holds `CAP_NET_ADMIN` but
/// **not** `CAP_DAC_OVERRIDE` — cannot `unlink()` the stale `zl-*-g.sock` left by
/// a prior instance: deleting a file needs write on the *parent* directory, which
/// the kernel denies on a foreign-owned dir without `CAP_DAC_OVERRIDE`. boringtun's
/// own pre-bind `remove_file` then also fails, `bind()` returns `EADDRINUSE`, and
/// the global overlay never comes up → the node's overlay IP stays `None` →
/// internal service endpoints fall back to binding `127.0.0.1` (and lose overlay
/// DNS). With the now-stable per-host instance id the global interface name is
/// constant across restarts, so the same un-removable socket collides on *every*
/// boot — a permanent wedge.
///
/// This runs once at startup, before any `SetupGlobalOverlay`, and is
/// **capability-free**: when the dir is foreign-owned we `rename` it aside within
/// the same parent (`/run`), which needs only write on the parent — held by uid 0
/// as its owner, no `CAP_DAC_OVERRIDE` required — then recreate it root-owned.
#[cfg(not(windows))]
fn normalize_uapi_sock_dir(dir: &std::path::Path) {
    use std::os::unix::fs::MetadataExt;
    use std::os::unix::fs::PermissionsExt;

    // pid + a process-local counter keep any move-aside name unique without a
    // wall-clock read.
    static ASIDE_SEQ: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

    // Rootless: the daemon entered a userns + private mount namespace and bound a
    // fresh tmpfs over `/var/run/wireguard` (see `bin/zlayer/src/rootless.rs`
    // `redirect_wireguard_sock_dir`). That tmpfs is owned by uid 0 *inside the
    // userns* and already empty, so the move-aside path below is both unnecessary
    // and impossible (a `rename` of the tmpfs mountpoint within `/run` fails). Keep
    // only the idempotent create_dir_all + stale-socket sweep.
    if std::env::var_os("ZLAYER_ROOTLESS").is_some() {
        if let Err(e) = std::fs::create_dir_all(dir) {
            tracing::warn!(dir = %dir.display(), error = %e, "failed to create UAPI socket dir (rootless)");
            return;
        }
        let _ = std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700));
        sweep_stale_uapi_sockets(dir);
        return;
    }

    // If the directory exists but is owned by a non-root uid, move it aside so we
    // can recreate it root-owned. Renaming an entry within its parent updates only
    // the parent's directory block (the moved dir's `..` is unchanged), so it needs
    // write on the parent alone — available to uid 0 without CAP_DAC_OVERRIDE.
    if let Ok(meta) = std::fs::metadata(dir) {
        if meta.uid() != 0 {
            if let Some(parent) = dir.parent() {
                let name = dir.file_name().map_or_else(
                    || "wireguard".to_string(),
                    |n| n.to_string_lossy().into_owned(),
                );
                let seq = ASIDE_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                let aside = parent.join(format!(".{name}.stale-{}-{seq}", std::process::id()));
                match std::fs::rename(dir, &aside) {
                    Ok(()) => tracing::warn!(
                        dir = %dir.display(),
                        uid = meta.uid(),
                        moved_to = %aside.display(),
                        "UAPI socket dir was not root-owned; moved aside and recreating root-owned"
                    ),
                    Err(e) => tracing::warn!(
                        dir = %dir.display(),
                        uid = meta.uid(),
                        error = %e,
                        "UAPI socket dir is not root-owned and could not be moved aside; \
                         overlay setup may fail to clear a stale socket"
                    ),
                }
            }
        }
    }

    // Ensure the dir exists (recreated root-owned if we just moved one aside) and
    // is private to root.
    if let Err(e) = std::fs::create_dir_all(dir) {
        tracing::warn!(dir = %dir.display(), error = %e, "failed to create UAPI socket dir");
        return;
    }
    let _ = std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700));

    // Now that we own the dir, sweep any stale boringtun control sockets so the
    // global overlay can bind cleanly.
    sweep_stale_uapi_sockets(dir);
}

#[cfg(all(test, not(windows)))]
mod tests {
    use super::*;
    use std::fs;
    use std::os::unix::ffi::OsStrExt;
    use std::os::unix::fs::MetadataExt;

    fn unique_tmp(tag: &str) -> std::path::PathBuf {
        static SEQ: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
        let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        std::env::temp_dir().join(format!(
            "zlayer-overlayd-uapitest-{tag}-{}-{seq}",
            std::process::id()
        ))
    }

    /// The stale-socket sweep removes `zl-*.sock` entries and leaves everything
    /// else alone. No root required.
    #[test]
    fn sweep_removes_only_zl_sockets() {
        let dir = unique_tmp("sweep");
        fs::create_dir_all(&dir).unwrap();
        let stale_global = dir.join("zl-124aeda7be-g.sock");
        let stale_service = dir.join("zl-81c6bc17c7-s.sock");
        let keep = dir.join("keep.txt");
        let keep_sock = dir.join("other.sock"); // not a zl- socket
        for p in [&stale_global, &stale_service, &keep, &keep_sock] {
            fs::write(p, b"x").unwrap();
        }

        sweep_stale_uapi_sockets(&dir);

        assert!(
            !stale_global.exists(),
            "stale global socket should be swept"
        );
        assert!(
            !stale_service.exists(),
            "stale service socket should be swept"
        );
        assert!(keep.exists(), "non-socket file must be preserved");
        assert!(
            keep_sock.exists(),
            "a .sock without the zl- prefix must be preserved"
        );

        let _ = fs::remove_dir_all(&dir);
    }

    /// On a directory already owned by root (the normal case), `normalize` keeps
    /// the directory in place and just sweeps stale sockets. No root required:
    /// when the test runs as root the dir we create is root-owned and the
    /// in-place path is exercised; when run as non-root the dir is moved aside and
    /// recreated, but the post-condition (dir exists, no stale `zl-*.sock`) holds
    /// in both cases — which is exactly the contract callers depend on.
    #[test]
    fn normalize_leaves_usable_root_owned_dir_without_stale_sockets() {
        let dir = unique_tmp("normalize");
        fs::create_dir_all(&dir).unwrap();
        let stale = dir.join("zl-124aeda7be-g.sock");
        fs::write(&stale, b"x").unwrap();

        normalize_uapi_sock_dir(&dir);

        assert!(dir.exists(), "uapi dir must exist after normalize");
        assert!(
            !dir.join("zl-124aeda7be-g.sock").exists(),
            "stale socket must not survive in the live dir after normalize"
        );

        let _ = fs::remove_dir_all(&dir);
        reap_aside_siblings(&dir);
    }

    /// Best-effort cleanup of any `.<name>.stale-*` sibling left when normalize
    /// took the move-aside path (non-root test runs).
    fn reap_aside_siblings(dir: &std::path::Path) {
        if let Some(parent) = dir.parent() {
            if let Some(name) = dir.file_name().map(|n| n.to_string_lossy().into_owned()) {
                let prefix = format!(".{name}.stale-");
                if let Ok(entries) = fs::read_dir(parent) {
                    for entry in entries.flatten() {
                        if entry.file_name().to_string_lossy().starts_with(&prefix) {
                            let _ = fs::remove_dir_all(entry.path());
                        }
                    }
                }
            }
        }
    }

    /// Root-gated: reproduces the exact production failure — a UAPI dir owned by a
    /// non-root uid with a root-owned stale socket inside. `normalize` must move it
    /// aside and hand back a root-owned, empty dir so the next `bind()` succeeds.
    /// Skipped (passes vacuously) when not run as root so `--workspace` stays green
    /// for non-root CI; exercised under the root-capable host run.
    #[test]
    #[allow(unsafe_code)] // libc::geteuid / libc::chown for the root-gated setup
    fn normalize_reclaims_foreign_owned_dir_root_only() {
        // SAFETY: `geteuid` is a pure read of the caller's effective uid.
        let is_root = unsafe { libc::geteuid() } == 0;
        if !is_root {
            eprintln!("skipping normalize_reclaims_foreign_owned_dir_root_only: requires root");
            return;
        }

        let dir = unique_tmp("foreign");
        fs::create_dir_all(&dir).unwrap();
        let stale = dir.join("zl-124aeda7be-g.sock");
        fs::write(&stale, b"x").unwrap();

        // chown the dir to a non-root uid (1 = daemon/bin, present on every Linux
        // box) to mimic the `/var/run/wireguard` owned-by-1000 production state.
        let c_dir = std::ffi::CString::new(dir.as_os_str().as_bytes()).unwrap();
        // SAFETY: chown on a path we just created; uid 1 / gid 1 are well-known.
        let rc = unsafe { libc::chown(c_dir.as_ptr(), 1, 1) };
        assert_eq!(rc, 0, "test setup: chown to uid 1 must succeed as root");
        assert_ne!(
            fs::metadata(&dir).unwrap().uid(),
            0,
            "test setup: dir should now be foreign-owned"
        );

        normalize_uapi_sock_dir(&dir);

        let meta = fs::metadata(&dir).expect("uapi dir must exist after normalize");
        assert_eq!(
            meta.uid(),
            0,
            "normalize must hand back a root-owned uapi dir"
        );
        assert!(
            !dir.join("zl-124aeda7be-g.sock").exists(),
            "the stale socket must be gone from the reclaimed dir"
        );

        let _ = fs::remove_dir_all(&dir);
        reap_aside_siblings(&dir);
    }
}