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 BPF_PROG_TYPE_CGROUP_DEVICE #466

Merged
merged 3 commits into from
Dec 15, 2022

Conversation

mdaverde
Copy link
Contributor

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.

An example use this eBPF program can be seen here: aya-cgroup-dev-example

Fixes: #212
Signed-off-by: Milan milan@mdaverde.com

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>
@netlify
Copy link

netlify bot commented Dec 14, 2022

Deploy Preview for aya-rs-docs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 925504f
🔍 Latest deploy log https://app.netlify.com/sites/aya-rs-docs/deploys/639b5ea64636930008625356
😎 Deploy Preview https://deploy-preview-466--aya-rs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Collaborator

@alessandrod alessandrod left a 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

sys::{bpf_link_create, bpf_prog_attach, kernel_version},
};

/// A program used to watch or prevent device interaction from a cgroup
Copy link
Collaborator

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

Copy link
Contributor Author

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.
Copy link
Collaborator

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

Copy link
Contributor Author

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>
Copy link
Member

@vadorovsky vadorovsky left a 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.

@aya-rs aya-rs deleted a comment from alessandrod Dec 15, 2022
Copy link
Member

@vadorovsky vadorovsky left a 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()?;
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 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

Copy link
Contributor Author

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)?;
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
/// 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Collaborator

@alessandrod alessandrod left a comment

Choose a reason for hiding this comment

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

Thank you!

@alessandrod alessandrod merged commit d1919a8 into aya-rs:main Dec 15, 2022
@dave-tucker dave-tucker added feature A PR that implements a new feature or enhancement aya-bpf This is about aya-bpf (kernel) aya This is about aya (userspace) labels Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aya This is about aya (userspace) aya-bpf This is about aya-bpf (kernel) feature A PR that implements a new feature or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for BPF_PROG_TYPE_CGROUP_DEVICE
4 participants