If I run su, it asks for the password of the target user. If i run it as root, it does not ask for a password. But if I run it in fakeroot, how is it possible that it will ask for a password? Should fakeroot not make all programs think that you are root? For my own program, the only way of doing this would be trying to run su, and look at the return code. but how does su do that?
2 Answers
su detects nothing.
From man fakeroot:
fakeroot works by replacing the file manipulation library functions (
chmod(2),stat(2)etc.) by ones that simulate the effect the real library functions would have had, had the user really been root.
These wrapper functions are in a shared library/usr/lib/*/libfake‐root-\*.soor similar location on your platform. The shared object is loaded through theLD_PRELOADmechanism of the dynamic loader. (Seeld.so(8))
su, being a setuid executable, always runs as root - but doesn't care about LD_PRELOAD¹. So, while "normal" programs are being told they're run as root by replacing the functions with which they ask for that, su never sees that modification. So, su just runs as usual.
¹ that would be a terrible security hole; you could just set LD_PRELOAD to contain a library that does whatever you want to be done, and it would automatically be executed as root if you could run any setuid executable
The documentation for fakeroot (see man fakeroot) seems to answer this for you:
DESCRIPTION
fakerootruns a command in an environment wherein it appears to have root privileges for file manipulation.
and,
EXAMPLES Only the effects that user
joostcould do anyway happen for real
and finally,
SECURITY ASPECTS
fakerootis a regular, non-setuid program. It does not enhance a user's privileges, or decrease the system's security.
In the first block, note the word appears. In the examples extract, consider the user joost to be you. In the final section, there's a clear statement that fakeroot does not add privileges or decrease security in any way. Specifically, this means that it does not change how the real su works.