Hi Greg,
- Make it so that include() and require() can only load specific
extensions like ".php" and ".phar"- At the same time, all file functions that write to a file (e.g. fwrite
and file_put_contents) must refuse to write to exactly these extensions.- Any functions like
rename()ormove_uploaded_file()should also be
counted among the files that "can write to files", so an attacker can't
rename a file to have the right extension.
I get the goal here (W^X, keeping executable code separate from writable
paths), but I'm curious how this would actually hold up once you trace it
down into php-src.
fwrite() only sees a php_stream, not a path, and PHP has custom stream
wrappers, symlinks, hard links, copy(), ZipArchive::extractTo(), and C
extensions like SQLite or GD that open files directly without going through
ext/standard. Even if the check lives in php_stream_open_wrapper()
alongside rename() and move_uploaded_file(), how do you handle symlinks,
case-insensitive filesystems on macOS/Windows, or TOCTOU races between
checking the path and opening the file? And on the include() side, if it
checks the URI suffix, how does that interact with
phar://upload.jpg/index.php or custom stream wrappers (like the
source-rewriting wrapper in your webserver) without either leaving bypasses
or breaking stream wrappers?
The .ini can be used as an escape hatch for the rare cases you actually
need this. And frankly, .ini is too coarse, I'd recommend doing it on a
RegEx basis, where you indicate the regex of what file paths are the
exception.
Most modern PHP stacks legitimately write .php files and then include them
(Laravel compiled views and bootstrap/cache, Symfony var/cache, Twig,
Doctrine proxies, Composer autoloaders). Once a sysadmin adds a php.ini
regex exception for those directories so the app can boot, those paths are
writable and executable again, just guarded by regexes in php.ini instead
of read-only mounts, OS permissions, or kernel checks like the recent
AT_EXECVE_CHECK pre-RFC on this list. What threat model does a php.ini
regex close that OS-level W^X doesn't already cover for the whole process,
including extensions and subprocesses?
Heredoc syntax should absolutely be used for provenance of what is being
sent toexec(), eval() and other "open-ended, dangerous functions".$code = <<<PHP
// this specific tag, "PHP" is needed
// in order to pass $foo to any eval()
You can interpolate $variables here, fine.
But this is at assignment time.
I recommend also allowing {{interpolation}}
that can be passed at eval time.
PHP;
If normal $variable interpolation is still allowed at assignment time,
doesn't that break provenance right away whenever any tainted input reaches
that variable before eval() or exec() runs? Even if $variable interpolation
were disallowed, how would provenance be tracked in the engine: only on the
AST node at compile time, or as a runtime flag on zend_string (like the
earlier is_literal() discussions)? If it's on zend_string, what happens
across concatenation, trim()/sprintf(), interned strings, or helper
functions?
You can even have entire autoloaded classes, like \Formats\SQL and
\Formats\JSON, that will handle sanitization for that specific language.
For <<<SHELL and <<<SQL, safe escaping depends on where {{interpolation}}
sits in the syntax (an SQL identifier vs a string literal vs a LIKE
pattern, or a shell command vs an argument vs a redirect). How would
\Formats\SQL or \Formats\SHELL handle that without embedding full SQL and
shell parsers in PHP core, when prepared statements and proc_open()'s argv
array already avoid string interpolation completely?
- There's no way userland code to execute RCE anymore, frankly not even
SQL injection, if you are conservative by default.
Even if .php writes, eval(), and exec() were all locked down, arbitrary
file write still lets an attacker write .user.ini (auto_prepend_file),
.htaccess, or a template like .blade.php / .twig that the app itself
compiles into an allowlisted cache directory. That's mostly why this feels
to me like it shifts the problem around the way safe_mode used to, rather
than eliminating userland RCE.
Curious how you'd approach those pieces, because from an engine perspective
it feels like the OS and parameterized APIs are already the cleaner
boundary.
Best,
Pratik