Skip to content

Commit

Permalink
chore(pkg/driver): use exec.LookPath instead of `exec.Command("hash…
Browse files Browse the repository at this point in the history
…", ...)`.

Moreover, bumped driverkit to latest HEAD of PR324.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed Mar 14, 2024
1 parent 62c3f83 commit 04b52d5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -9,7 +9,7 @@ require (
github.com/distribution/distribution/v3 v3.0.0-alpha.1
github.com/docker/cli v25.0.4+incompatible
github.com/docker/docker v25.0.4+incompatible
github.com/falcosecurity/driverkit v0.17.2-0.20240314074352-bddc7a84f884
github.com/falcosecurity/driverkit v0.17.2-0.20240314101310-98d1211a8587
github.com/go-oauth2/oauth2/v4 v4.5.2
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/go-containerregistry v0.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -276,8 +276,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/falcosecurity/driverkit v0.17.2-0.20240314074352-bddc7a84f884 h1:bmKyfrNaME6Cb1BIgE6awEHMnpGV8zRtbQI0ULzJpNk=
github.com/falcosecurity/driverkit v0.17.2-0.20240314074352-bddc7a84f884/go.mod h1:uVYeLnap5AXPaY9aDewzV4MAZMTHo2FECYnnMJ+i8sw=
github.com/falcosecurity/driverkit v0.17.2-0.20240314101310-98d1211a8587 h1:BVTrWkYQAehP5y5YOKC/XjSOpbhpDItLqXXo1JTylfc=
github.com/falcosecurity/driverkit v0.17.2-0.20240314101310-98d1211a8587/go.mod h1:uVYeLnap5AXPaY9aDewzV4MAZMTHo2FECYnnMJ+i8sw=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
Expand Down
26 changes: 9 additions & 17 deletions pkg/driver/type/kmod.go
Expand Up @@ -38,14 +38,6 @@ const (
rmmodWaitTime = 5 * time.Second
)

type errMissingDep struct {
program string
}

func (e *errMissingDep) Error() string {
return fmt.Sprintf("This program requires %s.", e.program)
}

func init() {
driverTypes[TypeKmod] = &kmod{}
}
Expand All @@ -61,26 +53,26 @@ func (k *kmod) String() string {
// Then, using dkms, it tries to fetch all
// dkms-installed versions of the module to clean them up.
func (k *kmod) Cleanup(printer *output.Printer, driverName string) error {
_, err := exec.Command("bash", "-c", "hash lsmod").Output()
lsmod, err := exec.LookPath("lsmod")
if err != nil {
return &errMissingDep{program: "lsmod"}
return err
}
_, err = exec.Command("bash", "-c", "hash rmmod").Output()
rmmod, err := exec.LookPath("rmmod")
if err != nil {
return &errMissingDep{program: "rmmod"}
return err
}

kmodName := strings.ReplaceAll(driverName, "-", "_")
printer.Logger.Info("Check if kernel module is still loaded.")
lsmodCmdArgs := fmt.Sprintf(`lsmod | cut -d' ' -f1 | grep -qx %q`, kmodName)
lsmodCmdArgs := fmt.Sprintf(`%s | cut -d' ' -f1 | grep -qx %q`, lsmod, kmodName)
_, err = exec.Command("bash", "-c", lsmodCmdArgs).Output() //nolint:gosec // false positive
if err == nil {
unloaded := false
// Module is still loaded, try to remove it
for i := 0; i < maxRmmodWait; i++ {
printer.Logger.Info("Kernel module is still loaded.")
printer.Logger.Info("Trying to unload it with 'rmmod'.")
if _, err = exec.Command("rmmod", kmodName).Output(); err == nil { //nolint:gosec // false positive
if _, err = exec.Command(rmmod, kmodName).Output(); err == nil {

Check failure on line 75 in pkg/driver/type/kmod.go

View workflow job for this annotation

GitHub Actions / Lint golang files

G204: Subprocess launched with variable (gosec)
printer.Logger.Info("OK! Unloading module succeeded.")
unloaded = true
break
Expand All @@ -97,14 +89,14 @@ func (k *kmod) Cleanup(printer *output.Printer, driverName string) error {
printer.Logger.Info("OK! There is no module loaded.")
}

_, err = exec.Command("bash", "-c", "hash dkms").Output()
dkms, err := exec.LookPath("dkms")
if err != nil {
printer.Logger.Info("Skipping dkms remove (dkms not found).")
return nil
}

printer.Logger.Info("Check all versions of kernel module in dkms.")
dkmsLsCmdArgs := fmt.Sprintf(`dkms status -m %q | tr -d "," | tr -d ":" | tr "/" " " | cut -d' ' -f2`, kmodName)
dkmsLsCmdArgs := fmt.Sprintf(`%s status -m %q | tr -d "," | tr -d ":" | tr "/" " " | cut -d' ' -f2`, dkms, kmodName)
out, err := exec.Command("bash", "-c", dkmsLsCmdArgs).Output() //nolint:gosec // false positive
if err != nil {
printer.Logger.Warn("Listing kernel module versions failed.", printer.Logger.Args("reason", err))
Expand All @@ -118,7 +110,7 @@ func (k *kmod) Cleanup(printer *output.Printer, driverName string) error {
scanner := bufio.NewScanner(outBuffer)
for scanner.Scan() {
dVer := scanner.Text()
dkmsRmCmdArgs := fmt.Sprintf(`dkms remove -m %s -v %q --all`, kmodName, dVer)
dkmsRmCmdArgs := fmt.Sprintf(`%s remove -m %s -v %q --all`, dkms, kmodName, dVer)
_, err = exec.Command("bash", "-c", dkmsRmCmdArgs).Output() //nolint:gosec // false positive
if err == nil {
printer.Logger.Info("OK! Removing succeeded.", printer.Logger.Args("version", dVer))
Expand Down

0 comments on commit 04b52d5

Please sign in to comment.