Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(pkg/driver): use unix where available. #353

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions pkg/driver/type/kmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"golang.org/x/net/context"
"golang.org/x/sys/unix"

"github.com/falcosecurity/falcoctl/pkg/output"
)
Expand All @@ -38,14 +39,6 @@
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,40 +54,45 @@
// 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()
if err != nil {
return &errMissingDep{program: "lsmod"}
}
_, err = exec.Command("bash", "-c", "hash rmmod").Output()
if err != nil {
return &errMissingDep{program: "rmmod"}
}

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)
_, err = exec.Command("bash", "-c", lsmodCmdArgs).Output() //nolint:gosec // false positive
f, err := os.Open("/proc/modules")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using lsmod, manually scan /proc/modules.

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
printer.Logger.Info("OK! Unloading module succeeded.")
unloaded = true
defer f.Close()
scanner := bufio.NewScanner(f)
found := false
// optionally, resize scanner's capacity for lines over 64K, see next example
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) > 0 && fields[0] == kmodName {
found = true
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 = unix.DeleteModule(kmodName, 0); err == nil {

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

View workflow job for this annotation

GitHub Actions / build (darwin, arm64)

undefined: unix.DeleteModule

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

View workflow job for this annotation

GitHub Actions / build (darwin, amd64)

undefined: unix.DeleteModule
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling rmmod directly, just use unit.DeleteModule.

printer.Logger.Info("OK! Unloading module succeeded.")
unloaded = true
break
}
printer.Logger.Info("Nothing to do...'falcoctl' will wait until you remove the kernel module to have a clean termination.")
printer.Logger.Info("Check that no process is using the kernel module with 'lsmod'.")
printer.Logger.Info("Sleep 5 seconds...")
time.Sleep(rmmodWaitTime)
}
if !unloaded {
printer.Logger.Warn("Kernel module is still loaded, you could have incompatibility issues.")
}
break
}
printer.Logger.Info("Nothing to do...'falcoctl' will wait until you remove the kernel module to have a clean termination.")
printer.Logger.Info("Check that no process is using the kernel module with 'lsmod'.")
printer.Logger.Info("Sleep 5 seconds...")
time.Sleep(rmmodWaitTime)
}
if !unloaded {
printer.Logger.Warn("Kernel module is still loaded, you could have incompatibility issues.")
if !found {
printer.Logger.Info("OK! There is no module loaded.")
}
} else {
printer.Logger.Info("OK! There is no module loaded.")
printer.Logger.Warn("Failed to parse /proc/modules.", printer.Logger.Args("err", err))
}

_, err = exec.Command("bash", "-c", "hash dkms").Output()
Expand Down
Loading