PHD Discussions Logo

Ask, Learn and Accelerate in your PhD Research

Question Icon Post Your Answer

Question Icon

4 months ago in Cybersecurity By Suresh

For LD_PRELOAD to work, does the target function need to be dynamically linked as a weak symbol, or is that just an implementation detail of glibc?

I was reading the glibc source and noticed malloc is declared with a weak alias. Some forum posts claim this is necessary for interposition. But I've successfully hooked functions that aren't weak. I'm trying to separate the ELF specification from glibc's specific implementation choices. What does the standard actually require, and what is just convention?

All Answers (2 Answers In All)

By Fathima M Answered 3 months ago

 Yes, you can achieve this through function hooking. The common method is to hijack the target function's entry in the Procedure Linkage Table (PLT), redirecting it to your custom code. Alternatively, you can use inline patching (overwriting the beginning of the function in memory) to insert a jump instruction to your desired address. Both techniques effectively let you control the execution flow and jump to a specific location within your preloaded library.

                             

By Neethi Answered 2 months ago

Let me separate specification from implementation for you. ELF itself doesn't require weak symbols for interposition. The dynamic linker's rule is simple: the first definition it sees wins, regardless of strength. If your preloaded library defines a strong symbol malloc, and libc defines a strong symbol malloc, this is normally a multiple-definition error for static linking. But the dynamic linker, at runtime, is more permissive and will accept either. The weak alias in glibc is a developer convenience it allows libc to provide a default malloc that can be overridden without the linker complaining during static analysis. I have successfully hooked strong symbols in proprietary libraries without weak aliases. The real requirement is that the symbol must be in the dynamic symbol table. That's the only non-negotiable.

 

Your Answer