#![cfg(feature = "landlock")]
use std::fs::{File, read_dir};
use extrasafe::builtins::SystemIO;
#[test]
fn landlock_with_seccomp_arg_filters_fails() {
let path = tempfile::tempdir().unwrap();
let res = extrasafe::SafetyContext::new()
.enable(
SystemIO::nothing()
.allow_open_readonly()
.allow_list_dir(&path)
);
assert!(res.is_err(), "Enabling filter succeeded with landlock and seccomp arg-restricted open");
let res = extrasafe::SafetyContext::new()
.enable(
SystemIO::nothing()
.allow_open_readonly()
).unwrap()
.enable(
SystemIO::nothing()
.allow_read_path(&path)
);
assert!(res.is_err(), "Enabling filter succeeded with landlock and seccomp arg-restricted open");
let res = extrasafe::SafetyContext::new()
.enable(
SystemIO::nothing()
.allow_read_path(&path)
).unwrap()
.enable(
SystemIO::nothing()
.allow_open_readonly()
);
assert!(res.is_err(), "Enabling filter succeeded with landlock and seccomp arg-restricted open");
}
#[test]
fn landlock_only() {
let res = extrasafe::SafetyContext::new()
.landlock_only()
.apply_to_current_thread();
assert!(res.is_err(), "extrasafe did not error when applying with no seccomp or landlock rules");
let err = res.unwrap_err();
assert!(err.to_string().contains("No rules were enabled"));
let dir = tempfile::tempdir().unwrap();
extrasafe::SafetyContext::new()
.enable(
SystemIO::nothing()
.allow_create_in_dir(&dir)
).unwrap()
.landlock_only()
.apply_to_current_thread().unwrap();
let pid = unsafe { libc::getpid() };
assert!(pid > 0, "Pid was negative: {}", pid);
let file_path = dir.path().join("okay.txt");
let file_res = File::create(file_path);
assert!(file_res.is_ok(), "Failed to create file in allowed dir: {:?}", file_res.unwrap_err());
let list_res = read_dir(&dir);
assert!(list_res.is_err(), "Incorrectly succeeded in listing directory");
let list_res = read_dir("/etc");
assert!(list_res.is_err(), "Incorrectly succeeded in listing directory");
}