-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from cgwalters/more-labeling
Rework SELinux labeling more
- Loading branch information
Showing
7 changed files
with
372 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,9 +132,10 @@ jobs: | |
- name: Integration tests | ||
run: | | ||
set -xeuo pipefail | ||
image=quay.io/centos-bootc/centos-bootc-dev:stream9 | ||
echo 'ssh-ed25519 ABC0123 [email protected]' > test_authorized_keys | ||
sudo podman run --rm -ti --privileged -v ./test_authorized_keys:/test_authorized_keys --env RUST_LOG=debug -v /:/target -v /var/lib/containers:/var/lib/containers -v ./usr/bin/bootc:/usr/bin/bootc --pid=host --security-opt label=disable \ | ||
quay.io/centos-bootc/centos-bootc-dev:stream9 bootc install to-filesystem \ | ||
${image} bootc install to-filesystem \ | ||
--karg=foo=bar --disable-selinux --replace=alongside --root-ssh-authorized-keys=/test_authorized_keys /target | ||
ls -al /boot/loader/ | ||
sudo grep foo=bar /boot/loader/entries/*.conf | ||
|
@@ -143,5 +144,5 @@ jobs: | |
sudo chattr -i /ostree/deploy/default/deploy/* | ||
sudo rm /ostree/deploy/default -rf | ||
sudo podman run --rm -ti --privileged --env BOOTC_SKIP_SELINUX_HOST_CHECK=1 --env RUST_LOG=debug -v /:/target -v /var/lib/containers:/var/lib/containers -v ./usr/bin/bootc:/usr/bin/bootc --pid=host --security-opt label=disable \ | ||
quay.io/centos-bootc/centos-bootc-dev:stream9 bootc install to-existing-root | ||
sudo ls -ldZ / /ostree/deploy/default/deploy/* /ostree/deploy/default/deploy/*/etc | ||
${image} bootc install to-existing-root | ||
sudo podman run --rm -ti --privileged -v /:/target -v ./usr/bin/bootc:/usr/bin/bootc --pid=host --security-opt label=disable ${image} bootc internal-tests verify-selinux /target/ostree --warn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,51 @@ | ||
use std::io::Write; | ||
|
||
use anyhow::Result; | ||
use camino::Utf8Path; | ||
use cap_std::fs::Dir; | ||
use cap_std_ext::{cap_std, dirext::CapStdExtDirExt}; | ||
use cap_std_ext::cap_std; | ||
use fn_error_context::context; | ||
use ostree_ext::ostree; | ||
|
||
const ETC_TMPFILES: &str = "etc/tmpfiles.d"; | ||
const ROOT_SSH_TMPFILE: &str = "bootc-root-ssh.conf"; | ||
|
||
#[context("Injecting root authorized_keys")] | ||
pub(crate) fn inject_root_ssh_authorized_keys<F>( | ||
pub(crate) fn inject_root_ssh_authorized_keys( | ||
root: &Dir, | ||
root_path: &Utf8Path, | ||
lsm_label_fn: F, | ||
sepolicy: Option<&ostree::SePolicy>, | ||
contents: &str, | ||
) -> Result<()> | ||
where | ||
F: Fn(&Utf8Path, &Utf8Path, bool) -> Result<()>, | ||
{ | ||
) -> Result<()> { | ||
// While not documented right now, this one looks like it does not newline wrap | ||
let b64_encoded = ostree_ext::glib::base64_encode(contents.as_bytes()); | ||
// See the example in https://systemd.io/CREDENTIALS/ | ||
let tmpfiles_content = format!("f~ /root/.ssh/authorized_keys 600 root root - {b64_encoded}\n"); | ||
|
||
let tmpfiles_dir = Utf8Path::new(ETC_TMPFILES); | ||
root.create_dir_all(tmpfiles_dir)?; | ||
let target = tmpfiles_dir.join(ROOT_SSH_TMPFILE); | ||
root.atomic_write(&target, &tmpfiles_content)?; | ||
|
||
let as_path = Utf8Path::new(ETC_TMPFILES).join(ROOT_SSH_TMPFILE); | ||
lsm_label_fn( | ||
&root_path.join(&as_path), | ||
&Utf8Path::new("/").join(&as_path), | ||
false, | ||
crate::lsm::ensure_dir_labeled(root, ETC_TMPFILES, None, 0o755.into(), sepolicy)?; | ||
let tmpfiles_dir = root.open_dir(ETC_TMPFILES)?; | ||
crate::lsm::atomic_replace_labeled( | ||
&tmpfiles_dir, | ||
ROOT_SSH_TMPFILE, | ||
0o644.into(), | ||
sepolicy, | ||
|w| w.write_all(tmpfiles_content.as_bytes()).map_err(Into::into), | ||
)?; | ||
|
||
println!("Injected: {target}"); | ||
println!("Injected: {ETC_TMPFILES}/{ROOT_SSH_TMPFILE}"); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_inject_root_ssh() -> Result<()> { | ||
use camino::Utf8PathBuf; | ||
use std::cell::Cell; | ||
|
||
let fake_lsm_label_called = Cell::new(0); | ||
let fake_lsm_label = |target: &Utf8Path, as_path: &Utf8Path, recurse: bool| -> Result<()> { | ||
assert_eq!( | ||
target, | ||
format!("/root/path/etc/tmpfiles.d/{ROOT_SSH_TMPFILE}") | ||
); | ||
assert_eq!(as_path, format!("/etc/tmpfiles.d/{ROOT_SSH_TMPFILE}")); | ||
assert_eq!(recurse, false); | ||
|
||
fake_lsm_label_called.set(fake_lsm_label_called.get() + 1); | ||
Ok(()) | ||
}; | ||
|
||
let root_path = &Utf8PathBuf::from("/root/path"); | ||
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
|
||
inject_root_ssh_authorized_keys( | ||
root, | ||
root_path, | ||
fake_lsm_label, | ||
"ssh-ed25519 ABCDE example@demo\n", | ||
) | ||
.unwrap(); | ||
// The code expects this to exist, reasonably so | ||
root.create_dir("etc")?; | ||
inject_root_ssh_authorized_keys(root, None, "ssh-ed25519 ABCDE example@demo\n").unwrap(); | ||
|
||
let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?; | ||
assert_eq!( | ||
content, | ||
"f~ /root/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n" | ||
); | ||
assert_eq!(fake_lsm_label_called, 1.into()); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.