libnetwork/pasta: fix possible panic in error cast

exec.Command().Output() can fail for several errors, i.e. maybe it was
not able to execute at all. We only get a error from type
`exec.ExitError` when the command exited > 0. For all other cases this
cast was unsafe and would cause a panic with a nil deref.

First check the error type correctly with errors.As() then return the
stderr and exit code. Otherwise just return the wrapped error as is.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2023-06-14 15:18:13 +02:00
parent 7f9a0dd858
commit 43befeae4d
1 changed files with 7 additions and 2 deletions

View File

@ -11,6 +11,7 @@
package pasta
import (
"errors"
"fmt"
"os/exec"
"strings"
@ -124,8 +125,12 @@ func Setup(opts *SetupOptions) error {
// pasta forks once ready, and quits once we delete the target namespace
_, err = exec.Command(path, cmdArgs...).Output()
if err != nil {
return fmt.Errorf("failed to start pasta:\n%s",
err.(*exec.ExitError).Stderr)
exitErr := &exec.ExitError{}
if errors.As(err, &exitErr) {
return fmt.Errorf("pasta failed with exit code %d:\n%s",
exitErr.ExitCode(), exitErr.Stderr)
}
return fmt.Errorf("failed to start pasta: %w", err)
}
return nil