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 c33e142
Show file tree
Hide file tree
Showing 2 changed files with 35 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
18 changes: 18 additions & 0 deletions syft/pkg/kernel_package_metadata.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package pkg

import (
"sort"

"github.com/scylladb/go-set/strset"
)

// KernelPackageMetadata represents all captured data for a Linux kernel
type KernelPackageMetadata struct {
Name string `mapstructure:"name" json:"name"`
Expand All @@ -16,6 +22,18 @@ type KernelPackageMetadata struct {
Modules []KernelModuleMetadata `mapstructure:"modules" json:"modules"`
}

func (m KernelPackageMetadata) OwnedFiles() (result []string) {
s := strset.New()
for _, m := range m.Modules {
if m.Path != "" {
s.Add(m.Path)
}
}
result = s.List()
sort.Strings(result)
return
}

type KernelModuleMetadata struct {
KernelVersion string `mapstructure:"kernelVersion" json:"kernelVersion,omitempty"`
VersionMagic string `mapstructure:"versionMagic" json:"versionMagic,omitempty"`
Expand Down

0 comments on commit c33e142

Please sign in to comment.