3

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)

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
Julien Fouilhé
  • 2,583
  • 3
  • 30
  • 56

2 Answers2

6

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.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
4

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).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Oh, that's cute. I figured it was possible with `ptrace` magic; it's nice to know that someone actually did it. – nneonneo Sep 07 '13 at 17:05
  • BTW right now it's fairly limited due to syscall differences between architectures and lack of resources to support all the archs Linux supports, but with the new "seccomp2" `ptrace` interfaces, it's possible to do syscall interception and filtering in an almost-arch-agnostic way. Adapting Fakeroot-ng to use the seccomp2 approach would make it a lot more portable and practical. – R.. GitHub STOP HELPING ICE Sep 07 '13 at 17:13
  • @nneonneo Thanks guys, I can't figure out which one of you two got my answer, works perfectly (has to be launched in sudo though) ;) – Julien Fouilhé Sep 07 '13 at 17:13
  • @R.. has your answer. I just gave the reason :) – nneonneo Sep 07 '13 at 17:15