diff --git a/features/map.go b/features/map.go index 4d418c468..b183bc28d 100644 --- a/features/map.go +++ b/features/map.go @@ -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 @@ -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 diff --git a/features/misc.go b/features/misc.go index 73d66d48c..d16db5fa0 100644 --- a/features/misc.go +++ b/features/misc.go @@ -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. @@ -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 diff --git a/features/prog.go b/features/prog.go index 2234ab172..4eaa64012 100644 --- a/features/prog.go +++ b/features/prog.go @@ -138,6 +138,9 @@ func haveProgramType(pt ebpf.ProgramType) error { } fd, err := sys.ProgLoad(attr) + if fd != nil { + fd.Close() + } switch { // EINVAL occurs when attempting to create a program with an unknown type. @@ -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 @@ -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 fd != 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 @@ -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)