Skip to content

Add ErrNotPermitted error for permission denied in feature probes #1949

Open
javiercardona-work wants to merge 2 commits intocilium:mainfrom
javiercardona-work:pr2
Open

Add ErrNotPermitted error for permission denied in feature probes #1949
javiercardona-work wants to merge 2 commits intocilium:mainfrom
javiercardona-work:pr2

Conversation

@javiercardona-work
Copy link

Add ErrNotPermitted to distinguish permission denied (EPERM) from feature not supported (EINVAL) in feature probes.

Problem

Previously, feature probes treated EPERM and EINVAL identically, returning ErrNotSupported for both. This was imprecise:

  • EINVAL: kernel doesn't recognize the request (feature not supported)
  • EPERM: kernel recognized the request but denied permission (feature exists)

Solution

Feature probes now return:

  • nil: feature is supported and accessible
  • ErrNotSupported: kernel doesn't have the feature
  • ErrNotPermitted: feature exists but permission denied

ErrNotPermitted wraps the original EPERM error so callers can unwrap if needed.

Usage

err := features.HaveProgramType(ebpf.XDP)                                                                                                                                                                                                  
switch {                                                                                                                                                                                                                                   
case err == nil:                                                                                                                                                                                                                           
    // feature works                                                                                                                                                                                                                       
case errors.Is(err, ebpf.ErrNotSupported):                                                                                                                                                                                                 
    // kernel doesn't have it                                                                                                                                                                                                              
case errors.Is(err, ebpf.ErrNotPermitted):                                                                                                                                                                                                 
    // permission denied (but feature exists)                                                                                                                                                                                              
}                                                                                                                                                                                                                                          

Breaking Change

Code checking only for ErrNotSupported will no longer catch permission errors. To maintain previous behavior:

  // Before:                                                                                                                                                                                                                                 
  if errors.Is(err, ebpf.ErrNotSupported) { ... }                                                                                                                                                                                            
                                                                                                                                                                                                                                             
  // After:                                                                                                                                                                                                                                  
  if errors.Is(err, ebpf.ErrNotSupported) || errors.Is(err, ebpf.ErrNotPermitted) { ... }                                                                                                                                                    
                                                                                                                                                                                                                                             
  // Or simply:                                                                                                                                                                                                                              
  if err != nil { ... }                                                           

Add ErrNotPermitted error to distinguish permission denied (EPERM)
from feature not supported (EINVAL) in feature probes.

Previously, EPERM and EINVAL were both mapped to ErrNotSupported.
This was imprecise: EPERM means the kernel recognized the request
but denied permission, indicating the feature exists. EINVAL means
the kernel doesn't recognize the request.

Now feature probes return:
- nil: feature is supported and accessible
- ErrNotSupported: kernel doesn't have the feature
- ErrNotPermitted: feature exists but permission denied

This gives callers richer information for diagnostics and error
messages while maintaining backward compatibility (callers can
still check err != nil for "can't use this feature").

ErrNotPermitted wraps the original EPERM error so callers can
unwrap if needed.

BREAKING CHANGE: Code that checks for ErrNotSupported to handle
"feature unavailable" cases will no longer catch permission errors.

To maintain previous behavior, update code as follows:

Before:
    if errors.Is(err, ebpf.ErrNotSupported) {
        // handle unavailable feature
    }

After:
    if errors.Is(err, ebpf.ErrNotSupported) || errors.Is(err, ebpf.ErrNotPermitted) {
        // handle unavailable feature
    }

Or simply check for any error:
    if err != nil {
        // handle unavailable feature
    }

Signed-off-by: Javier Cardona <jcardona@meta.com>
Add TestErrNotPermitted to verify:
- ErrNotPermitted is distinct from ErrNotSupported
- Wrapped errors can be matched with errors.Is()

Update DocDetectXDP example to show how to handle
ErrNotPermitted in addition to ErrNotSupported.

Signed-off-by: Javier Cardona <jcardona@meta.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant