Add further hardening for system services (#2542)

This commit is contained in:
Firstyear 2024-02-17 10:11:32 +10:00 committed by GitHub
parent 7394ac86cb
commit 62dff7565e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 48 additions and 0 deletions

23
Cargo.lock generated
View file

@ -1092,6 +1092,7 @@ dependencies = [
"kanidmd_core", "kanidmd_core",
"opentelemetry", "opentelemetry",
"opentelemetry_api", "opentelemetry_api",
"prctl",
"reqwest", "reqwest",
"sd-notify", "sd-notify",
"serde", "serde",
@ -3113,6 +3114,7 @@ dependencies = [
"libsqlite3-sys", "libsqlite3-sys",
"lru 0.8.1", "lru 0.8.1",
"notify-debouncer-full", "notify-debouncer-full",
"prctl",
"rpassword 7.3.1", "rpassword 7.3.1",
"rusqlite", "rusqlite",
"selinux", "selinux",
@ -3718,6 +3720,17 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "nix"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags 2.4.2",
"cfg-if",
"libc",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.3" version = "7.1.3"
@ -4487,6 +4500,16 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "prctl"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "059a34f111a9dee2ce1ac2826a68b24601c4298cfeb1a587c3cb493d5ab46f52"
dependencies = [
"libc",
"nix",
]
[[package]] [[package]]
name = "predicates" name = "predicates"
version = "3.1.0" version = "3.1.0"

View file

@ -184,6 +184,7 @@ tracing-opentelemetry = "0.21.0"
paste = "^1.0.14" paste = "^1.0.14"
pkg-config = "^0.3.27" pkg-config = "^0.3.27"
prctl = "1.0.0"
proc-macro2 = "1.0.69" proc-macro2 = "1.0.69"
qrcode = "^0.12.0" qrcode = "^0.12.0"
quote = "1" quote = "1"

View file

@ -21,6 +21,11 @@ ExecStart=/usr/sbin/kanidmd server -c /etc/kanidm/server.toml
AmbientCapabilities=CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE CapabilityBoundingSet=CAP_NET_BIND_SERVICE
# If OOM occurs, request a clean stop
OOMPolicy=stop
# Adjust our weight toward *not* being killed under pressure.
OOMScoreAdjust=-100
NoNewPrivileges=true NoNewPrivileges=true
PrivateTmp=true PrivateTmp=true
PrivateDevices=true PrivateDevices=true

View file

@ -43,6 +43,7 @@ serde_json.workspace = true
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
sd-notify.workspace = true sd-notify.workspace = true
prctl.workspace = true
[target.'cfg(target_family = "windows")'.dependencies] [target.'cfg(target_family = "windows")'.dependencies]
whoami = { workspace = true } whoami = { workspace = true }

View file

@ -226,6 +226,11 @@ async fn submit_admin_req(path: &str, req: AdminTaskRequest, output_mode: Consol
} }
fn main() -> ExitCode { fn main() -> ExitCode {
// On linux when debug assertions are disabled, prevent ptrace
// from attaching to us.
#[cfg(all(target_os = "linux", not(debug_assertions)))]
prctl::set_dumpable(false);
let maybe_rt = tokio::runtime::Builder::new_multi_thread() let maybe_rt = tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
.thread_name("kanidmd-thread-pool") .thread_name("kanidmd-thread-pool")

View file

@ -93,6 +93,9 @@ walkdir = { workspace = true }
[target.'cfg(not(target_family = "windows"))'.dependencies] [target.'cfg(not(target_family = "windows"))'.dependencies]
kanidm_utils_users = { workspace = true } kanidm_utils_users = { workspace = true }
[target.'cfg(target_os = "linux")'.dependencies]
prctl.workspace = true
[dev-dependencies] [dev-dependencies]
kanidmd_core = { workspace = true } kanidmd_core = { workspace = true }
kanidmd_testkit = { workspace = true } kanidmd_testkit = { workspace = true }

View file

@ -469,6 +469,11 @@ async fn write_hsm_pin(hsm_pin_path: &str) -> Result<(), Box<dyn Error>> {
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> ExitCode { async fn main() -> ExitCode {
// On linux when debug assertions are disabled, prevent ptrace
// from attaching to us.
#[cfg(all(target_os = "linux", not(debug_assertions)))]
prctl::set_dumpable(false);
let cuid = get_current_uid(); let cuid = get_current_uid();
let ceuid = get_effective_uid(); let ceuid = get_effective_uid();
let cgid = get_current_gid(); let cgid = get_current_gid();

View file

@ -261,6 +261,11 @@ async fn handle_tasks(stream: UnixStream, cfg: &KanidmUnixdConfig) {
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> ExitCode { async fn main() -> ExitCode {
// On linux when debug assertions are disabled, prevent ptrace
// from attaching to us.
#[cfg(all(target_os = "linux", not(debug_assertions)))]
prctl::set_dumpable(false);
// let cuid = get_current_uid(); // let cuid = get_current_uid();
// let cgid = get_current_gid(); // let cgid = get_current_gid();
// We only need to check effective id // We only need to check effective id