9e205c
From 840e7dad513b86f454573ad415701c0199f78d30 Mon Sep 17 00:00:00 2001
9e205c
From: TomSweeneyRedHat <tsweeney@redhat.com>
9e205c
Date: Tue, 24 Mar 2020 20:10:22 -0400
9e205c
Subject: [PATCH] Fix potential CVE in tarfile w/ symlink
9e205c
9e205c
Stealing @nalind 's workaround to avoid refetching
9e205c
content after a file read failure.  Under the right
9e205c
circumstances that could be a symlink to a file meant
9e205c
to overwrite a good file with bad data.
9e205c
9e205c
Testing:
9e205c
```
9e205c
goodstuff
9e205c
9e205c
[1] 14901
9e205c
9e205c
127.0.0.1 - - [24/Mar/2020 20:15:50] "GET / HTTP/1.1" 200 -
9e205c
127.0.0.1 - - [24/Mar/2020 20:15:50] "GET / HTTP/1.1" 200 -
9e205c
no FROM statement found
9e205c
9e205c
goodstuff
9e205c
```
9e205c
9e205c
Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>
9e205c
---
9e205c
 imagebuildah/util.go | 5 +++--
9e205c
 1 file changed, 3 insertions(+), 2 deletions(-)
9e205c
9e205c
diff --git a/imagebuildah/util.go b/imagebuildah/util.go
9e205c
index 29ea60970..5f14c9883 100644
9e205c
--- a/imagebuildah/util.go
9e205c
+++ b/imagebuildah/util.go
9e205c
@@ -14,6 +14,7 @@ import (
9e205c
 
9e205c
 	"github.com/containers/buildah"
9e205c
 	"github.com/containers/storage/pkg/chrootarchive"
9e205c
+	"github.com/containers/storage/pkg/ioutils"
9e205c
 	"github.com/opencontainers/runtime-spec/specs-go"
9e205c
 	"github.com/pkg/errors"
9e205c
 	"github.com/sirupsen/logrus"
9e205c
@@ -57,7 +58,7 @@ func downloadToDirectory(url, dir string) error {
9e205c
 		}
9e205c
 		dockerfile := filepath.Join(dir, "Dockerfile")
9e205c
 		// Assume this is a Dockerfile
9e205c
-		if err := ioutil.WriteFile(dockerfile, body, 0600); err != nil {
9e205c
+		if err := ioutils.AtomicWriteFile(dockerfile, body, 0600); err != nil {
9e205c
 			return errors.Wrapf(err, "Failed to write %q to %q", url, dockerfile)
9e205c
 		}
9e205c
 	}
9e205c
@@ -75,7 +76,7 @@ func stdinToDirectory(dir string) error {
9e205c
 	if err := chrootarchive.Untar(reader, dir, nil); err != nil {
9e205c
 		dockerfile := filepath.Join(dir, "Dockerfile")
9e205c
 		// Assume this is a Dockerfile
9e205c
-		if err := ioutil.WriteFile(dockerfile, b, 0600); err != nil {
9e205c
+		if err := ioutils.AtomicWriteFile(dockerfile, b, 0600); err != nil {
9e205c
 			return errors.Wrapf(err, "Failed to write bytes to %q", dockerfile)
9e205c
 		}
9e205c
 	}