Skip to content

Commit

Permalink
features: wrap EPERM errors from all probe APIs
Browse files Browse the repository at this point in the history
In an attempt to simplify the switch blocks in all types of feature
probes, lift the arbitrary EPERM wrapping exception.

Also, move the call to fd.Close() out of the switch statement to live
closer to the call that returns the fd. The fd can always be immediately
closed.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Jul 29, 2022
1 parent 39ee31d commit 05d253d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
10 changes: 3 additions & 7 deletions features/map.go
Expand Up @@ -130,6 +130,9 @@ func haveMapType(mt ebpf.MapType) error {
}

fd, err := sys.MapCreate(createMapTypeAttr(mt))
if err == nil {
fd.Close()
}

switch {
// For nested and storage map types we accept EBADF as indicator that these maps are supported
Expand All @@ -145,16 +148,9 @@ func haveMapType(mt ebpf.MapType) error {
case errors.Is(err, unix.EINVAL), errors.Is(err, unix.E2BIG):
err = fmt.Errorf("%w", ebpf.ErrNotSupported)

// EPERM is kept as-is and is not converted or wrapped.
case errors.Is(err, unix.EPERM):
break

// Wrap unexpected errors.
case err != nil:
err = fmt.Errorf("unexpected error during feature probe: %w", err)

default:
fd.Close()
}

mc.mapTypes[mt] = err
Expand Down
10 changes: 3 additions & 7 deletions features/misc.go
Expand Up @@ -92,6 +92,9 @@ func probeMisc(mt miscType) error {
}

fd, err := sys.ProgLoad(attr)
if err == nil {
fd.Close()
}

switch {
// EINVAL occurs when attempting to create a program with an unknown type.
Expand All @@ -101,16 +104,9 @@ func probeMisc(mt miscType) error {
case errors.Is(err, unix.EINVAL), errors.Is(err, unix.E2BIG):
err = fmt.Errorf("%w", ebpf.ErrNotSupported)

// EPERM is kept as-is and is not converted or wrapped.
case errors.Is(err, unix.EPERM):
break

// Wrap unexpected errors.
case err != nil:
err = fmt.Errorf("unexpected error during feature probe: %w", err)

default:
fd.Close()
}

miscs.miscTypes[mt] = err
Expand Down
21 changes: 6 additions & 15 deletions features/prog.go
Expand Up @@ -138,6 +138,9 @@ func haveProgramType(pt ebpf.ProgramType) error {
}

fd, err := sys.ProgLoad(attr)
if err == nil {
fd.Close()
}

switch {
// EINVAL occurs when attempting to create a program with an unknown type.
Expand All @@ -147,16 +150,9 @@ func haveProgramType(pt ebpf.ProgramType) error {
case errors.Is(err, unix.EINVAL), errors.Is(err, unix.E2BIG):
err = fmt.Errorf("%w", ebpf.ErrNotSupported)

// EPERM is kept as-is and is not converted or wrapped.
case errors.Is(err, unix.EPERM):
break

// Wrap unexpected errors.
case err != nil:
err = fmt.Errorf("unexpected error during feature probe: %w", err)

default:
fd.Close()
}

pc.types[pt] = err
Expand Down Expand Up @@ -210,12 +206,11 @@ func haveProgramHelper(pt ebpf.ProgramType, helper asm.BuiltinFunc) error {
}

fd, err := sys.ProgLoad(attr)

switch {
// If there is no error we need to close the FD of the prog.
case err == nil:
if err == nil {
fd.Close()
}

switch {
// EACCES occurs when attempting to create a program probe with a helper
// while the register args when calling this helper aren't set up properly.
// We interpret this as the helper being available, because the verifier
Expand All @@ -232,10 +227,6 @@ func haveProgramHelper(pt ebpf.ProgramType, helper asm.BuiltinFunc) error {
// TODO: possibly we need to check verifier output here to be sure
err = fmt.Errorf("%w", ebpf.ErrNotSupported)

// EPERM is kept as-is and is not converted or wrapped.
case errors.Is(err, unix.EPERM):
break

// Wrap unexpected errors.
case err != nil:
err = fmt.Errorf("unexpected error during feature probe: %w", err)
Expand Down

0 comments on commit 05d253d

Please sign in to comment.