Skip to content

Commit

Permalink
Add selinux enable flag to config
Browse files Browse the repository at this point in the history
Value defaults to runtime detection.
  • Loading branch information
Felix Obenhuber committed Sep 21, 2023
1 parent d86c95d commit 1c31e73
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
8 changes: 8 additions & 0 deletions northstar-runtime/src/runtime/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Config {
/// Notification buffer size
#[serde(default = "default_notification_buffer_size")]
pub notification_buffer_size: usize,
/// Enable SELinux. The option defaults to the presence of `/sys/fs/selinux/enforce`.
#[serde(default = "default_selinux")]
pub selinux: bool,
/// Console configuration.
#[serde(default)]
pub console: Console,
Expand Down Expand Up @@ -229,6 +232,11 @@ const fn default_notification_buffer_size() -> usize {
128
}

/// Default selinux enabled flag.
fn default_selinux() -> bool {
Path::new("/sys/fs/selinux/enforce").exists()
}

/// Default token validity time.
const fn default_token_validity() -> time::Duration {
time::Duration::from_secs(60)
Expand Down
2 changes: 1 addition & 1 deletion northstar-runtime/src/runtime/fork/forker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Forker {
debug_assert_eq!(manifest.console.is_some(), console.is_some());

// Request
let init = init::build(config, manifest, containers).await?;
let init = init::build(config, manifest, containers, config.selinux).await?;
let request = Message::CreateRequest {
init,
io,
Expand Down
5 changes: 2 additions & 3 deletions northstar-runtime/src/runtime/fork/init/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub async fn build<'a, I: Iterator<Item = &'a Container> + Clone>(
config: &Config,
manifest: &Manifest,
containers: I,
is_selinux_enabled: bool,
) -> Result<Init, Error> {
let container = manifest.container();
let root = config.run_dir.join(container.to_string());
Expand All @@ -46,9 +47,7 @@ pub async fn build<'a, I: Iterator<Item = &'a Container> + Clone>(
.map(Into::into)
.sorted()
.collect();
// TODO: Get the selinux state somewhen at boot.
let selinux_context = Path::new("/sys/fs/selinux/enforce")
.exists()
let selinux_context = is_selinux_enabled
.then(|| manifest.selinux.as_ref().map(|s| s.context.clone()))
.flatten();

Expand Down
19 changes: 8 additions & 11 deletions northstar-runtime/src/runtime/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl MountControl {
npk: &Npk,
target: &Path,
key: Option<&PublicKey>,
is_selinux_enabled: bool,
) -> impl Future<Output = Result<()>> {
let dm = self.dm.clone();
let lc = self.lc.clone();
Expand All @@ -124,10 +125,11 @@ impl MountControl {
let fsimg_offset = npk.fsimg_offset();
let container = npk.manifest().container();
let verity_header = npk.verity_header().cloned();
let selinux_context = npk.manifest().selinux.as_ref().map(|s| s.context.clone());
let selinux_context = is_selinux_enabled
.then(|| npk.manifest().selinux.as_ref().map(|s| s.context.clone()))
.flatten();
let hashes = npk.hashes().cloned();
let lo_timeout = self.lo_timeout;
let nosuid = npk.manifest().selinux.is_none();

task::spawn_blocking(move || {
let mount_info = Mount {
Expand All @@ -143,7 +145,7 @@ impl MountControl {
lo_timeout,
};
debug!("Mounting {container}");
mount(dm, lc, mount_info, nosuid).map(drop)
mount(dm, lc, mount_info).map(drop)
})
.map(|r| match r {
Ok(r) => r,
Expand Down Expand Up @@ -176,7 +178,6 @@ fn mount(
dm: Arc<devicemapper::DeviceMapper>,
lc: Arc<Mutex<LoopControl>>,
mount_info: Mount,
nosuid: bool,
) -> Result<()> {
let Mount {
container,
Expand Down Expand Up @@ -265,19 +266,15 @@ fn mount(

// nosuid prevents a successfull transition to the container context.
// https://danwalsh.livejournal.com/68723.html
let flags = if nosuid {
let flags = if selinux_context.is_none() {
MountFlags::MS_RDONLY | MountFlags::MS_NOSUID
} else {
MountFlags::MS_RDONLY
};
let source = Some(&device);
const FSTYPE: Option<&str> = Some(FS_TYPE);
// TODO: Get the selinux state somewhen at boot.
let data = if Path::new("/sys/fs/selinux/enforce").exists() {
selinux_context.map(|selinux_context| format!("{}{}", "context=", selinux_context.as_str()))
} else {
None
};
let data = selinux_context
.map(|selinux_context| format!("{}{}", "context=", selinux_context.as_str()));
let mount_result = nix::mount::mount(source, target, FSTYPE, flags, data.as_deref());

if let Err(ref e) = mount_result {
Expand Down
2 changes: 1 addition & 1 deletion northstar-runtime/src/runtime/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl State {
let root = self.config.run_dir.join(container.to_string());
let mount_control = self.mount_control.clone();
mount_control
.mount(npk, &root, key.as_ref())
.mount(npk, &root, key.as_ref(), self.config.selinux)
.map_ok(|_| root)
}

Expand Down
1 change: 1 addition & 0 deletions northstar-tests/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl Runtime {
loop_device_timeout: time::Duration::from_secs(10),
cgroup: NonNulString::try_from(format!("northstar-{}", nanoid!())).unwrap(),
repositories,
selinux: false,
console: Console {
global: Some(ConsoleGlobal {
bind: console_url(),
Expand Down

0 comments on commit 1c31e73

Please sign in to comment.