Skip to content

Commit

Permalink
[wip] combine kernel and kernel module cataloging
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
Signed-off-by: Avi Deitcher <avi@deitcher.net>
  • Loading branch information
wagoodman authored and deitch committed Mar 30, 2023
1 parent 787e8b6 commit 7f942f1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions syft/pkg/cataloger/kernel/parse_kernel_module_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ package kernel
import (
"debug/elf"
"fmt"
"github.com/anchore/syft/internal/log"
"regexp"
"strings"

"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/unionreader"
"github.com/anchore/syft/syft/source"
)

var (
kernelModulePathRE = regexp.MustCompile(`^(.*lib/modules/([^/]+)/)`)
)

func findKernelModules(resolver source.FileResolver, pkgLocations *source.LocationSet) ([]pkg.KernelModuleMetadata, error) {
locations, err := resolver.FilesByGlob("**/*.ko")
if err != nil {
return nil, err
}

// note: all collections in the metadata must be allocated
foundPaths := make(map[string]bool)
ret := make([]pkg.KernelModuleMetadata, 0)
for _, location := range locations {
contentReader, err := resolver.FileContentsByLocation(location)
Expand All @@ -35,9 +41,18 @@ func findKernelModules(resolver source.FileResolver, pkgLocations *source.Locati
if metadata == nil {
continue
}
pkgLocations.Add(location)
// get the base path
parts := kernelModulePathRE.FindStringSubmatch(location.RealPath)
if len(parts) > 1 {
basePath := parts[1]
if _, ok := foundPaths[basePath]; !ok {
foundPaths[basePath] = true
pkgLocations.Add(source.NewLocation(basePath))
}
}
ret = append(ret, *metadata)
}
// kernel modules have a common root, usually /lib/modules/<version>/kernel, so find the common base path
return ret, nil
}

Expand Down

0 comments on commit 7f942f1

Please sign in to comment.