syd 3.52.0

rock-solid application kernel
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
//
// Syd: rock-solid application kernel
// src/utils/syd-sh.rs: confined shell
//
// Copyright (c) 2024, 2025, 2026 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

#![allow(unused)]

use std::{
    env,
    io::{Read, Seek, SeekFrom, Stdin, Write},
    os::fd::{AsFd, AsRawFd, BorrowedFd},
    process::{exit, ExitCode},
};

use dur::Duration;
use linefeed::{Interface, ReadResult};
use nix::{
    errno::Errno,
    unistd::{isatty, Gid, Uid},
};
use syd::{
    compat::MFdFlags,
    config::*,
    cookie::safe_memfd_create,
    debug,
    fd::{seal_memfd_all, set_cloexec},
    human_size,
    io::ReadFd,
    lookup::safe_copy_if_exists,
    path::XPathBuf,
    syslog::LogLevel,
};

// Set global allocator to GrapheneOS allocator.
#[cfg(all(
    not(coverage),
    not(feature = "prof"),
    not(target_os = "android"),
    not(target_arch = "riscv64"),
    target_page_size_4k,
    target_pointer_width = "64"
))]
#[global_allocator]
static GLOBAL: hardened_malloc::HardenedMalloc = hardened_malloc::HardenedMalloc;

// Set global allocator to tcmalloc if profiling is enabled.
#[cfg(feature = "prof")]
#[global_allocator]
static GLOBAL: tcmalloc::TCMalloc = tcmalloc::TCMalloc;

#[cfg(not(target_os = "android"))]
#[expect(clippy::disallowed_types)]
enum Input {
    File(std::fs::File),
    Stdin(Stdin),
}

#[cfg(not(target_os = "android"))]
impl Read for Input {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        match self {
            Input::File(f) => f.read(buf),
            Input::Stdin(s) => s.read(buf),
        }
    }
}

#[cfg(not(target_os = "android"))]
impl AsFd for Input {
    fn as_fd(&self) -> BorrowedFd<'_> {
        match self {
            Input::File(f) => f.as_fd(),
            Input::Stdin(s) => s.as_fd(),
        }
    }
}

#[cfg(not(target_os = "android"))]
impl ReadFd for Input {}

#[cfg(target_os = "android")]
fn main() {
    eprintln!("syd-sh: bionic libc doesn't support wordexp(3)!");
    std::process::exit(libc::ENOSYS);
}

#[cfg(not(target_os = "android"))]
syd::main! {
    use lexopt::prelude::*;
    use syd::wordexp::*;

    // Set SIGPIPE handler to default.
    syd::set_sigpipe_dfl()?;

    // Initialize logging.
    syd::log::log_init_simple(LogLevel::Warn)?;

    // Parse options.
    let mut optc = false;
    let mut opte = false;
    let mut optl = false;
    let mut optx = false;

    // Skip options with `+` prefix for POSIX compat.
    // `-` at argv[0][0] triggers login mode.
    let mut args = Vec::new();
    let mut aend = false;
    for (idx, arg) in env::args().enumerate() {
        match arg.chars().next() {
            Some('-') if idx == 0 => {
                optl = true;
                continue;
            }
            _ if idx == 0 => continue,
            Some('+') if !aend => continue,
            Some('-') if arg == "--" => aend = true,
            _ => aend = true,
        }
        args.push(arg);
    }

    let mut parser = lexopt::Parser::from_args(&args);
    let mut args = Vec::new();
    while let Some(arg) = parser.next()? {
        match arg {
            Short('h') => {
                help();
                return Ok(ExitCode::SUCCESS);
            }
            Short('c') => optc = true,
            Short('e') => opte = true,
            Short('l') => optl = true,
            Short('x') => optx = true,
            // Ignore unknown options for POSIX compat.
            Short(_) | Long(_) => {}
            Value(prog) => {
                args.push(prog);
                args.extend(parser.raw_args()?);
            }
        }
    }

    // Create a memory fd to write input into,
    // and pass to the internal /bin/sh invoked
    // by wordexp(3).
    #[expect(clippy::disallowed_types)]
    let mut file = safe_memfd_create(
        c"syd-sh",
        MFdFlags::MFD_ALLOW_SEALING | MFdFlags::MFD_CLOEXEC).map(std::fs::File::from)?;
    debug!("ctx": "sh",
        "msg": format!("created memory-file {} with close-on-exec flag set",
            file.as_raw_fd()));

    // Configure options to pass to /bin/sh.
    if opte {
        file.write_all(b"set -e\n")?;
    }
    if optx {
        file.write_all(b"set -x\n")?;
    }

    // Define the `esyd` function.
    file.write_all(ESYD_SH.as_bytes())?;
    file.write_all(b"\n")?;

    // Handle system-wide configuration.
    if optl {
        safe_copy_if_exists(&mut file, "/etc/syd/init_login.sh")?;
        file.write_all(b"\n")?;
    }
    safe_copy_if_exists(&mut file, "/etc/syd/init.sh")?;
    file.write_all(b"\n")?;

    // Handle user-specific configuration.
    let uid = Uid::effective();
    let name = env::var_os("USER")
        .map(XPathBuf::from)
        .unwrap_or_else(|| "nobody".into());
    let home = env::var_os("HOME")
        .map(XPathBuf::from)
        .unwrap_or_else(|| "/var/empty".into());
    if optl {
        let init = home.join(b".config").join(b"syd").join(b"init_login.sh");
        safe_copy_if_exists(&mut file, &init)?;
        file.write_all(b"\n")?;
    }
    let init = home.join(b".config").join(b"syd").join(b"init.sh");
    safe_copy_if_exists(&mut file, &init)?;
    file.write_all(b"\n")?;

    // Handle -c command_name argument...
    let mut args = args.into_iter().peekable();
    if optc {
        if args.peek().is_none() {
            eprintln!("syd-sh: -c requires an argument!");
            return Ok(ExitCode::FAILURE);
        }

        let mut argc = 0;
        let mut input = String::new();
        for arg in args {
            argc += 1;

            let arg = arg.to_str().ok_or(Errno::EINVAL)?;
            file.write_all(quote(arg).as_bytes())?;
            file.write_all(b" ")?;

            if optx {
                input.push_str(arg);
                input.push(' ');
            }
        }
        file.write_all(b"\n")?;
        debug!("ctx": "sh",
            "msg": format!("written {argc} argument{} into memory-file {}",
                if argc > 1 { "s" } else { "" },
                file.as_raw_fd()));
        if optx {
            eprintln!("+ {input}");
        }

        seal_memfd_all(&file)?;
        debug!("ctx": "sh",
            "msg": format!("sealed memory-file {} against grows, shrinks and writes",
                file.as_raw_fd()));

        set_cloexec(&file, false)?;
        debug!("ctx": "sh",
            "msg": format!("set close-on-exec flag to off for memory-file {}",
                file.as_raw_fd()));

        let shell = format!("`. /proc/self/fd/{}`", file.as_raw_fd());
        debug!("ctx": "sh",
            "msg": format!("passing memory file {} to WordExp::expand with 3 seconds timeout...",
                file.as_raw_fd()));
        match WordExp::expand(&shell, true, Duration::from_secs(3)) {
            Ok(out) => {
                println!("{out}");
                return Ok(ExitCode::SUCCESS);
            }
            Err(err) => {
                let err = err.into();
                if opte {
                    eprintln!("syd-sh: 1: {}", wrde2str(err));
                }
                exit(err);
            }
        };
    }

    #[expect(clippy::disallowed_methods)]
    #[expect(clippy::disallowed_types)]
    let input: Option<(Input, String)> = if let Some(path) = args.next() {
        Some((
            Input::File(std::fs::File::open(&path)?),
            XPathBuf::from(path).to_string(),
        ))
    } else if isatty(std::io::stdin()).unwrap_or(false) {
        None
    } else {
        Some((Input::Stdin(std::io::stdin()), "standard input".to_string()))
    };

    if let Some((mut input_file, input_name)) = input {
        debug!("ctx": "sh",
            "msg": format!("copying from {input_name} to memory-file {}...",
                file.as_raw_fd()));
        let copylen = syd::io::copy(&mut input_file, &mut file)?;
        debug!("ctx": "sh",
            "msg": format!("copied {} from {input_name} to memory-file {}",
                human_size(copylen.try_into()?),
                file.as_raw_fd()));

        seal_memfd_all(&file)?;
        debug!("ctx": "sh",
            "msg": format!("sealed memory-file {} against grows, shrinks and writes",
                file.as_raw_fd()));

        set_cloexec(&file, false)?;
        debug!("ctx": "sh",
            "msg": format!("set close-on-exec flag to off for memory-file {}",
                file.as_raw_fd()));

        let shell = format!("`. /proc/self/fd/{}`", file.as_raw_fd());
        debug!("ctx": "sh",
            "msg": format!("passing memory file {} to WordExp::expand with 3 seconds timeout...",
                file.as_raw_fd()));
        match WordExp::expand(&shell, true, Duration::from_secs(3)) {
            Ok(val) => {
                println!("{val}");
                return Ok(ExitCode::SUCCESS);
            }
            Err(err) => {
                let err = err.into();
                if opte {
                    eprintln!("syd-sh: {err}");
                }
                exit(err);
            }
        }
    }

    // SAFETY: Quoting sh(1p):
    //   -i        Specify that the shell is interactive; see below. An
    //             implementation may treat specifying the -i option as an
    //             error if the real user ID of the calling process does
    //             not equal the effective user ID or if the
    // TODO: Make this check before we open the memory-fd to be polite.
    assert_eq!(
        Uid::current(),
        Uid::effective(),
        "real user ID must match effective user ID in interactive mode!",
    );
    assert_eq!(
        Gid::current(),
        Gid::effective(),
        "real group ID must match effective group ID in interactive mode!",
    );

    // Write successful commands who generate no output
    // to the memory fd. The user can also explicitly
    // save into history with the '>' prefix.
    // This way we maintain a simple form of shell state.
    let reader = Interface::new("syd-sh")?;
    reader.set_prompt("; ")?;
    while let ReadResult::Input(input) = reader.read_line()? {
        if matches!(input.chars().next(), Some('>')) {
            // explicit push into history.
            let histlen = file.seek(SeekFrom::End(0))?;
            file.write_all(&input.as_bytes()[1..])?;
            file.write_all(b"\n")?;
            let len = input.len();
            reader.set_prompt("OKHIST; ")?;
            debug!("ctx": "sh",
                "msg": format!("pushed {} into memory-file of {}",
                    human_size(len),
                    human_size(histlen.try_into()?)));
            continue;
        } else if matches!(input.trim().chars().next(), None | Some('#')) {
            reader.set_prompt("; ")?;
            continue;
        } else if optx {
            eprintln!("+ {input}");
        }

        // SAFETY: create a private, write-sealed copy of the memory-file.
        #[expect(clippy::disallowed_types)]
        let mut fdup = safe_memfd_create(
            c"syd-sh",
            MFdFlags::MFD_ALLOW_SEALING | MFdFlags::MFD_CLOEXEC).map(std::fs::File::from)?;
        debug!("ctx": "sh",
            "msg": format!("created memory-file {} with sealing allowed",
                fdup.as_raw_fd()));

        // rewrite history!
        file.seek(SeekFrom::Start(0))?;
        let copylen = syd::io::copy(&mut file, &mut fdup)?;
        debug!("ctx": "sh",
            "msg": format!("copied {} from memory-file {} to {}",
                human_size(copylen.try_into()?),
                file.as_raw_fd(),
                fdup.as_raw_fd()));

        fdup.write_all(input.as_bytes())?;
        debug!("ctx": "sh",
            "msg": format!("written {} of input to memory-file {}",
                human_size(input.len()),
                fdup.as_raw_fd()));

        seal_memfd_all(&fdup)?;
        debug!("ctx": "sh",
            "msg": format!("sealed memory-file {} against grows, shrinks and writes",
                fdup.as_raw_fd()));

        set_cloexec(&fdup, false)?;
        debug!("ctx": "sh",
            "msg": format!("set close-on-exec flag to off for memory-file {}",
                fdup.as_raw_fd()));

        let shell = format!("`. /proc/self/fd/{} 2>&1`", fdup.as_raw_fd());
        debug!("ctx": "sh",
            "msg": format!("passing memory-file {} to WordExp::expand with 3 seconds timeout...",
                fdup.as_raw_fd()));
        let result = WordExp::expand(&shell, true, Duration::from_secs(3));

        let fdup_fd = fdup.as_raw_fd();
        drop(fdup);
        match result {
            Ok(ref val) => {
                debug!("ctx": "sh",
                    "msg": format!("closed memory-file {fdup_fd} after WordExp::expand returned {} of output",
                        human_size(val.len())));
            }
            Err(ref err) => {
                debug!("ctx": "sh",
                    "msg": format!("closed memory-file {fdup_fd} after WordExp::expand error {err}"));
            }
        }

        match result {
            Ok(val) => {
                reader.set_prompt("; ")?;
                println!("{val}");
            }
            Err(WordExpError::BadValue) if !input.contains(';') => {
                reader.set_prompt("; ")?;
                if let Some(cmd) = input.split_whitespace().next() {
                    for builtin in SHELL_BUILTINS {
                        if cmd == *builtin {
                            let histlen = file.seek(SeekFrom::End(0))?;
                            file.write_all(input.as_bytes())?;
                            file.write_all(b"\n")?;
                            debug!("ctx": "sh",
                                "msg": format!("pushed {} into memory-file of {}",
                                    human_size(input.len() + 1),
                                    human_size(histlen.try_into()?)));
                            break;
                        }
                    }
                }
            }
            Err(err) => {
                let prompt = format!("{}; ", wrde2str(err.into()));
                reader.set_prompt(&prompt)?;
            }
        }
    }

    Ok(ExitCode::SUCCESS)
}

#[cfg(not(target_os = "android"))]
fn help() {
    println!("Usage:");
    println!("  syd-sh [-helsx] [--] [_command_file_ [argument...]]");
    println!("  syd-sh [-helx] -c _command_string_ [_command_name_ [argument...]]");
    println!("Simple confined shell based on wordexp(3)");
    println!("Given no arguments, enter read-eval-print loop.");
    println!("Given -c with an argument, evaluate and print the result.");
}

#[cfg(not(target_os = "android"))]
fn wrde2str(err: i32) -> String {
    use syd::wordexp::*;

    match err {
        0 => "".to_string(),
        128 => "ERR?".to_string(),
        WRDE_NOSPACE => "NOSPACE".to_string(),
        WRDE_BADCHAR => "BADCHAR".to_string(),
        WRDE_BADVAL => "BADVAL".to_string(),
        WRDE_CMDSUB => "CMDSUB".to_string(),
        WRDE_SYNTAX => "SYNTAX".to_string(),
        WRDE_SECCOMP => "SECCOMP".to_string(),
        WRDE_TIMEOUT => "TIMEOUT".to_string(),
        _ => format!("ERR{}", 128 - err),
    }
}

#[cfg(not(target_os = "android"))]
fn quote(input: &str) -> String {
    format!("'{}'", input.replace("'", "'\\''"))
}

#[cfg(not(target_os = "android"))]
const SHELL_BUILTINS: &[&str] = &[
    ".", "alias", "cd", "export", "hash", "readonly", "set", "shift", "source", "umask", "unalias",
    "unset",
];