ldd: handle behaviour on static executable
All checks were successful
Test / Create distribution (push) Successful in 25s
Test / Run NixOS test (push) Successful in 3m27s

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-02-23 18:02:33 +09:00
parent 83c8f0488b
commit dccb366608
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q

View File

@ -1,10 +1,10 @@
package ldd
import (
"bytes"
"context"
"os"
"os/exec"
"strings"
"time"
"git.gensokyo.uk/security/fortify/helper"
@ -13,6 +13,10 @@ import (
const lddTimeout = 2 * time.Second
var (
msgStaticGlibc = []byte("not a dynamic executable")
)
func Exec(ctx context.Context, p string) ([]*Entry, error) {
var h helper.Helper
@ -32,8 +36,8 @@ func Exec(ctx context.Context, p string) ([]*Entry, error) {
return nil, err
}
stdout := new(strings.Builder)
h.Stdout(stdout).Stderr(os.Stderr)
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
h.Stdout(stdout).Stderr(stderr)
c, cancel := context.WithTimeout(ctx, lddTimeout)
defer cancel()
@ -41,6 +45,12 @@ func Exec(ctx context.Context, p string) ([]*Entry, error) {
return nil, err
}
if err := h.Wait(); err != nil {
m := stderr.Bytes()
if bytes.Contains(m, msgStaticGlibc) {
return nil, nil
}
_, _ = os.Stderr.Write(m)
return nil, err
}