Top Qs
Timeline
Chat
Perspective
Rpath
Hard-coded search path From Wikipedia, the free encyclopedia
Remove ads
In computer science, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries.
This article may be too technical for most readers to understand. (December 2022) |
Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library). This RPATH header value (so named in the Executable and Linkable Format header standards) may either override or supplement the system default dynamic linking search paths.
The rpath of an executable or shared library is an optional entry in the .dynamic section of the ELF executable or shared libraries, with the type DT_RPATH, called the DT_RPATH attribute. It can be stored there at link time by the linker. Tools such as chrpath and patchelf can create or modify the entry later.
Remove ads
Background
Modern computer executables use dynamic libraries, which are separate loadable files, to reduce the duplication of code. When such an executable is run, the dynamic linker ld.so goes through the dynamic library entries and loads them for use by the executable program. Because libraries can be located in many possible locations, the ELF format has the DT_RPATH entry to specify to ld.so where to find the libraries.
The different dynamic linkers for ELF implement the use of the DT_RPATH attribute in different ways. The main divergence lies in:
- Handling of the new entry
DT_RUNPATH, a more restricted version of rpath. - Whether relative paths are available via
$ORIGIN. - Whether other variables such as
$PLATFORM,$LIBare available.
Remove ads
GNU ld.so
Summarize
Perspective
The dynamic linker of the GNU C Library searches for shared libraries in the following locations in order:[1]
- The (colon-separated) paths in the
DT_RPATHdynamic section attribute of the binary if present and theDT_RUNPATHattribute does not exist. - The (colon-separated) paths in the environment variable
LD_LIBRARY_PATH, unless the executable is asetuid/setgidbinary, in which case it is ignored.LD_LIBRARY_PATHcan be overridden by calling the dynamic linker with the option--library-path(e.g./lib/ld-linux.so.2 --library-path $HOME/mylibs myprogram). - The (colon-separated) paths in the
DT_RUNPATHdynamic section attribute of the binary if present. - Lookup based on the
ldconfigcache file (often located at/etc/ld.so.cache) which contains a compiled list of candidate libraries previously found in the augmented library path (set by/etc/ld.so.conf). If, however, the binary was linked with the-z nodefaultliblinker option, libraries in the default library paths are skipped. - In the trusted default path
/lib, and then/usr/lib. If the binary was linked with the-z nodefaultliblinker option, this step is skipped.
Failing to find the shared library in all these locations will raise the "cannot open shared object file: No such file or directory" error.
Notes:
readelf -d <binary_name> | grep 'R.*PATH'displays the RPATH or RUNPATH of a binary file. In gcc, for instance, one could specify RPATH by-Wl,-rpath,/custom/rpath/.- The option
--inhibit-rpath LISTof the dynamic linker instructs it to ignoreDT_RPATHandDT_RUNPATHattributes of the object names in LIST. To specify a main program in the LIST, give empty string. - Libraries specified by the environment variable
LD_PRELOADand then those listed in/etc/ld.so.preloadare loaded before the search begins. A preload can thus be used to replace some (or all) of the requested library's normal functionalities, or it can simply be used to supply a library that would otherwise not be found. - Static libraries are searched and linked into the ELF file at link time and are not searched at run time.
Relative paths are supported by the special token $ORIGIN, which expan≥ds to the directory of the executable at runtime. For example, -Wl,-rpath,$ORIGIN would add the directory the executable is in to the rpath. This allows for building binaries that can be freely moved across different folders.
The role of GNU ld
The GNU Linker (GNU ld) implements a feature which it calls "new-dtags", which can be used to insert an rpath that has lower precedence than the LD_LIBRARY_PATH environment variable.
[2]
If the new-dtags feature is enabled in the linker (--enable-new-dtags), GNU ld, besides setting the DT_RPATH attribute, also sets the DT_RUNPATH attribute to the same string. At run time, if the dynamic linker finds a DT_RUNPATH attribute, it ignores the value of the DT_RPATH attribute, with the effect that LD_LIBRARY_PATH is checked first and the paths in the DT_RUNPATH attribute are only searched afterwards.
The ld dynamic linker does not search DT_RUNPATH locations for transitive dependencies, unlike DT_RPATH.[3]
Instead of specifying the -rpath to the linker, the environment variable LD_RUN_PATH can be set to the same effect.
Remove ads
Solaris ld.so
The dynamic linker of Solaris, specifically /lib/ld.so of SunOS 5.8 and similar systems looks for libraries in the directories specified in the LD_LIBRARY_PATH variable before looking at the DT_RPATH attribute. Sun Microsystems was the first[citation needed] to introduce dynamic library loading. Sun later added the rpath option to ld and used it in essential libraries as an added security feature. GNU ld did the same to support Sun-style dynamic libraries.
$ORIGIN is supported.[4]
In other dynamic linkers
ELF format:
- musl's ld.so handles
DT_RUNPATHin the same way asDT_RPATH. It uses the following order:LD_LIBRARY_PATH, then rpath/runpath, then default paths. It understands$ORIGIN. - bionic libc's ld.so does not recognize any form of rpath.
- FreeBSD ld.so respects the non-inheritance and lower precedence of
DT_RUNPATH. It also handles$ORIGIN.[5]
Non-ELF formats:
Remove ads
Security considerations
The use of rpath and also runpath can present security risks where the value applied includes directories under an attacker's control. This can include cases where the value defined explicitly references an attacker writable location but also instances where a relative path is used via $ORIGIN, or where a directory statement is left unpopulated.
This can be leveraged to trick the binary into loading malicious libraries from one or other of the directories under an attacker's control.
This is especially dangerous with setUID binaries, as these binaries run as a different (usually higher-permission-level, often root) user.[7]
Remove ads
References
External links
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads