Skip to content

ArchiveReader: Preserve special permission bits during extraction - #839

Open
maksim-romanov wants to merge 1 commit into
apple:mainfrom
maksim-romanov:fix/archive-permission-bits
Open

ArchiveReader: Preserve special permission bits during extraction#839
maksim-romanov wants to merge 1 commit into
apple:mainfrom
maksim-romanov:fix/archive-permission-bits

Conversation

@maksim-romanov

Copy link
Copy Markdown

ArchiveReader.setFileAttributes was losing the sticky/set-user-ID/set-group-ID
bits on every extracted regular file and directory, for two independent
reasons:

  1. Mask high mode bits in archive reader #816 masked the applied fchmod mode to 0o777 to strip file-type bits
    picked up from the archived mode, but 0o777 also strips the
    sticky/set-user-ID/set-group-ID bits (0o7000) that extraction should
    keep.
  2. setFileAttributes called fchmod before fchown. On Darwin, fchown
    clears set-user-ID/set-group-ID as a security measure, even when the new
    owner/group match the file's existing owner/group, so applying ownership
    after the mode silently drops those bits regardless of (1).

Fix: apply ownership first, then fchmod with a 0o7777 mask (the full
POSIX permission field, still excluding unrelated file-type bits).

Added preserveSpecialPermissionBits() in ArchiveReaderTests.swift:
extracts a 0o1777 directory and a 0o6755 regular file (owned by the
current uid/gid so fchown succeeds without root) and asserts both survive
extraction. Verified the test fails without the fix (reproducing the
truncated values reported in the issues) and passes with it.
swift test --filter ContainerizationArchiveTests (51 tests) and
swift format lint --strict both clean.

Closes #818
Closes #819

ArchiveReader.setFileAttributes was losing the sticky/set-user-ID/set-group-ID
bits on every extracted regular file and directory, for two independent
reasons:

1. apple#816 masked the applied fchmod mode to 0o777 to strip file-type bits
   picked up from the archived mode, but 0o777 also strips the
   sticky/set-user-ID/set-group-ID bits (0o7000) that extraction should keep.
2. setFileAttributes called fchmod before fchown. On Darwin, fchown clears
   set-user-ID/set-group-ID as a security measure, even when the new
   owner/group match the file's existing owner/group, so applying ownership
   after the mode silently drops those bits regardless of (1).

Fix: apply ownership first, then fchmod with a 0o7777 mask (the full POSIX
permission field, still excluding unrelated file-type bits).

Added preserveSpecialPermissionBits() in ArchiveReaderTests.swift: extracts
a 0o1777 directory and a 0o6755 regular file (owned by the current uid/gid
so fchown succeeds without root) and asserts both survive extraction.
Verified the test fails without the fix (reproducing the truncated values
reported in the issues) and passes with it. swift test --filter
ContainerizationArchiveTests (51 tests) and swift format lint --strict both
clean.

Closes apple#818
Closes apple#819
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: archive ownership changes clear set-ID permission bits [Bug]: archive extraction strips special permission bits

1 participant