]> granicus.if.org Git - php/commitdiff
Reapply "Explicitly validate popen mode"
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 5 Aug 2020 09:27:13 +0000 (11:27 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 7 Aug 2020 08:33:27 +0000 (10:33 +0200)
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

index 98578376b001c90433bdeb7f431f9d3b29e775db..058d78688d0d23a52353ffb2f7ff6ba4558d514f 100644 (file)
@@ -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);