Skip to content

Commit

Permalink
Merge pull request #8721 from code0x9/b-kernel-builtin-module
Browse files Browse the repository at this point in the history
client/fingerprint: lookup kernel builtin bridge modules
  • Loading branch information
shoenig authored Aug 24, 2020
2 parents e065952 + 893d4d1 commit 1bb8f19
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions client/fingerprint/bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"

"github.com/hashicorp/nomad/nomad/structs"
"github.com/shirou/gopsutil/host"
)

const bridgeKernelModuleName = "bridge"
Expand Down Expand Up @@ -35,19 +36,39 @@ func (f *BridgeFingerprint) Fingerprint(req *FingerprintRequest, resp *Fingerpri
}

func (f *BridgeFingerprint) checkKMod(mod string) error {
file, err := os.Open("/proc/modules")
hostInfo, err := host.Info()
if err != nil {
return fmt.Errorf("could not read /proc/modules: %v", err)
return err
}

dynErr := f.checkKModFile(mod, "/proc/modules", fmt.Sprintf("%s\\s+.*$", mod))
if dynErr == nil {
return nil
}

builtinErr := f.checkKModFile(mod,
fmt.Sprintf("/lib/modules/%s/modules.builtin", hostInfo.KernelVersion),
fmt.Sprintf(".+\\/%s.ko$", mod))
if builtinErr == nil {
return nil
}

return fmt.Errorf("%v, %v", dynErr, builtinErr)
}

func (f *BridgeFingerprint) checkKModFile(mod, fileName, pattern string) error {
file, err := os.Open(fileName)
if err != nil {
return fmt.Errorf("could not read %s: %v", fileName, err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
pattern := fmt.Sprintf("%s\\s+.*$", mod)
for scanner.Scan() {
if matched, err := regexp.MatchString(pattern, scanner.Text()); matched {
return nil
} else if err != nil {
return fmt.Errorf("could not parse /proc/modules: %v", err)
return fmt.Errorf("could not parse %s: %v", fileName, err)
}
}

Expand Down

0 comments on commit 1bb8f19

Please sign in to comment.