# Overriding PHP file operations - Extended access control

- Thread: https://externals.io/message/11447
- Original archive: https://news-web.php.net/php.internals/11447
- Started by Wez Furlong on 2004-07-21T21:13:21+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11445 — Sara Golemon — 2004-07-21T20:53:22+00:00

URL: https://externals.io/message/11447#11445

> main/safe_mode.c: php_checkuid()
>
That's one good place as all local file system operations *SHOULD* go
through this point (if we've done our jobs right).  But anytime you change
PHP versions you'll need to reapply your patch, possibly rewrite parts of it
if the internals have changed noticably.

> I have also looked in other files:
> main/fopen_wrappers.c
> main/streams.c
>
The streams layer is the right place to put hooks like this, and lets you
tell one type of file operation (open, read, stat, etc...) but it may be
more than you need.  You can do it in a couple of ways:

1) You can build your "extended security" thingy as a PHP extension which,
when loaded removes the plainfiles wrapper and replaces it with your own
version (which may passthru to the original version after checks/logging).
This option has the plus of being completely modular from version to version
avoiding the need to create wonky hacks everytime, and can even be quickly
put-in/taken-out on an as-needed basis.  (Hint: This is the route I'd go if
I were doing this)

2) You could simply override/modify the plainfiles methods in the streams
code.  In PHP4 you'll find the plainfiles wrapper in amongst the code in
main/streams.c, in PHP5 it's moved into its own file in the main/streams/
directory.  This route would be easier, but would require re-doing it
everytime you upgrade versions.

-Sara

P.S. - All of the above assumes that you're ONLY talking about local
filesystem access and not remote resources (http, ftp, etc...) or stdio
pipes (stdin, stdout, stderr, etc...).  If you want to hook ALL streams I/O
then I'd probably recommend replacing php_stream_locate_wrapper() in
streams.c which is called in response to EVERY user generated stream
function (well okay, not socket streams like tcp, udp, unix, those are a bit
"special", but it does still include http://, and ftp:// actions).

---

## #11447 — Wez Furlong — 2004-07-21T21:13:21+00:00

URL: https://externals.io/message/11447#11447 · In reply to: #11445

Yeah, what she said ;)

But with one additional note:  some places in the streams internals
make assumptions by comparing the wrapper for the stream with the
address of the plain_files_wrapper, so if you do hook it, you'll
either need to poke the methods from your extension or patch the PHP
sources.

If you're wondering what kinds of assumptions, the biggest one that
springs to mind is the (probably now infamous) greedy fread/fgets
handling, where the behaviour is greedy for local files, but packet
based for everything else.

--Wez.


> > […]
> That's one good place as all local file system operations *SHOULD* go
> through this point (if we've done our jobs right).  But anytime you change
> PHP versions you'll need to reapply your patch, possibly rewrite parts of it
> if the internals have changed noticably.
> 
> > […]
> The streams layer is the right place to put hooks like this, and lets you
> tell one type of file operation (open, read, stat, etc...) but it may be
> more than you need.  You can do it in a couple of ways:
> 
> 1) You can build your "extended security" thingy as a PHP extension which,
> when loaded removes the plainfiles wrapper and replaces it with your own
> version (which may passthru to the original version after checks/logging).
> This option has the plus of being completely modular from version to version
> avoiding the need to create wonky hacks everytime, and can even be quickly
> put-in/taken-out on an as-needed basis.  (Hint: This is the route I'd go if
> I were doing this)
> 
> 2) You could simply override/modify the plainfiles methods in the streams
> code.  In PHP4 you'll find the plainfiles wrapper in amongst the code in
> main/streams.c, in PHP5 it's moved into its own file in the main/streams/
> directory.  This route would be easier, but would require re-doing it
> everytime you upgrade versions.
> 
> -Sara
> 
> P.S. - All of the above assumes that you're ONLY talking about local
> filesystem access and not remote resources (http, ftp, etc...) or stdio
> pipes (stdin, stdout, stderr, etc...).  If you want to hook ALL streams I/O
> then I'd probably recommend replacing php_stream_locate_wrapper() in
> streams.c which is called in response to EVERY user generated stream
> function (well okay, not socket streams like tcp, udp, unix, those are a bit
> "special", but it does still include http://, and ftp:// actions).

---

## #11448 — Sara Golemon — 2004-07-21T21:23:38+00:00

URL: https://externals.io/message/11447#11448 · In reply to: #11447

> If you're wondering what kinds of assumptions, the biggest one that
> springs to mind is the (probably now infamous) greedy fread/fgets
> handling, where the behaviour is greedy for local files, but packet
> based for everything else.
>
Thanks Wez, I had a lingering suspicion such shortcuts would exist but
wasn't actually aware that the greediness fix did a bypass for local files.
Does that cause any issues on networked filesystems? (NFS, SMB, NCP, etc...)
Can't think why it would, but....

-Sara

---

## #11452 — Wez Furlong — 2004-07-21T21:48:00+00:00

URL: https://externals.io/message/11447#11452 · In reply to: #11448

Dunno. </short_and_to_the_point>
Probably depends on your OS.

--Wez.


> > […]
> Thanks Wez, I had a lingering suspicion such shortcuts would exist but
> wasn't actually aware that the greediness fix did a bypass for local files.
> Does that cause any issues on networked filesystems? (NFS, SMB, NCP, etc...)
> Can't think why it would, but....
> 
> -Sara
