Why is the chown command root-only? Why can't non-root users use chown to give away files they own?

  • cant understand your question chown command can be used by non root user also Dec 21 '11 at 20:56
  • Maybe i put it wrong. Well the exact question of my proffesor was: "Why the move of rights from a normal user isn't allowed in UNIX systems?"...
    – phleg
    Dec 21 '11 at 20:59
  • 22
    I think the real question is: why can't non-root users use chown to give away files they own. (I've seen systems where, depending on the filesystem configuration, you can.) Dec 21 '11 at 21:49

Most unix systems prevent users from “giving away” files, that is, users may only run chown if they have the target user and group privileges. Since using chown requires owning the file or being root (users can never appropriate other users' files), only root can run chown to change a file's owner to another user.

The reason for this restriction is that giving away a file to another user can allow bad things to happen in uncommon, but still important situations. For example:

  • If a system has disk quotas enabled, Alice could create a world-writable file under a directory accessible only by her (so no one else could access that world-writable file), and then run chown to make that file owned by another user Bill. The file would then count under Bill's disk quota even though only Alice can use the file.
  • If Alice gives away a file to Bill, there is no trace that Bill didn't create that file. This can be a problem if the file contains illegal or otherwise compromising data.
  • Some programs require that their input file belongs to a particular user in order to authenticate a request (for example, the file contains some instructions that the program will perform on behalf of that user). This is usually not a secure design, because even if Bill created a file containing syntactically correct instructions, he might not have intended to execute them at this particular time. Nonetheless, allowing Alice to create a file with arbitrary content and have it taken as input from Bill can only make things worse.
  • 4
    At a previous job, I built a software system that depended on the inability to give away files. It used file ownership to verify that a request had been submitted by a particular user. It checked, during installation, whether giving away files was permitted, and if so it refused to proceed. Dec 22 '11 at 3:09
  • 3
    Another more critical issue is that a user could copy /bin/bash, setuid it, and then chown it to whoever they want. Now they have shell access as that person.
    – phemmer
    Mar 29 '13 at 22:30
  • 20
    @Patrick chown always clears the setuid and setgid bits. Mar 29 '13 at 23:06
  • 1
    @Gilles and for good reason... if you could copy a shell binary somewhere you can access it, set the setuid/gid bits on it, and chown it to root (or any order thereof that gets you 6755/0:0 perms/ownership) you can obtain root on that system. Oh missed Patrick's comment, exactly.
    – hanetzer
    Dec 16 '14 at 8:30
  • Ok, but if I own the dir (drwxr-xr-x ring0 ring0 .) in which root has a regular file (-rw-r--r-- root root file),why can't I do chown ring0 file since it is anyway allowed to do, as ring0, cp file x ; rm file ; mv x file (and some optional touch sometime file ...) ?
    – Déjà vu
    Dec 5 '15 at 7:37

On Linux, you need the CAP_CHOWN capability to chown. root is granted such. Refer to: http://vouters.dyndns.org/tima/Linux-OpenVMS-C-Implementing_chown.html for explanations. If you intend to give the CAP_CHOWN capability, build your code with libcap-ng or libcap as demonstrated by: http://vouters.dyndns.org/tima/Linux-PAM-C-Pluggable_Authentication_Modules_programming_example.html where you have to simple replace CAP_AUDIT_WRITE with CAP_CHOWN.

  • 1
    +1 for you don't have to be root. Because you don't have to be root any more. Feb 19 '15 at 23:38
  • 2
    Sometimes, you don't even need CAP_CHOWN: unix.stackexchange.com/questions/399975/… From my read of the kernel source, each filesystem implementation is tasked with checking permissions for chown. And it appears that with NFS, the permissions are checked on the server side. And if the server is... strange... then it's possible.
    – Mike S
    Oct 25 '17 at 14:19
  • @MikeS: It makes perfect sense; each filesystem has its own idea how users are identified. Windows user identities for instance combine a machine-ID and a relative user-ID. And on network systems like NFS, your local UID is often equally meaningless. Anyone can be root locally. CAP_CHOWN is necessary but not sufficient.
    – MSalters
    Mar 10 '20 at 13:39

You can launch the command but it will not work if you are not root. It is easy : imagine a user which can change a software to root user. It can add the setuid bit and, voilà, the guy is root ! So, the use can add the bit with chmod, but no chance to change the owner of files.

  • 12
    You can't add the setuid bit on a file you don't own, and implementations that allow giving away files clear the setuid bit. Dec 22 '11 at 0:55
  • I think the point of Dom's answer is this: Imagine if you could. Then there would be trouble. Your point, that you can't, is correct. But the OP is asking "why?" Clearing the setuid bit is another security feature that again begs the question "why?" Which I would then refer to Dom's answer: IF a user could chown, and IF a user could setuid, then that combo would be disastrous. I think he makes a fine point, even if he's missing a little bit.
    – Mike S
    Oct 23 '17 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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