2

I'm trying to use an exec system call to start another executable, but that executable is using readlink() on /proc/self/exe to determine its file location. I would like it to think that it has the same location as the parent executable, but it instead determines its actual location. I think that I could potentially accomplish something like this by using LD_PRELOAD to intercept the readlink() call, but is there any easier or more direct way to accomplish this? I'm looking only for solutions that can be implemented in the parent executable, modifying the child is not an option.

Ivanna
  • 1,087
  • 1
  • 9
  • 19
  • Why do you care what the *location* of the binary running as a process is? hint: try running `ls -l /proc/self/exe` from the command line. – wildplasser Mar 03 '18 at 15:57
  • Because the child process is looking for files relative to its own location, and I would like it to look for the files somewhere else. Was the "hint" supposed to be helpful? – Ivanna Mar 03 '18 at 16:04
  • You could try with a hard link in your own directory. This could need root rights. – wildplasser Mar 03 '18 at 16:09
  • Revise the program so that it looks where it is told to look, falling back on `/proc/self/exe` only as a last resort (or, as the default, normal behaviour — you choose the terminology)? You can specify the alternative by command line argument, environment variable or configuration (ini) file — all are possibilities. – Jonathan Leffler Mar 03 '18 at 18:21
  • As mentioned in the question "modifying the child is not an option." It's not my code. – Ivanna Mar 03 '18 at 18:23

1 Answers1

1

Create a hard link to the executable:


$sudo ln /bin/ls ./ls
[sudo] password for plasser: 

# check it:
$ls -l ls
-rwxr-xr-x 2 root root 110080 mrt 10  2016 ls

#call it:
$./ls -l /proc/self/exe
lrwxrwxrwx 1 plasser uri 0 mrt  3 18:36 /proc/self/exe -> /home/plasser/krant/ls
$
wildplasser
  • 38,231
  • 6
  • 56
  • 94