You could write a shared object that gets pre-loaded when your program starts running. In the .so you'd redefine the libc function open()
. When the caller (the program that's getting fooled) passes as argument the string /etc/mysql/my.cnf
, you'd instead open ~/my.cnf
and return the opened file descriptor. The caller wouldn't know the difference.
Your shared object would of course need to call the "real" open()
to to open the file handle; you can get a pointer to the original libc function using dlsym()
.
This seems overly complicated, but in fact it isn't, it works like a charm. I've used it on several occasions where I had to fool a program that I didn't have the sources for; and it simply works like a clockwork.
If you want to see a proof of concept, check out my blog where I wrote it up. Happy coding!