Skip to content

Commit

Permalink
fix: [kunai-common] fix verifier errors when compiling eBPF with late…
Browse files Browse the repository at this point in the history
…st nightly
  • Loading branch information
qjerome committed Jul 11, 2024
1 parent 9ebaea9 commit 0201214
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
8 changes: 2 additions & 6 deletions kunai-common/src/buffer/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ impl<const N: usize> Buffer<N> {

#[inline(always)]
pub unsafe fn read_user_at<P>(&mut self, from: *const P, size: u32) -> Result<(), Error> {
let size = cap_size(size, N as u32);

let buf = &mut self.buf[..size as usize];
let buf = &mut self.buf[..size.clamp(0, N as u32) as usize];
bpf_probe_read_user_buf(from as *const _, buf).map_err(|_| Error::FailedToRead)?;

self.len = size as usize;
Expand All @@ -138,9 +136,7 @@ impl<const N: usize> Buffer<N> {

#[inline(always)]
pub unsafe fn read_kernel_at<P>(&mut self, from: *const P, size: u32) -> Result<(), Error> {
let size = cap_size(size, N as u32);

let buf = &mut self.buf[..size as usize];
let buf = &mut self.buf[..size.clamp(0, N as u32) as usize];
bpf_probe_read_kernel_buf(from as *const _, buf).map_err(|_| Error::FailedToRead)?;

self.len = size as usize;
Expand Down
26 changes: 15 additions & 11 deletions kunai-common/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,23 @@ impl Path {

#[inline(always)]
pub fn get_byte(&self, i: usize) -> core::result::Result<u8, Error> {
match self.mode {
Mode::Append => Ok(self.buffer[i]),
let i = match self.mode {
Mode::Append => i,
Mode::Prepend => {
let len = self.len;
if len <= self.buffer.len() as u32 {
let i = self.buffer.len() - len as usize + i;
if i < self.buffer.len() {
return Ok(self.buffer[i]);
}
if len > self.buffer.len() as u32 {
return Err(Error::OutOfBound);
}
Err(Error::OutOfBound)
self.buffer.len() - len as usize + i
}
};

// bound checking
if i < self.buffer.len() {
return Ok(unsafe { *self.buffer.get_unchecked(i) });
}

Err(Error::OutOfBound)
}

#[inline(always)]
Expand All @@ -274,10 +278,10 @@ impl Path {
}

let Ok(b) = self.get_byte(i) else {
return false;
};
return false;
};

if b != start[i] {
if b != unsafe { *start.get_unchecked(i) } {
return false;
}
}
Expand Down

0 comments on commit 0201214

Please sign in to comment.