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

Add support for LSM programs attached to cgroups #439

Closed
wants to merge 1 commit into from
Closed
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
65 changes: 65 additions & 0 deletions aya-bpf-macros/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,37 @@ impl SkLookup {
}
}

pub struct CgroupLsm {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct CgroupLsm {
pub struct LsmCgroup {

item: ItemFn,
name: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: String,
name: Option<String>,

}

impl CgroupLsm {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl CgroupLsm {
impl LsmCgroup {

pub fn from_syn(mut args: Args, item: ItemFn) -> Result<CgroupLsm> {
let name = name_arg(&mut args)?.unwrap_or_else(|| item.sig.ident.to_string());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let name = name_arg(&mut args)?.unwrap_or_else(|| item.sig.ident.to_string());
let name = name_arg(&mut args)?;


Ok(CgroupLsm { item, name })
}

pub fn expand(&self) -> Result<TokenStream> {
let section_name = format!("lsm_cgroup/{}", self.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let section_name = format!("lsm_cgroup/{}", self.name);
let section_name = if let Some(name) = &self.name {
format!("lsm_cgroup/{}", name)
} else {
("lsm_cgroup").to_owned()
};

let fn_name = &self.item.sig.ident;
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 #fn_name(ctx: *mut ::core::ffi::c_void) -> i32 {
return #fn_name(::aya_bpf::programs::CgroupLsmContext::new(ctx));

#item
}
})
}
}

#[cfg(test)]
mod tests {
use syn::parse_quote;
Expand Down Expand Up @@ -893,4 +924,38 @@ mod tests {
.to_string()
.contains("[link_section = \"cgroup_skb/egress\"]"));
}

#[test]
fn cgroup_lsm_with_name() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn cgroup_lsm_with_name() {
fn lsm_cgroup_with_name() {

let prog = CgroupLsm::from_syn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let prog = CgroupLsm::from_syn(
let prog = LsmCgroup::from_syn(

parse_quote!(name = "foo"),
parse_quote!(
fn bar(ctx: LsmContext) -> i32 {
0
}
),
)
.unwrap();
let stream = prog.expand().unwrap();
assert!(stream
.to_string()
.contains("[link_section = \"lsm_cgroup/foo\"]"));
}

#[test]
fn cgroup_lsm_no_name() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn cgroup_lsm_no_name() {
fn lsm_cgroup_no_name() {

let prog = CgroupLsm::from_syn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let prog = CgroupLsm::from_syn(
let prog = LsmCgroup::from_syn(

parse_quote!(),
parse_quote!(
fn foo(ct: LsmContext) -> i32 {
0
}
),
)
.unwrap();
let stream = prog.expand().unwrap();
assert!(stream
.to_string()
.contains("[link_section = \"lsm_cgroup/foo\"]"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.contains("[link_section = \"lsm_cgroup/foo\"]"));
.contains("[link_section = \"lsm_cgroup\"]"));

}
}
51 changes: 48 additions & 3 deletions aya-bpf-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
mod expand;

use expand::{
Args, BtfTracePoint, CgroupSkb, CgroupSock, CgroupSockAddr, CgroupSockopt, CgroupSysctl,
FEntry, FExit, Lsm, Map, PerfEvent, Probe, ProbeKind, RawTracePoint, SchedClassifier, SkLookup,
SkMsg, SkSkb, SkSkbKind, SockAddrArgs, SockOps, SocketFilter, SockoptArgs, TracePoint, Xdp,
Args, BtfTracePoint, CgroupLsm, CgroupSkb, CgroupSock, CgroupSockAddr, CgroupSockopt,
CgroupSysctl, FEntry, FExit, Lsm, Map, PerfEvent, Probe, ProbeKind, RawTracePoint,
SchedClassifier, SkLookup, SkMsg, SkSkb, SkSkbKind, SockAddrArgs, SockOps, SocketFilter,
SockoptArgs, TracePoint, Xdp,
};
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemFn, ItemStatic};
Expand Down Expand Up @@ -507,3 +508,47 @@ pub fn sk_lookup(attrs: TokenStream, item: TokenStream) -> TokenStream {
.unwrap_or_else(|err| err.to_compile_error())
.into()
}

/// Marks a function as an LSM program that can be attached to Linux LSM hooks
/// within a [cgroup]. Used to implement security policy and audit logging.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is [cgroup] linking to?

///
/// LSM probes can be attached to the kernel's security hooks to implement mandatory
/// access control policy and security auditing.
///
/// LSM probes require a kernel compiled with `CONFIG_BPF_LSM=y` and
/// `CONFIG_DEBUG_INFO_BTF=y`. In order for the probes to fire, you also need
/// the BPF LSM to be enabled through your kernel's `lsm` option. If your kernel
/// is not built with `lsm=[...],bpf` option, BPF LSM needs to be enabled
/// through the kernel's boot parameter (like `lsm=lockdown,yama,bpf`).
///
/// # Minimum kernel version
///
/// The minimum kernel version required to use this feature is 6.0.
///
/// # Examples
///
/// ```no_run
/// use aya_bpf::{macros::lsm, programs::LsmContext};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// use aya_bpf::{macros::lsm, programs::LsmContext};
/// use aya_bpf::{macros::lsm_cgroup, programs::LsmContext};

///
/// #[lsm(name = "file_open")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// #[lsm(name = "file_open")]
/// #[lsm_cgroup(name = "file_open")]

/// pub fn file_open(ctx: LsmContext) -> i32 {
/// match unsafe { try_file_open(ctx) } {
/// Ok(ret) => ret,
/// Err(ret) => ret,
/// }
/// }
///
/// unsafe fn try_file_open(_ctx: LsmContext) -> Result<i32, i32> {
/// Ok(0)
/// }
/// ```
#[proc_macro_attribute]
pub fn cgroup_lsm(attrs: TokenStream, item: TokenStream) -> TokenStream {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn cgroup_lsm(attrs: TokenStream, item: TokenStream) -> TokenStream {
pub fn lsm_cgroup(attrs: TokenStream, item: TokenStream) -> TokenStream {

let args = parse_macro_input!(attrs as Args);
let item = parse_macro_input!(item as ItemFn);

CgroupLsm::from_syn(args, item)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CgroupLsm::from_syn(args, item)
LsmCgroup::from_syn(args, item)

.and_then(|u| u.expand())
.unwrap_or_else(|err| err.to_compile_error())
.into()
}
134 changes: 134 additions & 0 deletions aya/src/programs/cgroup_lsm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//! LSM probes attached to cgroups.
use std::os::unix::prelude::{AsRawFd, RawFd};

use crate::{
generated::{bpf_attach_type::BPF_LSM_CGROUP, bpf_prog_type::BPF_PROG_TYPE_LSM},
obj::btf::{Btf, BtfKind},
programs::{define_link_wrapper, load_program, FdLink, Link, ProgramData, ProgramError},
sys::bpf_link_create,
};

/// A program that attaches to Linux LSM hooks within a [cgroup]. Used to
/// implement security policy and audit logging.
///
/// LSM probes can be attached to the kernel's [security hooks][1] to implement
/// mandatory access control policy and security auditing.
///
/// LSM probes require a kernel compiled with `CONFIG_BPF_LSM=y` and
/// `CONFIG_DEBUG_INFO_BTF=y`. In order for the probes to fire, you also need
/// the BPF LSM to be enabled through your kernel's `lsm` option. If your kernel
/// is not built with `lsm=[...],bpf` option, BPF LSM needs to be enabled
/// through the kernel's boot parameter (like `lsm=lockdown,yama,bpf`).
///
/// # Minimum kernel version
///
/// The minimum kernel version required to use this feature is 6.0.
///
/// # Examples
///
/// ```no_run
/// # #[derive(thiserror::Error, Debug)]
/// # enum LsmError {
/// # #[error(transparent)]
/// # BtfError(#[from] aya::BtfError),
/// # #[error(transparent)]
/// # Program(#[from] aya::programs::ProgramError),
/// # #[error(transparent)]
/// # Bpf(#[from] aya::BpfError),
/// # }
/// # let mut bpf = Bpf::load_file("ebpf_programs.o")?;
/// use aya::{Bpf, programs::LsmCgroup, BtfError, Btf};
/// use std::{fs::File, os::unix::prelude::AsRawFd, path::Path};
///
/// let btf = Btf::from_sys_fs()?;
/// let program: &mut LsmCgroup = bpf.program_mut("lsm_prog").unwrap().try_into()?;
/// program.load("security_bprm_exec", &btf)?;
/// let cgroup = File::open(Path::new("/sys/fs/cgroup/unified/aya"))?;
/// program.attach(cgroup.as_raw_fd())?;
/// # Ok::<(), LsmError>(())
/// ```
#[derive(Debug)]
pub struct CgroupLsm {
pub(crate) data: ProgramData<CgroupLsmLink>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) data: ProgramData<CgroupLsmLink>,
pub(crate) data: ProgramData<CgroupLsmLink>,
pub(crate) lsm_hook_name: Option<&str>,

lsm_hook_name should be an Option given it can be provided as part of the section name.

}

impl CgroupLsm {
/// Loads the program inside the kernel.
pub fn load(&mut self, lsm_hook_name: &str, btf: &Btf) -> Result<(), ProgramError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to know the hook name at load time? If so, what's the point of SEC("lsm_cgroup") 🤔
If it is indeed needed here it should be in an Option<&str> given it may have been provided as part of the section name.

self.data.expected_attach_type = Some(BPF_LSM_CGROUP);
let type_name = format!("bpf_lsm_{}", lsm_hook_name);
self.data.attach_btf_id =
Some(btf.id_by_type_name_kind(type_name.as_str(), BtfKind::Func)?);
load_program(BPF_PROG_TYPE_LSM, &mut self.data)
}

/// Attaches the program.
///
/// The returned value can be used to detach, see [CgroupLsm::detach].
pub fn attach<T: AsRawFd>(&mut self, cgroup: T) -> Result<CgroupLsmLinkId, ProgramError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more ergonomic to use the lsm_hook_name: Option<&str> here - to allow the same program to be attached to different hooks - if that's supported (see above).

let prog_fd = self.data.fd_or_err()?;
let cgroup_fd = cgroup.as_raw_fd();
let attach_type = self.data.expected_attach_type.unwrap();
let btf_id = self.data.attach_btf_id;

let link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, btf_id, 0).map_err(
|(_, io_error)| ProgramError::SyscallError {
call: "bpf_link_create".to_owned(),
io_error,
},
)? as RawFd;
self.data
.links
.insert(CgroupLsmLink(CgroupLsmLinkInner::Fd(FdLink::new(link_fd))))
}

/// Detaches the program.
///
/// See [CgroupLsm::attach].
pub fn detach(&mut self, link_id: CgroupLsmLinkId) -> Result<(), ProgramError> {
self.data.links.remove(link_id)
}

/// Takes ownership of the link referenced by the provided link_id.
///
/// The link will be detached on `Drop` and the caller is now responsible
/// for managing its lifetime.
pub fn take_link(&mut self, link_id: CgroupLsmLinkId) -> Result<CgroupLsmLink, ProgramError> {
self.data.take_link(link_id)
}
}

#[derive(Debug, Hash, Eq, PartialEq)]
enum CgroupLsmLinkIdInner {
Fd(<FdLink as Link>::Id),
}

#[derive(Debug)]
enum CgroupLsmLinkInner {
Fd(FdLink),
}

impl Link for CgroupLsmLinkInner {
type Id = CgroupLsmLinkIdInner;

fn id(&self) -> Self::Id {
match self {
CgroupLsmLinkInner::Fd(fd) => CgroupLsmLinkIdInner::Fd(fd.id()),
}
}

fn detach(self) -> Result<(), ProgramError> {
match self {
CgroupLsmLinkInner::Fd(fd) => fd.detach(),
}
}
}

define_link_wrapper!(
/// The link used by [CgroupLsm] programs.
CgroupLsmLink,
/// The type returned by [CgroupLsm::attach]. Can be passed to [CgroupLsm::detach].
CgroupLsmLinkId,
CgroupLsmLinkInner,
CgroupLsmLinkIdInner
);
4 changes: 2 additions & 2 deletions aya/src/programs/lsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
/// access control policy and security auditing.
///
/// LSM probes require a kernel compiled with `CONFIG_BPF_LSM=y` and `CONFIG_DEBUG_INFO_BTF=y`.
/// In order for the probes to fire, you also need the BPF LSM to be enabled through your
/// kernel's boot paramters (like `lsm=lockdown,yama,bpf`).
/// If your kernel is not built with `lsm=[...],bpf` option, BPF LSM needs to be enabled
/// through the kernel's boot parameter (like `lsm=lockdown,yama,bpf`).
///
/// # Minimum kernel version
///
Expand Down
2 changes: 2 additions & 0 deletions aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
//! [`Bpf::program`]: crate::Bpf::program
//! [`Bpf::program_mut`]: crate::Bpf::program_mut
//! [`maps`]: crate::maps
pub mod cgroup_lsm;
pub mod cgroup_skb;
pub mod cgroup_sock;
pub mod cgroup_sock_addr;
Expand Down Expand Up @@ -72,6 +73,7 @@ use std::{
};
use thiserror::Error;

pub use cgroup_lsm::CgroupLsm;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub use cgroup_lsm::CgroupLsm;
pub use lsm_cgroup::LsmCgroup;

pub use cgroup_skb::{CgroupSkb, CgroupSkbAttachType};
pub use cgroup_sock::{CgroupSock, CgroupSockAttachType};
pub use cgroup_sock_addr::{CgroupSockAddr, CgroupSockAddrAttachType};
Expand Down