From: Nikita Popov Date: Wed, 5 Aug 2020 09:27:13 +0000 (+0200) Subject: Reapply "Explicitly validate popen mode" X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3bb183036976fc8bfdf039b41efe1e4312894937;p=php Reapply "Explicitly validate popen mode" To avoid behavior differences due to libc. This time with the check only for the non-win32 case, as Windows support additional modifiers here (t/b). --- diff --git a/ext/standard/file.c b/ext/standard/file.c index 98578376b0..058d78688d 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -930,8 +930,16 @@ PHP_FUNCTION(popen) char *z = memchr(posix_mode, 'b', mode_len); if (z) { memmove(z, z + 1, mode_len - (z - posix_mode)); + mode_len--; } } + + /* Musl only partially validates the mode. Manually check it to ensure consistent behavior. */ + if (mode_len != 1 || (*posix_mode != 'r' && *posix_mode != 'w')) { + php_error_docref2(NULL, command, posix_mode, E_WARNING, "Invalid mode"); + efree(posix_mode); + RETURN_FALSE; + } #endif fp = VCWD_POPEN(command, posix_mode);