0

Because of a typo I found out that this is allowed: ln -s useless useless.

Then, as expected ls useless/* triggers this error message:

ls: cannot access 'useless/*': Too many levels of symbolic links

What are the situations which need this so it is allowed and ln therefore does accept it?

I was testing it on SLES 12.4.

6
  • 1
    To what lengths do you feel it would be appropriate to go to avoid symbolic link loops? Note that a symbolic link may also point to its grand-parent directory, or to its parent (these are also loops).
    – Kusalananda
    Feb 25, 2019 at 20:43
  • @Kusalananda For me I would implement ln that way that I would check if the new symbolic link triggers the error and print - at least - a warning. My question is why is the obvious useless constellation allowed. Feb 25, 2019 at 20:45
  • A symlink could start (or stop) pointing to itself if it's moved elsewhere, or if its parent directory is renamed, or an infinite number of other scenarios. Should mv, cp, etc. all check and print an error for this condition, which, contrary to what you say, it's not an "erroneous modification of the file system" (symlinks by definition, are not validated and can contain anything).
    – user313992
    Feb 25, 2019 at 21:28
  • @mosvy I am not talking - and I tried to explain it in my comments - about what can happen AFTER the symlink has been created. I am only talking about the inconsistent situation WHEN the symlink is created. @ symlinks by definition, are not validated and can contain anything seems to be the correct answer. Feb 25, 2019 at 21:31
  • There's no inconsistency -- symlink loops are allowed by the filesystem. And you cannot make sure that you won't create a symlink loop no matter how hard you try (you can inadvertently create a symlink loop even when creating a hard link ;-)), so a special case for simple, trivial cases like that would be pointless.
    – user313992
    Feb 25, 2019 at 21:37

2 Answers 2

1

There is no need fr this to be allowed; it just doesn’t care what the symlink points to, even to non-existing path (or the symlink itself).

One could imagine forbidding making a symlink to a non existing path, but there would be several problems:

  • There’s no backreference to the symlink, so it would still be possible to build a symlink to a file and then remove the file to make the symlink dangling.
  • A symlink may point to a different filesystem which might be unmounted.
  • The filesystem that contains the symlink may be mounted on several mount points simultaneously, some where the symlink points to existing path and other where it points to non-existing path.
  • Symlinks that point to absolute path may point to an existing path for some processes, and be dangling for others, since different processes may have a different root directory.

Once you have a symlink that points to a non existing path, you may rename the symlink to that it points to itself. So, if you wanted to forbid symlinks that point to themselves, you would need to restrict the renaming.

2
  • This is all correct and you describe situations which happens AFTER the symlink has been created. I am just wondering why an obviously erroneous modification in the file system is allowed when the symlink is created Feb 25, 2019 at 20:57
  • 1
    @AlBundy Why would it bother? Is it worse than a dangling symlink? Feb 25, 2019 at 21:01
0

Symbolic links can be anything because a symbol can represent any number of things, to any number of programs/OS's/people. Like symbols in the physical world, a symbol can be made of anything and be made for anything. Their is no need for validation as the target already exists on the system and or the user would have access to it via their privilege any way.

If you really want that behavior to stop. You could create a script to check if it is going to point to it self and return an error if it does and create the link if it doesn't. After the script is made then you can create an alias for ln that points to your script.

Could look something like the following:

#!/bin/bash
if [ "$1" == "$2" ]
then 
echo "ERROR pointing to self!"
else
ln "$1" "$2"
fi

You must log in to answer this question.

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