Home > Back-end >  Circular Shebang #! (too many levels of symbolic links)
Circular Shebang #! (too many levels of symbolic links)

Time:04-04

For context: I was reading this example of trickery one can do with a shebang. It uses

#!/bin/rm

which ends up deleting the file you executed (very funny; you can extend this to create self-deleting messages). This shows that the program (rm) is invoked with the filename as an argument and therefore has access to the whole file including the shebang that invoked it.

Another trickery I've come up with is to invoke yourself as an interpreter in shebang to create an infinite loop. For example if /usr/bin/loop starts with

#!/usr/bin/loop

t should invoke itself with itself forever. Obviously at some point an error will occur and in my particular case I get:

bash: /usr/bin/loop: /usr/bin/loop: bad interpreter: Too many levels of symbolic links

It looks like it got two levels deep. Can somebody explain to me why this particular error occurs? Or maybe share some other error messages for different shells.

In particular I would like to understand why there are symbolic links involved and whether this is an implementation detail of bash or not.

CodePudding user response:

why this particular error occurs?

Because when kernel tries to run the executable, tries to run /usr/bin/loop, then tries to run /usr/bin/loop, then tries to run /usr/bin/loop, etc. and finally fails with ELOOP error. It checks that.

https://elixir.bootlin.com/linux/latest/source/fs/exec.c#L1767

hare some other error messages for different shells.

While it could be, this particular errno message comes from glibc strerror.

why there are symbolic links involved

Because ELOOP is typically returned when doing input/output operations, the message mentions them. Symbolic links are not involved.

this is an implementation detail of bash or not.

Not.

  • Related