subc-daemon 0.53.3

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::{
    collections::{BTreeMap, BTreeSet},
    fs::{self, File},
    io::{self, Read},
    os::{
        fd::{AsFd, RawFd},
        unix::fs::MetadataExt,
    },
    time::{Duration, Instant, SystemTime},
};

use rustix::{
    fs::{self as rfs, FileType, Mode, OFlags},
    io::{fcntl_setfd, FdFlags},
};
use sha2::{Digest, Sha256};
use subc_os::child_spawn::{monotonic_ns, InheritedDescriptors};

use crate::{
    config_store::{StoreRoot, StoreWriterLock},
    live_children::{self, AdoptedPids},
    run_dir_lock::{RunDirLock, RUN_DIR_LOCK_FILE_NAME},
    supervise::{
        handover::{
            self, Cohort, Handover, HexBytes, LockPaths, LockRole, StreamWitness, VerifiedImage,
        },
        live_upgrade::{reconcile_child, InheritedStreams, ReconciledChild},
        resume_hello::Preparation,
        RegisteredConfiguredModule,
    },
};

use super::{
    start_lock_path, BootstrapConfig, BootstrapError, Path, PathBuf, StartLock, Supervisor,
    SupervisorHandle,
};

/// Formats that the daemon binary can resume on this platform.
pub const HANDOVER_VERSIONS: &[u32] = handover::HANDOVER_VERSIONS;

/// A startup request whose handover has been checked before daemon startup.
pub struct ResumeBootstrap {
    config: BootstrapConfig,
    inherited: Result<Inherited, io::Error>,
}

impl ResumeBootstrap {
    /// Validate the handover using only read-only boot configuration and the
    /// descriptor snapshot taken at process entry. A different running image
    /// is allowed to boot cold, but a malformed handover refuses startup.
    pub fn validate(
        config: BootstrapConfig,
        path: &Path,
        key_fd: RawFd,
        inherited: InheritedDescriptors,
    ) -> Result<Self, BootstrapError> {
        let inherited = validate(&config, path, key_fd, inherited)?;
        Ok(Self { config, inherited })
    }

    pub async fn run(self) -> Result<(), BootstrapError> {
        let inherited = match self.inherited {
            Ok(inherited) => Some(inherited),
            Err(error) => {
                tracing::warn!(%error, "running image did not match the upgrade target; starting cold");
                eprintln!("ck-subc: running image did not match the upgrade target; starting cold: {error}");
                None
            }
        };
        super::run_with_config_inner(self.config, inherited).await
    }
}

pub(super) struct Inherited {
    pub(super) start: StartLock,
    pub(super) run: RunDirLock,
    pub(super) store: Option<(StoreRoot, StoreWriterLock)>,
    pub(super) resume: Resume,
}

pub(super) struct Resume {
    handover: Handover,
    streams: BTreeMap<RawFd, std::os::fd::OwnedFd>,
    deadline: Instant,
    #[cfg(feature = "test-support")]
    pause_install: Option<(String, PathBuf)>,
}

fn refused(error: impl std::fmt::Display) -> BootstrapError {
    BootstrapError::Resume(io::Error::other(error.to_string()))
}

fn validate(
    config: &BootstrapConfig,
    path: &Path,
    key_fd: RawFd,
    mut inherited: InheritedDescriptors,
) -> Result<Result<Inherited, io::Error>, BootstrapError> {
    let record = config
        .live_children_path
        .as_ref()
        .ok_or_else(|| refused("no live-children record configured"))?;
    let expected = record.with_file_name(handover::HANDOVER_FILE_NAME);
    if path.as_os_str() != expected.as_os_str() {
        return Err(refused(
            "handover path does not match the configured run directory",
        ));
    }
    let file = File::from(
        rfs::open(
            path,
            OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC,
            Mode::empty(),
        )
        .map_err(refused)?,
    );
    let stat = file.metadata().map_err(refused)?;
    if !stat.is_file() || stat.uid() != subc_os::effective_uid() || stat.mode() & 0o7777 != 0o600 {
        return Err(refused(
            "handover must be a regular file owned by this user with mode 0600",
        ));
    }
    let key =
        read_key(File::from(inherited.take_key(key_fd).map_err(refused)?)).map_err(refused)?;
    let mut bytes = Vec::new();
    (&file).read_to_end(&mut bytes).map_err(refused)?;
    let mut handover = handover::decode(&bytes, &key).map_err(refused)?;
    validate_cohort(&handover)?;
    let now_ms = crate::stderr_tail::unix_ms(SystemTime::now());
    let deadline = Instant::now()
        .checked_add(Duration::from_millis(
            handover.reconnect_by_ms.saturating_sub(now_ms),
        ))
        .ok_or_else(|| refused("handover reconnect deadline is out of range"))?;

    let run = record.with_file_name(RUN_DIR_LOCK_FILE_NAME);
    let publication = start_lock_path(&config.connection_file_path);
    let store = config
        .config_store_root()
        .map(StoreWriterLock::inherited_layout)
        .transpose()
        .map_err(refused)?;
    let paths = LockPaths {
        run_dir: &run,
        publication: &publication,
        store_writer: store.as_ref().map(|(_, lock)| lock.as_path()),
    };
    let expected_fds = handover
        .locks
        .iter()
        .map(|lock| lock.fd)
        .chain(
            handover
                .children
                .iter()
                .flat_map(|child| [&child.stdout, &child.stderr])
                .filter_map(|stream| match stream {
                    StreamWitness::Open { fd, .. } => Some(*fd),
                    StreamWitness::Ended {} => None,
                }),
        )
        .collect();
    let mut descriptors = inherited.into_named(&expected_fds).map_err(refused)?;
    handover::validate_inherited_fds(&handover, config, &paths, |fd| {
        descriptors.get(&fd).map(AsFd::as_fd)
    })
    .map_err(refused)?;

    if let Err(error) = verify_running_image(config, &handover.verified_image) {
        // Close the inherited descriptors without adopting their locks. The
        // cold boot removes the handover once it owns the run directory.
        return Ok(Err(error));
    }

    let current = fs::symlink_metadata(path).map_err(refused)?;
    if current.dev() != stat.dev() || current.ino() != stat.ino() {
        return Err(refused("handover file changed during validation"));
    }
    fs::remove_file(path).map_err(refused)?;
    for fd in descriptors.values() {
        fcntl_setfd(fd, FdFlags::CLOEXEC).map_err(refused)?;
    }

    let mut locks: BTreeMap<_, _> = handover
        .locks
        .drain(..)
        .map(|lock| {
            (
                lock.role,
                File::from(
                    descriptors
                        .remove(&lock.fd)
                        .expect("validated lock descriptor"),
                ),
            )
        })
        .collect();
    let start = StartLock {
        _file: locks
            .remove(&LockRole::Publication)
            .expect("validated publication lock"),
    };
    let run = RunDirLock::from_inherited(
        record.clone(),
        locks.remove(&LockRole::RunDir).expect("validated run lock"),
    );
    let store = store.map(|(root, _)| {
        (
            root,
            StoreWriterLock::from_inherited(
                locks
                    .remove(&LockRole::StoreWriter)
                    .expect("validated store lock"),
            ),
        )
    });
    Ok(Ok(Inherited {
        start,
        run,
        store,
        resume: Resume {
            handover,
            streams: descriptors,
            deadline,
            #[cfg(feature = "test-support")]
            pause_install: config
                .upgrade_binary
                .as_ref()
                .filter(|binary| binary.test_hooks)
                .and_then(|_| std::env::var("SUBC_TEST_RESUME_PAUSE_MODULE").ok())
                // No longer than `subc.sock`, so it fits the Unix socket path
                // limit wherever the daemon's own socket does.
                .map(|module| (module, record.with_file_name("rp.sock"))),
        },
    }))
}

fn read_key(mut pipe: File) -> io::Result<[u8; 32]> {
    let flags = rfs::fcntl_getfl(&pipe)?;
    if FileType::from_raw_mode(rfs::fstat(&pipe)?.st_mode) != FileType::Fifo
        || flags.intersects(OFlags::WRONLY | OFlags::RDWR)
    {
        return Err(io::Error::other(
            "handover key descriptor must be a pipe read end",
        ));
    }
    // The old image closes its writer before exec. Never block if a malformed
    // invocation presents a pipe whose writer is still open.
    rfs::fcntl_setfl(&pipe, flags | OFlags::NONBLOCK)?;
    let mut key = [0; 32];
    pipe.read_exact(&mut key)?;
    if pipe.read(&mut [0])? != 0 {
        return Err(io::Error::other("handover key must be exactly 32 bytes"));
    }
    Ok(key)
}

fn validate_cohort(handover: &Handover) -> Result<(), BootstrapError> {
    let cohort = &handover.cohort;
    let mut modules = BTreeSet::new();
    let carried: BTreeSet<_> = cohort.carried.iter().collect();
    let mut children = BTreeSet::new();
    let mut pids = BTreeSet::new();
    if !cohort
        .carried
        .iter()
        .chain(&cohort.restarting)
        .chain(&cohort.stopped)
        .all(|id| modules.insert(id))
        || !handover
            .children
            .iter()
            .all(|child| children.insert(&child.module_id) && pids.insert(child.pid))
        || carried != children
    {
        return Err(refused("handover children and cohort disagree"));
    }
    let mut streams = BTreeSet::new();
    for stream in handover
        .children
        .iter()
        .flat_map(|child| [&child.stdout, &child.stderr])
    {
        if let StreamWitness::Open { fd, .. } = stream {
            if !streams.insert(*fd) {
                return Err(refused(format!(
                    "duplicate handover stream descriptor {fd}"
                )));
            }
        }
    }
    Ok(())
}

fn verify_running_image(_config: &BootstrapConfig, expected: &VerifiedImage) -> io::Result<()> {
    #[cfg(target_os = "linux")]
    let (path, cdhash_matches) = (PathBuf::from("/proc/self/exe"), expected.cdhash.is_none());
    #[cfg(target_os = "macos")]
    let (path, cdhash_matches) = {
        use subc_os::code_signature::{self, SignatureError};
        let cdhash_matches = match (expected.cdhash, code_signature::self_cdhash()) {
            (Some(expected), Ok(actual)) => expected.0 == actual.0,
            #[cfg(feature = "test-support")]
            (None, Err(SignatureError::NotSigned)) => _config
                .upgrade_binary
                .as_ref()
                .is_some_and(|binary| binary.test_hooks),
            (_, Err(SignatureError::NotSigned)) | (None, Ok(_)) => false,
            (_, Err(error)) => return Err(io::Error::other(error)),
        };
        (std::env::current_exe()?, cdhash_matches)
    };
    let mut digest = Sha256::new();
    io::copy(&mut File::open(path)?, &mut digest)?;
    if !cdhash_matches || HexBytes(digest.finalize().into()) != expected.sha256 {
        return Err(io::Error::other(
            "running executable digest or code signature differs from the verified target",
        ));
    }
    Ok(())
}

/// Cold cleanup is only callable with ownership of the same run directory.
pub(super) fn discard_leftover(owner: &RunDirLock) {
    let path = owner
        .live_children_record()
        .with_file_name(handover::HANDOVER_FILE_NAME);
    if let Err(error) = fs::remove_file(&path) {
        if error.kind() != io::ErrorKind::NotFound {
            tracing::warn!(path = %path.display(), %error, "could not remove a leftover daemon handover");
        }
    }
}

pub(super) struct AdoptionPlan {
    pub(super) protected: AdoptedPids,
    pub(super) monotonic_ns: u64,
    cohort: Cohort,
    children: BTreeMap<String, (ReconciledChild, InheritedStreams)>,
    attempt: HexBytes<16>,
    started_at_ms: u64,
    deadline: Instant,
    #[cfg(feature = "test-support")]
    pause_install: Option<(String, PathBuf)>,
}

impl Resume {
    pub(super) fn verified_digest(&self) -> HexBytes<32> {
        self.handover.verified_image.sha256
    }

    pub(super) fn reconcile(mut self, owner: &RunDirLock) -> Result<AdoptionPlan, BootstrapError> {
        let records = match live_children::read_record_entries(owner.live_children_record()) {
            Ok((records, _)) => records,
            Err(error) => {
                // Missing identity makes the supervisor hold, never signal, a
                // listed child. The normal sweep quarantines the unusable file.
                tracing::warn!(%error, "could not read the carried children's durable identities");
                Vec::new()
            }
        };
        let by_pid: BTreeMap<_, _> = records
            .iter()
            .map(|record| (record.child.pid, record))
            .collect();
        let mut adopted = Vec::new();
        let mut held = Vec::new();
        let mut children = BTreeMap::new();
        for mut child in self.handover.children {
            let streams = [&mut child.stdout, &mut child.stderr].map(|stream| {
                match std::mem::replace(stream, StreamWitness::Ended {}) {
                    StreamWitness::Open { fd, tail } => Some((
                        self.streams
                            .remove(&fd)
                            .expect("validated stream descriptor"),
                        tail,
                    )),
                    StreamWitness::Ended {} => None,
                }
            });
            let pid = child.pid;
            let module = child.module_id.clone();
            let reconciled = reconcile_child(child, by_pid.get(&pid).copied()).map_err(refused)?;
            match &reconciled {
                ReconciledChild::Adopt(_) => adopted.push(pid),
                ReconciledChild::Held(_) => held.push(pid),
                ReconciledChild::Exited => {}
            }
            children.insert(module, (reconciled, streams));
        }
        Ok(AdoptionPlan {
            protected: AdoptedPids::from_classes(adopted, held),
            monotonic_ns: self.handover.monotonic_ns,
            cohort: self.handover.cohort,
            children,
            attempt: self.handover.attempt_id,
            started_at_ms: self.handover.started_at_ms,
            deadline: self.deadline,
            #[cfg(feature = "test-support")]
            pause_install: self.pause_install,
        })
    }
}

type RegisteredCohort = BTreeMap<String, (RegisteredConfiguredModule, bool)>;
pub(super) type PlannedStarts = Vec<(String, Option<RegisteredConfiguredModule>, bool)>;
type Preparations = BTreeMap<String, Preparation>;

impl AdoptionPlan {
    fn modules(&self) -> impl Iterator<Item = &String> {
        self.cohort
            .carried
            .iter()
            .chain(&self.cohort.restarting)
            .chain(&self.cohort.stopped)
    }

    pub(super) fn begin_report(&self, supervisor: &SupervisorHandle) {
        supervisor.begin_resume_report(
            self.attempt.to_string(),
            self.started_at_ms,
            self.modules().cloned(),
        );
    }

    pub(super) fn enabled(&self, module: &str, configured: bool) -> Option<bool> {
        self.modules()
            .any(|id| id == module)
            .then(|| self.cohort.allow_start(module, Some(configured)))
    }

    pub(super) fn stage_preparations(
        &self,
        handle: &SupervisorHandle,
        registered: &RegisteredCohort,
    ) -> Result<Preparations, BootstrapError> {
        self.children
            .iter()
            .filter_map(|(module, (child, _))| {
                let ReconciledChild::Adopt(child) = child else {
                    return None;
                };
                registered.get(module).filter(|(_, enabled)| *enabled)?;
                Some(
                    child
                        .begin_preparation(handle, self.deadline.into())
                        .map(|guard| (module.clone(), guard))
                        .map_err(refused),
                )
            })
            .collect()
    }

    pub(super) async fn install(
        mut self,
        supervisor: &Supervisor,
        mut registered: RegisteredCohort,
        mut preparations: Preparations,
    ) -> Result<PlannedStarts, BootstrapError> {
        let mut planned = Vec::new();
        let mut installing = tokio::task::JoinSet::new();
        let attempt = self.attempt.0;
        let deadline = self.deadline.into();
        for module in self
            .cohort
            .carried
            .into_iter()
            .chain(self.cohort.restarting)
            .chain(self.cohort.stopped)
        {
            let (registered, enabled) = match registered.remove(&module) {
                Some((registered, enabled)) => (Some(registered), enabled),
                None => (None, false),
            };
            match self.children.remove(&module) {
                Some((ReconciledChild::Adopt(child), streams)) => {
                    if let Some(registered) = registered {
                        let supervisor = supervisor.clone();
                        let preparation = preparations.remove(&module);
                        #[cfg(feature = "test-support")]
                        let pause = self
                            .pause_install
                            .as_ref()
                            .filter(|(id, _)| *id == module)
                            .map(|(_, path)| path.clone());
                        installing.spawn(async move {
                            let _preparation = preparation;
                            #[cfg(feature = "test-support")]
                            if let Some(path) = pause {
                                let _ = tokio::time::timeout_at(
                                    deadline,
                                    crate::control::upgrade_in_place::wait_at_gate(&path, 1),
                                )
                                .await;
                            }
                            supervisor
                                .start_adopted_after_upgrade(
                                    registered, *child, streams, attempt, deadline, enabled,
                                )
                                .await
                        });
                    } else {
                        supervisor.watch_unresolved_after_upgrade(child.stop(), None, false);
                    }
                }
                Some((ReconciledChild::Held(child), _)) => {
                    supervisor.watch_unresolved_after_upgrade(*child, registered, enabled);
                }
                Some((ReconciledChild::Exited, _)) | None => {
                    planned.push((module, registered, enabled))
                }
            }
        }
        while let Some(result) = installing.join_next().await {
            result.map_err(refused)?.map_err(refused)?;
        }
        Ok(planned)
    }
}

pub(super) fn log_accept(sample: u64) {
    match monotonic_ns() {
        Ok(now) => tracing::info!(
            resume_accept_ms = now.saturating_sub(sample) / 1_000_000,
            "local listener accepting daemon upgrade reconnects"
        ),
        Err(error) => {
            tracing::warn!(%error, "could not measure when the local listener started accepting")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn duplicate_stream_descriptor_is_refused_before_adoption() {
        let mut handover = handover::decode(
            include_bytes!("../../tests/fixtures/handover-format-2.txt"),
            b"0123456789abcdef0123456789abcdef",
        )
        .unwrap();
        validate_cohort(&handover).unwrap();
        let StreamWitness::Open { fd, .. } = handover.children[0].stdout else {
            panic!("golden handover must have an open stdout");
        };
        handover.children[1].stderr = StreamWitness::Open { fd, tail: vec![] };
        assert!(validate_cohort(&handover)
            .unwrap_err()
            .to_string()
            .contains("duplicate handover stream descriptor"));
    }

    #[test]
    fn handover_key_requires_exactly_32_bytes_and_a_closed_writer() {
        for key in [&[7; 31][..], &[7; 32], &[7; 33]] {
            let (reader, mut writer) = io::pipe().unwrap();
            writer.write_all(key).unwrap();
            drop(writer);
            let result = read_key(File::from(std::os::fd::OwnedFd::from(reader)));
            if key.len() == 32 {
                assert_eq!(result.unwrap(), [7; 32]);
            } else {
                assert!(result.is_err());
            }
        }
        let (reader, mut writer) = io::pipe().unwrap();
        writer.write_all(&[7; 32]).unwrap();
        let error = read_key(File::from(std::os::fd::OwnedFd::from(reader))).unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::WouldBlock);
        drop(writer);
    }
}