-
Notifications
You must be signed in to change notification settings - Fork 286
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 BPF_PROG_TYPE_CGROUP_DEVICE #466
Conversation
Kernel 4.15 added a new eBPF program that can be used with cgroup v2 to control & observe device access (e.g. read, write, mknod) - `BPF_PROG_TYPE_CGROUP_DEVICE`. We add the ability to create these programs with the `cgroup_device` proc macro which creates the `cgroup/dev` link section. Device details are available to the eBPF program in `DeviceContext`. The userspace representation is provided with the `CgroupDevice` structure. Fixes: aya-rs#212 Signed-off-by: Milan <milan@mdaverde.com>
✅ Deploy Preview for aya-rs-docs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff, nearly flawless! See nits and failing lint
aya/src/programs/cgroup_device.rs
Outdated
sys::{bpf_link_create, bpf_prog_attach, kernel_version}, | ||
}; | ||
|
||
/// A program used to watch or prevent device interaction from a cgroup |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing period at the end of the sentence
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
pub fn load(&mut self) -> Result<(), ProgramError> { | ||
load_program(BPF_PROG_TYPE_CGROUP_DEVICE, &mut self.data) | ||
} | ||
/// Attaches the program to the given cgroup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add newline before comment pls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Signed-off-by: Milan <milan@mdaverde.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome. Thanks for your contribution!
Just one comment. Nevermind, my comment was wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions for fixing the CI errors from here:
https://github.com/aya-rs/aya/actions/runs/3698602058/jobs/6265097628
/// use aya::programs::CgroupDevice; | ||
/// | ||
/// let cgroup = std::fs::File::open("/sys/fs/cgroup/unified")?; | ||
/// let program: &mut CgroupDevice = bpf.program_mut("cgroup_dev").unwrap().try_into()?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// let program: &mut CgroupDevice = bpf.program_mut("cgroup_dev").unwrap().try_into()?; | |
/// # let mut bpf = Bpf::load_file("ebpf_programs.o")?; | |
/// let program: &mut CgroupDevice = bpf.program_mut("cgroup_dev").unwrap().try_into()?; |
bpf
variable is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, went ahead and drew from CgroupSysctl's docs!
/// let cgroup = std::fs::File::open("/sys/fs/cgroup/unified")?; | ||
/// let program: &mut CgroupDevice = bpf.program_mut("cgroup_dev").unwrap().try_into()?; | ||
/// program.load()?; | ||
/// program.attach(cgroup)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// program.attach(cgroup)?; | |
/// program.attach(cgroup)?; | |
/// # Ok::<(), anyhow::Error>((()) |
Otherwise you can't use ?
for handling errors, because your code isn't returning any Result
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Kernel 4.15 added a new eBPF program that can be used with cgroup v2 to control & observe device access (e.g. read, write, mknod) -
BPF_PROG_TYPE_CGROUP_DEVICE
.We add the ability to create these programs with the
cgroup_device
proc macro which creates thecgroup/dev
link section. Device details are available to the eBPF program inDeviceContext
.The userspace representation is provided with the
CgroupDevice
structure.An example use this eBPF program can be seen here: aya-cgroup-dev-example
Fixes: #212
Signed-off-by: Milan milan@mdaverde.com