github.com/moby/docker@v26.1.3+incompatible/pkg/archive/diff.go (about)

     1  package archive // import "github.com/docker/docker/pkg/archive"
     2  
     3  import (
     4  	"archive/tar"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"strings"
    12  
    13  	"github.com/containerd/log"
    14  	"github.com/docker/docker/pkg/pools"
    15  	"github.com/docker/docker/pkg/system"
    16  )
    17  
    18  // UnpackLayer unpack `layer` to a `dest`. The stream `layer` can be
    19  // compressed or uncompressed.
    20  // Returns the size in bytes of the contents of the layer.
    21  func UnpackLayer(dest string, layer io.Reader, options *TarOptions) (size int64, err error) {
    22  	tr := tar.NewReader(layer)
    23  	trBuf := pools.BufioReader32KPool.Get(tr)
    24  	defer pools.BufioReader32KPool.Put(trBuf)
    25  
    26  	var dirs []*tar.Header
    27  	unpackedPaths := make(map[string]struct{})
    28  
    29  	if options == nil {
    30  		options = &TarOptions{}
    31  	}
    32  	if options.ExcludePatterns == nil {
    33  		options.ExcludePatterns = []string{}
    34  	}
    35  
    36  	aufsTempdir := ""
    37  	aufsHardlinks := make(map[string]*tar.Header)
    38  
    39  	// Iterate through the files in the archive.
    40  	for {
    41  		hdr, err := tr.Next()
    42  		if err == io.EOF {
    43  			// end of tar archive
    44  			break
    45  		}
    46  		if err != nil {
    47  			return 0, err
    48  		}
    49  
    50  		size += hdr.Size
    51  
    52  		// Normalize name, for safety and for a simple is-root check
    53  		hdr.Name = filepath.Clean(hdr.Name)
    54  
    55  		// Windows does not support filenames with colons in them. Ignore
    56  		// these files. This is not a problem though (although it might
    57  		// appear that it is). Let's suppose a client is running docker pull.
    58  		// The daemon it points to is Windows. Would it make sense for the
    59  		// client to be doing a docker pull Ubuntu for example (which has files
    60  		// with colons in the name under /usr/share/man/man3)? No, absolutely
    61  		// not as it would really only make sense that they were pulling a
    62  		// Windows image. However, for development, it is necessary to be able
    63  		// to pull Linux images which are in the repository.
    64  		//
    65  		// TODO Windows. Once the registry is aware of what images are Windows-
    66  		// specific or Linux-specific, this warning should be changed to an error
    67  		// to cater for the situation where someone does manage to upload a Linux
    68  		// image but have it tagged as Windows inadvertently.
    69  		if runtime.GOOS == "windows" {
    70  			if strings.Contains(hdr.Name, ":") {
    71  				log.G(context.TODO()).Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name)
    72  				continue
    73  			}
    74  		}
    75  
    76  		// Ensure that the parent directory exists.
    77  		err = createImpliedDirectories(dest, hdr, options)
    78  		if err != nil {
    79  			return 0, err
    80  		}
    81  
    82  		// Skip AUFS metadata dirs
    83  		if strings.HasPrefix(hdr.Name, WhiteoutMetaPrefix) {
    84  			// Regular files inside /.wh..wh.plnk can be used as hardlink targets
    85  			// We don't want this directory, but we need the files in them so that
    86  			// such hardlinks can be resolved.
    87  			if strings.HasPrefix(hdr.Name, WhiteoutLinkDir) && hdr.Typeflag == tar.TypeReg {
    88  				basename := filepath.Base(hdr.Name)
    89  				aufsHardlinks[basename] = hdr
    90  				if aufsTempdir == "" {
    91  					if aufsTempdir, err = os.MkdirTemp(dest, "dockerplnk"); err != nil {
    92  						return 0, err
    93  					}
    94  					defer os.RemoveAll(aufsTempdir)
    95  				}
    96  				if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, options); err != nil {
    97  					return 0, err
    98  				}
    99  			}
   100  
   101  			if hdr.Name != WhiteoutOpaqueDir {
   102  				continue
   103  			}
   104  		}
   105  		//#nosec G305 -- The joined path is guarded against path traversal.
   106  		path := filepath.Join(dest, hdr.Name)
   107  		rel, err := filepath.Rel(dest, path)
   108  		if err != nil {
   109  			return 0, err
   110  		}
   111  
   112  		// Note as these operations are platform specific, so must the slash be.
   113  		if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
   114  			return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
   115  		}
   116  		base := filepath.Base(path)
   117  
   118  		if strings.HasPrefix(base, WhiteoutPrefix) {
   119  			dir := filepath.Dir(path)
   120  			if base == WhiteoutOpaqueDir {
   121  				_, err := os.Lstat(dir)
   122  				if err != nil {
   123  					return 0, err
   124  				}
   125  				err = filepath.WalkDir(dir, func(path string, info os.DirEntry, err error) error {
   126  					if err != nil {
   127  						if os.IsNotExist(err) {
   128  							err = nil // parent was deleted
   129  						}
   130  						return err
   131  					}
   132  					if path == dir {
   133  						return nil
   134  					}
   135  					if _, exists := unpackedPaths[path]; !exists {
   136  						return os.RemoveAll(path)
   137  					}
   138  					return nil
   139  				})
   140  				if err != nil {
   141  					return 0, err
   142  				}
   143  			} else {
   144  				originalBase := base[len(WhiteoutPrefix):]
   145  				originalPath := filepath.Join(dir, originalBase)
   146  				if err := os.RemoveAll(originalPath); err != nil {
   147  					return 0, err
   148  				}
   149  			}
   150  		} else {
   151  			// If path exits we almost always just want to remove and replace it.
   152  			// The only exception is when it is a directory *and* the file from
   153  			// the layer is also a directory. Then we want to merge them (i.e.
   154  			// just apply the metadata from the layer).
   155  			if fi, err := os.Lstat(path); err == nil {
   156  				if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
   157  					if err := os.RemoveAll(path); err != nil {
   158  						return 0, err
   159  					}
   160  				}
   161  			}
   162  
   163  			trBuf.Reset(tr)
   164  			srcData := io.Reader(trBuf)
   165  			srcHdr := hdr
   166  
   167  			// Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
   168  			// we manually retarget these into the temporary files we extracted them into
   169  			if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), WhiteoutLinkDir) {
   170  				linkBasename := filepath.Base(hdr.Linkname)
   171  				srcHdr = aufsHardlinks[linkBasename]
   172  				if srcHdr == nil {
   173  					return 0, fmt.Errorf("Invalid aufs hardlink")
   174  				}
   175  				tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
   176  				if err != nil {
   177  					return 0, err
   178  				}
   179  				defer tmpFile.Close()
   180  				srcData = tmpFile
   181  			}
   182  
   183  			if err := remapIDs(options.IDMap, srcHdr); err != nil {
   184  				return 0, err
   185  			}
   186  
   187  			if err := createTarFile(path, dest, srcHdr, srcData, options); err != nil {
   188  				return 0, err
   189  			}
   190  
   191  			// Directory mtimes must be handled at the end to avoid further
   192  			// file creation in them to modify the directory mtime
   193  			if hdr.Typeflag == tar.TypeDir {
   194  				dirs = append(dirs, hdr)
   195  			}
   196  			unpackedPaths[path] = struct{}{}
   197  		}
   198  	}
   199  
   200  	for _, hdr := range dirs {
   201  		//#nosec G305 -- The header was checked for path traversal before it was appended to the dirs slice.
   202  		path := filepath.Join(dest, hdr.Name)
   203  		if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
   204  			return 0, err
   205  		}
   206  	}
   207  
   208  	return size, nil
   209  }
   210  
   211  // ApplyLayer parses a diff in the standard layer format from `layer`,
   212  // and applies it to the directory `dest`. The stream `layer` can be
   213  // compressed or uncompressed.
   214  // Returns the size in bytes of the contents of the layer.
   215  func ApplyLayer(dest string, layer io.Reader) (int64, error) {
   216  	return applyLayerHandler(dest, layer, &TarOptions{}, true)
   217  }
   218  
   219  // ApplyUncompressedLayer parses a diff in the standard layer format from
   220  // `layer`, and applies it to the directory `dest`. The stream `layer`
   221  // can only be uncompressed.
   222  // Returns the size in bytes of the contents of the layer.
   223  func ApplyUncompressedLayer(dest string, layer io.Reader, options *TarOptions) (int64, error) {
   224  	return applyLayerHandler(dest, layer, options, false)
   225  }
   226  
   227  // IsEmpty checks if the tar archive is empty (doesn't contain any entries).
   228  func IsEmpty(rd io.Reader) (bool, error) {
   229  	decompRd, err := DecompressStream(rd)
   230  	if err != nil {
   231  		return true, fmt.Errorf("failed to decompress archive: %v", err)
   232  	}
   233  	defer decompRd.Close()
   234  
   235  	tarReader := tar.NewReader(decompRd)
   236  	if _, err := tarReader.Next(); err != nil {
   237  		if err == io.EOF {
   238  			return true, nil
   239  		}
   240  		return false, fmt.Errorf("failed to read next archive header: %v", err)
   241  	}
   242  
   243  	return false, nil
   244  }
   245  
   246  // do the bulk load of ApplyLayer, but allow for not calling DecompressStream
   247  func applyLayerHandler(dest string, layer io.Reader, options *TarOptions, decompress bool) (int64, error) {
   248  	dest = filepath.Clean(dest)
   249  
   250  	// We need to be able to set any perms
   251  	restore := overrideUmask(0)
   252  	defer restore()
   253  
   254  	if decompress {
   255  		decompLayer, err := DecompressStream(layer)
   256  		if err != nil {
   257  			return 0, err
   258  		}
   259  		defer decompLayer.Close()
   260  		layer = decompLayer
   261  	}
   262  	return UnpackLayer(dest, layer, options)
   263  }