Skip to content

Commit cfd8515

Browse files
committed
features: tolerate EPERM in haveObjName and objNameAllowsDot
When loading ELFs using unprivileged tools or tests, EPERM causes dots and underscores to be stripped from MapSpec.Name, causing strange results for maps like .rodata.foo. Also, only consider ErrNotSupported as conclusive and bubble up any other errors to the caller. Most callers just check err != nil, but in case someone indirectly relies on the sentinel, this should result in more accurate output. Signed-off-by: Timo Beckers <timo@isovalent.com>
1 parent ddcaa8c commit cfd8515

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

syscalls.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,18 @@ var haveObjName = internal.NewFeatureTest("object names", func() error {
179179
MapName: sys.NewObjName("feature_test"),
180180
}
181181

182+
// Tolerate EPERM as this runs during ELF loading which is potentially
183+
// unprivileged. Only EINVAL is conclusive, thrown from CHECK_ATTR.
182184
fd, err := sys.MapCreate(&attr)
183-
if err != nil {
185+
if errors.Is(err, unix.EPERM) {
186+
return nil
187+
}
188+
if errors.Is(err, unix.EINVAL) {
184189
return internal.ErrNotSupported
185190
}
191+
if err != nil {
192+
return err
193+
}
186194

187195
_ = fd.Close()
188196
return nil
@@ -201,10 +209,19 @@ var objNameAllowsDot = internal.NewFeatureTest("dot in object names", func() err
201209
MapName: sys.NewObjName(".test"),
202210
}
203211

212+
// Tolerate EPERM, otherwise MapSpec.Name has its dots removed when run by
213+
// unprivileged tools. (bpf2go, other code gen). Only EINVAL is conclusive,
214+
// thrown from bpf_obj_name_cpy().
204215
fd, err := sys.MapCreate(&attr)
205-
if err != nil {
216+
if errors.Is(err, unix.EPERM) {
217+
return nil
218+
}
219+
if errors.Is(err, unix.EINVAL) {
206220
return internal.ErrNotSupported
207221
}
222+
if err != nil {
223+
return err
224+
}
208225

209226
_ = fd.Close()
210227
return nil

0 commit comments

Comments
 (0)