Skip to content

fix(client): stop applying archive mode bits and match ZIP entries by exact path CU-17tkuw5vk8q - #3342

Merged
oleksandrkelbas merged 5 commits into
mainfrom
fix/harden-archive-extraction
Dec 4, 2026
Merged

oleksandrkelbas merged 5 commits into
mainfrom
fix/harden-archive-extraction

Conversation

@oleksandrkelbas

@oleksandrkelbas oleksandrkelbas commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What

The client no longer copies file modes from downloaded archives, and on Windows it takes a file from a ZIP only when the whole entry path matches.

  • macOS tar.gz extract-all (MeshAgent): each file gets 0755 when the archive marks it executable, otherwise 0644. Setuid, setgid, sticky and group or other write bits are dropped. Folders the tar lists get 0755; a ./ entry never changes the extraction folder itself, so /Applications keeps its mode. A file that fails to chmod now fails the extraction; before, the error was ignored.
  • macOS DMG copy (chat, including the standard-to-GuiApp migration): after cp -R, the copied item is walked and set to the same modes, directories 0755. Symlinks are skipped, never followed. Each item is opened with O_NOFOLLOW and changed through the open file, and a file with more than one hard link fails the update, so a link swapped into /Applications cannot redirect the root chmod. The walk runs in spawn_blocking.
  • Windows ZIP (tools, assets, client self-update): zip_entry_matches compares the whole path. Case is ignored, / and \ are the same separator, . and empty parts are ignored. Entry names are read without opening the entries, so an entry that cannot be opened does not block the target. Directory entries are skipped, and a ZIP with two matching entries is rejected. Before, the first entry whose name ended with the target matched, so fleet-osqueryd.exe matched osqueryd.exe.
  • The "not found in ZIP archive" error now lists the first 10 entry names, each escaped and cut to 64 characters, so a ZIP that moves its file into a folder shows up in the log on one line.

Why

The client runs as root on macOS. A tar entry with setuid, or a group or other write bit, kept that mode on disk, so a tampered or badly built archive could leave a root-owned setuid binary or a file any local user can rewrite. The suffix match on Windows could pick the wrong file when two entries end with the same name.

Worth knowing

  • Windows has no setuid bits, so only the ZIP change applies there. archive_permissions is macOS-only.
  • On macOS 26 a plain cp -R as root already drops setuid, so the DMG walk is defense in depth. The tar path is the real fix.
  • The executable bit still comes from the archive. MeshAgent's tar marks every file 0644, and the callers set 0755 on the binary right after extraction, the same as before this change.
  • Single-file installs are unchanged. They write their own mode.

Known limitations

  • On Windows the target exe must sit at the ZIP root, where every current release has it. A targetFileName with a folder goes to folder extraction, which Windows does not support, so a release ZIP that moves the exe into a folder fails the update and no server config can fix it. The release workflows should check the root layout: CU-17tkuw6cgpm.
  • The macOS tar single-file path (extract_from_tar_gz) still takes the first basename match. No server config uses it with a nested name; it is out of scope here.
  • The DMG permission walk runs on the bundle path in /Applications, which is admin-writable, so the bundle folder itself can still be swapped before the walk lists it. Hard links and symlinked items are refused. Moving the walk into a root-owned staging folder: CU-17tkuw6cgpq.

Compatibility check

  • All 786 Windows ZIP releases the server can point at (meshagent 36, fleet 77, osquery 19, client 333, chat 321): the new match picks the same entry as the old suffix match in every one, and none is missing. Checked by running zip_entry_matches over each release's entry list.
  • All 36 MeshAgent macOS tar releases: no entry has setuid, setgid or shared write bits, so the new modes change nothing for shipped versions.

Tests

  • archive_permissions_tests.rs: modes for executable, plain and directory entries; a tree with setuid, setgid and shared write files is normalized and stays the same on a second run; a symlink pointing outside the tree keeps its target's mode; a hard-linked file is refused and keeps its mode; a missing root is an error.
  • extracted_files_get_agent_decided_modes_not_archive_modes and existing_setuid_file_is_reset_when_overwritten (macOS): both fail on the old code. existing_directory_listed_in_the_archive_gets_0755 covers folders, and current_dir_entry_leaves_the_target_folder_mode_alone checks a ./ entry leaves the target at 0775.
  • dmg_extractor_tests.rs (macOS): builds a DMG with hdiutil, extracts it, and checks a 0600 file and a 0700 folder come out 0644 and 0755. It fails without the permission walk.
  • github_download_service_zip_tests.rs, 7 tests on all platforms: case, suffix-only names, both separators, ./ prefix, .. and drive-letter entries do not match, a leading / is ignored like any empty part (harmless: the bytes only go to memory), empty target. 4 more on Windows run extract_from_zip on a built ZIP: a directory entry is skipped and the right file's contents come back, an encrypted entry after the target does not block it, two matching entries are rejected, and the not-found error is one line with escaped, shortened names.
  • macOS: make lint clean, make test 822 passed, 0 failed.
  • Windows: cargo xwin check --lib clean. cargo xwin clippy --lib --tests clean. The unit suite ran in a Windows 11 ARM64 VM: 818 passed, 0 failed, and the 10 ZIP tests pass there on the final commit.
  • Linux: not compiled. CI does not build it.

Device QA

  • macOS (arm64, macOS 26.7, QA tenant): install, MeshAgent tar.gz update, chat DMG update while chat was open, Fleet and osqueryd update, uninstall and reinstall. No file under the tool folders kept setuid, setgid or shared write, and every tool ran after each update.
  • Windows 11 ARM64 VM (QA tenant): install and forced update of MeshAgent, chat and Fleet with osqueryd picked meshagent.exe, openframe-chat.exe, fleet.exe and osqueryd.exe. Client self-update from 1.7.78-dev to 1.7.79 picked openframe-client.exe. No "not found in ZIP" lines.
  • Not covered: Windows Server, Intel Macs.

Change-Set: fix-harden-archive-extraction

Change set flamingo-stack/fix-harden-archive-extraction: this pull request is the only one in it so far. Another pull request joins by naming this one in a Depends-On line, or by carrying the same Change-Set line.

Linked work

  • …and 3 linked items not shown here

Linked by the Depends-On / Change-Set lines in these descriptions; this block is maintained by the hub.

@flamingo

flamingo Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

🦩 flamingo Code Review

✅ Tests and checks passed on this commit (1 lane).

1 finding: 🔴 0 action required · 🟠 1 recommended · 🔵 0 informational

Advisory: findings do not block the merge.

1 defect outside any rule

Files Lines Checks 🔴 Action required 🟠 Recommended 🔵 Informational
7 +290 / -22 ✅ 1 / 1 passed 0 1 0

🟠 Recommended (1)

# Where Finding Flagged by
1 github_download_service.rs:39 ZIP lookup now requires a full-path match, so binaries nested in an archive folder are no longer found Review

✅ What this change does well

  • archive_permissions.rs centralises mode decisions so setuid/setgid/world-write bits from archives can never reach disk, and archive_permissions_tests.rs covers the setuid, setgid, symlink-skip and idempotence branches explicitly.
  • dmg_extractor.rs now propagates the copy failure (result?) instead of silently continuing, and reports a failed spawn_blocking join distinctly from a normalization failure.
  • github_download_service.rs replaces a silently-ignored set_permissions(...).ok() with a contextual error, so a failed chmod no longer leaves an archive-mode file behind unnoticed.
  • clients/openframe-client/src/platform/archive_permissions.rs keeps the mode rule in one small function used by both the tar.gz and the DMG path.
  • clients/openframe-client/src/services/github_download_service_zip_tests.rs runs on every CI OS, so the ZIP matching rule is tested without a Windows runner.

Inline comments: 1 new

2 possible problems checked and ruled out, 1 after reading more of the repository
  • clients/openframe-client/src/platform/archive_permissions.rs:11 5 new or changed definitions have no test referencing them: The PR adds archive_permissions_tests.rs and github_download_service_zip_tests.rs, and archive_permissions.rs:6-8 wires the test module in; the 'no test references them' fact is contradicted by the added files.
  • clients/openframe-client/src/platform/archive_permissions.rs:31 normalize_tree recurses without a depth bound over untrusted DMG contents: Unbounded recursion over directory nesting in archive_permissions.rs:37 is standard filesystem-walk practice; real filesystem path limits make stack overflow impractical, pedantic.

🧪 Tests and mutation

Checks

Check Kind Language Project Status Time
OpenGrep security Rust (OpenGrep) . ✅ passed 11.6 s

Stage 2 planned only the OpenGrep lane for this Rust change (passed, no new results); no Rust test or mutation lane ran, so no mutation data exists for the new lines.

Run by hand on the same commit c1b04e1 (macOS arm64): make lint clean, make test 817 passed, 0 failed; cargo xwin check --lib for x86_64-pc-windows-msvc clean.

The real zip_entry_matches was run over the entry lists of all 786 Windows ZIP releases the server configs point at (meshagent, fleet, osquery, client, chat): same entry as the old suffix match in every one, none missing.

🤖 Prompt for AI agents: fix every finding
Address these code-review findings on pull request #3342 of flamingo-stack/openframe-oss-lib, most important first.
For each one, make the minimal change that resolves it and do not refactor unrelated code.
If you can show a finding is wrong, skip it and say why.

1. [Recommended] clients/openframe-client/src/services/github_download_service.rs:39 ZIP lookup now requires a full-path match, so binaries nested in an archive folder are no longer found
   `extract_from_zip` previously matched any entry whose lowercased name ended with `target_filename`, which let it find a binary nested inside an archive directory (e.g. `fleetctl_v4.x_windows/fleetctl.exe`). `zip_entry_matches` compares the complete component list, so it only matches when the entry path equals the target path exactly; the new test `name_that_only_ends_with_the_target_is_rejected` asserts `sub/openframe-client.exe` no longer matches `openframe-client.exe`. `target_file_name` comes from the server payload `agentFileName` (clients/openframe-client/src/listener/command/integration_tests.rs:2177) and is used elsewhere as the *destination* filename (github_download_service.rs:324, tool_installation_service.rs:902), i.e. it is not guaranteed to be the in-archive path. Tightening the prefix-confusion hole is right, but dropping the nested-entry case can break existing Windows tool downloads.

Next: commit a suggestion, or hand a comment's prompt to your coding agent. React 👍 on a finding that helped and 👎 on one that is wrong: the reviewer learns from both.

Review again. New commits are not reviewed until you ask:

  • Review the new commits: only what was pushed since this review
  • Review the whole diff again: everything, including what was already reviewed

Or comment @flamingo-review (new commits) or @flamingo-review full (everything). Add the flamingo-review-always label to review every push.

Updated 2026-12-04 09:53 UTC · reviewed from Oleksandr Kelbas's machine

@flamingo

flamingo Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

🦩 flamingo Code Review / security

✅ No findings on the current head.

Advisory: findings do not block the merge.

Files Lines 🔴 Action required 🟠 Recommended 🔵 Informational
7 +290 / -22 0 0 0
1 possible problem checked and ruled out, 1 after reading more of the repository
  • clients/openframe-client/src/platform/archive_permissions.rs:27 normalize_tree chmods by path, so a symlink swapped in after the stat makes the root agent chmod an arbitrary file: Verified the flow: standard_to_gui_app.rs:131,140 passes /Applications as target_dir - github_download_service.rs:371 - dmg_extractor.rs:116-121 spawns normalize_tree(/Applications/name) with the agent's privileges; archive_permissions.rs:21 stats with symlink_metadata but line 28 chmods via path-based fs::set_permissions, which does follow symlinks, so the stat/chmod pair is a genuine TOCTOU…

Review again. New commits are not reviewed until you ask:

  • Security-check the new commits: only what was pushed since this security review
  • Security-check the whole diff again: everything, including what was already checked

Or comment @flamingo-review security (new commits) or @flamingo-review security full (everything). Add the flamingo-security-review-always label to security-review every push.

Updated 2026-12-04 10:09 UTC · reviewed from Oleksandr Kelbas's machine

@yevhenii-flamingo yevhenii-flamingo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few follow-ups on upgrade rollback, ZIP matching and coverage.

@danylo-babenko-flamingo

Copy link
Copy Markdown
Contributor

zip_entry_matches (github_download_service.rs:41): an entry named /fleet.exe matches the bare target fleet.exe, because empty path parts are dropped. The description says absolute entries are rejected, but parent_and_absolute_entries_do_not_match_a_bare_target only checks C:\fleet.exe. This does no harm, since the bytes only go into memory and nothing is written by that path. Fix: correct the description, or add a test that pins the current behaviour.

@oleksandrkelbas

Copy link
Copy Markdown
Contributor Author

Thanks, you're right: /fleet.exe matches fleet.exe because empty path parts are dropped, and that's harmless here. In 95112e1 I renamed the test to parent_and_drive_entries_do_not_match_a_bare_target and added leading_slash_is_ignored_like_an_empty_part to pin the current behaviour. The PR description now says drive-letter entries are rejected and a leading / is ignored.

… exact path

macOS tar.gz extract-all and the DMG copy now set 0755 for executables and
directories and 0644 for other files, so setuid, setgid and group/other write
bits from an archive never reach disk. A failed chmod fails the extraction.

Windows ZIP extraction takes an entry only when its whole path matches the
target (case-insensitive, either separator), instead of any name ending with
it, and the not-found error lists the first entries.

CU-17tkuw5vk8q
… the error

A ZIP with two entries matching the target is rejected. The not-found error
lists entry names escaped and cut to 64 characters. Named the permission
constants and added Windows tests for extract_from_zip.

CU-17tkuw5vk8q
…r modes

The ZIP scan reads entries with by_index_raw, so an entry that cannot be
opened no longer fails the extraction of the target. Folders listed in a
tar get 0755 like the DMG walk gives them. Added a DMG extraction test
that fails without the permission walk.

CU-17tkuw5vk8q
…inks in the DMG walk

A `./` tar entry no longer changes the mode of the extraction folder, so
/Applications keeps 0775. The DMG permission walk opens each item without
following symlinks, refuses a file with more than one link and changes the
mode through the open file.

CU-17tkuw5vk8q
@oleksandrkelbas
oleksandrkelbas force-pushed the fix/harden-archive-extraction branch from 95112e1 to e0d5b10 Compare December 4, 2026 14:53
@oleksandrkelbas
oleksandrkelbas added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit ef38d79 Dec 4, 2026
26 checks passed
@oleksandrkelbas
oleksandrkelbas deleted the fix/harden-archive-extraction branch December 4, 2026 15:56
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.

4 participants