I have a librandom.so library and a main exectuable which was compiled as follows:
$ clang++ -o main main.o -lrandom -L. -Wl,-rpath,"\$ORIGIN"
They are both in the same directory. Since main has $ORIGIN in its rpath, it works fine - ./main returns without errors.
Now, I'm setting main to run with setuid as root:
$ sudo chown root main
$ sudo chmod a+s main
$ ./main
I expected main to fail since $ORIGIN is not expanded in setuid applications. Surprisingly, it works.
If I run main from another directory, though, it does fail as expected:
$ cd /tmp
$ /path/to/main
/path/to/main: error while loading shared libraries: librandom.so: cannot open shared object file: No such file or directory
Why does it work when I run main from its containing directory?