Commit Graph

169 Commits

Author SHA1 Message Date
Kir Kolyshkin b7fb12e894 Remove unneeded conversion
Those are the cases where the value being converted is already of that
type (checked to be that way for all os/arch combinations).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-01 16:18:43 -07:00
Kir Kolyshkin c3ff7f58df Use any instead of interface{}
It's available since Go 1.18 (see https://pkg.go.dev/builtin#any).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-31 15:37:25 -07:00
Kir Kolyshkin 5d509c5e64 Apply De Morgan's law
This fixes a bunch of linter warnings like this:

> QF1001: could apply De Morgan's law

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-26 18:43:02 -07:00
Han-Wen Nienhuys d39dba2d92 archive: introduce tarWithOptionsTo goroutine
Gives a name to the anonymous goroutine populating the archive, and
makes parameter dependencies explicit, in preparation to moving it
out. No functional changes.

Signed-off-by: Han-Wen Nienhuys <hanwen@engflow.com>
2025-03-17 14:29:34 +01:00
Han-Wen Nienhuys bf8ac771fb pkg/archive: don't write TarOptions in TarWithOptions
Before, this was breaking the convention that options passed by
reference are not mutated. In particular, TestOverlayTarUntar passes
the same object to both TarWithOptions and Untar, resulting in a race
detector failure.

Signed-off-by: Han-Wen Nienhuys <hanwen@engflow.com>
2025-03-17 14:29:34 +01:00
Han-Wen Nienhuys 3eac9030ff pkg/archive: drop gratuitious writes to TarOptions.ExcludePatterns.
A nil and empty slice are equivalent, except resulting JSON
serialization.  There is JSON serialization in applyLayerHandler(),
but it's internal to the storage package and therefore not subject to
compatibility promises.

Signed-off-by: Han-Wen Nienhuys <hanwen@engflow.com>
2025-03-17 14:29:34 +01:00
Adam Eijdenberg f51eeb9662 feat: add Timestamp to TarOptions
This allows overriding of timestamps for entries within an archive.

Signed-off-by: Adam Eijdenberg <adam@continusec.com>
2025-02-20 07:18:07 +00:00
openshift-merge-bot[bot] 141b059b31
Merge pull request #2219 from hanwen-flow/uvvttvyy
archive: regularize some naming
2025-02-14 09:30:10 +00:00
Nalin Dahyabhai cd5ef3f211 Fix compilation of pkg/archive tests
Unixy systems all have Lutimes(), and this code should have been stubbed
out for non-Unixy systems.

h/t @kolyshkin

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-02-04 16:08:02 -05:00
Han-Wen Nienhuys 42beb84f5d archive: regularize some naming
* tarAppender did not append to tar files

* createTarFile did not create tar files

* addTarFile did not add tar files

Signed-off-by: Han-Wen Nienhuys <hanwen@engflow.com>
2025-02-04 19:10:49 +01:00
Giuseppe Scrivano f5c1ad9129
archive: detect symlink target changes
improve detection of changes in symlinks when both the size and the
mtime were changed.

Previously, the detection relied on a difference in either size or
atime/mtime to identify changes in a symlink.  This is not enough, as
the atime/mtime and it size could be left unaltered while only its
target changes.

Fixes: https://github.com/containers/buildah/issues/5861

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-12-18 14:24:02 +01:00
Giuseppe Scrivano 3fbe90a217
archive: store the override xattr with the inode type
Fixes: https://github.com/containers/storage/issues/2174

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-12-06 08:50:08 +01:00
Minseo Kim 5015968dd8 pkg/system: fix `EOPNOTSUPP` to `ENOTSUP` for xattr syscalls
The lgetxattr(2), lsetxattr(2), and llistxattr(2) syscalls on Linux
return ENOTSUP instead of EOPNOTSUPP. The same applies to getxattr(2),
setxattr(2), and listxattr(2) on macOS.

Note that EOPNOTSUPP and ENOTSUP have the same value in Linux (refer to
errno(3)).

Signed-off-by: Minseo Kim <kimminss0@outlook.kr>
2024-11-29 18:32:32 +09:00
Minseo Kim be173a9fab pkg/system: add xattr syscalls on FreeBSD
FreeBSD does not offer xattr system calls, but it can be mimicked using
extattr system calls.

Signed-off-by: Minseo Kim <kimminss0@outlook.kr>
2024-11-29 18:32:32 +09:00
Minseo Kim 28548e9e10 pkg/archive: add support for applying xattr on FreeBSD during layer unpacking
This commit enables applying extended file attributes (xattr) to files
during the unpacking of layers.

The implementation mimics how FreeBSD's linux compatibility layer and
BSD tar handles xattr using extattr system calls.  Linux and FreeBSD
supports different namespaces for extended attributes:

- Linux: security, system, trusted, user
- FreeBSD: system, user

The `system` and `user` namespaces are directly mapped between the two
systems. For unsupported namespaces on FreeBSD, such as `security` and
`trusted`, the attributes are just dropped.

Signed-off-by: Minseo Kim <kimminss0@outlook.kr>
2024-11-16 14:01:46 +09:00
Jan Rodák d8c5cc5428
Fix deprecated use of hdr.Xattrs (SA1019)
Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2024-06-26 20:12:43 +02:00
Jan Rodák ee13fd9d11 Fix deprecated use of tar.TypeRegA (SA1019)
Signed-off-by: Jan Rodák <hony.com@seznam.cz>
2024-06-20 11:44:32 -04:00
Giuseppe Scrivano ae8836fa44
archive: use zstd if available
the performance improvement is not as clear as with pigz, but it is
still measurable difference.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-06-11 16:17:54 +02:00
Giuseppe Scrivano b3319f0ef7
archive: use pigz if available
use the pigz command line tool when available as it is much faster to
decompress a gzip stream.

On my machine I've seen a 50% pull time reduction when pulling some
big images.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-06-11 16:17:54 +02:00
Giuseppe Scrivano 17cd45f150
archive: do not leak buffer on errors
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-06-11 16:17:54 +02:00
Akihiko Odaki b3a2f6a07a idtools: Use SetContainersOverrideXattr()
Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
2024-06-11 15:36:10 +09:00
Akihiko Odaki 96545b8496 archive: always fix mode for root dir with ForceMask
We used to override the root directory permission only when the record
for the root directory exists in the archive. However,
docker.io/ubuntu:jammy-20240427 lacks such a record and not overriding
the root directory permission makes all other permission overrides
invalid.

Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
2024-05-29 08:42:06 +09:00
Giuseppe Scrivano 2b26cbfd37
archive: use fileutils.{Le,E}xists()
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-04-08 16:07:27 +02:00
Christopher Homberger 19e5e83c32 Stop putting `user.overlay.*` into container layer
Signed-off-by: Christopher Homberger <christopher.homberger@web.de>
2024-02-28 20:59:15 +00:00
Giuseppe Scrivano e024854ba3
pkg/archive: avoid user lookups when generating tarball
This change introduces a new approach to generating tar headers in a way
that avoids system-dependent lookups and potential calls to glibc, enhancing
portability and security.

The same logic is used by Moby, and the override function is based on
code from Moby.

Closes: https://github.com/containers/storage/issues/1836

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-02-23 09:21:03 +01:00
Tom Pohl 93deea6953 refactor: change socket warning to info
Signed-off-by: Tom Pohl <thomas.pohl@coherentminds.de>
2023-12-18 13:14:41 +01:00
Miloslav Trmač 819ac44dc6 Don't expose account names when creating tar files with hard-coded account IDs
A prerequisite for fixing https://github.com/containers/image/issues/1627 .

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2023-11-28 20:38:53 +01:00
Giuseppe Scrivano ff5851dcad
archive: fix mode for root dir with ForceMask
if force_mask is in use, we need to store the root directory
permission after we read it from the tar archive.  We were incorrectly
reading it from the directory on the filesystem.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2023-10-29 22:08:50 +01:00
Daniel J Walsh f9be80cf2f
Remove use of fillGo18FileTypeBits
Fixes for https://github.com/containers/buildah/pull/4772

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-06-28 07:56:29 -04:00
Kir Kolyshkin a4d8f720a2 Format sources with gofumpt
gofumpt is a superset of gofmt, enabling some more code formatting
rules.

This commit is brought to you by

	gofumpt -w .

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2023-05-26 16:17:31 -07:00
Miloslav Trmač 90fa73079a Don't use bytes.NewBuffer to read data
The documentation says
> The new Buffer takes ownership of buf, and the
> caller should not use buf after this call.

so use the more directly applicable, and simpler, bytes.Reader
instead, to avoid this potentially risky use.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2023-04-14 22:34:53 +02:00
Doug Rabson 056232e4e5 pkg/archive: Add support for file flags on FreeBSD
This encodes flag information into the tar stream using
ReadFileFlagsToTarHeader and decodes with WriteFileFlagsFromTarHeader.

To support applying diffs to trees with flags, this adds logic to
reset immutable flags during the UnpackLayer process.  To support
immutable directories, we also need to defer setting flags on
directories until after all modifications to the directory contents.
Fortunately, something similar is already in place for setting
directory modify times.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-10-20 08:58:04 +01:00
Miloslav Trmač 04a2f46d5f Abort the writer in TarWithOptions on error
It can currently only fail with a broken pipe, or an invalid
pattern; in either case there's no point continuing with the loop.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-14 17:17:53 +02:00
Miloslav Trmač f42467020f Misc. warning cleanups
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-01 02:53:06 +02:00
Doug Rabson 544b12d197 pkg/archive: Disable tests which cannot work on FreeBSD
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-27 17:47:10 +01:00
Doug Rabson 6147ac0c31 pkg/archive: Add a wrapper for os.Link which does not follow symlinks
On FreeBSD, the link syscall follows symlinks which makes
TestUntarHardlinkToSymlink fail. We can match the expected semantics on
FreeBSD using linkat.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-09-27 17:47:09 +01:00
Miloslav Trmač cfbc77122a Use os.CreateTemp instead of ioutil.TempFile
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-09-12 16:30:48 +02:00
Giuseppe Scrivano f0cd0b97b1
chrootarchive: use unshare to detect user namespace
use the function in containers/storage that has the same
functionalities.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-09-08 23:07:14 +02:00
Nalin Dahyabhai 245a2ac80d CopyFileWithTarAndChown(): don't wrap a nil hashError
If everything went well, hashError can be nil.  Don't wrap it in error
context and return an error in those cases.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-07-25 10:56:31 -04:00
Daniel J Walsh 3f8c0dc0de
Wrap errors properly with fmt.Errorf
Also returned errors should not begine with a capatalized errors.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-07-12 13:26:10 -04:00
Sascha Grunert 3455d12729
Switch to golang native error wrapping
We now use the golang error wrapping format specifier `%w` instead of the
deprecated github.com/pkg/errors package.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
2022-07-07 13:22:46 +02:00
Sergio Lopez 7617ae34d8 Limited support for operating on images on macOS
Supporting macOS presents a number of challenges since the lack of
user namespaces means that operations that alter the ownership or
permissions on a file face harsh limitations, since are requested by
an unprivileged user that may need to potentially operate on arbitrary
uids/gids.

To overcome this challenges, we rely on the existing "forceMask"
feature to store the actual ownership and permission bits as an
extended attribute entry on each file.

In addition to automatically enabling "forceMask" when running on
macOS, we also need some minor changes to ensure the compatibility
with this OS. The most significant is the fact that, on macOS
SafeLchmod and SafeChmod also operate on the extended attributes, to
allow us to properly create the directories supporting the image.

With this changes in place, buildah is able to perform basic
operations on OCI images, such as downloading the image, creating a
working container, and removing them.

Signed-off-by: Sergio Lopez <slp@redhat.com>
2022-06-06 08:01:54 +02:00
Sergio Lopez cbe248edf7 archive: move containersOverrideXattr to idtools
We're going to need to reference containersOverrideXattr from idtools
in the next commit. Since archive.go is already importing idtools.go,
move containersOverrideXattr to the latter and make it public, so we
don't need to alter the imports.

Signed-off-by: Sergio Lopez <slp@redhat.com>
2022-06-06 07:57:36 +02:00
Daniel J Walsh 04b2f0f741
Switch most calls to filepath.Walk to filepath.WalkDir
It is faster then Walk, when you don't need to stat every
file and directory.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-04-06 09:42:25 -04:00
Giuseppe Scrivano 53083dae61
archive: skip lchown if force_mask is set
when the option ForceMask is in place and the file UID, GID and mode
are written to a xattr skip the lchown syscall.

[NO TESTS NEEDED] as it requires a setup with NFS.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-01-25 17:59:03 +01:00
Giuseppe Scrivano 3cd30f386f
archive: skip adding sockets to the tarball
the tar format doesn't support sockets so avoid adding them to the tar
stream.

Closes: https://github.com/containers/storage/issues/1115

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2022-01-25 11:55:34 +01:00
Giuseppe Scrivano 9408ac33a3
archive: ignore the security.selinux xattr
ignore the security.selinux xattr if it is present in the tarball
header, since invalid labels cannot be set by unprivileged users and
the `lsetxattr` syscall fails with EINVAL.

Closes: https://github.com/containers/storage/issues/1076

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2021-11-26 11:06:28 +01:00
Daniel J Walsh 534b0b3281
Standardize on capatalized logrus messages, and remove stutters
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-09-23 14:43:35 -04:00
Daniel J Walsh a5fdf90395
Creating fifo files while non root should be supported
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-07-23 05:52:00 -04:00
Daniel J Walsh b3531c3df8
Vendor in opencontainers/runc v1.0.0
The RunningInUserNS Function was removed from libcontainer and is now
in contianerd.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-06-22 08:41:17 -04:00