Skip to content

new(bpf,pkg): drop ldd for dynamic_so and parse ldso instead - #6019

Draft
FedeDP wants to merge 11 commits into
mainfrom
fededp/new/sopath-ldso-resolve-libc
Draft

FedeDP wants to merge 11 commits into
mainfrom
fededp/new/sopath-ldso-resolve-libc

Conversation

@FedeDP

@FedeDP FedeDP commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

In #5563 , we used ldd to determine fullpath to libc for the binary that was going to be traced with override.sopath action.
As noted here: #5825, ldd would not work for uprobes in container (once #5190 is in) and it has also some security concerns: #5563 (comment)

This PR implements an ld.so.cache parser and ELF headers parsing direct dependencies looking for libc, thus avoiding ldd.
Also, a Root interface is introduced that will allow an easy introduction for container support once needed.
Finally, musl support is introduced; since bpf code was hardcoded to look for libc.so.6 in process VMA, we now pass an additional libc_name in regs_map that is used by bpf code as libc to look for.

AIL: 3 (https://danielmiessler.com/blog/ai-influence-level-ail)

Changelog

new(bpf,pkg): drop `ldd` for dynamic_so and parse ldso instead

@netlify

netlify Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit d28b294
🔍 Latest deploy log https://app.netlify.com/projects/tetragon/deploys/6b04280b66bd82adcb9eaf44
😎 Deploy Preview https://deploy-preview-6019--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@FedeDP
FedeDP force-pushed the fededp/new/sopath-ldso-resolve-libc branch from 673fd0a to 5e7d99c Compare November 5, 2026 14:08
@FedeDP FedeDP added kind/enhancement This improves or streamlines existing functionality release-note/minor This PR introduces a minor user-visible change labels Nov 5, 2026
@FedeDP

FedeDP commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Example output on an alpine container (with a convenient libcFile print in (dynOv *DynamicOverride) PopulateUprobeRegs():

libcFile ld-musl-x86_64.so.1
level=info msg="Loaded generic uprobe program: bpf/objs/bpf_generic_uprobe_v61.o -> /home/main [foo]"
level=info msg="Loaded sensor successfully" sensor=generic_uprobe
level=info msg="Added TracingPolicy with success" TracingPolicy=override-so.yaml metadata.namespace="" metadata.name=uprobe-dyn-test
level=info msg="Perf ring buffer size (bytes)" percpu=68K total=1M
level=info msg="Events queue size (events)" size=63K
level=info msg="Listening for events..."

/work # /home/main 
22
bar 22
bar 22
creating secondary thread
bar 22
end
/work # fg
./tetragon --bpf-lib bpf/objs/ --tracing-policy override-so.yaml
^Clevel=info msg="Received signal interrupt, shutting down..."
level=info msg="Listening for events completed." error="context canceled"
level=info msg="Unloading sensor generic_uprobe"
level=info msg="Sensor unloaded" sensor=generic_uprobe maps-error=[]
level=info msg="Unloading sensor __base__"
level=info msg="Sensor unloaded" sensor=__base__ maps-error=[]
level=info msg="BPF events statistics: 121 received, 0% events loss"
level=info msg="Observer events statistics" received=121 lost=0 errors=0 filterPass=0 filterDrop=0
/work # /home/main 
22
foo 22
foo 22
creating secondary thread
foo 22
end

@immanuwell

Copy link
Copy Markdown
Member

could searchDirs() check active glibc-hwcaps subdirectories first?
I reproduced wrong mmap/dlopen offsets when the loader picked the v3 libc

@FedeDP

FedeDP commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@immanuwell totally makes sense, thanks for the hint! I'll fix it asap 🚀

@FedeDP
FedeDP force-pushed the fededp/new/sopath-ldso-resolve-libc branch from 5e7d99c to b878b46 Compare November 6, 2026 09:30
@FedeDP

FedeDP commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

can you re-test @immanuwell ? Thanks!

@FedeDP
FedeDP force-pushed the fededp/new/sopath-ldso-resolve-libc branch 2 times, most recently from fb323e6 to d12f33b Compare November 6, 2026 11:45
@immanuwell

Copy link
Copy Markdown
Member

@FedeDP re-tested this on d12f33b,
the resolver now picks the same glibc-hwcaps libc as the loader.

with baseline and x86-64-v3 libc paths available, both ldd and ResolveLibc() selected the v3 one.
other related tests pass as well.
so it's fully fixed, thanks

@FedeDP
FedeDP force-pushed the fededp/new/sopath-ldso-resolve-libc branch from d12f33b to e75fa36 Compare November 6, 2026 15:43
This work will enable the support for musl, that will be
introduced in subsequent commits.

In bpf code, the uprobe `sopath` override looks for the libc base
address in the process VMA by comparing their filename against
hardcoded "libc.so.6".
Let userspace provide the library name instead,through a new
`libc_name` field of `struct uprobe_regs`, sized `SONAME_MAX`,
following what we already did for `soname`, since it is
compared by find_library_base() the same way.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
It abstracts the filesystem in which a binary and its shared library
dependencies are resolved. Paths passed to a `Root` method
are absolute paths as seen from inside it, so that the same resolution
logic can later be applied to container root filesystems, by providing
an implementation that confines lookups (including symlinks) to them.

For now, only `HostRoot()` is provided.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Add a parser for glibc `/etc/ld.so.cache`, that maps sonames to library paths.

It supports only the "new" format, written by default by ldconfig
since glibc 2.32.
The old and compat formats are intentionally left out:
uprobe `sopath` override feature requires glibc >= 2.34 anyway.
Such caches will be reported as malformed, so that the resolver
can fall back to the default library directories.

The cache may come from an untrusted root, so its size is capped and
every offset is bounds checked.

Tests cover truncated and malformed caches, and compare the result of
parsing the host cache against `ldconfig -p`.

AIL: 3 (https://danielmiessler.com/blog/ai-influence-level-ail)

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Add `ResolveNeeded()`, which looks up a direct dependency of a
dynamically linked binary by parsing its ELF headers and following the
glibc dynamic loader search order: DT_RPATH (if no DT_RUNPATH),
DT_RUNPATH, /etc/ld.so.cache and the default directories, the latter
two being skipped for binaries linked with `-z nodefaultlib`.

$ORIGIN, $LIB and $PLATFORM are expanded in DT_RUNPATH/DT_RPATH.
Default directories cover both multiarch (Debian/Ubuntu) and lib64
(Fedora/RHEL) layouts. Candidates whose ELF class, data encoding or
machine don't match the binary are skipped, like ld.so does.
LD_LIBRARY_PATH is not taken into account, as it depends on each
process environment.

Static binaries, musl binaries and binaries not directly depending on
the requested library are reported with dedicated errors.

Unlike ldd, nothing is executed, and all lookups go through a `Root`,
so that binaries from other root filesystems can be supported.

Tests run against synthetic root filesystems, built from minimal
generated ELF files, and check we agree with ldd on host binaries.

AIL: 3 (https://danielmiessler.com/blog/ai-influence-level-ail)

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Add `ResolveInterpreter()`, returning the program interpreter
(PT_INTERP) of a binary, opened through the `Root` and checked to be
compatible with the binary like any other resolved library.

This is meant for musl binaries, where libc and the dynamic loader are
the same object: e.g. on Alpine, binaries depend on
libc.musl-x86_64.so.1, a symlink to their interpreter
/lib/ld-musl-x86_64.so.1. Looking it up through the interpreter avoids
dealing with arch and distro specific musl sonames, and with musl's own
library search order.

`ErrMusl` documentation is updated to point to it.

AIL: 3 (https://danielmiessler.com/blog/ai-influence-level-ail)

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP
FedeDP force-pushed the fededp/new/sopath-ldso-resolve-libc branch from e75fa36 to d28b294 Compare November 23, 2026 11:48
@FedeDP

FedeDP commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Dropped first commit that is now part of #6132

It's going to be used to detect the CPU features glibc relies on to
pick glibc-hwcaps subdirectories. golang.org/x/sys is already a
dependency, so this only vendors one more of its packages.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
glibc >= 2.33 looks up libraries in the glibc-hwcaps subdirectories of
each search directory first (e.g. /usr/lib64/glibc-hwcaps/x86-64-v3/),
for each subdirectory supported by the CPU, in priority order.

Add `hostHwcaps()`, returning the glibc-hwcaps subdirectories supported
on the host for binaries of a given ELF machine and class, in priority
order. On x86-64, they are computed from the CPU features glibc checks
for each ISA level, read through CPUID like glibc does. On arm64, none
is returned, as glibc defines no glibc-hwcaps subdirectories for it.

The CPU is shared by all processes on the machine, containers
included. Per process overrides (ld.so --glibc-hwcaps-* options,
glibc.cpu.hwcaps tunable) are not taken into account.

Tests check the result against the host ld.so `--help` output.

AIL: 3 (https://danielmiessler.com/blog/ai-influence-level-ail)

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Distros use glibc-hwcaps subdirectories to ship library builds
optimized for newer CPUs, whose symbol offsets differ from the
baseline build: resolving the baseline one while ld.so loads an
optimized one leads to wrong offsets.

Like ld.so, in `ResolveNeeded()`:
- for each search directory, try its glibc-hwcaps subdirectories
  supported by the host first, in priority order;
- parse the glibc-hwcaps cache extension of ld.so.cache, and prefer
  entries of supported subdirectories, by priority, over regular ones,
  skipping entries of unsupported subdirectories. A missing or
  malformed extension only drops the glibc-hwcaps entries. Entries
  flagged with legacy hwcaps, removed in glibc 2.37, are ignored.

Tests also check the cache parser against `ldconfig -p` on a cache
with glibc-hwcaps entries generated by ldconfig itself.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Add `ldso.ResolveLibc()`, which resolves the libc of a binary: the
libc.so.6 (`GlibcSoname`) dependency for glibc binaries, resolved with
`ResolveNeeded()`, or the program interpreter for musl binaries, where
libc is also the dynamic loader, resolved with `ResolveInterpreter()`.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Replace the ldd-based `resolveDep()` with `ldso.ResolveLibc()` to find
the libc of the uprobed binary. ldd does not work for binaries from
another root filesystem, e.g. a container's, and it may execute the
binary it inspects. As a bonus, musl binaries are now supported too:
musl libc exports both mmap and dlopen, and the libc file name passed
to the bpf side is the musl dynamic loader one.

The resolved libc is parsed from the file returned by the resolver
rather than reopened by path, and its real path is computed through
the resolver `Root`.

Also, report a clear error when libc does not export dlopen, which was
moved from libdl into libc with glibc 2.34.

Drop `TestDynamicSOResolveDep`, now covered by pkg/elf/ldso tests.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Add mention of new musl support and properly explain
how is libc.so.6 looked up in glibc executables.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
@FedeDP
FedeDP force-pushed the fededp/new/sopath-ldso-resolve-libc branch from d28b294 to 7c486b1 Compare November 23, 2026 11:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement This improves or streamlines existing functionality release-note/minor This PR introduces a minor user-visible change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants