Skip to content

Commit da70509

Browse files
committed
install: manually label {/etc/fstab,tmpfile.d/bootc-root-ssh.conf}
Right now bootc supports an experimental install from a non-selinux host when using the `BOOTC_SKIP_SELINUX_HOST_CHECK=1` option. This is nice and works relatively well. However files written during the install like /etc/fstab or the tmpfiles.dfile in /etc/tmpfile.d/bootc-root-ssh.conf must be labeled too. This commit adds a (rather crude) manual way to do this. Closes #362 Signed-off-by: Michael Vogt <[email protected]>
1 parent ac17000 commit da70509

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

lib/src/install.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ async fn initialize_ostree_root_from_self(
633633
let root = rootfs_dir
634634
.open_dir(path.as_str())
635635
.context("Opening deployment dir")?;
636+
let root_path = &rootfs.join(&path.as_str());
636637
let mut f = {
637638
let mut opts = cap_std::fs::OpenOptions::new();
638639
root.open_with("etc/fstab", opts.append(true).write(true).create(true))
@@ -644,8 +645,16 @@ async fn initialize_ostree_root_from_self(
644645
}
645646
f.flush()?;
646647

648+
let fstab_path = root_path.join("etc/fstab");
649+
state.lsm_label(&fstab_path, "/etc/fstab".into(), false)?;
650+
647651
if let Some(contents) = state.root_ssh_authorized_keys.as_deref() {
648-
osconfig::inject_root_ssh_authorized_keys(&root, contents)?;
652+
osconfig::inject_root_ssh_authorized_keys(
653+
&root,
654+
&root_path,
655+
|target, path, recurse| state.lsm_label(target, path, recurse),
656+
contents,
657+
)?;
649658
}
650659

651660
let uname = rustix::system::uname();

lib/src/install/osconfig.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ const ETC_TMPFILES: &str = "etc/tmpfiles.d";
88
const ROOT_SSH_TMPFILE: &str = "bootc-root-ssh.conf";
99

1010
#[context("Injecting root authorized_keys")]
11-
pub(crate) fn inject_root_ssh_authorized_keys(root: &Dir, contents: &str) -> Result<()> {
11+
pub(crate) fn inject_root_ssh_authorized_keys<F>(
12+
root: &Dir,
13+
root_path: &Utf8Path,
14+
lsm_label_fn: F,
15+
contents: &str,
16+
) -> Result<()>
17+
where
18+
F: Fn(&Utf8Path, &Utf8Path, bool) -> Result<()>,
19+
{
1220
// While not documented right now, this one looks like it does not newline wrap
1321
let b64_encoded = ostree_ext::glib::base64_encode(contents.as_bytes());
1422
// See the example in https://systemd.io/CREDENTIALS/
@@ -18,20 +26,53 @@ pub(crate) fn inject_root_ssh_authorized_keys(root: &Dir, contents: &str) -> Res
1826
root.create_dir_all(tmpfiles_dir)?;
1927
let target = tmpfiles_dir.join(ROOT_SSH_TMPFILE);
2028
root.atomic_write(&target, &tmpfiles_content)?;
29+
30+
let as_path = Utf8Path::new(ETC_TMPFILES).join(ROOT_SSH_TMPFILE);
31+
lsm_label_fn(
32+
&root_path.join(&as_path),
33+
&Utf8Path::new("/").join(&as_path),
34+
false,
35+
)?;
36+
2137
println!("Injected: {target}");
2238
Ok(())
2339
}
2440

2541
#[test]
2642
fn test_inject_root_ssh() -> Result<()> {
43+
use camino::Utf8PathBuf;
44+
use std::cell::Cell;
45+
46+
let fake_lsm_label_called = Cell::new(0);
47+
let fake_lsm_label = |target: &Utf8Path, as_path: &Utf8Path, recurse: bool| -> Result<()> {
48+
assert_eq!(
49+
target,
50+
format!("/root/path/etc/tmpfiles.d/{ROOT_SSH_TMPFILE}")
51+
);
52+
assert_eq!(as_path, format!("/etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"));
53+
assert_eq!(recurse, false);
54+
55+
fake_lsm_label_called.set(fake_lsm_label_called.get() + 1);
56+
Ok(())
57+
};
58+
59+
let root_path = &Utf8PathBuf::from("/root/path");
2760
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
2861

29-
inject_root_ssh_authorized_keys(root, "ssh-ed25519 ABCDE example@demo\n").unwrap();
62+
inject_root_ssh_authorized_keys(
63+
root,
64+
root_path,
65+
fake_lsm_label,
66+
"ssh-ed25519 ABCDE example@demo\n",
67+
)
68+
.unwrap();
3069

3170
let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?;
3271
assert_eq!(
3372
content,
3473
"f~ /root/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n"
3574
);
75+
assert_eq!(fake_lsm_label_called, 1.into());
76+
3677
Ok(())
3778
}

0 commit comments

Comments
 (0)