# Safemode subdirectory workaround

- Thread: https://externals.io/message/17163
- Original archive: https://news-web.php.net/php.internals/17163
- Started by Bostjan Skufca on 2005-07-07T11:30:16+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #17163 — Bostjan Skufca — 2005-07-07T11:30:16+00:00

URL: https://externals.io/message/17163#17163

Hello,

I created a patch to allow subdirectories to be created 
and used when safemode is enabled.

Please let me demonstrate the approach I took to comment 
it (implementation also).

We have the following file/dir structure:
/script.php                                             (owned by sUID)
/parent_directory/                                   (owned by sUID, perm=777)
/parent_directory/child_directory/             (owned by rUID)
/parent_directory/child_directory/file.txt     (owned by rUID)

sUID stands for "scriptUID"    (the UID of user owning the script)
rUID stands for "runningUID" (the UID of user executing the script)

Safemode by design works like this: 
1. script.php is executed and it want's to access 
	"/parent_directory/child_directory/file.txt" for reading or inclusion;
2. PHP first checks if file.txt owner is the same as sUID - if 
	condition is met access is granted;
3. if first condition is not met PHP continues and checks if 
	"/parent_directory/child_directory/" owner (directory which file.txt 
	resides in) is the same as sUID - if condition is met 
	access is granted
4. If both conditions fail then access is denied and PHP issues an error

The idea for modification is quite simple and it extends third statement:
3. if "child_directory/" owner UID is not the same as sUID *but* it is the 
	same as rUID, recursively check parent directories until directory 
	with different owner UID is found or only root directory is left to check

Then PHP checks directories towards root and when different UID than rUID 
is found it has 2 options:
1. if it is the same as sUID access is granted
2. if it is not the same as sUID then access is denied.

The concept / idea behind is that if running user (rUID) is able to write into 
directory owned by himself he had to create that directory. And to create 
that directory he had to had a permission to do so.


I also attached a patch for evaluation but please do not think I consider it 
good - I am not a C programmer - though it works (at least it seems so)...


Best regards,
Bostjan
