1
0

Implements parameter "--max=X" for autogen

This commit is contained in:
dump_stack() 2019-08-12 22:58:34 +00:00
parent 238592e546
commit 5dbbb33297
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
3 changed files with 45 additions and 8 deletions

View File

@ -9,6 +9,8 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math"
"math/rand"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
@ -20,6 +22,8 @@ import (
"github.com/naoina/toml" "github.com/naoina/toml"
) )
var KERNELS_ALL int64 = math.MaxInt64
func kernelListHandler(kcfg config.KernelConfig) (err error) { func kernelListHandler(kcfg config.KernelConfig) (err error) {
if len(kcfg.Kernels) == 0 { if len(kcfg.Kernels) == 0 {
return errors.New("No kernels found") return errors.New("No kernels found")
@ -393,7 +397,17 @@ func hasKernel(ki config.KernelInfo, kcfg config.KernelConfig) bool {
return false return false
} }
func generateKernels(km config.KernelMask) (err error) { func shuffle(a []string) []string {
// FisherYates shuffle
for i := len(a) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
return a
}
func generateKernels(km config.KernelMask, max int64) (err error) {
log.Println("Generating for kernel mask", km)
err = generateBaseDockerImage(km) err = generateBaseDockerImage(km)
if err != nil { if err != nil {
return return
@ -406,9 +420,20 @@ func generateKernels(km config.KernelMask) (err error) {
return return
} }
for i, pkg := range pkgs { for i, pkg := range shuffle(pkgs) {
log.Println(i, "/", len(pkgs)) if max <= 0 {
dockerImageAppend(km, pkg) log.Println("Max is reached")
break
}
log.Println(i, "/", len(pkgs), pkg)
err = dockerImageAppend(km, pkg)
if err == nil {
max -= 1
} else {
log.Println("dockerImageAppend", err)
}
} }
err = kickImage(km.DockerName()) err = kickImage(km.DockerName())
@ -425,7 +450,7 @@ func generateKernels(km config.KernelMask) (err error) {
return return
} }
func kernelAutogenHandler(workPath string) (err error) { func kernelAutogenHandler(workPath string, max int64) (err error) {
ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml") ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml")
if err != nil { if err != nil {
return return
@ -437,7 +462,7 @@ func kernelAutogenHandler(workPath string) (err error) {
return return
} }
err = generateKernels(sk) err = generateKernels(sk, max)
if err != nil { if err != nil {
return return
} }
@ -499,7 +524,7 @@ func kernelGenallHandler(distro, version string) (err error) {
DistroRelease: version, DistroRelease: version,
ReleaseMask: ".*", ReleaseMask: ".*",
} }
err = generateKernels(km) err = generateKernels(km, KERNELS_ALL)
if err != nil { if err != nil {
return return
} }

11
main.go
View File

@ -7,10 +7,12 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
"sort" "sort"
"time"
kingpin "gopkg.in/alecthomas/kingpin.v2" kingpin "gopkg.in/alecthomas/kingpin.v2"
@ -70,6 +72,8 @@ func checkDockerPermissions() (err error) {
} }
func main() { func main() {
rand.Seed(time.Now().UnixNano())
app := kingpin.New( app := kingpin.New(
"out-of-tree", "out-of-tree",
"kernel {module, exploit} development tool", "kernel {module, exploit} development tool",
@ -117,6 +121,11 @@ func main() {
kernelListCommand := kernelCommand.Command("list", "List kernels") kernelListCommand := kernelCommand.Command("list", "List kernels")
kernelAutogenCommand := kernelCommand.Command("autogen", kernelAutogenCommand := kernelCommand.Command("autogen",
"Generate kernels based on a current config") "Generate kernels based on a current config")
kernelAutogenMax := kernelAutogenCommand.Flag("max",
"Download random kernels from set defined by regex in "+
"release_mask, but no more than X for each of "+
"release_mask").PlaceHolder("X").Default(
fmt.Sprint(KERNELS_ALL)).Int64()
kernelDockerRegenCommand := kernelCommand.Command("docker-regen", kernelDockerRegenCommand := kernelCommand.Command("docker-regen",
"Regenerate kernels config from out_of_tree_* docker images") "Regenerate kernels config from out_of_tree_* docker images")
kernelGenallCommand := kernelCommand.Command("genall", kernelGenallCommand := kernelCommand.Command("genall",
@ -200,7 +209,7 @@ func main() {
case kernelListCommand.FullCommand(): case kernelListCommand.FullCommand():
err = kernelListHandler(kcfg) err = kernelListHandler(kcfg)
case kernelAutogenCommand.FullCommand(): case kernelAutogenCommand.FullCommand():
err = kernelAutogenHandler(*path) err = kernelAutogenHandler(*path, *kernelAutogenMax)
case kernelDockerRegenCommand.FullCommand(): case kernelDockerRegenCommand.FullCommand():
err = kernelDockerRegenHandler() err = kernelDockerRegenHandler()
case kernelGenallCommand.FullCommand(): case kernelGenallCommand.FullCommand():

3
pew.go
View File

@ -39,6 +39,9 @@ func dockerRun(timeout time.Duration, container, workdir, command string) (
raw, err := cmd.CombinedOutput() raw, err := cmd.CombinedOutput()
if err != nil { if err != nil {
e := fmt.Sprintf("error `%v` for cmd `%v` with output `%v`",
err, command, string(raw))
err = errors.New(e)
return return
} }