From 2ac433449cdea32f10c8fc88218799995946032d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Fri, 15 Oct 2021 08:13:18 +0000 Subject: [PATCH] Minor tweaks to make the verifier's job easier --- aya-log/aya-log-ebpf/src/lib.rs | 11 +++++++++-- aya-log/aya-log-ebpf/src/macros.rs | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/aya-log/aya-log-ebpf/src/lib.rs b/aya-log/aya-log-ebpf/src/lib.rs index 448f0871..56ae6746 100644 --- a/aya-log/aya-log-ebpf/src/lib.rs +++ b/aya-log/aya-log-ebpf/src/lib.rs @@ -11,7 +11,8 @@ use aya_bpf::{ maps::{PerCpuArray, PerfEventByteArray}, }; pub use aya_log_common::Level; -use aya_log_common::{RecordField, LOG_BUF_CAPACITY}; +use aya_log_common::RecordField; +pub use aya_log_common::LOG_BUF_CAPACITY; #[doc(hidden)] #[repr(C)] @@ -59,12 +60,18 @@ impl<'a> ufmt::uWrite for LogBufWriter<'a> { fn write_str(&mut self, s: &str) -> Result<(), Self::Error> { let bytes = s.as_bytes(); let len = bytes.len(); + + // this is to make sure the verifier knows about the upper bound + if len > LOG_BUF_CAPACITY { + return Err(()); + } + let available = self.data.len() - self.pos; if available < len { return Err(()); } - self.data[self.pos..self.pos + len].copy_from_slice(bytes); + self.data[self.pos..self.pos + len].copy_from_slice(&bytes[..len]); self.pos += len; Ok(()) } diff --git a/aya-log/aya-log-ebpf/src/macros.rs b/aya-log/aya-log-ebpf/src/macros.rs index f4c420f1..2d2ee666 100644 --- a/aya-log/aya-log-ebpf/src/macros.rs +++ b/aya-log/aya-log-ebpf/src/macros.rs @@ -183,7 +183,10 @@ macro_rules! log { if let Some(buf) = unsafe { $crate::AYA_LOG_BUF.get_mut(0) } { if let Ok(header_len) = $crate::write_record_header(&mut buf.buf, module_path!(), $lvl, module_path!(), file!(), line!()) { if let Ok(message_len) = $crate::write_record_message!(&mut buf.buf[header_len..], $($arg)+) { - let _ = unsafe { $crate::AYA_LOGS.output($ctx, &buf.buf[..header_len + message_len], 0) }; + let record_len = header_len + message_len; + if record_len <= $crate::LOG_BUF_CAPACITY { + let _ = unsafe { $crate::AYA_LOGS.output($ctx, &buf.buf[..header_len + message_len], 0) }; + } }; } }