util/noaslr porting to FreeBSD (the non-lib part). (#1337)

to port to the library, might need a nice layer over the FreeBSD's C api (nix?)
before hand.
This commit is contained in:
David CARLIER 2023-07-03 00:51:32 +01:00 committed by GitHub
parent c0e1236a07
commit 4897c3f205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 17 deletions

View File

@ -1,4 +1,6 @@
use {std::error::Error, vergen::EmitBuilder}; use std::error::Error;
use vergen::EmitBuilder;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder() EmitBuilder::builder()

View File

@ -1,16 +1,16 @@
mod args; mod args;
use { use std::{
crate::args::Args, env,
anyhow::{anyhow, Result}, fs::File,
clap::Parser, io::{BufRead, BufReader},
std::{
env,
fs::File,
io::{BufRead, BufReader},
},
}; };
use anyhow::{anyhow, Result};
use clap::Parser;
use crate::args::Args;
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
for (i, a) in env::args().enumerate() { for (i, a) in env::args().enumerate() {

View File

@ -1,3 +1,4 @@
#[cfg(any(target_os = "linux", target_os = "android"))]
use { use {
anyhow::{anyhow, Result}, anyhow::{anyhow, Result},
ctor::ctor, ctor::ctor,
@ -8,6 +9,7 @@ use {
std::{ffi::CString, fs::File, io::Read}, std::{ffi::CString, fs::File, io::Read},
}; };
#[cfg(any(target_os = "linux", target_os = "android"))]
fn read_null_lines(path: &str) -> Result<Vec<CString>> { fn read_null_lines(path: &str) -> Result<Vec<CString>> {
let mut file = File::open(path).map_err(|e| anyhow!("Failed to open maps: {e:}"))?; let mut file = File::open(path).map_err(|e| anyhow!("Failed to open maps: {e:}"))?;
let mut data = String::new(); let mut data = String::new();
@ -20,6 +22,7 @@ fn read_null_lines(path: &str) -> Result<Vec<CString>> {
.collect::<Result<Vec<CString>>>() .collect::<Result<Vec<CString>>>()
} }
#[cfg(any(target_os = "linux", target_os = "android"))]
fn libnoaslr() -> Result<()> { fn libnoaslr() -> Result<()> {
let mut persona = personality::get().map_err(|e| anyhow!("Failed to get personality: {e:}"))?; let mut persona = personality::get().map_err(|e| anyhow!("Failed to get personality: {e:}"))?;
if (persona & Persona::ADDR_NO_RANDOMIZE) == Persona::ADDR_NO_RANDOMIZE { if (persona & Persona::ADDR_NO_RANDOMIZE) == Persona::ADDR_NO_RANDOMIZE {
@ -36,6 +39,7 @@ fn libnoaslr() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(any(target_os = "linux", target_os = "android"))]
#[ctor] #[ctor]
fn init() { fn init() {
libnoaslr().unwrap(); libnoaslr().unwrap();

View File

@ -13,3 +13,4 @@ log = { version = "0.4.19", default-features = false }
nix = { version = "0.26.2", default-features = false, features = ["process", "personality"] } nix = { version = "0.26.2", default-features = false, features = ["process", "personality"] }
readonly = { version = "0.2.8", default-features = false } readonly = { version = "0.2.8", default-features = false }
simplelog = { version = "0.12.1", default-features = false } simplelog = { version = "0.12.1", default-features = false }
libc = "0.2"

View File

@ -1,4 +1,6 @@
use {std::error::Error, vergen::EmitBuilder}; use std::error::Error;
use vergen::EmitBuilder;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder() EmitBuilder::builder()

View File

@ -1,7 +1,6 @@
use { use std::iter;
clap::{builder::Str, Parser},
std::iter, use clap::{builder::Str, Parser};
};
#[derive(Default)] #[derive(Default)]
pub struct Version; pub struct Version;

View File

@ -1,5 +1,14 @@
mod args; mod args;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
use {
crate::args::Args,
anyhow::{anyhow, Result},
clap::Parser,
nix::unistd::execvp,
std::ffi::CString,
};
#[cfg(any(target_os = "linux", target_os = "android"))]
use { use {
crate::args::Args, crate::args::Args,
anyhow::{anyhow, Result}, anyhow::{anyhow, Result},
@ -11,11 +20,35 @@ use {
std::ffi::CString, std::ffi::CString,
}; };
fn main() -> Result<()> { #[cfg(any(target_os = "linux", target_os = "android"))]
let args = Args::parse(); fn disable_aslr() -> Result<()> {
let mut persona = personality::get().map_err(|e| anyhow!("Failed to get personality: {e:}"))?; let mut persona = personality::get().map_err(|e| anyhow!("Failed to get personality: {e:}"))?;
persona |= Persona::ADDR_NO_RANDOMIZE; persona |= Persona::ADDR_NO_RANDOMIZE;
personality::set(persona).map_err(|e| anyhow!("Failed to set personality: {e:}"))?; personality::set(persona).map_err(|e| anyhow!("Failed to set personality: {e:}"))?;
Ok(())
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn disable_aslr() -> Result<()> {
let mut status = libc::PROC_ASLR_FORCE_DISABLE;
let r = unsafe {
libc::procctl(
libc::P_PID,
0,
libc::PROC_ASLR_CTL,
&mut status as *mut i32 as *mut libc::c_void,
)
};
if r < 0 {
return Err(anyhow!("Failed to set aslr control"));
}
Ok(())
}
fn main() -> Result<()> {
let args = Args::parse();
disable_aslr()?;
let cargs = args let cargs = args
.argv() .argv()