diff --git a/utils/noaslr/noaslr/Cargo.toml b/utils/noaslr/noaslr/Cargo.toml index 4b224c4fee..6708c8c26a 100644 --- a/utils/noaslr/noaslr/Cargo.toml +++ b/utils/noaslr/noaslr/Cargo.toml @@ -14,5 +14,5 @@ nix = { version = "0.26.2", default-features = false, features = ["process", "pe readonly = { version = "0.2.8", default-features = false } simplelog = { version = "0.12.1", default-features = false } -[target.'cfg(any(target_os = "freebsd", target_os = "dragonfly"))'.dependencies] +[target.'cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))'.dependencies] libc = "0.2" diff --git a/utils/noaslr/noaslr/src/main.rs b/utils/noaslr/noaslr/src/main.rs index f2e34c34a5..a00be2982f 100644 --- a/utils/noaslr/noaslr/src/main.rs +++ b/utils/noaslr/noaslr/src/main.rs @@ -67,6 +67,34 @@ fn disable_aslr() -> Result<()> { Ok(()) } +#[cfg(target_os = "netbsd")] +fn disable_aslr() -> Result<()> { + unsafe { + let mut aslr: i32 = 0; + let mut s = std::mem::size_of::(); + let nm = CString::new("security.pax.aslr.enabled") + .map_err(|e| anyhow!("Failed to create sysctl oid: {e:}")) + .unwrap(); + if libc::sysctlbyname( + nm.as_ptr(), + &mut aslr as *mut i32 as _, + &mut s, + std::ptr::null(), + 0, + ) < 0 + { + return Err(anyhow!("Failed to get aslr status")); + } + + if aslr > 0 { + return Err(anyhow!( + "Please disable aslr with sysctl -w security.pax.aslr.enabled=0 as privileged user" + )); + } + } + Ok(()) +} + fn main() -> Result<()> { let args = Args::parse();