Skip to content

Commit e075206

Browse files
authored
Merge pull request #4548 from cyphar/ebpf-enotsup
cgroups: ebpf: use link.Anchor to check for BPF_F_REPLACE support
2 parents fe371b9 + c0044c7 commit e075206

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

libcontainer/cgroups/devices/ebpf_linux.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ func haveBpfProgReplace() bool {
107107
},
108108
})
109109
if err != nil {
110-
logrus.Debugf("checking for BPF_F_REPLACE support: ebpf.NewProgram failed: %v", err)
110+
logrus.Warnf("checking for BPF_F_REPLACE support: ebpf.NewProgram failed: %v", err)
111111
return
112112
}
113113
defer prog.Close()
114114

115115
devnull, err := os.Open("/dev/null")
116116
if err != nil {
117-
logrus.Debugf("checking for BPF_F_REPLACE support: open dummy target fd: %v", err)
117+
logrus.Warnf("checking for BPF_F_REPLACE support: open dummy target fd: %v", err)
118118
return
119119
}
120120
defer devnull.Close()
@@ -123,20 +123,26 @@ func haveBpfProgReplace() bool {
123123
// BPF_CGROUP_DEVICE programs. If passing BPF_F_REPLACE gives us EINVAL
124124
// we know that the feature isn't present.
125125
err = link.RawAttachProgram(link.RawAttachProgramOptions{
126-
// We rely on this fd being checked after attachFlags.
126+
// We rely on this fd being checked after attachFlags in the kernel.
127127
Target: int(devnull.Fd()),
128-
// Attempt to "replace" bad fds with this program.
128+
// Attempt to "replace" our BPF program with itself. This will
129+
// always fail, but we should get -EINVAL if BPF_F_REPLACE is not
130+
// supported.
131+
Anchor: link.ReplaceProgram(prog),
129132
Program: prog,
130133
Attach: ebpf.AttachCGroupDevice,
131-
Flags: unix.BPF_F_ALLOW_MULTI | unix.BPF_F_REPLACE,
134+
Flags: unix.BPF_F_ALLOW_MULTI,
132135
})
133-
if errors.Is(err, unix.EINVAL) {
136+
if errors.Is(err, ebpf.ErrNotSupported) || errors.Is(err, unix.EINVAL) {
134137
// not supported
135138
return
136139
}
137-
// attach_flags test succeeded.
138140
if !errors.Is(err, unix.EBADF) {
139-
logrus.Debugf("checking for BPF_F_REPLACE: got unexpected (not EBADF or EINVAL) error: %v", err)
141+
// If we see any new errors here, it's possible that there is a
142+
// regression due to a cilium/ebpf update and the above EINVAL
143+
// checks are not working. So, be loud about it so someone notices
144+
// and we can get the issue fixed quicker.
145+
logrus.Warnf("checking for BPF_F_REPLACE: got unexpected (not EBADF or EINVAL) error: %v", err)
140146
}
141147
haveBpfProgReplaceBool = true
142148
})

0 commit comments

Comments
 (0)