How to know if user is root or fakeroot?

1.6k views Asked by At

I would like to know if user is root, without minding if that user is using a fakeroot-like tool or not.

I tried the functions getuid(), geteuid() and getlogin(), but when I launch fakeroot command each of these sends my own account information instead of root.

For this code:

printf("%d %d %s\n", getuid(), geteuid(), getlogin());

Here is what I get:

% fakeroot ./busybox rm 
1000 1000 julien

When I would like to get something like:

0 0 root

(the login would be enough)

2

There are 2 answers

4
R.. GitHub STOP HELPING ICE On BEST ANSWER

nneonneo got the reason right, but here's the solution: Fakeroot-ng. It uses ptrace and system call interception, rather than LD_PRELOAD and library call interception, which makes it compatible with static linking, much more robust, and even able to handle calls made from inside libc (which otherwise would not be hookable).

0
nneonneo On

It looks as if your binary (busybox) is compiled against a static libc. fakeroot uses dynamic library preloading to intercept and replace calls to various libc functions, but this only works if your binary is dynamically linked to libc. If it is statically linked, the function calls are bound to the real calls inside the binary, so there is no way to intercept them.