From 05d253de29b6f1fcbecc52014f716269d624916a Mon Sep 17 00:00:00 2001 From: Timo Beckers Date: Wed, 27 Jul 2022 16:14:34 +0200 Subject: [PATCH] features: wrap EPERM errors from all probe APIs 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 --- features/map.go | 10 +++------- features/misc.go | 10 +++------- features/prog.go | 21 ++++++--------------- 3 files changed, 12 insertions(+), 29 deletions(-) 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..f798a5eff 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 err == 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 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 @@ -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)