Reputation: 193
The application produces data by user. Each user has a unique user id and associated unique file permissions. User files are stored in individual directories with associated user permissions on each directory.
The requirements are to provide secure individual user access to only user's files via a portal, assume PHP. A design being considered is to mimic the directory structure and permissions in the portal environment. If it were possible to run PHP as a user then the system permission access security could be used. (this would limit the scope of security implementation to the login process and not to the application.)
Question: Is it possible to run PHP as a user and assume user file permissions?
Research has identified some similar questions, but not the direct question of running PHP as an individual user.
Upvotes: 0
Views: 490
Reputation: 32272
There are a handful of solutions, from best to worst:
posix_seteuid()
and posix_setegid()
to change the effective UID and GID of the running process."But wait!" I hear you say, "That last option seems like exactly what I need! Why is it the worst?"
Because in order to change the UID or GID of a process that process must first be running as a user that is permitted to do such a thing. That user is root.
Running PHP as root, even briefly in order to drop to a different UID/GID, is a massive security hole. Even the most minor bug or flaw is now game over, and this is exponentially more true if you're writing a file manager.
"That's fine," you retort, "this is only for internal use with trusted users, so I'm not worried about security."
NO. BAD. [bops you with a rolled-up newspaper]
Never. Trust. Users.
At best they will never intentionally break or compromise your app, but:
and the list goes on.
TL;DR: Unless you're setting up per-user vhosts/sites/apps. Store the files outside of the docroot and use Option #2 to gate access via PHP. If anyone catches you running PHP as root you're going to have a bad time.
Upvotes: 2