mirror of https://github.com/aya-rs/aya
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.5 KiB
Rust
117 lines
3.5 KiB
Rust
1 year ago
|
use std::borrow::Cow;
|
||
|
|
||
|
use proc_macro2::TokenStream;
|
||
|
use quote::quote;
|
||
|
use syn::{ItemFn, Result};
|
||
|
|
||
1 year ago
|
use crate::args::{err_on_unknown_args, pop_bool_arg, pop_string_arg};
|
||
1 year ago
|
|
||
|
pub(crate) struct Lsm {
|
||
|
item: ItemFn,
|
||
1 year ago
|
hook: Option<String>,
|
||
1 year ago
|
sleepable: bool,
|
||
|
}
|
||
|
|
||
|
impl Lsm {
|
||
|
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Lsm> {
|
||
|
let item = syn::parse2(item)?;
|
||
1 year ago
|
let mut args = syn::parse2(attrs)?;
|
||
|
let hook = pop_string_arg(&mut args, "hook");
|
||
|
let sleepable = pop_bool_arg(&mut args, "sleepable");
|
||
1 year ago
|
err_on_unknown_args(&args)?;
|
||
|
Ok(Lsm {
|
||
|
item,
|
||
|
hook,
|
||
1 year ago
|
sleepable,
|
||
1 year ago
|
})
|
||
|
}
|
||
|
|
||
|
pub(crate) fn expand(&self) -> Result<TokenStream> {
|
||
|
let section_prefix = if self.sleepable { "lsm.s" } else { "lsm" };
|
||
1 year ago
|
let section_name: Cow<'_, _> = if let Some(hook) = &self.hook {
|
||
|
format!("{}/{}", section_prefix, hook).into()
|
||
|
} else {
|
||
|
section_prefix.into()
|
||
|
};
|
||
1 year ago
|
let fn_vis = &self.item.vis;
|
||
|
let fn_name = self.item.sig.ident.clone();
|
||
|
let item = &self.item;
|
||
|
// LSM probes need to return an integer corresponding to the correct
|
||
|
// policy decision. Therefore we do not simply default to a return value
|
||
|
// of 0 as in other program types.
|
||
|
Ok(quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = #section_name]
|
||
|
#fn_vis fn #fn_name(ctx: *mut ::core::ffi::c_void) -> i32 {
|
||
8 months ago
|
return #fn_name(::aya_ebpf::programs::LsmContext::new(ctx));
|
||
1 year ago
|
|
||
|
#item
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use syn::parse_quote;
|
||
|
|
||
1 year ago
|
use super::*;
|
||
|
|
||
1 year ago
|
#[test]
|
||
|
fn test_lsm_sleepable() {
|
||
|
let prog = Lsm::parse(
|
||
|
parse_quote! {
|
||
|
sleepable,
|
||
|
hook = "bprm_committed_creds"
|
||
|
},
|
||
|
parse_quote! {
|
||
8 months ago
|
fn bprm_committed_creds(ctx: &mut ::aya_ebpf::programs::LsmContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "lsm.s/bprm_committed_creds"]
|
||
|
fn bprm_committed_creds(ctx: *mut ::core::ffi::c_void) -> i32 {
|
||
8 months ago
|
return bprm_committed_creds(::aya_ebpf::programs::LsmContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn bprm_committed_creds(ctx: &mut ::aya_ebpf::programs::LsmContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
|
|
||
1 year ago
|
#[test]
|
||
|
fn test_lsm() {
|
||
|
let prog = Lsm::parse(
|
||
|
parse_quote! {
|
||
|
hook = "bprm_committed_creds"
|
||
|
},
|
||
|
parse_quote! {
|
||
8 months ago
|
fn bprm_committed_creds(ctx: &mut ::aya_ebpf::programs::LsmContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.unwrap();
|
||
|
let expanded = prog.expand().unwrap();
|
||
|
let expected = quote! {
|
||
|
#[no_mangle]
|
||
|
#[link_section = "lsm/bprm_committed_creds"]
|
||
|
fn bprm_committed_creds(ctx: *mut ::core::ffi::c_void) -> i32 {
|
||
8 months ago
|
return bprm_committed_creds(::aya_ebpf::programs::LsmContext::new(ctx));
|
||
1 year ago
|
|
||
8 months ago
|
fn bprm_committed_creds(ctx: &mut ::aya_ebpf::programs::LsmContext) -> i32 {
|
||
1 year ago
|
0
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
assert_eq!(expected.to_string(), expanded.to_string());
|
||
|
}
|
||
|
}
|