Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ebpf): replace fn calls by clamp #169

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions kunai-common/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
errors::ProbeError, macros::bpf_target_code, macros::not_bpf_target_code, utils::cap_size,
};
use crate::{errors::ProbeError, macros::bpf_target_code, macros::not_bpf_target_code};

use super::time::Time;
use super::utils::bound_value_for_verifier;

use kunai_macros::BpfError;

Expand Down Expand Up @@ -318,12 +315,11 @@ impl Path {
pub fn as_slice(&self) -> &[u8] {
match self.mode {
Mode::Append => {
let len =
bound_value_for_verifier(self.len as isize, 0, self.buffer.len() as isize);
&self.buffer[..len as usize]
let len = (self.len as usize).clamp(0, self.buffer.len());
&self.buffer[..len]
}
Mode::Prepend => {
let len = cap_size(self.len(), MAX_PATH_LEN - 255);
let len = self.len().clamp(0, MAX_PATH_LEN - 255);
&self.buffer[(self.buffer.len() - len)..]
}
}
Expand Down
13 changes: 6 additions & 7 deletions kunai-common/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
errors::ProbeError, macros::bpf_target_code, macros::not_bpf_target_code,
utils::bound_value_for_verifier,
};
use crate::{errors::ProbeError, macros::bpf_target_code, macros::not_bpf_target_code};
use core::mem;
use kunai_macros::BpfError;

Expand Down Expand Up @@ -107,9 +104,11 @@ impl<const N: usize> String<N> {
if self.is_full() {
return Err(Error::StringIsFull);
}
let k = bound_value_for_verifier(at as isize, 0, (self.cap() - 1) as isize);
self.s.as_mut()[k as usize] = b;
self.len += 1;
if let Some(at) = self.s.get_mut(at) {
*at = b;
self.len += 1;
}

Ok(())
}

Expand Down
63 changes: 0 additions & 63 deletions kunai-common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,6 @@
use core::ops::Rem;

use crate::macros::bpf_target_code;

bpf_target_code! {
mod bpf;
pub use bpf::*;
}

#[inline(always)]
#[allow(unused_variables)]
pub fn bound_value_for_verifier(v: isize, min: isize, max: isize) -> isize {
#[cfg(target_arch = "bpf")]
{
if v < min {
return min;
}
if v > max {
return max;
}
}
v
}

// This function must be used to limit the size of a bpf_probe_read call
// it seems to be a generic enough solution that meet the requirements
// the verifier expects to be happy
#[inline(always)]
#[allow(unused_variables)]
#[allow(unused_mut)]
#[allow(unused_assignments)]
#[allow(clippy::let_and_return)]
pub fn cap_size<T: Copy + PartialOrd + Rem<Output = T>>(size: T, cap: T) -> T {
let mut ret = size;
#[cfg(target_arch = "bpf")]
{
if size >= cap {
return cap;
}
ret = size % cap;
}
ret
}

mod test {

#[test]
#[allow(unused_variables)]
fn test_stringify_in_macro() {
#[derive(Default)]
#[allow(dead_code)]
struct Dummy {
a: u32,
b: u64,
}

macro_rules! test_stringify {
($struc:expr, $field:ident) => {
println!(stringify!($struc.$field));
};
}

let d = Dummy {
..Default::default()
};

test_stringify!(d, a);
}
}
Loading