fork 0.11.0

Library for creating a new process detached from the controlling terminal (daemon)
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
//! Serial, process-local lowered-rlimit descriptor isolation contracts.
//! Linux also disables `close_range` with seccomp; macOS uses the fallback natively.

use std::{
    error::Error,
    fs::File,
    io::{self, Read},
    os::fd::{AsRawFd, FromRawFd, OwnedFd},
    time::Duration,
};

use fork::{ChildEvent, PreparedCommand, ProcessGroupGuard, Signal, signal_process, wait_event};

const TIMEOUT: Duration = Duration::from_secs(2);

fn main() -> Result<(), Box<dyn Error>> {
    #[cfg(target_os = "linux")]
    if std::env::args().any(|arg| arg == "--reject-real-ranges") {
        reject_real_ranges()?;
        return check_range_failure();
    }
    #[cfg(target_os = "linux")]
    if std::env::args().any(|arg| arg == "--deny-inventory") {
        // The high fd is below the hard limit but above the later soft limit.
        // No /dev/fd open is possible, and close_range is unavailable.
        let limit = libc::rlimit {
            rlim_cur: 256,
            rlim_max: 256,
        };
        // SAFETY: this isolated subprocess only lowers its own finite limit.
        if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &raw const limit) } != 0 {
            return Err(io::Error::last_os_error().into());
        }
        deny_inventory()?;
        force_fallback()?;
        return check_descriptors();
    }
    check_descriptors()?;
    check_inventory_exhaustion()?;
    #[cfg(target_os = "linux")]
    {
        let status = std::process::Command::new(std::env::current_exe()?)
            .arg("--reject-real-ranges")
            .status()?;
        if !status.success() {
            return Err(io::Error::other("successful-probe/failed-range contract failed").into());
        }
        let status = std::process::Command::new(std::env::current_exe()?)
            .arg("--deny-inventory")
            .status()?;
        assert!(
            status.success(),
            "denied inventory must still close high descriptors"
        );
        force_fallback()?;
        check_descriptors()?;
        check_inventory_exhaustion()?;
    }
    Ok(())
}

/// Restore the process-local limit even on an early error.
struct Limit(libc::rlimit);
impl Limit {
    fn lower() -> io::Result<Self> {
        let mut old = libc::rlimit {
            rlim_cur: 0,
            rlim_max: 0,
        };
        // SAFETY: old is writable storage for the process-local limit.
        if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &raw mut old) } != 0 {
            return Err(io::Error::last_os_error());
        }
        let lower = libc::rlimit {
            rlim_cur: 64,
            ..old
        };
        // SAFETY: lower retains the hard limit; only this standalone process changes.
        if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &raw const lower) } != 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(Self(old))
    }
}
impl Drop for Limit {
    fn drop(&mut self) {
        // SAFETY: the original soft and hard limits came from getrlimit above.
        unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &raw const self.0) };
    }
}

fn high_pipe() -> io::Result<(File, OwnedFd)> {
    let (reader, writer) = fork::pipe_cloexec()?.into_parts();
    // SAFETY: the live writer is duplicated above the later lowered soft limit.
    // F_DUPFD deliberately leaves CLOEXEC unset to test prepared exec isolation.
    let raw = unsafe { libc::fcntl(writer.as_raw_fd(), libc::F_DUPFD, 128) };
    if raw < 0 {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: fcntl returned a newly owned descriptor.
    let high = unsafe { OwnedFd::from_raw_fd(raw) };
    // SAFETY: reader is owned and this test needs bounded, nonblocking reads.
    if unsafe { libc::fcntl(reader.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK) } < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok((File::from(reader), high))
}

fn check_descriptors() -> Result<(), Box<dyn Error>> {
    let (mut reader, high) = high_pipe()?;
    let limit = Limit::lower()?;
    let guard = ProcessGroupGuard::new()?;
    drop(high);
    let guard_result = reader.read(&mut [0]);
    guard.disarm(TIMEOUT)?;
    drop(limit);

    let (mut reader, high) = high_pipe()?;
    let limit = Limit::lower()?;
    let mut command = PreparedCommand::new("/bin/sleep")?;
    command.arg("30")?;
    let child = command.spawn(TIMEOUT)?;
    drop(high);
    let result = reader.read(&mut [0]);
    // Always clean up before checking the contract, including on the old code.
    signal_process(child.process(), Signal::KILL)?;
    let event = wait_event(child.process())?;
    drop(limit);
    if !matches!(
        event,
        ChildEvent::Signalled {
            signal: Signal::KILL,
            ..
        }
    ) {
        return Err(io::Error::other(format!("unexpected child event: {event:?}")).into());
    }
    if !matches!(guard_result, Ok(0)) || !matches!(result, Ok(0)) {
        return Err(io::Error::other(format!(
            "high descriptor isolation: guard={guard_result:?}, prepared child={result:?}"
        ))
        .into());
    }
    Ok(())
}

/// Exhaust low descriptors, then leave only enough for pre-inventory setup.
fn fill_descriptors(spare: usize) -> io::Result<Vec<File>> {
    let mut fillers = Vec::new();
    loop {
        match File::open("/dev/null") {
            Ok(file) => fillers.push(file),
            Err(error) if error.raw_os_error() == Some(libc::EMFILE) => break,
            Err(error) => return Err(error),
        }
    }
    for _ in 0..spare {
        drop(fillers.pop());
    }
    Ok(fillers)
}

/// EMFILE cannot open an inventory: descriptor-free probing must still permit
/// preparation, without silently using only the lowered soft limit.
fn check_inventory_exhaustion() -> Result<(), Box<dyn Error>> {
    let (mut reader, high) = high_pipe()?;
    let limit = Limit::lower()?;
    let fillers = fill_descriptors(2)?; // exactly the guard socketpair
    let guard_rejected = match ProcessGroupGuard::new() {
        Ok(guard) => {
            guard.disarm(TIMEOUT)?;
            false
        }
        Err(error) if error.raw_os_error() == Some(libc::EMFILE) => true,
        Err(error) => return Err(error.into()),
    };
    drop(high);
    let guard_eof = reader.read(&mut [0]);
    drop(fillers);
    drop(limit);

    let (mut reader, high) = high_pipe()?;
    let mut command = PreparedCommand::new("/bin/sleep")?;
    command.arg("30")?.map_descriptor(high, 3)?;
    let limit = Limit::lower()?;
    // The status pipe needs two slots and its duplicated writer a third.
    // Mapping the high source then fills the freed original status slot;
    // dropping the source above the soft limit does not free a low slot.
    let fillers = fill_descriptors(3)?;
    let child_rejected = match command.spawn(TIMEOUT) {
        Ok(child) => {
            signal_process(child.process(), Signal::KILL)?;
            wait_event(child.process())?;
            false
        }
        Err(fork::SpawnError::OperatingSystem {
            stage: fork::SpawnStage::DescriptorDuplication,
            error,
            cleanup_pending: None,
        }) if error.raw_os_error() == Some(libc::EMFILE) => true,
        Err(error) => return Err(error.into()),
    };
    let child_eof = reader.read(&mut [0]);
    drop(fillers);
    drop(limit);
    assert!(matches!(guard_eof, Ok(0)) && matches!(child_eof, Ok(0)));
    assert!(
        matches!(fork::wait_any_event_nohang(), Err(error) if error.raw_os_error() == Some(libc::ECHILD))
    );
    if guard_rejected || child_rejected {
        return Err(io::Error::other(format!(
            "descriptor-free fallback failed: guard rejected={guard_rejected}, prepared spawn rejected={child_rejected}"
        )).into());
    }
    Ok(())
}

/// Model a sandbox without directory access, rather than descriptor exhaustion.
#[cfg(target_os = "linux")]
fn deny_inventory() -> io::Result<()> {
    let mut filter = [
        libc::sock_filter {
            code: 0x20,
            jt: 0,
            jf: 0,
            k: 0,
        },
        libc::sock_filter {
            code: 0x15,
            jt: 0,
            jf: 3,
            k: u32::try_from(libc::SYS_openat).map_err(io::Error::other)?,
        },
        libc::sock_filter {
            code: 0x20,
            jt: 0,
            jf: 0,
            k: if cfg!(target_endian = "little") {
                32
            } else {
                36
            },
        },
        libc::sock_filter {
            code: 0x45,
            jt: 0,
            jf: 1,
            k: libc::O_DIRECTORY.cast_unsigned(),
        },
        libc::sock_filter {
            code: 0x06,
            jt: 0,
            jf: 0,
            k: libc::SECCOMP_RET_ERRNO | libc::EACCES.cast_unsigned(),
        },
        libc::sock_filter {
            code: 0x06,
            jt: 0,
            jf: 0,
            k: libc::SECCOMP_RET_ALLOW,
        },
    ];
    let program = libc::sock_fprog {
        len: 6,
        filter: filter.as_mut_ptr(),
    };
    // SAFETY: live BPF storage; only this standalone subprocess is restricted.
    if unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) } != 0
        || unsafe {
            libc::prctl(
                libc::PR_SET_SECCOMP,
                libc::SECCOMP_MODE_FILTER,
                &raw const program,
            )
        } != 0
    {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn force_fallback() -> io::Result<()> {
    let mut filter = [
        libc::sock_filter {
            code: 0x20,
            jt: 0,
            jf: 0,
            k: 0,
        },
        libc::sock_filter {
            code: 0x15,
            jt: 0,
            jf: 1,
            k: u32::try_from(libc::SYS_close_range).map_err(io::Error::other)?,
        },
        libc::sock_filter {
            code: 0x06,
            jt: 0,
            jf: 0,
            k: libc::SECCOMP_RET_ERRNO | libc::ENOSYS.cast_unsigned(),
        },
        libc::sock_filter {
            code: 0x06,
            jt: 0,
            jf: 0,
            k: libc::SECCOMP_RET_ALLOW,
        },
    ];
    let program = libc::sock_fprog {
        len: 4,
        filter: filter.as_mut_ptr(),
    };
    // SAFETY: this single-threaded standalone test restricts only itself and
    // its children. The BPF program is live and permits all but close_range.
    if unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) } != 0
        // SAFETY: program points to the initialized filter for this syscall.
        || unsafe { libc::prctl(libc::PR_SET_SECCOMP, libc::SECCOMP_MODE_FILTER, &raw const program) } != 0
    {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// No incomplete soft-limit fallback may turn a failed range close into success.
#[cfg(target_os = "linux")]
fn check_range_failure() -> Result<(), Box<dyn Error>> {
    let (mut reader, high) = high_pipe()?;
    let limit = Limit::lower()?;
    let guard_rejected = match ProcessGroupGuard::new() {
        Ok(guard) => {
            guard.disarm(TIMEOUT)?;
            false
        }
        // The helper reports the failed real close's errno, as the prepared
        // command and checked daemon do.
        Err(error) if error.raw_os_error() == Some(libc::ENOSYS) => true,
        Err(error) => return Err(error.into()),
    };
    drop(high);
    let guard_eof = reader.read(&mut [0]);
    drop(limit);

    let (mut reader, high) = high_pipe()?;
    let limit = Limit::lower()?;
    let mut command = PreparedCommand::new("/bin/sleep")?;
    command.arg("30")?;
    let child_rejected = match command.spawn(TIMEOUT) {
        Ok(child) => {
            signal_process(child.process(), Signal::KILL)?;
            wait_event(child.process())?;
            false
        }
        Err(fork::SpawnError::OperatingSystem {
            stage: fork::SpawnStage::DescriptorDuplication,
            error,
            cleanup_pending: None,
        }) if error.raw_os_error() == Some(libc::ENOSYS) => true,
        Err(error) => return Err(error.into()),
    };
    drop(high);
    let child_eof = reader.read(&mut [0]);
    drop(limit);
    assert!(matches!(guard_eof, Ok(0)) && matches!(child_eof, Ok(0)));
    assert!(
        matches!(fork::wait_any_event_nohang(), Err(error) if error.raw_os_error() == Some(libc::ECHILD))
    );
    let daemon_rejected = daemon_rejects_range_failure()?;
    assert!(
        guard_rejected && child_rejected && daemon_rejected,
        "real close_range failure was accepted: guard={guard_rejected}, child={child_rejected}, daemon={daemon_rejected}"
    );
    Ok(())
}

/// An argument-aware sandbox can permit the harmless probe but deny real closes.
#[cfg(target_os = "linux")]
fn reject_real_ranges() -> io::Result<()> {
    let mut filter = [
        libc::sock_filter {
            code: 0x20,
            jt: 0,
            jf: 0,
            k: 0,
        },
        libc::sock_filter {
            code: 0x15,
            jt: 0,
            jf: 3,
            k: u32::try_from(libc::SYS_close_range).map_err(io::Error::other)?,
        },
        // Offset 16 is the low word of seccomp_data.args[0] on the CI targets.
        libc::sock_filter {
            code: 0x20,
            jt: 0,
            jf: 0,
            k: if cfg!(target_endian = "little") {
                16
            } else {
                20
            },
        },
        libc::sock_filter {
            code: 0x15,
            jt: 1,
            jf: 0,
            k: u32::MAX,
        },
        libc::sock_filter {
            code: 0x06,
            jt: 0,
            jf: 0,
            k: libc::SECCOMP_RET_ERRNO | libc::ENOSYS.cast_unsigned(),
        },
        libc::sock_filter {
            code: 0x06,
            jt: 0,
            jf: 0,
            k: libc::SECCOMP_RET_ALLOW,
        },
    ];
    let program = libc::sock_fprog {
        len: 6,
        filter: filter.as_mut_ptr(),
    };
    // SAFETY: the standalone child installs only a process-local filter. The
    // program is initialized and live; all syscalls except real ranges pass.
    if unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) } != 0
        // SAFETY: program points to the complete BPF array during this call.
        || unsafe { libc::prctl(libc::PR_SET_SECCOMP, libc::SECCOMP_MODE_FILTER, &raw const program) } != 0
    {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// Checked daemonization shares the same isolation plan, including its failure.
#[cfg(target_os = "linux")]
fn daemon_rejects_range_failure() -> Result<bool, Box<dyn Error>> {
    match fork::checked_daemon(fork::DaemonOptions::new(), TIMEOUT) {
        Err(fork::DaemonError::OperatingSystem {
            stage: fork::DaemonStage::Descriptors,
            error,
            cleanup_pending,
        }) if error.raw_os_error() == Some(libc::ENOSYS) && cleanup_pending.is_empty() => Ok(true),
        Ok(fork::CheckedDaemon::Daemon(notifier)) => {
            let _ = notifier.notify_ready();
            // SAFETY: a regressed plan returned in the detached fixture child.
            unsafe { libc::_exit(0) }
        }
        Ok(fork::CheckedDaemon::Parent(process)) => {
            // The fixture exits immediately, but its ID is no longer ours to
            // signal. Await init reaping it before reporting the regression.
            let deadline = std::time::Instant::now() + TIMEOUT;
            while fork::process_group_exists(process.process_group())? {
                if std::time::Instant::now() >= deadline {
                    return Err("daemon fixture did not exit".into());
                }
                std::thread::sleep(Duration::from_millis(2));
            }
            Ok(false)
        }
        Err(error) => Err(error.into()),
    }
}