Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
cant understand your question chown command can be used by non root user also –  harish.venkat 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
10  
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.) –  Keith Thompson Dec 21 '11 at 21:49

3 Answers 3

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 directory), 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.
share|improve this answer
    
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. –  Keith Thompson Dec 22 '11 at 3:09
1  
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. –  Patrick Mar 29 '13 at 22:30
3  
@Patrick chown always clears the setuid and setgid bits. –  Gilles Mar 29 '13 at 23:06

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.

In the hope this can help you.

share|improve this answer

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.

share|improve this answer
5  
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. –  Gilles Dec 22 '11 at 0:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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