From 22a8e32e2c840a0e248624fa61d08b20faf66bad Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sun, 14 Jun 2020 20:14:59 +0000 Subject: [PATCH] Implements modules preload list --- CHANGELOG.md | 15 ++- config/config.go | 8 ++ debug.go | 6 + examples/preload/.out-of-tree.toml | 12 ++ examples/preload/Makefile | 11 ++ examples/preload/README.md | 5 + examples/preload/module.c | 17 +++ gen.go | 3 + go.mod | 3 +- go.sum | 54 +++++++++ main.go | 3 + pew.go | 6 + preload.go | 172 +++++++++++++++++++++++++++++ 13 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 examples/preload/.out-of-tree.toml create mode 100644 examples/preload/Makefile create mode 100644 examples/preload/README.md create mode 100644 examples/preload/module.c create mode 100644 preload.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a1b267..59f9a3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,17 @@ - Parameter `--docker-timeout` may also be set in the artifact configuration file. +- Preload modules before inserting module or run exploit. Modules can + be specified by git repository path in the `repo` parameter of + section `[[preload]]`. Also, there is a `path` parameter for local + projects. Note that `repo` is using a cache that uses last commit + hash to check is project needs to be rebuilt, so it's not suitable + for local development (except if you will commit each time before + run out-of-tree). + +- Flag `--disable-preload` to ignore `[[preload]]` section of + configuration file. + ## [1.3.0] 2020-05-30 ### Added @@ -169,7 +180,7 @@ - Temporary files is moved to `~/.out-of-tree/tmp/` to avoid docker mounting issues on some systems. -## [0.2.0] - 2019-12-01 +## [0.2.0] - 2018-12-01 The main purpose of the release is to simplify installation. @@ -191,7 +202,7 @@ The main purpose of the release is to simplify installation. - No warning anymore if test.sh is not exists. -## [0.1.0] - 2019-11-20 +## [0.1.0] - 2018-11-20 Initial release that was never tagged. diff --git a/config/config.go b/config/config.go index a510db0..c7dbef4 100644 --- a/config/config.go +++ b/config/config.go @@ -102,6 +102,12 @@ func (d Duration) MarshalTOML() (data []byte, err error) { return } +type PreloadModule struct { + Repo string + Path string + TimeoutAfterLoad Duration +} + // Artifact is for .out-of-tree.toml type Artifact struct { Name string @@ -125,6 +131,8 @@ type Artifact struct { DisableKaslr bool DisableKpti bool } + + Preload []PreloadModule } func (ka Artifact) checkSupport(ki KernelInfo, km KernelMask) ( diff --git a/debug.go b/debug.go index 65fefa0..af25865 100644 --- a/debug.go +++ b/debug.go @@ -179,6 +179,12 @@ func debugHandler(kcfg config.KernelConfig, workPath, kernRegex, gdb string, } defer os.RemoveAll(tmp) + err = preloadModules(q, ka, ki, dockerTimeout) + if err != nil { + log.Println(err) + return + } + outFile, output, err := build(tmp, ka, ki, dockerTimeout) if err != nil { log.Println(err, output) diff --git a/examples/preload/.out-of-tree.toml b/examples/preload/.out-of-tree.toml new file mode 100644 index 0000000..f7875af --- /dev/null +++ b/examples/preload/.out-of-tree.toml @@ -0,0 +1,12 @@ +name = "out-of-tree preload" +type = "module" + +[[supported_kernels]] +distro_type = "Ubuntu" +distro_release = "18.04" +release_mask = ".*" + +[[preload]] +repo = "https://github.com/jollheef/lkrg" +#path = "/local/path/to/lkrg" +timeout_after_load = "1s" diff --git a/examples/preload/Makefile b/examples/preload/Makefile new file mode 100644 index 0000000..a3b9d7d --- /dev/null +++ b/examples/preload/Makefile @@ -0,0 +1,11 @@ +KERNEL := /lib/modules/$(shell uname -r)/build +TARGET := module + +obj-m += $(TARGET).o +$(TARGET)-objs = module.o + +all: + make -C $(KERNEL) M=$(PWD) modules + +clean: + make -C $(KERNEL) M=$(PWD) clean diff --git a/examples/preload/README.md b/examples/preload/README.md new file mode 100644 index 0000000..ae94611 --- /dev/null +++ b/examples/preload/README.md @@ -0,0 +1,5 @@ +# out-of-tree kernel module preload example + +See .out-of-tree.toml + +Note that it should fail to insert module if lkrg is enabled in the preload list. diff --git a/examples/preload/module.c b/examples/preload/module.c new file mode 100644 index 0000000..0fc718b --- /dev/null +++ b/examples/preload/module.c @@ -0,0 +1,17 @@ +#include +#include + +int init_module(void) +{ + char *argv[] = { "/bin/sh", "--help", NULL }; + char *envp[] = { NULL }; + + /* trigger lkrg */ + return call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); +} + +void cleanup_module(void) +{ +} + +MODULE_LICENSE("GPL"); diff --git a/gen.go b/gen.go index 54baddf..c774155 100644 --- a/gen.go +++ b/gen.go @@ -22,6 +22,9 @@ func genConfig(at config.ArtifactType) (err error) { DistroRelease: "18.04", ReleaseMask: ".*", }) + a.Preload = append(a.Preload, config.PreloadModule{ + Repo: "Repo name (e.g. https://github.com/jollheef/lkrg)", + }) buf, err := toml.Marshal(&a) if err != nil { diff --git a/go.mod b/go.mod index a255f02..406d160 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ replace code.dumpstack.io/tools/out-of-tree/config => ./config require ( github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect + github.com/go-git/go-git/v5 v5.1.0 github.com/kylelemons/godebug v1.1.0 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect github.com/mattn/go-sqlite3 v1.11.0 @@ -20,7 +21,7 @@ require ( github.com/remeh/sizedwaitgroup v0.0.0-20180822144253-5e7302b12cce github.com/stretchr/testify v1.5.1 // indirect github.com/zcalusic/sysinfo v0.0.0-20190429151633-fbadb57345c2 - golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 + golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/logrusorgru/aurora.v2 v2.0.0-20190417123914-21d75270181e ) diff --git a/go.sum b/go.sum index 2e89635..3391765 100644 --- a/go.sum +++ b/go.sum @@ -1,21 +1,54 @@ bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U= bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= +github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= +github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM= +github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git v1.0.0 h1:YcN9iDGDoXuIw0vHls6rINwV416HYa0EB2X+RBsyYp4= +github.com/go-git/go-git v4.7.0+incompatible h1:+W9rgGY4DOKKdX2x6HxSR7HNeTxqiKrOvKnuittYVdA= +github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw= +github.com/go-git/go-git/v5 v5.1.0 h1:HxJn9g/E7eYvKW3Fm7Jt4ee8LXfPOm/H1cdDu8vEssk= +github.com/go-git/go-git/v5 v5.1.0/go.mod h1:ZKfuPUoY1ZqIG4QG9BDBh3G4gLM5zvPuSJAozQrZuyM= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= +github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8= github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88= github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/otiai10/copy v1.0.1 h1:gtBjD8aq4nychvRZ2CyJvFWAw0aja+VHazDdruZKGZA= @@ -27,28 +60,49 @@ github.com/otiai10/mint v1.2.3 h1:PsrRBmrxR68kyNu6YlqYHbNlItc5vOkuS6LBEsNttVA= github.com/otiai10/mint v1.2.3/go.mod h1:YnfyPNhBvnY8bW4SGQHCs/aAFhkgySlMZbrF5U0bOVw= github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remeh/sizedwaitgroup v0.0.0-20180822144253-5e7302b12cce h1:aP+C+YbHZfOQlutA4p4soHi7rVUqHQdWEVMSkHfDTqY= github.com/remeh/sizedwaitgroup v0.0.0-20180822144253-5e7302b12cce/go.mod h1:3j2R4OIe/SeS6YDhICBy22RWjJC5eNCJ1V+9+NVNYlo= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= +github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/zcalusic/sysinfo v0.0.0-20190429151633-fbadb57345c2 h1:uMiaKNX5zFLOa6nNtun+d/lpV5bOBh7BvE4q9jfZacQ= github.com/zcalusic/sysinfo v0.0.0-20190429151633-fbadb57345c2/go.mod h1:zAn3FAIbgZPYnutDND49Ivf8sb/mXYk8UjZdqMswgHg= +golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/logrusorgru/aurora.v2 v2.0.0-20190417123914-21d75270181e h1:Wc0601/F/0TByNewL9UAKk18FfwumyYyT8pJMIHcolA= gopkg.in/logrusorgru/aurora.v2 v2.0.0-20190417123914-21d75270181e/go.mod h1:Wm+IEn1fgFp8E2paL93oFVrHZW4toMKARNE85fDY5w8= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index e04eed2..07b851f 100644 --- a/main.go +++ b/main.go @@ -126,6 +126,9 @@ func main() { thresholdFlag := app.Flag("threshold", "Reliablity threshold for exit code") threshold := thresholdFlag.Default("1.00").Float64() + disablePreloadFlag := app.Flag("disable-preload", "Disable module preload") + disablePreload = disablePreloadFlag.Bool() + pewCommand := app.Command("pew", "Build, run and test module/exploit") pewMax := pewCommand.Flag("max", "Test no more than X kernels"). diff --git a/pew.go b/pew.go index 49af13c..fffbe71 100644 --- a/pew.go +++ b/pew.go @@ -395,6 +395,12 @@ func whatever(swg *sizedwaitgroup.SizedWaitGroup, ka config.Artifact, return } + err = preloadModules(q, ka, ki, dockerTimeout) + if err != nil { + log.Println(err) + return + } + copyArtifactAndTest(q, ka, &result, remoteTest) } diff --git a/preload.go b/preload.go new file mode 100644 index 0000000..706eb51 --- /dev/null +++ b/preload.go @@ -0,0 +1,172 @@ +// Copyright 2020 Mikhail Klementev. All rights reserved. +// Use of this source code is governed by a AGPLv3 license +// (or later) that can be found in the LICENSE file. + +package main + +import ( + "crypto/sha1" + "encoding/hex" + "errors" + "io/ioutil" + "log" + "os" + "os/user" + "path/filepath" + "time" + + "github.com/go-git/go-git/v5" + + "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/qemu" +) + +var disablePreload *bool + +func preloadModules(q *qemu.System, ka config.Artifact, ki config.KernelInfo, + dockerTimeout time.Duration) (err error) { + + if *disablePreload { + return + } + + for _, pm := range ka.Preload { + err = preload(q, ki, pm, dockerTimeout) + if err != nil { + return + } + } + return +} + +func preload(q *qemu.System, ki config.KernelInfo, pm config.PreloadModule, + dockerTimeout time.Duration) (err error) { + + var workPath, cache string + if pm.Path != "" { + log.Println("Use non-git path for preload module (no cache)") + workPath = pm.Path + } else if pm.Repo != "" { + workPath, cache, err = cloneOrPull(pm.Repo, ki) + if err != nil { + return + } + } else { + errors.New("No repo/path in preload entry") + } + + err = buildAndInsmod(workPath, q, ki, dockerTimeout, cache) + if err != nil { + return + } + + time.Sleep(pm.TimeoutAfterLoad.Duration) + return +} + +func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo, + dockerTimeout time.Duration, cache string) (err error) { + + tmp, err := ioutil.TempDir("", "out-of-tree_") + if err != nil { + return + } + defer os.RemoveAll(tmp) + + var artifact string + if exists(cache) { + artifact = cache + } else { + artifact, err = buildPreload(workPath, tmp, ki, dockerTimeout) + if err != nil { + return + } + if cache != "" { + err = copyFile(artifact, cache) + if err != nil { + return + } + } + } + + output, err := q.CopyAndInsmod(artifact) + if err != nil { + log.Println(output) + return + } + return +} + +func buildPreload(workPath, tmp string, ki config.KernelInfo, + dockerTimeout time.Duration) (artifact string, err error) { + + ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml") + if err != nil { + return + } + + ka.SourcePath = workPath + + km := config.KernelMask{DistroType: ki.DistroType, + DistroRelease: ki.DistroRelease, + ReleaseMask: ki.KernelRelease, + } + ka.SupportedKernels = []config.KernelMask{km} + + if ka.Docker.Timeout.Duration != 0 { + dockerTimeout = ka.Docker.Timeout.Duration + } + + artifact, _, err = build(tmp, ka, ki, dockerTimeout) + return +} + +func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, err error) { + usr, err := user.Current() + if err != nil { + return + } + base := filepath.Join(usr.HomeDir, "/.out-of-tree/preload/") + workPath = filepath.Join(base, "/repos/", sha1sum(repo)) + + var r *git.Repository + if exists(workPath) { + r, err = git.PlainOpen(workPath) + if err != nil { + return + } + + var w *git.Worktree + w, err = r.Worktree() + if err != nil { + return + } + + err = w.Pull(&git.PullOptions{}) + if err != nil && err != git.NoErrAlreadyUpToDate { + log.Println(repo, "pull error:", err) + } + } else { + r, err = git.PlainClone(workPath, false, &git.CloneOptions{URL: repo}) + if err != nil { + return + } + } + + ref, err := r.Head() + if err != nil { + return + } + + cachedir := filepath.Join(base, "/cache/") + os.MkdirAll(cachedir, 0700) + + filename := sha1sum(repo + ki.KernelPath + ref.Hash().String()) + cache = filepath.Join(cachedir, filename) + return +} + +func sha1sum(data string) string { + h := sha1.Sum([]byte(data)) + return hex.EncodeToString(h[:]) +}