Skip to content

Commit e515c5c

Browse files
committed
feat: adapted to latest aya and kernel version
1 parent 3d33b2e commit e515c5c

File tree

9 files changed

+67
-63
lines changed

9 files changed

+67
-63
lines changed

.github/workflows/build-net-tc-filter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: rust-net-tc-filter
22
on: [push]
33
env:
4-
RUSTUP_TOOLCHAIN: "1.79.0"
4+
RUSTUP_TOOLCHAIN: "1.82.0"
55
RUSTUP_HOME: "./rust/rustup"
66
CARGO_HOME: "./rust/cargo"
77

.github/workflows/build-sock-filter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: rust-sock-filter
22
on: [push]
33
env:
4-
RUSTUP_TOOLCHAIN: "1.79.0"
4+
RUSTUP_TOOLCHAIN: "1.82.0"
55
RUSTUP_HOME: "./rust/rustup"
66
CARGO_HOME: "./rust/cargo"
77

.github/workflows/build-uprobe-libcall-filter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: rust-uprobe-libcall-filter
22
on: [push]
33
env:
4-
RUSTUP_TOOLCHAIN: "1.79.0"
4+
RUSTUP_TOOLCHAIN: "1.82.0"
55
RUSTUP_HOME: "./rust/rustup"
66
CARGO_HOME: "./rust/cargo"
77

conf/uprobe-libcall-filter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
applications:
22
filter:
33
openssl3.1:
4-
openssl_lib: "/lib64/glibc-hwcaps/x86-64-v3/libssl.so.3.1.2"
4+
openssl_lib: "/lib64/glibc-hwcaps/x86-64-v3/libssl.so.3.1.4"
55

uprobe-libcall-filter/uprobe-libcall-filter-app/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
anyhow = "1.0.68"
9+
anyhow = "1.0.86"
1010
# TODO: change to the stable version by the next release = 0.12
1111
aya = { git = "https://github.com/aya-rs/aya", branch = "main", features = [
1212
"async_tokio",
1313
] }
1414
aya-log = { git = "https://github.com/aya-rs/aya", branch = "main" }
1515
bytes = {version = "1"}
1616
clap = { version = "4.1.1", features = ["derive"] }
17-
env_logger = "0.10"
17+
env_logger = "0.11"
1818
uprobe-libcall-filter-common = { path = "../uprobe-libcall-filter-common", features=["user"] }
1919
log = {version="0.4"}
20-
serde = {version="1.0.152", features = ["derive"] }
20+
serde = {version="1.0.204", features = ["derive"] }
2121
serde_yaml = {version = "0.9.16"}
2222
tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "signal"] }
2323

24+
2425
[dev-dependencies]
2526
rand = {version="0.8.5", features = ["small_rng"]}
2627

uprobe-libcall-filter/uprobe-libcall-filter-app/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use aya::{
77
Ebpf,
88
};
99
use aya_log::EbpfLogger;
10-
1110
use bytes::BytesMut;
1211
use clap::Parser;
1312
use log::{info, warn};
@@ -68,6 +67,7 @@ async fn main() -> Result<(), anyhow::Error> {
6867
&application_definition.openssl_lib,
6968
None,
7069
)?;
70+
7171
let program_ossreadprobe_ret: &mut UProbe =
7272
bpf.program_mut("osslreadretprobe").unwrap().try_into()?;
7373
program_ossreadprobe_ret.load()?;
@@ -103,7 +103,7 @@ async fn main() -> Result<(), anyhow::Error> {
103103
let mut ssl_read_perf_array =
104104
AsyncPerfEventArray::try_from(bpf.take_map("SSLREADDATA").unwrap())?;
105105

106-
for cpu_id in online_cpus()? {
106+
for cpu_id in online_cpus().map_err(|(_, error)| error)? {
107107
let mut buf = ssl_read_perf_array.open(cpu_id, None)?;
108108
task::spawn(async move {
109109
let mut buffers = (0..10)
@@ -123,11 +123,11 @@ async fn main() -> Result<(), anyhow::Error> {
123123
}
124124

125125
// Get feedback from eBPF module of calls to SSL_write with unecrypted data
126-
let mut ssl_read_perf_array =
126+
let mut ssl_write_perf_array =
127127
AsyncPerfEventArray::try_from(bpf.take_map("SSLWRITEDATA").unwrap())?;
128128

129-
for cpu_id in online_cpus()? {
130-
let mut buf = ssl_read_perf_array.open(cpu_id, None)?;
129+
for cpu_id in online_cpus().map_err(|(_, error)| error)? {
130+
let mut buf = ssl_write_perf_array.open(cpu_id, None)?;
131131
task::spawn(async move {
132132
let mut buffers = (0..10)
133133
.map(|_| BytesMut::with_capacity(uprobe_libcall_filter_common::DATA_BUF_CAPACITY))
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#![no_std]
22

3-
4-
5-
6-
73
// how much data can be exchanged in a buffer between eBPF and user space application in one go
84
pub const DATA_BUF_CAPACITY: usize = 16384;
9-
// note: per SSL_read/SSL_write call OpenSSL supports up to 16 KB (cf. https://www.openssl.org/docs/man1.1.1/man3/SSL_read.html), ie it does not make sense to configure here much more
10-
// this is also according to the TLS specification: https://www.rfc-editor.org/rfc/rfc8446
5+
// note: per SSL_read/SSL_write call OpenSSL supports up to 16 KB (cf. https://docs.openssl.org/3.0/man3/SSL_read/), ie it does not make sense to configure here much more
6+
// this is also according to the TLS specification: https://www.rfc-editor.org/rfc/rfc8446

uprobe-libcall-filter/uprobe-libcall-filter-ebpf/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ aya-ebpf = { git = "https://github.com/aya-rs/aya", branch = "main" }
1111
aya-ebpf-bindings = { git = "https://github.com/aya-rs/aya", branch = "main" }
1212
aya-log-ebpf = { git = "https://github.com/aya-rs/aya", branch = "main" }
1313
uprobe-libcall-filter-common = { path = "../uprobe-libcall-filter-common" }
14-
memoffset = "0.8"
14+
memoffset = "0.9"
1515

1616
[[bin]]
1717
name = "uprobe-libcall-filter"

uprobe-libcall-filter/uprobe-libcall-filter-ebpf/src/main.rs

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#![no_main]
77

88
use aya_ebpf::{
9+
bindings::__u32,
910
macros::map,
1011
macros::uprobe,
1112
macros::uretprobe,
1213
maps::{HashMap, PerCpuArray, PerfEventByteArray},
1314
programs::ProbeContext,
1415
programs::RetProbeContext,
1516
};
16-
use aya_ebpf_bindings::helpers::{bpf_get_current_pid_tgid, bpf_probe_read};
17+
use aya_ebpf_bindings::helpers::{bpf_get_current_pid_tgid, bpf_probe_read_user};
1718
use aya_log_ebpf::warn;
1819
#[allow(non_upper_case_globals)]
1920
#[allow(non_snake_case)]
@@ -26,10 +27,10 @@ pub struct DataBuf {
2627

2728
// Data structures for exchanging SSL_read data with user space
2829
#[map]
29-
pub static mut SSL_READ_DATA_BUF: PerCpuArray<DataBuf> = PerCpuArray::with_max_entries(1, 0);
30+
static SSLREADDATABUF: PerCpuArray<DataBuf> = PerCpuArray::with_max_entries(1, 0);
3031

3132
#[map]
32-
pub static mut SSLREADDATA: PerfEventByteArray = PerfEventByteArray::new(0);
33+
static SSLREADDATA: PerfEventByteArray = PerfEventByteArray::new(0);
3334

3435
#[map] // contains the pointer to the read buffer containing the decrypted data provided by OpenSSL
3536
// key is the tgid_pid of the process
@@ -39,10 +40,10 @@ static mut SSLREADARGSMAP: HashMap<u64, *const core::ffi::c_void> =
3940

4041
// Data structures for exchanging SSL_write data with user space
4142
#[map]
42-
pub static mut SSL_WRITE_DATA_BUF: PerCpuArray<DataBuf> = PerCpuArray::with_max_entries(1, 0);
43+
static SSLWRITEDATABUF: PerCpuArray<DataBuf> = PerCpuArray::with_max_entries(1, 0);
4344

4445
#[map]
45-
pub static mut SSLWRITEDATA: PerfEventByteArray = PerfEventByteArray::new(0);
46+
static SSLWRITEDATA: PerfEventByteArray = PerfEventByteArray::new(0);
4647

4748
#[map] // contains the pointer to the read buffer containing the decrypted data provided by OpenSSL
4849
// key is the tgid_pid of the process
@@ -60,13 +61,15 @@ pub fn osslreadprobe(ctx: ProbeContext) -> u32 {
6061
// get the current process id
6162
let current_pid_tgid = unsafe { bpf_get_current_pid_tgid() };
6263

63-
// get the parameter containing the read buffer, cf. https://www.openssl.org/docs/man1.1.1/man3/SSL_read.html, Note: aya starts from 0 (ie Parameter 2 = arg(1))
64-
let buffer_ptr: *const core::ffi::c_void = *&ctx.arg(1).unwrap();
65-
64+
// get the parameter containing the read buffer, cf. https://docs.openssl.org/3.0/man3/SSL_read/, Note: aya starts from 0 (ie Parameter 2 = arg(1))
65+
let buffer_ptr: *const core::ffi::c_void = match *&ctx.arg(1) {
66+
Some(ptr) => ptr,
67+
None => return 0,
68+
};
6669
unsafe {
67-
SSLREADARGSMAP
68-
.insert(&current_pid_tgid, &buffer_ptr, 0)
69-
.unwrap();
70+
match SSLREADARGSMAP.insert(&current_pid_tgid, &buffer_ptr, 0) {
71+
_ => (),
72+
};
7073
}
7174
return 0;
7275
}
@@ -83,31 +86,31 @@ pub fn osslreadretprobe(ctx: RetProbeContext) -> u32 {
8386
let current_pid_tgid = unsafe { bpf_get_current_pid_tgid() };
8487

8588
// get return value (is the length of data read)
86-
let ret_value_len: i32 = ctx.ret().unwrap();
89+
// get return value (is the length of data read)
90+
let ret_value_len: i32 = match ctx.ret() {
91+
Some(ret) => ret,
92+
None => return 0,
93+
};
8794
if ret_value_len > 0 {
8895
// only if there was actually sth. to read.
89-
90-
if ret_value_len
91-
> uprobe_libcall_filter_common::DATA_BUF_CAPACITY
92-
.try_into()
93-
.unwrap()
94-
{
96+
if ret_value_len as usize > uprobe_libcall_filter_common::DATA_BUF_CAPACITY {
9597
warn!(
9698
&ctx,
97-
"Read Buffer is larger than Buffer Capacity - data is not processed"
99+
"Read Buffer {} is larger than Buffer Capacity {} - data is not processed",
100+
ret_value_len,
101+
uprobe_libcall_filter_common::DATA_BUF_CAPACITY
98102
);
99103
} else {
100104
// get pointer stored when the read function was called
101-
102105
unsafe {
103106
match SSLREADARGSMAP.get(&current_pid_tgid) {
104107
Some(src_buffer_ptr) => {
105-
if let Some(output_buf_ptr) = SSL_READ_DATA_BUF.get_ptr_mut(0) {
108+
if let Some(output_buf_ptr) = SSLREADDATABUF.get_ptr_mut(0) {
106109
let output_buf = &mut *output_buf_ptr;
107-
108-
bpf_probe_read(
110+
bpf_probe_read_user(
109111
output_buf.buf.as_mut_ptr() as *mut core::ffi::c_void,
110-
(&ret_value_len).clone().try_into().unwrap(),
112+
ret_value_len as u32
113+
& (uprobe_libcall_filter_common::DATA_BUF_CAPACITY - 1) as u32, // needed by eBPF verifier to be able to ensure that not more than necessary is read
111114
*src_buffer_ptr,
112115
);
113116

@@ -122,7 +125,9 @@ pub fn osslreadretprobe(ctx: RetProbeContext) -> u32 {
122125

123126
// clean up map
124127
unsafe {
125-
SSLREADARGSMAP.remove(&current_pid_tgid).unwrap();
128+
match SSLREADARGSMAP.remove(&current_pid_tgid) {
129+
_ => (),
130+
}
126131
}
127132
return 0;
128133
}
@@ -137,15 +142,16 @@ pub fn osslwriteprobe(ctx: ProbeContext) -> u32 {
137142
// get the current process id
138143
let current_pid_tgid = unsafe { bpf_get_current_pid_tgid() };
139144

140-
// get the parameter containing the write buffer, cf. https://www.openssl.org/docs/man1.1.1/man3/SSL_write.html, Note: aya starts from 0 (ie Parameter 2 = arg(1))
141-
let buffer_ptr: *const core::ffi::c_void = *&ctx.arg(1).unwrap();
142-
145+
// get the parameter containing the write buffer, cf. https://docs.openssl.org/3.0/man3/SSL_write/, Note: aya starts from 0 (ie Parameter 2 = arg(1))
146+
let buffer_ptr: *const core::ffi::c_void = match *&ctx.arg(1) {
147+
Some(ptr) => ptr,
148+
None => return 0,
149+
};
143150
unsafe {
144-
SSLWRITEARGSMAP
145-
.insert(&current_pid_tgid, &buffer_ptr, 0)
146-
.unwrap();
151+
match SSLWRITEARGSMAP.insert(&current_pid_tgid, &buffer_ptr, 0) {
152+
_ => (),
153+
};
147154
}
148-
149155
return 0;
150156
}
151157

@@ -161,15 +167,14 @@ pub fn osslwriteretprobe(ctx: RetProbeContext) -> u32 {
161167
let current_pid_tgid = unsafe { bpf_get_current_pid_tgid() };
162168

163169
// get return value (is the length of data read)
164-
let ret_value_len: i32 = ctx.ret().unwrap();
170+
let ret_value_len: i32 = match ctx.ret() {
171+
Some(ret) => ret,
172+
None => return 0,
173+
};
165174
if ret_value_len > 0 {
166175
// only if there was actually sth. to read.
167176

168-
if ret_value_len
169-
> uprobe_libcall_filter_common::DATA_BUF_CAPACITY
170-
.try_into()
171-
.unwrap()
172-
{
177+
if ret_value_len as usize > uprobe_libcall_filter_common::DATA_BUF_CAPACITY {
173178
warn!(
174179
&ctx,
175180
"Write Buffer is larger than Buffer Capacity - data is not processed"
@@ -180,12 +185,12 @@ pub fn osslwriteretprobe(ctx: RetProbeContext) -> u32 {
180185
unsafe {
181186
match SSLWRITEARGSMAP.get(&current_pid_tgid) {
182187
Some(src_buffer_ptr) => {
183-
if let Some(output_buf_ptr) = SSL_WRITE_DATA_BUF.get_ptr_mut(0) {
188+
if let Some(output_buf_ptr) = SSLWRITEDATABUF.get_ptr_mut(0) {
184189
let output_buf = &mut *output_buf_ptr;
185-
186-
bpf_probe_read(
190+
bpf_probe_read_user(
187191
output_buf.buf.as_mut_ptr() as *mut core::ffi::c_void,
188-
(&ret_value_len).clone().try_into().unwrap(),
192+
ret_value_len as u32
193+
& (uprobe_libcall_filter_common::DATA_BUF_CAPACITY - 1) as u32, // needed by eBPF verifier to be able to ensure that not more than necessary is read
189194
*src_buffer_ptr,
190195
);
191196

@@ -200,12 +205,14 @@ pub fn osslwriteretprobe(ctx: RetProbeContext) -> u32 {
200205

201206
// clean up map
202207
unsafe {
203-
SSLWRITEARGSMAP.remove(&current_pid_tgid).unwrap();
208+
match SSLWRITEARGSMAP.remove(&current_pid_tgid) {
209+
_ => (),
210+
}
204211
}
205212
return 0;
206213
}
207214

208215
#[panic_handler]
209216
fn panic(_info: &core::panic::PanicInfo) -> ! {
210-
unsafe { core::hint::unreachable_unchecked() }
217+
loop {}
211218
}

0 commit comments

Comments
 (0)