From 73f5df24251342553d4f131a35be360c2e1987a0 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 18 May 2023 18:48:09 +0000 Subject: [PATCH] feat!: new kernel config structure BREAKING CHANGE: kernel definition in the configuration files has switched from [[targets]] distro = { id = "Ubuntu", release = "18.04" } release_mask = ".*" to [[targets]] distro = { id = "Ubuntu", release = "18.04" } kernel = { regex = ".*" } --- .github/workflows/ubuntu.yml | 2 +- config/config.go | 112 +++------------------- config/config_test.go | 48 ++-------- distro/debian/debian.go | 2 +- distro/debian/debian_test.go | 4 +- distro/oraclelinux/oraclelinux.go | 4 +- distro/ubuntu/ubuntu.go | 4 +- examples/kernel-exploit/.out-of-tree.toml | 15 +-- examples/kernel-module/.out-of-tree.toml | 8 +- examples/preload/.out-of-tree.toml | 2 +- examples/script/.out-of-tree.toml | 2 +- gen.go | 8 +- kernel.go | 12 +-- pew.go | 9 +- preload.go | 4 +- 15 files changed, 61 insertions(+), 175 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index e5f00c9..33ceb2e 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -141,7 +141,7 @@ jobs: echo 'script = "script.sh"' >> .out-of-tree.toml echo '[[targets]]' >> .out-of-tree.toml echo 'distro = { id = "${{ matrix.os.distro }}", release = "${{ matrix.os.release }}" }' >> .out-of-tree.toml - echo 'release_mask = ".*"' >> .out-of-tree.toml + echo 'kernel = { regex = ".*" }' >> .out-of-tree.toml echo -e '#!/bin/sh\necho ok' >> script.sh diff --git a/config/config.go b/config/config.go index 6f58e3b..56b4521 100644 --- a/config/config.go +++ b/config/config.go @@ -5,12 +5,10 @@ package config import ( - "errors" "fmt" "io/ioutil" "os" "regexp" - "strconv" "strings" "time" @@ -19,21 +17,21 @@ import ( "github.com/naoina/toml" ) -type kernel struct { - Version []int - Major []int - Minor []int - Patch []int +type Kernel struct { + // TODO + // Version string + // From string + // To string + + // prev. ReleaseMask + Regex string } // Target defines the kernel type Target struct { Distro distro.Distro - ReleaseMask string - - // Overrides ReleaseMask - Kernel kernel + Kernel Kernel } // DockerName is returns stable name for docker container @@ -131,11 +129,11 @@ type Patch struct { // Artifact is for .out-of-tree.toml type Artifact struct { - Name string - Type ArtifactType - TestFiles []FileTransfer - SourcePath string - Targets []Target + Name string + Type ArtifactType + TestFiles []FileTransfer + SourcePath string + Targets []Target Script string @@ -181,7 +179,7 @@ func (ka Artifact) checkSupport(ki KernelInfo, km Target) ( return } - supported, err = regexp.MatchString(km.ReleaseMask, ki.KernelRelease) + supported, err = regexp.MatchString(km.Kernel.Regex, ki.KernelRelease) return } @@ -259,66 +257,6 @@ func ReadKernelConfig(path string) (kernelCfg KernelConfig, err error) { return } -func rangeRegexp(start, end int) (s string) { - s += "(" - for i := start; i <= end; i++ { - s += strconv.Itoa(i) - if i != end { - s += "|" - } - } - s += ")" - return -} - -func versionRegexp(l []int) (s string, err error) { - switch len(l) { - case 1: - s += strconv.Itoa(l[0]) - case 2: - s += rangeRegexp(l[0], l[1]) - default: - err = errors.New("version must contain one value or range") - return - } - return -} - -func genReleaseMask(km kernel) (mask string, err error) { - s, err := versionRegexp(km.Version) - if err != nil { - return - } - mask += s + "[.]" - - s, err = versionRegexp(km.Major) - if err != nil { - return - } - mask += s + "[.]" - - s, err = versionRegexp(km.Minor) - if err != nil { - return - } - mask += s - - switch len(km.Patch) { - case 0: - // ok - case 1: - mask += "-" + strconv.Itoa(km.Patch[0]) + "-" - case 2: - mask += "-" + rangeRegexp(km.Patch[0], km.Patch[1]) + "-" - default: - err = errors.New("version must contain one value or range") - return - } - - mask += ".*" - return -} - // ReadArtifactConfig is for read .out-of-tree.toml func ReadArtifactConfig(path string) (ka Artifact, err error) { buf, err := readFileAll(path) @@ -327,25 +265,5 @@ func ReadArtifactConfig(path string) (ka Artifact, err error) { } err = toml.Unmarshal(buf, &ka) - if err != nil { - return - } - - for i, _ := range ka.Targets { - km := &ka.Targets[i] - if len(km.Kernel.Version) != 0 && km.ReleaseMask != "" { - s := "Only one way to define kernel version is allowed" - err = errors.New(s) - return - } - - if km.ReleaseMask == "" { - km.ReleaseMask, err = genReleaseMask(km.Kernel) - if err != nil { - return - } - } - } - return } diff --git a/config/config_test.go b/config/config_test.go index 9c7e83d..7f24870 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,10 +18,15 @@ func TestMarshalUnmarshal(t *testing.T) { Type: KernelModule, } artifactCfg.Targets = append(artifactCfg.Targets, - Target{distro.Distro{ - ID: distro.Ubuntu, - Release: "18.04", - }, ".*", kernel{}}) + Target{ + Distro: distro.Distro{ + ID: distro.Ubuntu, + Release: "18.04", + }, + Kernel: Kernel{ + Regex: ".*", + }, + }) buf, err := toml.Marshal(&artifactCfg) if err != nil { t.Fatal(err) @@ -33,38 +38,3 @@ func TestMarshalUnmarshal(t *testing.T) { t.Fatal(err) } } - -func TestKernelRegex(t *testing.T) { - mask := "4[.]4[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116)-.*" - k := kernel{ - Version: []int{4}, - Major: []int{4}, - Minor: []int{0}, - Patch: []int{1, 116}, - } - - gmask, err := genReleaseMask(k) - if err != nil { - t.Fatal(err) - } - - if mask != gmask { - t.Fatal("Got", gmask, "instead of", mask) - } - - mask = "4[.]4[.]0.*" - k = kernel{ - Version: []int{4}, - Major: []int{4}, - Minor: []int{0}, - } - - gmask, err = genReleaseMask(k) - if err != nil { - t.Fatal(err) - } - - if mask != gmask { - t.Fatal("Got", gmask, "instead of", mask) - } -} diff --git a/distro/debian/debian.go b/distro/debian/debian.go index ab2e9a4..bcd30fc 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -120,7 +120,7 @@ func Match(km config.Target) (pkgs []string, err error) { release := releaseFromString(km.Distro.Release) - r := regexp.MustCompile(km.ReleaseMask) + r := regexp.MustCompile(km.Kernel.Regex) for _, dk := range kernels { p := strings.Replace(dk.Image.Deb.Name, ".deb", "", -1) diff --git a/distro/debian/debian_test.go b/distro/debian/debian_test.go index fc22fb6..a7d7e58 100644 --- a/distro/debian/debian_test.go +++ b/distro/debian/debian_test.go @@ -25,8 +25,8 @@ func TestMatch(t *testing.T) { config.Directory = tmp km := config.Target{ - ReleaseMask: "3.2.0-4", - Distro: distro.Distro{Release: "7"}, + Distro: distro.Distro{Release: "7"}, + Kernel: config.Kernel{Regex: "3.2.0-4"}, } pkgs, err := Match(km) diff --git a/distro/oraclelinux/oraclelinux.go b/distro/oraclelinux/oraclelinux.go index 4a8769d..6901802 100644 --- a/distro/oraclelinux/oraclelinux.go +++ b/distro/oraclelinux/oraclelinux.go @@ -57,13 +57,13 @@ func Match(km config.Target) (pkgs []string, err error) { return } - r, err := regexp.Compile("kernel-" + km.ReleaseMask) + r, err := regexp.Compile("kernel-" + km.Kernel.Regex) if err != nil { return } for _, pkg := range strings.Fields(output) { - if r.MatchString(pkg) || strings.Contains(pkg, km.ReleaseMask) { + if r.MatchString(pkg) || strings.Contains(pkg, km.Kernel.Regex) { log.Trace().Msg(pkg) pkgs = append(pkgs, pkg) } diff --git a/distro/ubuntu/ubuntu.go b/distro/ubuntu/ubuntu.go index cd0ca90..b4952a9 100644 --- a/distro/ubuntu/ubuntu.go +++ b/distro/ubuntu/ubuntu.go @@ -69,13 +69,13 @@ func Match(km config.Target) (pkgs []string, err error) { return } - r, err := regexp.Compile("linux-image-" + km.ReleaseMask) + r, err := regexp.Compile("linux-image-" + km.Kernel.Regex) if err != nil { return } for _, pkg := range strings.Fields(output) { - if r.MatchString(pkg) || strings.Contains(pkg, km.ReleaseMask) { + if r.MatchString(pkg) || strings.Contains(pkg, km.Kernel.Regex) { pkgs = append(pkgs, pkg) } } diff --git a/examples/kernel-exploit/.out-of-tree.toml b/examples/kernel-exploit/.out-of-tree.toml index 6e09ed4..71fc491 100644 --- a/examples/kernel-exploit/.out-of-tree.toml +++ b/examples/kernel-exploit/.out-of-tree.toml @@ -5,11 +5,11 @@ type = "exploit" [[targets]] distro = { id = "Ubuntu", release = "16.04" } -release_mask = "4[.]4[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116)-.*" +kernel = { regex = "4[.]4[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116)-.*" } [[targets]] distro = { id = "Ubuntu", release = "16.04" } -release_mask = "4[.]8[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58)-.*" +kernel = { regex = "4[.]8[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58)-.*" } [[targets]] # Can be Ubuntu/CentOS/Debian/etc. @@ -17,17 +17,12 @@ distro = { id = "Ubuntu", release = "16.04" } # regex for `uname -r` # See also: regex-golang.appspot.com # stupid way to generate: $ echo '4.4.0-('$(seq 44 | xargs echo | sed 's/ /|/g')')-.*' -release_mask = "4[.]10[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42)-.*" +kernel = { regex = "4[.]10[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42)-.*" } [[targets]] distro = { id = "Ubuntu", release = "16.04" } -release_mask = "4[.]11[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14)-.*" +kernel = { regex = "4[.]11[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14)-.*" } [[targets]] distro = { id = "Ubuntu", release = "16.04" } -# equivalent for "4[.]13[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21)-.*" -[targets.kernel] -version = [ 4 ] -major = [ 13 ] -minor = [ 0 ] -patch = [ 1, 21 ] +kernel = { regex = "4[.]13[.]0-(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21)-.*" } diff --git a/examples/kernel-module/.out-of-tree.toml b/examples/kernel-module/.out-of-tree.toml index 2cd2201..b568a05 100644 --- a/examples/kernel-module/.out-of-tree.toml +++ b/examples/kernel-module/.out-of-tree.toml @@ -8,19 +8,19 @@ type = "module" distro = { id = "Ubuntu", release = "16.04" } # regex for `uname -r` # See also: regex-golang.appspot.com -release_mask = "4[.]4[.]0-70-.*" +kernel = { regex = "4[.]4[.]0-70-.*" } # [[targets]] may be defined unlimited number of times [[targets]] distro = { id = "Ubuntu", release = "18.04" } # Also you can use only one kernel -release_mask = "4[.]15[.]0-(24|29)-generic" +kernel = { regex = "4[.]15[.]0-(24|29)-generic" } [[targets]] distro = { id = "Ubuntu", release = "18.04" } # Also you can use only one kernel -release_mask = "4[.]15[.]0-23-generic" +kernel = { regex = "4[.]15[.]0-23-generic" } [[targets]] distro = { id = "CentOS", release = "7" } -release_mask = "3[.]10[.]0-862.el7.x86_64" +kernel = { regex = "3[.]10[.]0-862.el7.x86_64" } diff --git a/examples/preload/.out-of-tree.toml b/examples/preload/.out-of-tree.toml index 98cf350..c1932d0 100644 --- a/examples/preload/.out-of-tree.toml +++ b/examples/preload/.out-of-tree.toml @@ -3,7 +3,7 @@ type = "module" [[targets]] distro = { id = "Ubuntu", release = "18.04" } -release_mask = ".*" +kernel = { regex = ".*" } [[preload]] repo = "https://github.com/openwall/lkrg" diff --git a/examples/script/.out-of-tree.toml b/examples/script/.out-of-tree.toml index 32147e5..2040d99 100644 --- a/examples/script/.out-of-tree.toml +++ b/examples/script/.out-of-tree.toml @@ -7,4 +7,4 @@ script = "script.sh" [[targets]] distro = { id = "Ubuntu", release = "22.04" } -release_mask = ".*" +kernel = { regex = ".*" } diff --git a/gen.go b/gen.go index 5214922..1ba7f8b 100644 --- a/gen.go +++ b/gen.go @@ -33,12 +33,12 @@ func genConfig(at config.ArtifactType) (err error) { Type: at, } a.Targets = append(a.Targets, config.Target{ - Distro: distro.Distro{ID: distro.Ubuntu, Release: "18.04"}, - ReleaseMask: ".*", + Distro: distro.Distro{ID: distro.Ubuntu, Release: "18.04"}, + Kernel: config.Kernel{Regex: ".*"}, }) a.Targets = append(a.Targets, config.Target{ - Distro: distro.Distro{ID: distro.Debian, Release: "8"}, - ReleaseMask: ".*", + Distro: distro.Distro{ID: distro.Debian, Release: "8"}, + Kernel: config.Kernel{Regex: ".*"}, }) a.Preload = append(a.Preload, config.PreloadModule{ Repo: "Repo name (e.g. https://github.com/openwall/lkrg)", diff --git a/kernel.go b/kernel.go index ffb33fa..f098a08 100644 --- a/kernel.go +++ b/kernel.go @@ -65,8 +65,8 @@ func (cmd *KernelListRemoteCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error } km := config.Target{ - Distro: distro.Distro{ID: distroType, Release: cmd.Ver}, - ReleaseMask: ".*", + Distro: distro.Distro{ID: distroType, Release: cmd.Ver}, + Kernel: config.Kernel{Regex: ".*"}, } _, err = kernel.GenRootfsImage(container.Image{Name: km.DockerName()}, false) @@ -150,8 +150,8 @@ func (cmd *KernelGenallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) { kernel.SetSigintHandler(&shutdown) km := config.Target{ - Distro: distro.Distro{ID: distroType, Release: cmd.Ver}, - ReleaseMask: ".*", + Distro: distro.Distro{ID: distroType, Release: cmd.Ver}, + Kernel: config.Kernel{Regex: ".*"}, } err = kernel.GenerateKernels(km, g.Config.Docker.Registry, @@ -187,8 +187,8 @@ func (cmd *KernelInstallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) { kernel.SetSigintHandler(&shutdown) km := config.Target{ - Distro: distro.Distro{ID: distroType, Release: cmd.Ver}, - ReleaseMask: cmd.Kernel, + Distro: distro.Distro{ID: distroType, Release: cmd.Ver}, + Kernel: config.Kernel{Regex: cmd.Kernel}, } err = kernel.GenerateKernels(km, g.Config.Docker.Registry, diff --git a/pew.go b/pew.go index d70e9f6..3331ddd 100644 --- a/pew.go +++ b/pew.go @@ -843,15 +843,18 @@ func kernelMask(kernel string) (km config.Target, err error) { return } - km = config.Target{Distro: distro.Distro{ID: dt}, ReleaseMask: parts[1]} + km = config.Target{ + Distro: distro.Distro{ID: dt}, + Kernel: config.Kernel{Regex: parts[1]}, + } return } func genAllKernels() (sk []config.Target, err error) { for _, id := range distro.IDs { sk = append(sk, config.Target{ - Distro: distro.Distro{ID: id}, - ReleaseMask: ".*", + Distro: distro.Distro{ID: id}, + Kernel: config.Kernel{Regex: ".*"}, }) } return diff --git a/preload.go b/preload.go index f0e6c55..dad2745 100644 --- a/preload.go +++ b/preload.go @@ -102,8 +102,8 @@ func buildPreload(workPath, tmp string, ki config.KernelInfo, ka.SourcePath = workPath km := config.Target{ - Distro: ki.Distro, - ReleaseMask: ki.KernelRelease, + Distro: ki.Distro, + Kernel: config.Kernel{Regex: ki.KernelRelease}, } ka.Targets = []config.Target{km}