3

I was hooking few functions in my code and it was working pretty code till today and then I came across a bug due to call back function.

Lets say..

If I do something like

puts("Hi!\n");

works great. I can hook this.

But If I do this...

typeof(puts) *fptr = puts;
fptr("Hi \n");

Hooking does not work?

I am using OSX env and searching for symbols in order to do hooking. Can someone suggest me whats wrong with callback functions and what I should be doing in to hook in hooking algorithm?

EDIT: I did some more debugging, in case if with the following information anyone who can provide some opinion.

I think this can be source of problem?

bool Hook(const char *name, void *impl) {
    ...   
    void **EntryInAdressTable = find(name);
    if(EntryInAdressTable) {
        *EntryInAdressTable = impl;
    }
}
 ...
}

So, What's happening here is, I change the Entry in address table for corresponding symbol with my implementation and the in My implementation I call original function.

So, my guess is, If we use callback function, it means we referred directly to function address without going through the address table and thats why hooked method is not called.

Am I right on this one? If so can any one suggest me any workaround?

Mansuro
  • 4,558
  • 4
  • 36
  • 76
RLT
  • 4,219
  • 4
  • 37
  • 91

2 Answers2

0

If we hook by looking into symbol table and replacing the function adress there corresponding to symbol entry will work as long as we are making function call using symbol entry in symbol table. If we use function address directly (callback function), it will not work.

RLT
  • 4,219
  • 4
  • 37
  • 91
0

Assuming you're using LD_PRELOAD to hook your functions, when you take a pointer to puts, the pointer is presumably bound to the version the linker knows about when you link, in the standard library, and isn't overridden when your preloaded library comes into existence. I can't think of any way to bypass this.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • I dont take a pointer to puts. I take a symbol "_puts". Then I look up for commands and try to find if this string is present. – RLT Oct 27 '11 at 16:08
  • I have updated question with my findings. Please let me know your thoughts – RLT Oct 28 '11 at 12:22