1
0

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 = ".*" }
This commit is contained in:
dump_stack() 2023-05-18 18:48:09 +00:00
parent d551cc8fc4
commit 73f5df2425
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
15 changed files with 61 additions and 175 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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)-.*" }

View File

@ -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" }

View File

@ -3,7 +3,7 @@ type = "module"
[[targets]]
distro = { id = "Ubuntu", release = "18.04" }
release_mask = ".*"
kernel = { regex = ".*" }
[[preload]]
repo = "https://github.com/openwall/lkrg"

View File

@ -7,4 +7,4 @@ script = "script.sh"
[[targets]]
distro = { id = "Ubuntu", release = "22.04" }
release_mask = ".*"
kernel = { regex = ".*" }

8
gen.go
View File

@ -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)",

View File

@ -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,

9
pew.go
View File

@ -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

View File

@ -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}