]> granicus.if.org Git - php/commitdiff
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>
Thu, 6 Aug 2020 08:26:46 +0000 (10:26 +0200)
To avoid behavior differences due to libc.

ext/standard/file.c
ext/standard/tests/file/popen_pclose_error.phpt

index 98578376b001c90433bdeb7f431f9d3b29e775db..88ffba8102928feb6ffd8d977553ca4228be5343 100644 (file)
@@ -930,10 +930,18 @@ PHP_FUNCTION(popen)
                char *z = memchr(posix_mode, 'b', mode_len);
                if (z) {
                        memmove(z, z + 1, mode_len - (z - posix_mode));
+                       mode_len--;
                }
        }
 #endif
 
+       /* 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;
+       }
+
        fp = VCWD_POPEN(command, posix_mode);
        if (!fp) {
                php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
index 2f05e7788d982203308df27964dfdab5180c7ffb..07132808701bdd0b6c0ec0f99ada368e713ee4d0 100644 (file)
@@ -22,7 +22,7 @@ unlink($file_path."/popen.tmp");
 --EXPECTF--
 *** Testing for error conditions ***
 
-Warning: popen(abc.txt,rw): %s on line %d
+Warning: popen(abc.txt,rw): Invalid mode in %s on line %d
 bool(false)
 
 --- Done ---