2018-02-05 21:05:59 +00:00
|
|
|
package chrootarchive // import "github.com/docker/docker/pkg/chrootarchive"
|
2014-10-29 19:06:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2014-12-08 21:19:24 +00:00
|
|
|
"path/filepath"
|
2014-10-29 19:06:51 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-10-14 18:35:48 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2014-10-29 19:06:51 +00:00
|
|
|
)
|
|
|
|
|
2017-05-24 15:53:41 +00:00
|
|
|
// NewArchiver returns a new Archiver which uses chrootarchive.Untar
|
2022-03-14 19:24:29 +00:00
|
|
|
func NewArchiver(idMapping idtools.IdentityMapping) *archive.Archiver {
|
2017-08-04 00:22:00 +00:00
|
|
|
return &archive.Archiver{
|
2017-11-16 06:20:33 +00:00
|
|
|
Untar: Untar,
|
|
|
|
IDMapping: idMapping,
|
2017-08-04 00:22:00 +00:00
|
|
|
}
|
2017-05-24 15:53:41 +00:00
|
|
|
}
|
2014-12-08 21:14:56 +00:00
|
|
|
|
2015-06-16 09:51:27 +00:00
|
|
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive may be compressed with one of the following algorithms:
|
2022-07-08 16:27:07 +00:00
|
|
|
// identity (uncompressed), gzip, bzip2, xz.
|
2014-12-08 21:14:56 +00:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
2019-05-30 18:15:09 +00:00
|
|
|
return untarHandler(tarArchive, dest, options, true, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UntarWithRoot is the same as `Untar`, but allows you to pass in a root directory
|
|
|
|
// The root directory is the directory that will be chrooted to.
|
|
|
|
// `dest` must be a path within `root`, if it is not an error will be returned.
|
|
|
|
//
|
|
|
|
// `root` should set to a directory which is not controlled by any potentially
|
|
|
|
// malicious process.
|
|
|
|
//
|
|
|
|
// This should be used to prevent a potential attacker from manipulating `dest`
|
|
|
|
// such that it would provide access to files outside of `dest` through things
|
|
|
|
// like symlinks. Normally `ResolveSymlinksInScope` would handle this, however
|
|
|
|
// sanitizing symlinks in this manner is inherrently racey:
|
|
|
|
// ref: CVE-2018-15664
|
|
|
|
func UntarWithRoot(tarArchive io.Reader, dest string, options *archive.TarOptions, root string) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, true, root)
|
2015-07-27 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive must be an uncompressed stream.
|
|
|
|
func UntarUncompressed(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
2019-05-30 18:15:09 +00:00
|
|
|
return untarHandler(tarArchive, dest, options, false, dest)
|
2015-07-27 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for teasing out the automatic decompression
|
2019-05-30 18:15:09 +00:00
|
|
|
func untarHandler(tarArchive io.Reader, dest string, options *archive.TarOptions, decompress bool, root string) error {
|
2014-12-08 21:14:56 +00:00
|
|
|
if tarArchive == nil {
|
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
if options == nil {
|
|
|
|
options = &archive.TarOptions{}
|
|
|
|
}
|
2014-10-23 21:30:11 +00:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 21:14:56 +00:00
|
|
|
}
|
2014-10-29 19:06:51 +00:00
|
|
|
|
2021-05-19 23:51:35 +00:00
|
|
|
// If dest is inside a root then directory is created within chroot by extractor.
|
|
|
|
// This case is only currently used by cp.
|
|
|
|
if dest == root {
|
2022-03-14 19:24:29 +00:00
|
|
|
rootIDs := options.IDMap.RootPair()
|
2015-10-14 18:35:48 +00:00
|
|
|
|
2021-05-19 23:51:35 +00:00
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
2023-08-24 15:56:24 +00:00
|
|
|
if err := idtools.MkdirAllAndChownNew(dest, 0o755, rootIDs); err != nil {
|
2021-05-19 23:51:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-10-29 19:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-29 15:28:44 +00:00
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
r := io.NopCloser(tarArchive)
|
2015-07-27 13:46:20 +00:00
|
|
|
if decompress {
|
|
|
|
decompressedArchive, err := archive.DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
|
|
|
r = decompressedArchive
|
2014-12-08 21:19:24 +00:00
|
|
|
}
|
2014-11-08 15:38:42 +00:00
|
|
|
|
2019-05-30 18:15:09 +00:00
|
|
|
return invokeUnpack(r, dest, options, root)
|
2014-10-29 19:06:51 +00:00
|
|
|
}
|
2019-05-30 21:55:52 +00:00
|
|
|
|
|
|
|
// Tar tars the requested path while chrooted to the specified root.
|
|
|
|
func Tar(srcPath string, options *archive.TarOptions, root string) (io.ReadCloser, error) {
|
|
|
|
if options == nil {
|
|
|
|
options = &archive.TarOptions{}
|
|
|
|
}
|
|
|
|
return invokePack(srcPath, options, root)
|
|
|
|
}
|