1
0

Implements build on host and support for custom kernels

Resolves #6
This commit is contained in:
dump_stack() 2019-08-16 05:05:26 +00:00
parent 15a8c6b1e4
commit d035e4f8ad
4 changed files with 102 additions and 18 deletions

View File

@ -163,6 +163,7 @@ type KernelInfo struct {
KernelRelease string KernelRelease string
// Build-time information // Build-time information
KernelSource string // module/exploit will be build on host
ContainerName string ContainerName string
// Runtime information // Runtime information

View File

@ -18,8 +18,10 @@ import (
"strings" "strings"
"time" "time"
"code.dumpstack.io/tools/out-of-tree/config"
"github.com/naoina/toml" "github.com/naoina/toml"
"github.com/zcalusic/sysinfo"
"code.dumpstack.io/tools/out-of-tree/config"
) )
var KERNELS_ALL int64 = math.MaxInt64 var KERNELS_ALL int64 = math.MaxInt64
@ -304,16 +306,73 @@ func listDockerImages() (diis []dockerImageInfo, err error) {
return return
} }
func updateKernelsCfg() (err error) { func genHostKernels() (kcfg config.KernelConfig, err error) {
si := sysinfo.SysInfo{}
si.GetSysInfo()
distroType, err := config.NewDistroType(si.OS.Vendor)
if err != nil {
return
}
cmd := exec.Command("ls", "/lib/modules")
rawOutput, err := cmd.CombinedOutput()
if err != nil {
log.Println(string(rawOutput), err)
return
}
kernelsBase := "/boot/"
files, err := ioutil.ReadDir(kernelsBase)
if err != nil {
return
}
// only for compatibility, docker is not really used
dii := dockerImageInfo{
ContainerName: config.KernelMask{
DistroType: distroType,
DistroRelease: si.OS.Version,
}.DockerName(),
}
for _, k := range strings.Fields(string(rawOutput)) {
ki := config.KernelInfo{
DistroType: distroType,
DistroRelease: si.OS.Version,
KernelRelease: k,
KernelSource: "/lib/modules/" + k + "/build",
KernelPath: kernelsBase + genKernelPath(files, k),
InitrdPath: kernelsBase + genInitrdPath(files, k),
RootFS: genRootfsImage(dii),
}
kcfg.Kernels = append(kcfg.Kernels, ki)
}
return
}
func updateKernelsCfg(host bool) (err error) {
newkcfg := config.KernelConfig{}
if host {
// Get host kernels
newkcfg, err = genHostKernels()
if err != nil {
return
}
}
// Get docker kernels
dockerImages, err := listDockerImages() dockerImages, err := listDockerImages()
if err != nil { if err != nil {
return return
} }
newkcfg := config.KernelConfig{}
for _, d := range dockerImages { for _, d := range dockerImages {
err = genKernels(d, &newkcfg) err = genDockerKernels(d, &newkcfg)
if err != nil { if err != nil {
log.Println("gen kernels", d.ContainerName, ":", err) log.Println("gen kernels", d.ContainerName, ":", err)
continue continue
@ -350,7 +409,7 @@ func updateKernelsCfg() (err error) {
return return
} }
func genKernels(dii dockerImageInfo, newkcfg *config.KernelConfig) ( func genDockerKernels(dii dockerImageInfo, newkcfg *config.KernelConfig) (
err error) { err error) {
name := dii.ContainerName name := dii.ContainerName
@ -450,7 +509,7 @@ func generateKernels(km config.KernelMask, max int64) (err error) {
return return
} }
func kernelAutogenHandler(workPath string, max int64) (err error) { func kernelAutogenHandler(workPath string, max int64, host bool) (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
@ -468,11 +527,11 @@ func kernelAutogenHandler(workPath string, max int64) (err error) {
} }
} }
err = updateKernelsCfg() err = updateKernelsCfg(host)
return return
} }
func kernelDockerRegenHandler() (err error) { func kernelDockerRegenHandler(host bool) (err error) {
dockerImages, err := listDockerImages() dockerImages, err := listDockerImages()
if err != nil { if err != nil {
return return
@ -510,10 +569,10 @@ func kernelDockerRegenHandler() (err error) {
} }
} }
return updateKernelsCfg() return updateKernelsCfg(host)
} }
func kernelGenallHandler(distro, version string) (err error) { func kernelGenallHandler(distro, version string, host bool) (err error) {
distroType, err := config.NewDistroType(distro) distroType, err := config.NewDistroType(distro)
if err != nil { if err != nil {
return return
@ -529,5 +588,5 @@ func kernelGenallHandler(distro, version string) (err error) {
return return
} }
return updateKernelsCfg() return updateKernelsCfg(host)
} }

View File

@ -131,6 +131,7 @@ func main() {
pewDist := pewDistFlag.Default(PATH_DEV_NULL).String() pewDist := pewDistFlag.Default(PATH_DEV_NULL).String()
kernelCommand := app.Command("kernel", "Manipulate kernels") kernelCommand := app.Command("kernel", "Manipulate kernels")
kernelUseHost := kernelCommand.Flag("host", "Use also host kernels").Bool()
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")
@ -239,11 +240,11 @@ func main() {
case kernelListCommand.FullCommand(): case kernelListCommand.FullCommand():
err = kernelListHandler(kcfg) err = kernelListHandler(kcfg)
case kernelAutogenCommand.FullCommand(): case kernelAutogenCommand.FullCommand():
err = kernelAutogenHandler(*path, *kernelAutogenMax) err = kernelAutogenHandler(*path, *kernelAutogenMax, *kernelUseHost)
case kernelDockerRegenCommand.FullCommand(): case kernelDockerRegenCommand.FullCommand():
err = kernelDockerRegenHandler() err = kernelDockerRegenHandler(*kernelUseHost)
case kernelGenallCommand.FullCommand(): case kernelGenallCommand.FullCommand():
err = kernelGenallHandler(*distro, *version) err = kernelGenallHandler(*distro, *version, *kernelUseHost)
case genModuleCommand.FullCommand(): case genModuleCommand.FullCommand():
err = genConfig(config.KernelModule) err = genConfig(config.KernelModule)
case genExploitCommand.FullCommand(): case genExploitCommand.FullCommand():

23
pew.go
View File

@ -72,10 +72,33 @@ func build(tmp string, ka config.Artifact, ki config.KernelInfo,
} }
kernel := "/lib/modules/" + ki.KernelRelease + "/build" kernel := "/lib/modules/" + ki.KernelRelease + "/build"
if ki.KernelSource != "" {
kernel = ki.KernelSource
}
if ki.ContainerName != "" {
output, err = dockerRun(dockerTimeout, ki.ContainerName, output, err = dockerRun(dockerTimeout, ki.ContainerName,
tmpSourcePath, "make KERNEL="+kernel+" TARGET="+target+ tmpSourcePath, "make KERNEL="+kernel+" TARGET="+target+
" && chmod -R 777 /work") " && chmod -R 777 /work")
} else {
command := "make KERNEL=" + kernel + " TARGET=" + target
cmd := exec.Command("bash", "-c", "cd "+tmpSourcePath+" && "+command)
timer := time.AfterFunc(dockerTimeout, func() {
cmd.Process.Kill()
})
defer timer.Stop()
var raw []byte
raw, err = cmd.CombinedOutput()
if err != nil {
e := fmt.Sprintf("error `%v` for cmd `%v` with output `%v`",
err, command, string(raw))
err = errors.New(e)
return
}
output = string(raw)
}
return return
} }