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