-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Avi Deitcher <avi@deitcher.net>
- Loading branch information
Showing
12 changed files
with
244 additions
and
0 deletions.
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Package kernel provides a concrete Cataloger implementation for linux kernel and module files. | ||
*/ | ||
package kernel | ||
|
||
import ( | ||
"github.com/anchore/syft/syft/pkg/cataloger/generic" | ||
) | ||
|
||
type KernelCatalogerOpts struct { | ||
FilenameAppends []string | ||
} | ||
|
||
var kernelFiles = []string{ | ||
"kernel", | ||
"kernel-*", | ||
"vmlinux", | ||
"vmlinux-*", | ||
"vmlinuz", | ||
"vmlinuz-*", | ||
} | ||
|
||
// NewKernelCataloger returns a new kernel files cataloger object. | ||
func NewKernelCataloger(opts KernelCatalogerOpts) *generic.Cataloger { | ||
var fileList []string | ||
for _, file := range kernelFiles { | ||
fileList = append(fileList, "**/"+file) | ||
} | ||
for _, file := range opts.FilenameAppends { | ||
fileList = append(fileList, "**/"+file) | ||
} | ||
return generic.NewCataloger("linux-kernel-cataloger"). | ||
WithParserByGlobs(parseKernelFile, fileList...) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kernel | ||
|
||
const ( | ||
linuxKernelName = "Linux kernel" | ||
linuxKernelVersionPrefix = "version " | ||
packageName = "linux-kernel" | ||
) |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package kernel | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/anchore/packageurl-go" | ||
) | ||
|
||
// packageURL returns the PURL for the specific Kernel package (see https://github.com/package-url/purl-spec) | ||
func packageURL(name, version string) string { | ||
var namespace string | ||
|
||
fields := strings.SplitN(name, "/", 2) | ||
if len(fields) > 1 { | ||
namespace = fields[0] | ||
name = fields[1] | ||
} | ||
|
||
return packageurl.NewPackageURL( | ||
packageName, | ||
namespace, | ||
name, | ||
version, | ||
nil, | ||
"", | ||
).ToString() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package kernel | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/anchore/syft/internal/log" | ||
"github.com/anchore/syft/syft/artifact" | ||
"github.com/anchore/syft/syft/pkg" | ||
"github.com/anchore/syft/syft/pkg/cataloger/generic" | ||
"github.com/anchore/syft/syft/pkg/cataloger/internal/unionreader" | ||
"github.com/anchore/syft/syft/source" | ||
"github.com/deitch/magic/pkg/magic" | ||
) | ||
|
||
func parseKernelFile(resolver source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) { | ||
unionReader, err := unionreader.GetUnionReader(reader) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to get union reader for file: %w", err) | ||
} | ||
magicType, err := magic.GetType(unionReader) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to get magic type for file: %w", err) | ||
} | ||
if len(magicType) < 1 || magicType[0] != linuxKernelName { | ||
return nil, nil, nil | ||
} | ||
metadata := parseKernelMetadata(magicType) | ||
if metadata.Version == "" { | ||
return nil, nil, nil | ||
} | ||
p := pkg.Package{ | ||
Name: packageName, | ||
Version: metadata.ExtendedVersion, | ||
PURL: packageURL(packageName, metadata.Version), | ||
Type: pkg.KernelPkg, | ||
MetadataType: pkg.KernelPackageMetadataType, | ||
Metadata: metadata, | ||
} | ||
|
||
p.SetID() | ||
return []pkg.Package{p}, nil, nil | ||
} | ||
|
||
func parseKernelMetadata(magicType []string) (p pkg.KernelPackageMetadata) { | ||
// Linux kernel x86 boot executable bzImage, | ||
// version 5.10.121-linuxkit (root@buildkitsandbox) #1 SMP Fri Dec 2 10:35:42 UTC 2022, | ||
// RO-rootFS, | ||
// swap_dev 0XA, | ||
// Normal VGA | ||
for _, t := range magicType { | ||
switch { | ||
case strings.HasPrefix(t, "x86 "): | ||
p.Architecture = "x86" | ||
case strings.Contains(t, "ARM64 "): | ||
p.Architecture = "arm64" | ||
case strings.Contains(t, "ARM "): | ||
p.Architecture = "arm" | ||
case t == "bzImage": | ||
p.Format = "bzImage" | ||
case t == "zImage": | ||
p.Format = "zImage" | ||
case strings.HasPrefix(t, "version "): | ||
p.ExtendedVersion = strings.TrimPrefix(t, "version ") | ||
fields := strings.Fields(p.ExtendedVersion) | ||
if len(fields) > 0 { | ||
p.Version = fields[0] | ||
} | ||
case strings.Contains(t, "rootFS") && strings.HasPrefix(t, "RW-"): | ||
p.RWRootFS = true | ||
case strings.HasPrefix(t, "swap_dev "): | ||
swapDevStr := strings.TrimPrefix(t, "swap_dev ") | ||
swapDev, err := strconv.ParseInt(swapDevStr, 16, 32) | ||
if err != nil { | ||
log.Warnf("unable to parse swap device: %s", err) | ||
continue | ||
} | ||
p.SwapDevice = int(swapDev) | ||
case strings.HasPrefix(t, "root_dev "): | ||
rootDevStr := strings.TrimPrefix(t, "root_dev ") | ||
rootDev, err := strconv.ParseInt(rootDevStr, 16, 32) | ||
if err != nil { | ||
log.Warnf("unable to parse root device: %s", err) | ||
continue | ||
} | ||
p.SwapDevice = int(rootDev) | ||
case strings.Contains(t, "VGA") || strings.Contains(t, "Video"): | ||
p.VideoMode = t | ||
} | ||
} | ||
return | ||
} | ||
|
||
// PURL: mustPURL("pkg:generic/linux-kernel@version"), | ||
// CPEs: singleCPE("cpe:2.3:a:linux-kernel:kernel:*:*:*:*:*:*:*:*"), |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package pkg | ||
|
||
// KernelPackageMetadata represents all captured data for a Linux kernel | ||
type KernelPackageMetadata struct { | ||
Name string `mapstructure:"name" json:"name"` | ||
Architecture string `mapstructure:"architecture" json:"architecture"` | ||
Version string `mapstructure:"version" json:"version"` | ||
ExtendedVersion string `mapstructure:"extendedVersion" json:"extendedVersion,omitempty"` | ||
BuildTime string `mapstructure:"buildTime" json:"buildTime,omitempty"` | ||
Author string `mapstructure:"author" json:"author,omitempty"` | ||
Format string `mapstructure:"format" json:"format,omitempty"` | ||
RWRootFS bool `mapstructure:"rwRootFS" json:"rwRootFS,omitempty"` | ||
SwapDevice int `mapstructure:"swapDevice" json:"swapDevice,omitempty"` | ||
RootDevice int `mapstructure:"rootDevice" json:"rootDevice,omitempty"` | ||
VideoMode string `mapstructure:"videoMode" json:"videoMode,omitempty"` | ||
} |
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
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