Skip to content

Commit e1b6de3

Browse files
committed
aya: Set attach mode flag to 0 in bpf_link_create
1 parent a22ec37 commit e1b6de3

File tree

8 files changed

+110
-60
lines changed

8 files changed

+110
-60
lines changed

aya/src/programs/cgroup_device.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Cgroup device programs.
22
3+
use log::warn;
34
use std::os::fd::AsFd;
45

56
use aya_obj::generated::{
@@ -67,6 +68,11 @@ impl CgroupDevice {
6768
/// Attaches the program to the given cgroup.
6869
///
6970
/// The returned value can be used to detach, see [CgroupDevice::detach]
71+
///
72+
/// # Warning
73+
///
74+
/// On kernels 5.7.0 and later, attach modes other than CgroupAttachMode::default() are not passed to bpf_link_create.
75+
/// On older kernels that use bpf_prog_attach, the attach mode is honored.
7076
pub fn attach<T: AsFd>(
7177
&mut self,
7278
cgroup: T,
@@ -77,11 +83,17 @@ impl CgroupDevice {
7783
let cgroup_fd = cgroup.as_fd();
7884

7985
if KernelVersion::at_least(5, 7, 0) {
86+
if mode != CgroupAttachMode::default() {
87+
warn!(
88+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
89+
mode
90+
);
91+
}
8092
let link_fd = bpf_link_create(
8193
prog_fd,
8294
LinkTarget::Fd(cgroup_fd),
8395
BPF_CGROUP_DEVICE,
84-
mode.into(),
96+
0,
8597
None,
8698
)
8799
.map_err(|io_error| SyscallError {

aya/src/programs/cgroup_skb.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Cgroup skb programs.
22
3-
use std::{hash::Hash, os::fd::AsFd, path::Path};
4-
53
use aya_obj::generated::{
64
bpf_attach_type::{BPF_CGROUP_INET_EGRESS, BPF_CGROUP_INET_INGRESS},
75
bpf_prog_type::BPF_PROG_TYPE_CGROUP_SKB,
86
};
7+
use log::warn;
8+
use std::{hash::Hash, os::fd::AsFd, path::Path};
99

1010
use crate::{
1111
VerifierLogLevel,
@@ -86,6 +86,10 @@ impl CgroupSkb {
8686
/// Attaches the program to the given cgroup.
8787
///
8888
/// The returned value can be used to detach, see [CgroupSkb::detach].
89+
///
90+
/// # Warning
91+
///
92+
/// On kernels 5.7.0 and later, attach modes other than CgroupAttachMode::default() are not passed to bpf_link_create.
8993
pub fn attach<T: AsFd>(
9094
&mut self,
9195
cgroup: T,
@@ -101,17 +105,17 @@ impl CgroupSkb {
101105
CgroupSkbAttachType::Egress => BPF_CGROUP_INET_EGRESS,
102106
};
103107
if KernelVersion::at_least(5, 7, 0) {
104-
let link_fd = bpf_link_create(
105-
prog_fd,
106-
LinkTarget::Fd(cgroup_fd),
107-
attach_type,
108-
mode.into(),
109-
None,
110-
)
111-
.map_err(|io_error| SyscallError {
112-
call: "bpf_link_create",
113-
io_error,
114-
})?;
108+
if mode != CgroupAttachMode::default() {
109+
warn!(
110+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
111+
mode
112+
);
113+
}
114+
let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(cgroup_fd), attach_type, 0, None)
115+
.map_err(|io_error| SyscallError {
116+
call: "bpf_link_create",
117+
io_error,
118+
})?;
115119
self.data
116120
.links
117121
.insert(CgroupSkbLink::new(CgroupSkbLinkInner::Fd(FdLink::new(

aya/src/programs/cgroup_sock.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Cgroup socket programs.
22
3+
use log::warn;
34
use std::{hash::Hash, os::fd::AsFd, path::Path};
45

56
use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCK;
@@ -70,6 +71,10 @@ impl CgroupSock {
7071
/// Attaches the program to the given cgroup.
7172
///
7273
/// The returned value can be used to detach, see [CgroupSock::detach].
74+
///
75+
/// # Warning
76+
///
77+
/// On kernels 5.7.0 and later, attach modes other than CgroupAttachMode::default() are not passed to bpf_link_create. On older kernels that use bpf_prog_attach, the attach mode is still honored.
7378
pub fn attach<T: AsFd>(
7479
&mut self,
7580
cgroup: T,
@@ -80,17 +85,17 @@ impl CgroupSock {
8085
let cgroup_fd = cgroup.as_fd();
8186
let attach_type = self.data.expected_attach_type.unwrap();
8287
if KernelVersion::at_least(5, 7, 0) {
83-
let link_fd = bpf_link_create(
84-
prog_fd,
85-
LinkTarget::Fd(cgroup_fd),
86-
attach_type,
87-
mode.into(),
88-
None,
89-
)
90-
.map_err(|io_error| SyscallError {
91-
call: "bpf_link_create",
92-
io_error,
93-
})?;
88+
if mode != CgroupAttachMode::default() {
89+
warn!(
90+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
91+
mode
92+
);
93+
}
94+
let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(cgroup_fd), attach_type, 0, None)
95+
.map_err(|io_error| SyscallError {
96+
call: "bpf_link_create",
97+
io_error,
98+
})?;
9499
self.data
95100
.links
96101
.insert(CgroupSockLink::new(CgroupSockLinkInner::Fd(FdLink::new(

aya/src/programs/cgroup_sock_addr.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Cgroup socket address programs.
22
3+
use log::warn;
34
use std::{hash::Hash, os::fd::AsFd, path::Path};
45

56
use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
@@ -71,6 +72,11 @@ impl CgroupSockAddr {
7172
/// Attaches the program to the given cgroup.
7273
///
7374
/// The returned value can be used to detach, see [CgroupSockAddr::detach].
75+
///
76+
/// # Warning
77+
///
78+
/// On kernels 5.7.0 and later, attach modes other than `CgroupAttachMode::default()` are not passed to `bpf_link_create`.
79+
/// On older kernels (using `bpf_prog_attach`), the attach mode is honored.
7480
pub fn attach<T: AsFd>(
7581
&mut self,
7682
cgroup: T,
@@ -81,17 +87,17 @@ impl CgroupSockAddr {
8187
let cgroup_fd = cgroup.as_fd();
8288
let attach_type = self.data.expected_attach_type.unwrap();
8389
if KernelVersion::at_least(5, 7, 0) {
84-
let link_fd = bpf_link_create(
85-
prog_fd,
86-
LinkTarget::Fd(cgroup_fd),
87-
attach_type,
88-
mode.into(),
89-
None,
90-
)
91-
.map_err(|io_error| SyscallError {
92-
call: "bpf_link_create",
93-
io_error,
94-
})?;
90+
if mode != CgroupAttachMode::default() {
91+
warn!(
92+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
93+
mode
94+
);
95+
}
96+
let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(cgroup_fd), attach_type, 0, None)
97+
.map_err(|io_error| SyscallError {
98+
call: "bpf_link_create",
99+
io_error,
100+
})?;
95101
self.data
96102
.links
97103
.insert(CgroupSockAddrLink::new(CgroupSockAddrLinkInner::Fd(

aya/src/programs/cgroup_sockopt.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Cgroup socket option programs.
22
3+
use log::warn;
34
use std::{hash::Hash, os::fd::AsFd, path::Path};
45

56
use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCKOPT;
@@ -68,6 +69,11 @@ impl CgroupSockopt {
6869
/// Attaches the program to the given cgroup.
6970
///
7071
/// The returned value can be used to detach, see [CgroupSockopt::detach].
72+
///
73+
/// # Warning
74+
///
75+
/// On kernels 5.7.0 and later, attach modes other than CgroupAttachMode::default() are not passed to
76+
/// `bpf_link_create`. On older kernels, attach modes are honored.
7177
pub fn attach<T: AsFd>(
7278
&mut self,
7379
cgroup: T,
@@ -78,17 +84,17 @@ impl CgroupSockopt {
7884
let cgroup_fd = cgroup.as_fd();
7985
let attach_type = self.data.expected_attach_type.unwrap();
8086
if KernelVersion::at_least(5, 7, 0) {
81-
let link_fd = bpf_link_create(
82-
prog_fd,
83-
LinkTarget::Fd(cgroup_fd),
84-
attach_type,
85-
mode.into(),
86-
None,
87-
)
88-
.map_err(|io_error| SyscallError {
89-
call: "bpf_link_create",
90-
io_error,
91-
})?;
87+
if mode != CgroupAttachMode::default() {
88+
warn!(
89+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
90+
mode
91+
);
92+
}
93+
let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(cgroup_fd), attach_type, 0, None)
94+
.map_err(|io_error| SyscallError {
95+
call: "bpf_link_create",
96+
io_error,
97+
})?;
9298
self.data
9399
.links
94100
.insert(CgroupSockoptLink::new(CgroupSockoptLinkInner::Fd(

aya/src/programs/cgroup_sysctl.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Cgroup sysctl programs.
22
3+
use log::warn;
34
use std::{hash::Hash, os::fd::AsFd};
45

56
use aya_obj::generated::{
@@ -66,6 +67,10 @@ impl CgroupSysctl {
6667
/// Attaches the program to the given cgroup.
6768
///
6869
/// The returned value can be used to detach, see [CgroupSysctl::detach].
70+
///
71+
/// # Warning
72+
///
73+
/// On kernels 5.7.0 and later, attach modes other than CgroupAttachMode::default() are not passed to bpf_link_create.
6974
pub fn attach<T: AsFd>(
7075
&mut self,
7176
cgroup: T,
@@ -76,11 +81,17 @@ impl CgroupSysctl {
7681
let cgroup_fd = cgroup.as_fd();
7782

7883
if KernelVersion::at_least(5, 7, 0) {
84+
if mode != CgroupAttachMode::default() {
85+
warn!(
86+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
87+
mode
88+
);
89+
}
7990
let link_fd = bpf_link_create(
8091
prog_fd,
8192
LinkTarget::Fd(cgroup_fd),
8293
BPF_CGROUP_SYSCTL,
83-
mode.into(),
94+
0,
8495
None,
8596
)
8697
.map_err(|io_error| SyscallError {

aya/src/programs/links.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub trait Link: std::fmt::Debug + Eq + std::hash::Hash + 'static {
3838
}
3939

4040
/// Program attachment mode.
41-
#[derive(Clone, Copy, Debug, Default)]
41+
#[derive(Clone, Copy, Debug, Default, PartialEq)]
4242
pub enum CgroupAttachMode {
4343
/// Allows only one BPF program in the cgroup subtree.
4444
#[default]

aya/src/programs/sock_ops.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Socket option programs.
2+
use log::warn;
23
use std::os::fd::AsFd;
34

45
use aya_obj::generated::{
@@ -65,6 +66,11 @@ impl SockOps {
6566
/// Attaches the program to the given cgroup.
6667
///
6768
/// The returned value can be used to detach, see [SockOps::detach].
69+
///
70+
/// # Warning
71+
///
72+
/// On kernels 5.7.0 and later, attach modes other than `CgroupAttachMode::default()` are not passed to `bpf_link_create`.
73+
/// On older kernels (using `bpf_prog_attach`), the attach mode is honored.
6874
pub fn attach<T: AsFd>(
6975
&mut self,
7076
cgroup: T,
@@ -75,17 +81,17 @@ impl SockOps {
7581
let cgroup_fd = cgroup.as_fd();
7682
let attach_type = BPF_CGROUP_SOCK_OPS;
7783
if KernelVersion::at_least(5, 7, 0) {
78-
let link_fd = bpf_link_create(
79-
prog_fd,
80-
LinkTarget::Fd(cgroup_fd),
81-
attach_type,
82-
mode.into(),
83-
None,
84-
)
85-
.map_err(|io_error| SyscallError {
86-
call: "bpf_link_create",
87-
io_error,
88-
})?;
84+
if mode != CgroupAttachMode::default() {
85+
warn!(
86+
"CgroupAttachMode {:?} will not be passed on to bpf_link_create",
87+
mode
88+
);
89+
}
90+
let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(cgroup_fd), attach_type, 0, None)
91+
.map_err(|io_error| SyscallError {
92+
call: "bpf_link_create",
93+
io_error,
94+
})?;
8995
self.data
9096
.links
9197
.insert(SockOpsLink::new(SockOpsLinkInner::Fd(FdLink::new(link_fd))))

0 commit comments

Comments
 (0)