-
Notifications
You must be signed in to change notification settings - Fork 60
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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{} | ||
} | ||
|
@@ -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") | ||
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 GitHub Actions / build (darwin, arm64)
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of calling |
||
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() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.