-
Notifications
You must be signed in to change notification settings - Fork 848
Add ErrNotPermitted error for permission denied in feature probes
#1949
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package btf | |
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "math" | ||
|
|
||
| "github.com/cilium/ebpf/internal" | ||
|
|
@@ -14,10 +15,14 @@ import ( | |
| var haveBTF = internal.NewFeatureTest("BTF", func() error { | ||
| // 0-length anonymous integer | ||
| err := probeBTF(&Int{}) | ||
| if errors.Is(err, unix.EINVAL) || errors.Is(err, unix.EPERM) { | ||
| switch { | ||
| case errors.Is(err, unix.EINVAL): | ||
| return internal.ErrNotSupported | ||
| case errors.Is(err, unix.EPERM): | ||
| return fmt.Errorf("%w: %w", internal.ErrNotPermitted, err) | ||
| default: | ||
| return err | ||
| } | ||
| return err | ||
| }, "4.18") | ||
|
|
||
| // haveMapBTF attempts to load a minimal BTF blob containing a Var. It is | ||
|
|
@@ -34,11 +39,12 @@ var haveMapBTF = internal.NewFeatureTest("Map BTF (Var/Datasec)", func() error { | |
| } | ||
|
|
||
| err := probeBTF(v) | ||
| if errors.Is(err, unix.EINVAL) || errors.Is(err, unix.EPERM) { | ||
| // Treat both EINVAL and EPERM as not supported: creating the map may still | ||
| // succeed without Btf* attrs. | ||
| if errors.Is(err, unix.EINVAL) { | ||
| return internal.ErrNotSupported | ||
| } | ||
| if errors.Is(err, unix.EPERM) { | ||
| return fmt.Errorf("%w: %w", internal.ErrNotPermitted, err) | ||
| } | ||
| return err | ||
| }, "5.2") | ||
|
|
||
|
|
@@ -56,9 +62,12 @@ var haveProgBTF = internal.NewFeatureTest("Program BTF (func/line_info)", func() | |
| } | ||
|
|
||
| err := probeBTF(fn) | ||
| if errors.Is(err, unix.EINVAL) || errors.Is(err, unix.EPERM) { | ||
| if errors.Is(err, unix.EINVAL) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, the style as mentioned in #1949 (comment) should also be applied here. |
||
| return internal.ErrNotSupported | ||
| } | ||
| if errors.Is(err, unix.EPERM) { | ||
| return fmt.Errorf("%w: %w", internal.ErrNotPermitted, err) | ||
| } | ||
| return err | ||
| }, "5.0") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package internal | |
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
|
|
@@ -88,3 +89,14 @@ func TestFeatureTestNotSupportedOnOS(t *testing.T) { | |
| qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "1.0")(), sentinel)) | ||
| } | ||
| } | ||
|
|
||
| func TestErrNotPermitted(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test feels like we are testing Go standard library behavior instead of our own code, so I don't see a lot of value in this test. |
||
| // ErrNotPermitted should be distinct from ErrNotSupported | ||
| qt.Assert(t, qt.Not(qt.ErrorIs(ErrNotPermitted, ErrNotSupported))) | ||
| qt.Assert(t, qt.Not(qt.ErrorIs(ErrNotSupported, ErrNotPermitted))) | ||
|
|
||
| // Wrapped errors should be matchable | ||
| wrapped := fmt.Errorf("%w: some details", ErrNotPermitted) | ||
| qt.Assert(t, qt.ErrorIs(wrapped, ErrNotPermitted)) | ||
| qt.Assert(t, qt.Not(qt.ErrorIs(wrapped, ErrNotSupported))) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ package link | |
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/cilium/ebpf" | ||
| "github.com/cilium/ebpf/asm" | ||
|
|
@@ -105,6 +106,11 @@ var haveProgQuery = internal.NewFeatureTest("BPF_PROG_QUERY", func() error { | |
| if errors.Is(err, unix.EBADF) { | ||
| return nil | ||
| } | ||
| // EPERM means the kernel recognized the syscall but denied permission. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, the style as mentioned in #1949 (comment) should also be applied here. |
||
| // The feature exists but is restricted (e.g., in a user namespace). | ||
| if errors.Is(err, unix.EPERM) { | ||
| return fmt.Errorf("%w: %w", ErrNotPermitted, err) | ||
| } | ||
| if err != nil { | ||
| return ErrNotSupported | ||
| } | ||
|
|
||
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.
For consistency, the style as mentioned in #1949 (comment) should also be applied here.