28 Commits
Author SHA1 Message Date
Kathryn Baldauf 50f7722296 Mask high mode bits in archive reader (#816)
This PR masks the high mode bits when setting file attributes for
consistency with mode at file creation.

Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
2026-07-27 11:42:07 -07:00
Mojtaba HosseiniandJ Logan 7e2ae5a170 docs: fix traversal in a public method's documentation (#770)
Seems like a typo.
The change only affected a comment and does not disturb the code at all
(backward compatible).

Co-authored-by: J Logan <john_logan@apple.com>
2026-06-16 12:41:46 -07:00
J Logan d3d7d2143c Adds FileDescriptor-based enumerate(). (#746)
- Closes #745.
- Facilitates TOCTOU-safe recursion over directory contents.
- Replace FileDescriptor extensions with a static utility type to
prevent potential namespacing issues as this project and Swift evolve.
2026-05-20 15:55:29 -07:00
jwhur f2c42402e7 Refactor ArchiveWriter to expose archiving each path independently (#693)
This PR refactors `ArchiveWriter` to add an API `archive(_ paths:
base:)`. This API is used to archive the contents at each path
independently, similar to doing `tar -cvf archive.tar foo.bin
/bar/baz.txt`.
2026-04-30 18:23:47 -07:00
Anthony 35f97f225d Add progress handler for rootfs unpacking (#515)
I've added an optional progress handler for rootfs unpacking so that
consumers can show progress. The Ubuntu image takes about 8 seconds to
unpack on my machine, and I'm developing an app where it would be useful
to show progress in the UI.

Total size is determined in an optional first pass that scans archive
headers. Bytes written are then reported during unpacking. The optional
first pass adds 15 (Alpine) to 115 (Ubuntu) ms to unpacking duration on
my machine, depending on image size.
2026-04-02 17:33:55 -07:00
Danny Canter a864456240 ArchiveWriter: Fix uaf in finishEncoding() (#622)
finishEncoding() called archive_free() and then passed the freed pointer
to wrap(), which would call archive_error_string() on it if the return
code was not ARCHIVE_OK. Fix by nil-ing the pointer before freeing and
throwing directly without attempting to read the (already freed) error
string.
2026-04-01 18:29:46 -07:00
Danny Canter 1f495bc5be ArchiveReader: Cleanup tempdirs/files (#623)
There was a couple spots we were leaving around tempdirs
2026-04-01 14:20:44 -07:00
Aditya Ramani 420b915d8b Write archive contents in chunks (#589)
Fix a bug in ArchiveWriter where large files would only partly be
written into an archive.
This is done so by writing the data in chunks.

---------

Signed-off-by: Aditya Ramani <a_ramani@apple.com>
2026-03-18 13:06:28 -07:00
Aditya Ramani d3ff56e5dd Use libarchive provided archive open/write/close/free callback methods (#573)
ContainerizationArchive internally used a `FileArchiveWriterDelegate`
type that defined callback methods which were handed to
`archive_write_open2` from libarchive

```
int archive_write_open2(struct archive *, void *,
		     archive_open_callback *, archive_write_callback *,
		     archive_close_callback *, archive_free_callback *);
```

Turns out those methods were at the very least partly broken, when used
alongside gzip compression.

Utilities like gnutar and gunzip couldn't entirely process the archives
produced by them, leading to errors like
```
➜ gtar -tvzf rootfs.tar.gz
...
drwxr-xr-x 0/0               0 2026-03-07 11:24 sbin/
-rwxr-xr-x 0/0       122451504 2026-03-07 11:24 sbin/vminitd
-rwxr-xr-x 0/0       121138856 2026-03-07 11:24 sbin/vmexec
gzip: (stdin): trailing garbage ignored
..
gtar: Child returned status 2
gtar: Error is not recoverable: exiting now
```

libarchive provides alternate APIs, `archive_write_open_filename` and
`archive_write_open_fd` where these callbacks are defined within the
library.

Lets use those instead and let the library handle this / future edge
cases (in this particular case it was adding some padding at the end of
the gzipped compressed file)
2026-03-08 09:11:05 -07:00
Danny Canter 1be13d1d76 LinuxContainer: Support copying directories (#571)
Now that the static linux SDK we use has libarchive linked against it,
we can finally live our dream (/s) of supporting copying directories
in/out a little easier. This was.. kind of annoying. I think the very
simple route of writing archive to guest/host -> streaming over grpc is
simplest, but on the guest end we have a couple problems:

1. The VMs rootfs is read only today always, which is a good thing to me
and I don't want to change if we don't have to.
2. Writing the archive to the containers rootfs temporarily could work,
but it's a bit weird, and the user can make the containers rootfs
readonly which would screw that plan.
3. We could write it to /run or /tmp, but they're tmpfs and dealing with
the headache of the user possibly tarring an enormous dir is one I don't
want to care about.

So, that leaves us with the (truthfully better to me) approach of trying
to write the tar data directly to the host and skipping grpc which kinda
forces us to have a temp spot. Because of that, I made it such that we
pass a port number from host<->guest, and transfer the actual data
(either the single file or tarred dir contents) over the vsock port
instead. The stream is kinda clunky, but it just serves as a means to
exchange metadata and a "we're done" signifier.
2026-03-06 15:30:00 -08:00
J Logan a092fc4bd2 Remove libzstd imports from ContainerizationArchive. (#554)
- Closes #532.
- Relocate all zstd calls down into CArchive, and export a function
zstd_decompress_fd(src_fd, dst_fd) that ContainerizationArchive can use.
This eliminates problems that can occur when treating Swift warnings as
errors in Xcode projects that use ContainerizationArchive.
2026-02-24 00:42:30 -08:00
J Logan c3fe889a2f Fix for decompressing zstd with localpath URL. (#512)
- InputSource(url:) rejects URLs with nil scheme, which was what was
passed when running the `container system kernel set` integration test
with a local kernel archive path.
2026-02-02 20:32:07 -08:00
J LoganandAditya Ramani 1b50c817b6 Adds zstd decompression for layer content blobs. (#508)
- Closes apple/container#988.
- macOS libarchive is not built with zstd support, so the workaround in
ArchiveReader is to attempt to decompress every archive as zstd. If
decompression fails, we pass the original archive to libarchive. If it
succeeds, we pass the uncompressed archive.
- Adds blob media type recognition for zstd to EXT4Unpacker. Tested zstd
blob unpack using `image pull tonistiigi/hello-world:zstd-docker`.

Co-authored-by: Aditya Ramani <a_ramani@apple.com>
2026-02-02 13:10:22 -08:00
J Logan 3e93416b9a Merge commit from fork
* Admit only valid OCI layout files when loading image.

- Adds `AdmissionMapper` protocol for validating archive
  member paths and normalizing them to relative paths
  under the extraction root directory.
- Renames `Reader.swift` to `ArchiveReader.swift` to
  match type name.
- Rework `TempDir` to address `NSString.utf8String`
  deprecation warning in Swift 6.2.3.
- Rework `ArchiveReader.extractContent()` to use an
  `AdmissionMapper` to validate and remap archive
   members before extracting.
- Adds `IdentityAdmissionWrapper` for naive extraction.
- Adds `NoSymlinkAdmissionWrapper` that only extracts
  regular files and directories under the extraction root.
- Adds `OCIImageAdmissionWrapper` that only extracts
  valid OCI image layout paths.
- Use `OCIImageAdmissionWrapper` for `cctl image load`
  and print rejected paths.

* PR feedback.

* Adds public init() for TrustedAdmissionMapper.

* Replace AdmissionMapper with more secure extraction.

- Adds FileDescriptor.mkdirSecure() to prevent
  root escapes on member pathnames, and to prevent
  symlink traversal.
- Adds FileDescriptor.unlinkRecursive() to
  facilitate overwrites when there are multiple
  archive entries with the same member path.
- Adds FileDescriptor.validateSymlinkTargetInRoot()
  to validate that extracted symlink targets do
  not escape the root.
- Rewrite ArchiveReader.extractContents() to use
  secure path functions.

* Remove unneeded symlink check, rename files.

* Simplify the lexical normalizer workaround.

* Reject member paths containing parent traversal components.

* Remove unused lexical normalization workaround.

* Fix leaking fds, extract absolute members as relative.
2026-01-15 12:52:02 -06:00
Kathryn Baldauf ec2ee3e94d Update license header on all files to include the current year (#470)
Related to https://github.com/apple/container/pull/1024

Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
2026-01-05 13:08:48 -08:00
Dmitry Kovba 96d37e2e21 Fix multi-sentence error messages (#443)
Fixes multi-sentence error messages, where they start from a lowercased
letter.
2025-12-09 12:32:09 -08:00
Dmitry Kovba bb0cd39177 Lowercase error messages (#440)
For consistency, all error messages are lowercased.
2025-12-09 05:38:48 -08:00
Danny Canter 8b39713a00 EXT4: Optimize unpack (#378)
Optimize unpack a little by trying to reduce allocations in the hot
path. Today for every file we read the entire file into memory and then
pass the data blob to the ext4 writer to eventually be written to the
sparse file. Before being written to the sparse file the data is copied
*again* to a temp buffer before finally hitting write(2) in FileHandle.

This change moves things around such that we can pass an optional buffer
to the ext4 create() (so we can reuse a buffer for file writes), as well
as stops reading entire files into memory by passing the archive entry
itself (wrapped in a ReaderStream object albeit) down to the writer.

Testing with unpacking every platform for
`docker.io/jenkins/jenkins:lts` on an M1 Max:

Old Avg (5 runs): 7.43s
New Avg (5 runs): 5.31s
2025-11-03 12:22:38 -08:00
Kathryn Baldauf a14fd26a24 Add fallback to use lstat to get file permissions in archive writer (#337)
Allow different path for getting file permissions in archive writer,
gated by OS check

Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
2025-10-16 16:04:00 -07:00
J Logan 995a231348 Removes "all rights reserved" from license header. (#309) 2025-10-03 13:27:59 -07:00
Danny Canter 1992cfe779 Containerization: Reduce allocations for image subsystems (#152)
Continue the allocations journey for anything that is in the codepaths
for pulling images. This time there's a couple spots in archive and ext4
we can get rid of some copies.
2025-06-18 16:11:46 -07:00
Josh Soref c00ede68c1 fix comment misspellings (#131)
This PR corrects misspellings identified by the [check-spelling
action](https://github.com/marketplace/actions/check-spelling)

The misspellings have been reported at
https://github.com/jsoref/containerization/actions/runs/15662940240/attempts/1#summary-44123291170
The action reports that the changes in this PR would make it happy:
https://github.com/jsoref/containerization/actions/runs/15662940315/attempts/1#summary-44123291367

---------

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2025-06-16 16:06:37 -04:00
Michael Crosby 5d2d7a1bc3 update license header removing new line (#99)
Fixes #63

Signed-off-by: crosbymichael <michael_crosby@apple.com>
2025-06-12 09:57:35 -04:00
Noritaka Kobayashi 4181e50775 refactor: fix typos (#64)
fix typos
2025-06-10 08:19:50 -07:00
Aditya Ramani 5f29918ecb Add docs for ContainerizationArchive (#28)
Also removes some dead code.

Signed-off-by: Aditya Ramani <a_ramani@apple.com>
2025-06-06 16:29:11 -07:00
Danny Canter f49f668936 Updates to license headers (#17)
- Capitalize containerization.
- Wrap "All rights reserved" to a new line.

Signed-off-by: Danny Canter <danny_canter@apple.com>
2025-06-05 16:16:22 -07:00
Kathryn Baldauf 502dab6108 Update containerization apple main (#4)
Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
2025-06-05 16:15:44 -07:00
+6 3407cc3f16 initial commit
Co-authored-by: Aditya Ramani <a_ramani@apple.com>
Co-authored-by: Agam Dua <agam_dua@apple.com>
Co-authored-by: Danny Canter <danny_canter@apple.com>
Co-authored-by: Dmitry Kovba <dkovba@apple.com>
Co-authored-by: Eric Ernst <eric_ernst@apple.com>
Co-authored-by: Evan Hazlett <ehazlett@apple.com>
Co-authored-by: Gilbert Song <gilbertsong@apple.com>
Co-authored-by: Hugh Bussell <hbussell@apple.com>
Co-authored-by: John Logan <john_logan@apple.com>
Co-authored-by: Kathryn Baldauf <k_baldauf@apple.com>
Co-authored-by: Madhu Venugopal <mvenugopal@apple.com>
Co-authored-by: Michael Crosby <michael_crosby@apple.com>
Co-authored-by: Sidhartha Mani <sidhartha_mani@apple.com>
Co-authored-by: Tanweer Noor <tnoor@apple.com>
Co-authored-by: Ximena Perez Diaz <xperez528@gmail.com>
Co-authored-by: Yibo Zhuang <yzhuang@apple.com>
2025-06-05 16:15:21 -07:00