Browse code

archive: prevent breakout in ApplyLayer

Signed-off-by: Tibor Vass <teabee89@gmail.com>

Tibor Vass authored on 2014/11/01 02:18:39
Showing 1 changed files
... ...
@@ -18,6 +18,8 @@ import (
18 18
 // ApplyLayer parses a diff in the standard layer format from `layer`, and
19 19
 // applies it to the directory `dest`.
20 20
 func ApplyLayer(dest string, layer ArchiveReader) error {
21
+	dest = filepath.Clean(dest)
22
+
21 23
 	// We need to be able to set any perms
22 24
 	oldmask, err := system.Umask(0)
23 25
 	if err != nil {
... ...
@@ -91,6 +93,12 @@ func ApplyLayer(dest string, layer ArchiveReader) error {
91 91
 
92 92
 		path := filepath.Join(dest, hdr.Name)
93 93
 		base := filepath.Base(path)
94
+
95
+		// Prevent symlink breakout
96
+		if !strings.HasPrefix(path, dest) {
97
+			return breakoutError(fmt.Errorf("%q is outside of %q", path, dest))
98
+		}
99
+
94 100
 		if strings.HasPrefix(base, ".wh.") {
95 101
 			originalBase := base[len(".wh."):]
96 102
 			originalPath := filepath.Join(filepath.Dir(path), originalBase)