Skip to content

fix path traversal bug in unshield.c - #172

Open
eqawasm wants to merge 3 commits into
twogood:mainfrom
eqawasm:eqawasm-patch-1
Open

eqawasm wants to merge 3 commits into
twogood:mainfrom
eqawasm:eqawasm-patch-1

Conversation

@eqawasm

@eqawasm eqawasm commented May 17, 2023

Copy link
Copy Markdown

fix path traversal bug

fix path traversal bug
@eqawasm eqawasm changed the title Update unshield.c fix path traversal bug in unshield.c May 17, 2023
@twogood

twogood commented May 18, 2023

Copy link
Copy Markdown
Owner

Resolves #171

@twogood

twogood commented May 27, 2023

Copy link
Copy Markdown
Owner

@kratz00 What do you think?

Comment thread src/unshield.c
/* use GNU extension to return non-existing files to real_output_directory */
realpath(output_directory, real_output_directory);
realpath(filename, real_filename);
strncat(real_output_directory, "/", PATH_MAX);

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.

I think this could overflow real_output_directory as strncat will add a terminating null-character.

If I see it correctly this PR is intended to fix CVE-2015-1386, therefor it might make sense to clean-up/review the existing code which supposedly should already address it and most importantly double check the test in https://github.com/twogood/unshield/tree/main/test/v5/CVE-2015-1386

This is just shooting from the hip, I will have a deeper look as soon as possible.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

So it's the same CVE? I didn't notice. Strange that the old test case passes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Saw this and throwing in my two cents.

Looks like it should be strncat(..., PATH_MAX - 1) to avoid the null character causing an off-by-one.

Regarding CVE-2015-1386, #171 looks like a corner case that the original fix missed. In the PoC @eqawasm provided, they appear to be exploiting a common prefix between the real paths: /tm. Suffixing / to real_output_directory prevents the directory name from being misinterpreted as a file name.

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.

@carter-yagemann summed it up quite nicely 👍

I think the proposed patch for this corner case is not sufficient, even when fixing the off-by-one issue, here is why.
The reproductions steps in #171 can be simplified and do not even require root permissions:

git clone git@github.com:twogood/unshield.git
cd unshield
cmake -B build
cmake --build build
./build/src/unshield -d /tmp x test/v5/CVE-2015-1386/data1.cab
Cabinet: test/v5/CVE-2015-1386/data1.cab
  extracting: /tmp/Bovine_Files/../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/moo
 --------  -------
          1 files

real_filename will be always /tmp/moo, in case of this manipulated cabinet file.
For the strncmp call (see https://github.com/twogood/unshield/blob/main/src/unshield.c#L496), we only need to make sure to match the first four bytes at most by influencing the real_output_directory, this can be easily done by using the -d command line option.

In the /tn case adding the slash would work to solve the issue but not for the /tmp case.

fix path traversal bug
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a08d272e-e0de-4cd7-80fe-1b327ee88939
📥 Commits

Reviewing files that changed from the base of the PR and between 313534b and 3a62684.

📒 Files selected for processing: 1
  • src/unshield.c
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@twogood

twogood commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

What this change does: Adds a trailing slash to the canonical output directory before checking whether an extracted path stays inside it. That addresses the real shared-prefix case: /tmp/output-other/file must not count as being inside /tmp/output.

Must fix: strncat(real_output_directory, "/", PATH_MAX) does not mean “keep the whole result within PATH_MAX.” It permits copying up to PATH_MAX source bytes; with an existing PATH_MAX-1-byte path, copying the slash and terminator writes past the allocation. Decreasing the third argument to PATH_MAX-1 still does not account for the destination's existing length.

It also turns a canonical root output directory / into //, causing the prefix comparison to reject ordinary absolute paths inside that valid output directory.

Check the directory separator boundary in the comparison without appending to the buffer (and handle root correctly), or append only after checking remaining capacity. Add regression cases for a shared-prefix sibling directory, a maximum-length output path, and root. Also check realpath() return values rather than relying on the preallocated pointer being non-NULL.

Verdict: High-value bug to fix, but do not merge this particular patch. The local macOS suite passes because the changed __GLIBC__ branch is not compiled there; I did not runtime-test that branch on glibc.


Branch update correction: The initial update used a force-pushed rebase. The original PR commits have now been restored as first-parent history with a merge commit, pushed normally without force. Both the original and previously rebased commits remain reachable. The reviewed file contents are unchanged.

Review basis: main 313534b (after #199), with current logging, assertions, I/O callbacks, and GCC 14/15 CI preserved in conflict resolutions. No functional fixes to the findings above were folded into the branch update. Local checks used macOS arm64, shared Debug builds with bundled MD5 unless stated otherwise; native MSVC and glibc runtime checks were not performed.

Restore the original contribution commits as the first-parent history.
Keep the reviewed conflict resolutions and current main without rewriting published history again.
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