Try this code

root /home/user $ touch hehehe

then as a normal user

user ~ $ rm -f hehehe

Our user can remove a root-owned file inside of their home directory. This works not only in their home directory, but inside of any directory writable by them.

Now try

root /home/user $ mkdir -p testdir


user ~ $ rm -rf testdir

Works too. However:

root /home/user $ mkdir -p testdir/childdir
user ~ $ rm -rf testdir/childdir

fails, so our user apparently cannot delete non-empty non-writable directories inside writable directories.

By my understanding, all three of these test cases should have failed. But apparently when a directory is writable, that endows a user with some non-trivial capabilities with regard to its contents.

What are the exact rules regarding which of the primitive filesystem operations (create, delete, etc.)` may be performed with respect to the filesystem permissions that a user has?

  • You have write access for a directory you can remove entries from it. In your latest example you would try to remove a directory from a directory where you don't have write access and as such it fails. – Seth Jul 6 '18 at 7:46

The answers become very clear, once you understand that in all UNIX-compatible file systems a file is not something that lives inside a directory, but something completely independent. A directory is nothing but a collections of links to files (this is, why the syscall to remove a file from a directory is unlink()). When there are no links to a file (the refcount is zero), the file itself dies.

This has a few implications:

  • One and the same file can perfectly well exist in more than one directory on a file system
  • Your question: Deleting a file (or a subdirectory) from a directory (removing a link to it) or creating a link to it is not an operation on the file, but on the directory. This implies, that the permissions of the file are irrelevant, it is the permissions of the directory, that count.
  • I still don't understand why testdir can be removed from the user-owned directory but testdir/childdir cannot be. If the user has write access to the directory, then why can't they unlink testdir/childdir from it? – jcarpenter2 Jul 6 '18 at 7:55
  • The user has no write privileges on testdir- so he can't remove anything (including testdir/childdir) from it. The user does have write privileges to his home directory, so he can remove testdir from it, which will obviously make everything below inaccessable. – Eugen Rieck Jul 6 '18 at 7:59
  • Yes - but why does the user have to remove childdir from testdir first before they can remove testdir from their home directory? – jcarpenter2 Jul 6 '18 at 7:59
  • You can't remove a link to a non-empty directory. This has nothing to do with privileges, even root can't rmdir a non-empty dir. – Eugen Rieck Jul 6 '18 at 8:01
  • Fancy. Seems good. – jcarpenter2 Jul 6 '18 at 8:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.