Skip to content

Commit 5e49ec2

Browse files
committed
Use http.Get to download binaries instead of exec.Command
Signed-off-by: AdamKorcz <[email protected]>
1 parent 11a90c7 commit 5e49ec2

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

contrib/fuzz/container_fuzzer.go

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,55 +32,68 @@ import (
3232
"github.com/containerd/containerd/sys"
3333
"io"
3434
"io/ioutil"
35+
"net/http"
3536
"os"
3637
"os/exec"
3738
"strings"
3839
"time"
3940
)
4041

4142
var (
42-
haveInstalledWget = false
4343
haveDownloadedbinaries = false
4444
haveExtractedBinaries = false
4545
haveChangedPATH = false
4646
haveInitialized = false
47+
48+
downloadLink = "https://github.com/containerd/containerd/releases/download/v1.5.4/containerd-1.5.4-linux-amd64.tar.gz"
49+
downloadPath = "/tmp/containerd-1.5.4-linux-amd64.tar.gz"
50+
binariesDir = "/tmp/containerd-binaries"
4751
)
4852

53+
// downloadFile downloads a file from a url
54+
func downloadFile(filepath string, url string) (err error) {
55+
56+
out, err := os.Create(filepath)
57+
if err != nil {
58+
return err
59+
}
60+
defer out.Close()
61+
62+
resp, err := http.Get(url)
63+
if err != nil {
64+
return err
65+
}
66+
defer resp.Body.Close()
67+
68+
_, err = io.Copy(out, resp.Body)
69+
if err != nil {
70+
return err
71+
}
72+
73+
return nil
74+
}
75+
4976
// initInSteps() performs initialization in several steps
5077
// The reason for spreading the initialization out in
5178
// multiple steps is that each fuzz iteration can maximum
5279
// take 25 seconds when running through OSS-fuzz.
5380
// Should an iteration exceed that, then the fuzzer stops.
5481
func initInSteps() bool {
55-
// Install wget
56-
if !haveInstalledWget {
57-
cmd := exec.Command("apt-get", "install", "-y", "wget")
58-
err := cmd.Run()
59-
if err != nil {
60-
return true
61-
}
62-
haveInstalledWget = true
63-
return true
64-
}
6582
// Download binaries
6683
if !haveDownloadedbinaries {
67-
tarLink := "https://github.com/containerd/containerd/releases/download/v1.5.4/containerd-1.5.4-linux-amd64.tar.gz"
68-
cmd := exec.Command("wget", tarLink)
69-
err := cmd.Run()
84+
err := downloadFile(downloadPath, downloadLink)
7085
if err != nil {
71-
return true
86+
panic(err)
7287
}
7388
haveDownloadedbinaries = true
74-
return true
7589
}
7690
// Extract binaries
77-
binariesDir := "/tmp/containerd-binaries"
7891
if !haveExtractedBinaries {
7992
err := os.MkdirAll(binariesDir, 0777)
8093
if err != nil {
8194
return true
8295
}
83-
cmd := exec.Command("tar", "xvf", "containerd-1.5.4-linux-amd64.tar.gz", "-C", binariesDir)
96+
cmd := exec.Command("tar", "xvf", downloadPath, "-C", binariesDir)
8497
err = cmd.Run()
8598
if err != nil {
8699
return true
@@ -91,7 +104,7 @@ func initInSteps() bool {
91104
// Add binaries to $PATH:
92105
if !haveChangedPATH {
93106
oldPathEnv := os.Getenv("PATH")
94-
newPathEnv := fmt.Sprintf("%s:%s/bin", oldPathEnv, binariesDir)
107+
newPathEnv := fmt.Sprintf("%s/bin:%s", binariesDir, oldPathEnv)
95108
err := os.Setenv("PATH", newPathEnv)
96109
if err != nil {
97110
return true

0 commit comments

Comments
 (0)