From b7100d0a3adb86c0374f161f862e3067efacc790 Mon Sep 17 00:00:00 2001 From: Pratham Patel Date: Sat, 21 Jun 2025 20:15:22 +0530 Subject: [PATCH] fix shared obj lookup in `getLibraryDependencies()` Sometimes, the `ld-linux` shared object is presented as an absolute path, instead of the shared object name and the path where that shared object is found. Something like this: ``` $ ldd /nix/store/s7cjsl5g5hdi4lfmb5qghghgni69ch1y-coreutils-full-9.7/bin/true linux-vdso.so.1 (0x0000ffffb47fc000) libcrypto.so.3 => /nix/store/ffdmy5fhjr6m1gkb8mycyf3swakvygd1-openssl-3.4.1/lib/libcrypto.so.3 (0x0000ffffb4240000) libacl.so.1 => /nix/store/ccy32hi8wvjj1im3dbjld658rpvl226n-acl-2.3.2/lib/libacl.so.1 (0x0000ffffb4210000) libattr.so.1 => /nix/store/3fqsrmsjdpfpy6dyxymbg6r01f9y39ln-attr-2.5.2/lib/libattr.so.1 (0x0000ffffb41e0000) libgmp.so.10 => /nix/store/xmdlv9xmhyiqip377326ph94k1ipndz2-gmp-with-cxx-6.3.0/lib/libgmp.so.10 (0x0000ffffb4140000) libc.so.6 => /nix/store/7kpxf47mzykkdn39lcnhj9z9ngpihamf-glibc-2.40-66/lib/libc.so.6 (0x0000ffffb3f60000) /nix/store/7kpxf47mzykkdn39lcnhj9z9ngpihamf-glibc-2.40-66/lib/ld-linux-aarch64.so.1 (0x0000ffffb47c0000) libdl.so.2 => /nix/store/7kpxf47mzykkdn39lcnhj9z9ngpihamf-glibc-2.40-66/lib/libdl.so.2 (0x0000ffffb3f30000) libpthread.so.0 => /nix/store/7kpxf47mzykkdn39lcnhj9z9ngpihamf-glibc-2.40-66/lib/libpthread.so.0 (0x0000ffffb3f00000) ``` Here, because the line with `ld-linux-aarch64.so.1` does not have the pattern `=>`, it gets excluded and the binary execution (with the `--ldd` flag) fails due to a permissions error. --- cmd/landrun/main.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/landrun/main.go b/cmd/landrun/main.go index efd99b4..a4f3801 100644 --- a/cmd/landrun/main.go +++ b/cmd/landrun/main.go @@ -25,12 +25,32 @@ func getLibraryDependencies(binary string) ([]string, error) { var libPaths []string lines := strings.Split(string(output), "\n") for _, line := range lines { - // Skip empty lines and the first line (usually the binary name) - if line == "" || !strings.Contains(line, "=>") { + // Skip lines without shared information about shared objects + if !strings.Contains(line, ".so") { continue } // Extract the library path parts := strings.Fields(line) + + // Add only shared objects which are a **path**, because sometimes + // they are present as ` ()` + // instead of ` => ()`. + // And `ld-linux` sometimes has that behaviour and not including it + // in `libPaths` definitely results in an error in executing the + // binary. + // + // For example, the line `/lib/ld-linux-aarch64.so.1 ()` will + // match and the path `/lib/ld-linux-aarch64.so.1` will be added to + // `libPaths`. The line `linux-vdso.so.1 ()` will not match as + // `linux-vdso.so.1` is not a path so we have nothing to + // "whitelist" per se. + if len(parts) == 2 { + libPath := strings.Trim(parts[0], "()") + if strings.Contains(libPath, "/") { + libPaths = append(libPaths, libPath) + } + } + if len(parts) >= 3 { libPath := strings.Trim(parts[2], "()") if libPath != "" {