fix(debian,ubuntu): collect running kernel source package (#1935)
This commit is contained in:
@@ -4,10 +4,13 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/future-architect/vuls/constant"
|
||||
)
|
||||
|
||||
// Packages is Map of Package
|
||||
@@ -282,3 +285,174 @@ func IsRaspbianPackage(name, version string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// RenameKernelSourcePackageName is change common kernel source package
|
||||
func RenameKernelSourcePackageName(family, name string) string {
|
||||
switch family {
|
||||
case constant.Debian, constant.Raspbian:
|
||||
return strings.NewReplacer("linux-signed", "linux", "linux-latest", "linux", "-amd64", "", "-arm64", "", "-i386", "").Replace(name)
|
||||
case constant.Ubuntu:
|
||||
return strings.NewReplacer("linux-signed", "linux", "linux-meta", "linux").Replace(name)
|
||||
default:
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
// IsKernelSourcePackage check whether the source package is a kernel package
|
||||
func IsKernelSourcePackage(family, name string) bool {
|
||||
switch family {
|
||||
case constant.Debian, constant.Raspbian:
|
||||
switch ss := strings.Split(RenameKernelSourcePackageName(family, name), "-"); len(ss) {
|
||||
case 1:
|
||||
return ss[0] == "linux"
|
||||
case 2:
|
||||
if ss[0] != "linux" {
|
||||
return false
|
||||
}
|
||||
switch ss[1] {
|
||||
case "grsec":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[1], 64)
|
||||
return err == nil
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
case constant.Ubuntu: // https://git.launchpad.net/ubuntu-cve-tracker/tree/scripts/cve_lib.py#n1219
|
||||
switch ss := strings.Split(RenameKernelSourcePackageName(family, name), "-"); len(ss) {
|
||||
case 1:
|
||||
return ss[0] == "linux"
|
||||
case 2:
|
||||
if ss[0] != "linux" {
|
||||
return false
|
||||
}
|
||||
switch ss[1] {
|
||||
case "armadaxp", "mako", "manta", "flo", "goldfish", "joule", "raspi", "raspi2", "snapdragon", "allwinner", "aws", "azure", "bluefield", "dell300x", "gcp", "gke", "gkeop", "ibm", "iot", "laptop", "lowlatency", "kvm", "nvidia", "oem", "oracle", "euclid", "hwe", "riscv", "starfive", "realtime", "mtk":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[1], 64)
|
||||
return err == nil
|
||||
}
|
||||
case 3:
|
||||
if ss[0] != "linux" {
|
||||
return false
|
||||
}
|
||||
switch ss[1] {
|
||||
case "ti":
|
||||
return ss[2] == "omap4"
|
||||
case "raspi", "raspi2", "allwinner", "gke", "gkeop", "ibm", "oracle", "riscv", "starfive":
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
case "aws":
|
||||
switch ss[2] {
|
||||
case "hwe", "edge":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
case "azure":
|
||||
switch ss[2] {
|
||||
case "cvm", "fde", "edge":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
case "gcp":
|
||||
switch ss[2] {
|
||||
case "edge":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
case "intel":
|
||||
switch ss[2] {
|
||||
case "iotg", "opt":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
case "oem":
|
||||
switch ss[2] {
|
||||
case "osp1":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
case "lts":
|
||||
switch ss[2] {
|
||||
case "utopic", "vivid", "wily", "xenial":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
case "hwe":
|
||||
switch ss[2] {
|
||||
case "edge":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
case "xilinx":
|
||||
return ss[2] == "zynqmp"
|
||||
case "nvidia":
|
||||
switch ss[2] {
|
||||
case "tegra":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[2], 64)
|
||||
return err == nil
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
case 4:
|
||||
if ss[0] != "linux" {
|
||||
return false
|
||||
}
|
||||
switch ss[1] {
|
||||
case "azure":
|
||||
if ss[2] != "fde" {
|
||||
return false
|
||||
}
|
||||
_, err := strconv.ParseFloat(ss[3], 64)
|
||||
return err == nil
|
||||
case "intel":
|
||||
if ss[2] != "iotg" {
|
||||
return false
|
||||
}
|
||||
_, err := strconv.ParseFloat(ss[3], 64)
|
||||
return err == nil
|
||||
case "lowlatency":
|
||||
if ss[2] != "hwe" {
|
||||
return false
|
||||
}
|
||||
_, err := strconv.ParseFloat(ss[3], 64)
|
||||
return err == nil
|
||||
case "nvidia":
|
||||
if ss[2] != "tegra" {
|
||||
return false
|
||||
}
|
||||
switch ss[3] {
|
||||
case "igx":
|
||||
return true
|
||||
default:
|
||||
_, err := strconv.ParseFloat(ss[3], 64)
|
||||
return err == nil
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/k0kubun/pp"
|
||||
|
||||
"github.com/future-architect/vuls/constant"
|
||||
)
|
||||
|
||||
func TestMergeNewVersion(t *testing.T) {
|
||||
@@ -428,3 +430,163 @@ func Test_NewPortStat(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameKernelSourcePackageName(t *testing.T) {
|
||||
type args struct {
|
||||
family string
|
||||
name string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "debian linux-signed -> linux",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "linux-signed",
|
||||
},
|
||||
want: "linux",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := RenameKernelSourcePackageName(tt.args.family, tt.args.name); got != tt.want {
|
||||
t.Errorf("RenameKernelSourcePackageName() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsKernelSourcePackage(t *testing.T) {
|
||||
type args struct {
|
||||
family string
|
||||
name string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "debian apt",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "apt",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "debian linux",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "linux",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "debian linux",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "linux",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "debian linux-5.10",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "linux-5.10",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "debian linux-grsec",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "linux-grsec",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "debian linux-base",
|
||||
args: args{
|
||||
family: constant.Debian,
|
||||
name: "linux-base",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "ubuntu apt",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "apt",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux-aws",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux-aws",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux-5.9",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux-5.9",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux-base",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux-base",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux-aws-edge",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux-aws-edge",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux-aws-5.15",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux-aws-5.15",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "ubuntu linux-lowlatency-hwe-5.15",
|
||||
args: args{
|
||||
family: constant.Ubuntu,
|
||||
name: "linux-lowlatency-hwe-5.15",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsKernelSourcePackage(tt.args.family, tt.args.name); got != tt.want {
|
||||
t.Errorf("IsKernelSourcePackage() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user