Skip to content

Fix PHAR_GET_32 in ext/phar/tar.c reading only 1 byte on little-endian - #25798

Open
hohphu8 wants to merge 1 commit into
php:PHP-8.4from
hohphu8:fix-phar-tar-sig-flags-32bit
Open

hohphu8 wants to merge 1 commit into
php:PHP-8.4from
hohphu8:fix-phar-tar-sig-flags-32bit

Conversation

@hohphu8

@hohphu8 hohphu8 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

In phar_parse_tarfile() (ext/phar/tar.c), the little-endian definition of PHAR_GET_32(buffer) is (uint32_t) *(buffer). Because buffer is char *, this only reads the first signed char byte instead of a 32-bit little-endian integer, ignoring the upper 3 bytes of sig_flags in .phar/signature.bin (and sign-extending values with bit 7 set).

This replaces both #ifdef WORDS_BIGENDIAN branches with a single 4-byte little-endian read using unsigned char * and explicit uint32_t casts.

Adds ext/phar/tests/tar/tar_sig_flags_32bit.phpt, which corrupts the upper 3 bytes of sig_flags and checks that verification fails with broken or unsupported signature. Verified FAIL -> PASS on PHP-8.4 (Debug + ZTS) with ext/phar/tests/tar/ passing.

I found this through source inspection with an automated/LLM-assisted tool, then reproduced and verified it myself.

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tests nits. Fix fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would suggest you to validate the three "corrupted" high bits seperately in the test?

$pos = strpos($raw, ".phar/signature.bin\0");
$dataOff = $pos + 512;

// Corrupt upper 3 bytes of 32-bit sig_flags (0x03000000 -> 0x03ffffff)

@LamentXU123 LamentXU123 Nov 27, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it clearer to make the comment "03 00 00 00 -> 03 ff ff ff"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated as suggested. Thanks!

@hohphu8
hohphu8 force-pushed the fix-phar-tar-sig-flags-32bit branch from 5c7803e to 5a131f0 Compare November 27, 2026 12:05

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/cc @iliaal

@hohphu8
hohphu8 requested a review from LamentXU123 November 27, 2026 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants