|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | + Portions from https://github.com/moby/moby/blob/v23.0.1/pkg/archive/archive.go#L419-L464 |
| 19 | + Copyright (C) Docker/Moby authors. |
| 20 | + Licensed under the Apache License, Version 2.0 |
| 21 | + NOTICE: https://github.com/moby/moby/blob/v23.0.1/NOTICE |
| 22 | +*/ |
| 23 | + |
| 24 | +package tarheader |
| 25 | + |
| 26 | +import ( |
| 27 | + "archive/tar" |
| 28 | + "os" |
| 29 | +) |
| 30 | + |
| 31 | +// nosysFileInfo hides the system-dependent info of the wrapped FileInfo to |
| 32 | +// prevent tar.FileInfoHeader from introspecting it and potentially calling into |
| 33 | +// glibc. |
| 34 | +// |
| 35 | +// From https://github.com/moby/moby/blob/v23.0.1/pkg/archive/archive.go#L419-L434 . |
| 36 | +type nosysFileInfo struct { |
| 37 | + os.FileInfo |
| 38 | +} |
| 39 | + |
| 40 | +func (fi nosysFileInfo) Sys() interface{} { |
| 41 | + // A Sys value of type *tar.Header is safe as it is system-independent. |
| 42 | + // The tar.FileInfoHeader function copies the fields into the returned |
| 43 | + // header without performing any OS lookups. |
| 44 | + if sys, ok := fi.FileInfo.Sys().(*tar.Header); ok { |
| 45 | + return sys |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// sysStat, if non-nil, populates hdr from system-dependent fields of fi. |
| 51 | +// |
| 52 | +// From https://github.com/moby/moby/blob/v23.0.1/pkg/archive/archive.go#L436-L437 . |
| 53 | +var sysStat func(fi os.FileInfo, hdr *tar.Header) error |
| 54 | + |
| 55 | +// FileInfoHeaderNoLookups creates a partially-populated tar.Header from fi. |
| 56 | +// |
| 57 | +// Compared to the archive/tar.FileInfoHeader function, this function is safe to |
| 58 | +// call from a chrooted process as it does not populate fields which would |
| 59 | +// require operating system lookups. It behaves identically to |
| 60 | +// tar.FileInfoHeader when fi is a FileInfo value returned from |
| 61 | +// tar.Header.FileInfo(). |
| 62 | +// |
| 63 | +// When fi is a FileInfo for a native file, such as returned from os.Stat() and |
| 64 | +// os.Lstat(), the returned Header value differs from one returned from |
| 65 | +// tar.FileInfoHeader in the following ways. The Uname and Gname fields are not |
| 66 | +// set as OS lookups would be required to populate them. The AccessTime and |
| 67 | +// ChangeTime fields are not currently set (not yet implemented) although that |
| 68 | +// is subject to change. Callers which require the AccessTime or ChangeTime |
| 69 | +// fields to be zeroed should explicitly zero them out in the returned Header |
| 70 | +// value to avoid any compatibility issues in the future. |
| 71 | +// |
| 72 | +// From https://github.com/moby/moby/blob/v23.0.1/pkg/archive/archive.go#L439-L464 . |
| 73 | +func FileInfoHeaderNoLookups(fi os.FileInfo, link string) (*tar.Header, error) { |
| 74 | + hdr, err := tar.FileInfoHeader(nosysFileInfo{fi}, link) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + if sysStat != nil { |
| 79 | + return hdr, sysStat(fi, hdr) |
| 80 | + } |
| 81 | + return hdr, nil |
| 82 | +} |
0 commit comments