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