14

In linux it's possible to change the owner or the group owner of a symbolic link (symlink). I was wondering why someone would want to do that, since permissions of a symlink are not used when accessing a file through it.

I can only imagine one use case where it could be useful: to allow a user to delete a symlink in a directory with sticky bit.

Do you know other cases where it might be useful to change the owner or group owner of a symlink ?

5 Answers 5

9

Suppose root is working in a directory that Eve can write to. There's a file foo in this directory that needs to be changed to belong to Eve. So root types chown eve foo. But just before root hits Enter, Eve runs ln -sf /etc/passwd foo. Now /etc/passwd belongs to Eve! If root can run chown -h eve foo to make sure not to follow symlinks, then the most harm that can be done is that some other file in the same directory has been changed to belong to Eve.

lchown is also convenient when you're changing the owner of a directory tree. You don't need to worry about accidentally affecting a file outside the tree because you called chown on a symbolic link.

5
  • "If root can run chown -h bob foo to make sure not to follow symlinks, then the most harm that can be done is that some other file in the same directory has been changed to belong to Eve.". I guess you mean "chown -h eve foo". The other file that might be changed, it is the symbolic link am i right ?
    – user368507
    Commented Mar 2, 2012 at 0:22
  • @user5528 The other file might not be a symlink: Eve can still run mv myfile foo, and root will end up changing the owner of myfile. But myfile has to be a file that Eve can create or move into that directory, it can't be any file on the system. Commented Mar 2, 2012 at 0:47
  • 2
    While interesting, and obviously approved of by the asker, I don't see how this answer addresses the question. It seems more to explain why one might use chown -h as a cautionary measure when changing ownership of a file that isn't supposed to be a symlink, but still might be (an edge case, IMO). It doesn't explain why one might wish to change the ownership of a file that is in fact intended to be a symlink, which is what the question asked.
    – Ivan X
    Commented Jan 18, 2015 at 6:03
  • Why would chown change the owner of "a file outside the tree" is all you are changing is the directory's owner?
    – Melab
    Commented Nov 18, 2016 at 4:16
  • @Melab When you're changing the owner of a directory tree, i.e. unning the utility chown -R, that calls the (l)chown system call on each directory entry. If the directory entry is a symbolic link then you must not call the chown system call on it because that would affect the target of the link which may be located outside the tree. Commented Nov 18, 2016 at 10:31
9

Apache can be configured to follow symlinks only if the owner of the link matches the owner of the destination. This can help prevent users from creating links for web access to files they don't own (e.g. /etc/passwd).

... so let's say you, as root, wanted apache to follow a link to display a certain logfile, which was owned by xymon or something, but you didn't want to relax apache's security by allowing it to follow symlinks regardless of owner. Then you might want to make xymon the owner of the symlink.

2
  • 1
    Ok. I know it's unrelated but what is the point of this behaviour in apache ? I mean if the user is able to read the file, why bother read it from web access ? thx
    – user368507
    Commented Mar 2, 2012 at 0:11
  • Well, it's not just a local user reading the file; if apache can read it, then potentially everybody can read it. And if some apache vulnerability allowed the creation of a symlink to /etc/passwd, then the badguy might have read access to that file without having any other local access -- but would be thwarted by the symlink being owned by apache. Commented Mar 13, 2012 at 15:35
5

The first answer doesn't seem to adress the question, and the second one only applies to Apache.

One thing I can think of for linux in general is that it's only possible for an ordinary user to make a hard link to a symbolic link if the user is the owner of the symbolic link. Why one would want to make such a link, I don't know.

Another thing is that an ordinary user can only change the group ownership of a file if the user owns the file (and is also a member of the group the file is being added to.) That brings up the question of what the group ownership of a symbolic link does. In an organization, it might be useful as a tag to indicate which team would have need of the link.

Also, on Ubuntu at least, anyone can update the timestamp of a symbolic link. However, there may be some systems that only allow the owner to. What good the timestamp does for a symbolic link, I'm not sure, but it may give some useful information on how much it's used.

Edit: I just realized another reason why ownership would be important. The link could be inside of a sticky directory, where only the owner of a file can delete or rename it.

1

If you want to have a link to a file on your opening screen, the symbolic link has to be in the

"/home/username/Desktop"

directory.

And, the symbolic link itself must have root:root (0:0) ownership, otherwise the link does not work.

(Ubuntu/Debian etc.)

0

I have an program that appends to a log file. These log files are created monthly each with a different name. Rather than have the software figure out the exact filename, I use a "generic" filename (say data.log) which is a symbolic link that points to the current file for that month. This is automated on a cron job.

Now as a new monthly file is created, it needs to point the symbolic link to the new file. If there is an ownership/group conflict, the software can't change the symbolic link. So you need ownership/group write privileges to change the symbolic link.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .