Skip to content

Fix GH-25967: umask() narrows out-of-range values to int - #25970

Open
sanketpatel32 wants to merge 4 commits into
php:masterfrom
sanketpatel32:devin/1796065112-gh25967-umask-range
Open

sanketpatel32 wants to merge 4 commits into
php:masterfrom
sanketpatel32:devin/1796065112-gh25967-umask-range

Conversation

@sanketpatel32

@sanketpatel32 sanketpatel32 commented Nov 30, 2026 •

Copy link
Copy Markdown

Summary

Fixes #25967.

umask() now rejects explicitly provided masks that are not representable as an int before probing or changing the process umask. This prevents large zend_long values from narrowing through (int) and inadvertently clearing or altering the process mask, while preserving round-trips for platform-supported masks above 0777 such as 01022. The prior mask and BG(umask) bookkeeping remain untouched on the error path.

This targets master because rejecting invalid input with a new ValueError is a behavior change. It can be backported to the lowest supported bugfix branch if the maintainers prefer.

Adds ext/standard/tests/file/gh25967.phpt covering positive and negative out-of-int-range values, acceptance of 01022, and preservation of the prior mask on errors.

Tests

  • ./buildconf && ./configure --disable-all --enable-debug && make -j$(nproc)
  • make test TESTS=ext/standard/tests/file/ — 915 tests: 624 passed, 291 skipped, 0 failed

@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.

I don't think the current fix is correct. We have an example in the man page that use umask(0)
https://www.php.net/manual/en/function.umask.php#refsect1-function.umask-examples

If I am remembering correct, macOS preserve bit masks smaller than 07777. If PHP's process inherits from 01022, ,umask(0) might get 01022. And umask(01022) will get a ValueError. I suggest we just

-	if (!mask_is_null && (mask < 0 || mask > 0777)) {
-		zend_argument_value_error(1, "must be between 0 and 0777");
+	if (!mask_is_null && !ZEND_IN_RANGE_OF(mask, int)) {
+		zend_argument_value_error(1, "must be between %d and %d", INT_MIN, INT_MAX);
 		RETURN_THROWS();
 	}

Please wait for other's review since this is not my field so I am not sure.

@sanketpatel32

Copy link
Copy Markdown
Author

@LamentXU123 Thanks for the review — updated in 71dc625 to reject only values outside the int range with ZEND_IN_RANGE_OF(mask, int), so 01022 remains accepted. make test TESTS=ext/standard/tests/file/ passes locally (624 passed, 291 skipped, 0 failed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants