Use http.Get to download binaries instead of exec.Command

Signed-off-by: AdamKorcz <adam@adalogics.com>
This commit is contained in:
AdamKorcz 2021-08-10 12:48:33 +01:00
parent 11a90c7ff4
commit 5e49ec27dc

View File

@ -32,6 +32,7 @@ import (
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/sys"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -39,48 +40,60 @@ import (
) )
var ( var (
haveInstalledWget = false
haveDownloadedbinaries = false haveDownloadedbinaries = false
haveExtractedBinaries = false haveExtractedBinaries = false
haveChangedPATH = false haveChangedPATH = false
haveInitialized = false haveInitialized = false
downloadLink = "https://github.com/containerd/containerd/releases/download/v1.5.4/containerd-1.5.4-linux-amd64.tar.gz"
downloadPath = "/tmp/containerd-1.5.4-linux-amd64.tar.gz"
binariesDir = "/tmp/containerd-binaries"
) )
// downloadFile downloads a file from a url
func downloadFile(filepath string, url string) (err error) {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
// initInSteps() performs initialization in several steps // initInSteps() performs initialization in several steps
// The reason for spreading the initialization out in // The reason for spreading the initialization out in
// multiple steps is that each fuzz iteration can maximum // multiple steps is that each fuzz iteration can maximum
// take 25 seconds when running through OSS-fuzz. // take 25 seconds when running through OSS-fuzz.
// Should an iteration exceed that, then the fuzzer stops. // Should an iteration exceed that, then the fuzzer stops.
func initInSteps() bool { func initInSteps() bool {
// Install wget
if !haveInstalledWget {
cmd := exec.Command("apt-get", "install", "-y", "wget")
err := cmd.Run()
if err != nil {
return true
}
haveInstalledWget = true
return true
}
// Download binaries // Download binaries
if !haveDownloadedbinaries { if !haveDownloadedbinaries {
tarLink := "https://github.com/containerd/containerd/releases/download/v1.5.4/containerd-1.5.4-linux-amd64.tar.gz" err := downloadFile(downloadPath, downloadLink)
cmd := exec.Command("wget", tarLink)
err := cmd.Run()
if err != nil { if err != nil {
return true panic(err)
} }
haveDownloadedbinaries = true haveDownloadedbinaries = true
return true
} }
// Extract binaries // Extract binaries
binariesDir := "/tmp/containerd-binaries"
if !haveExtractedBinaries { if !haveExtractedBinaries {
err := os.MkdirAll(binariesDir, 0777) err := os.MkdirAll(binariesDir, 0777)
if err != nil { if err != nil {
return true return true
} }
cmd := exec.Command("tar", "xvf", "containerd-1.5.4-linux-amd64.tar.gz", "-C", binariesDir) cmd := exec.Command("tar", "xvf", downloadPath, "-C", binariesDir)
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
return true return true
@ -91,7 +104,7 @@ func initInSteps() bool {
// Add binaries to $PATH: // Add binaries to $PATH:
if !haveChangedPATH { if !haveChangedPATH {
oldPathEnv := os.Getenv("PATH") oldPathEnv := os.Getenv("PATH")
newPathEnv := fmt.Sprintf("%s:%s/bin", oldPathEnv, binariesDir) newPathEnv := fmt.Sprintf("%s/bin:%s", binariesDir, oldPathEnv)
err := os.Setenv("PATH", newPathEnv) err := os.Setenv("PATH", newPathEnv)
if err != nil { if err != nil {
return true return true