From 3bb183036976fc8bfdf039b41efe1e4312894937 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 5 Aug 2020 11:27:13 +0200 Subject: [PATCH] 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). --- ext/standard/file.c | 8 ++++++++ 1 file changed, 8 insertions(+) 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); -- 2.50.1