Skip to content

Fix Asherah on musl (Alpine): install-native asset selection and thread stack overflow - #405

Open
awankhede-godaddy wants to merge 2 commits into
godaddy:mainfrom
awankhede-godaddy:fix/musl-support
Open

awankhede-godaddy wants to merge 2 commits into
godaddy:mainfrom
awankhede-godaddy:fix/musl-support

Conversation

@awankhede-godaddy

@awankhede-godaddy awankhede-godaddy commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

asherah-go v1.0.0 doesn't work on musl-based images such as Alpine. This PR fixes two separate problems:

  1. install-native always installs the glibc library, which fails to load on Alpine.
  2. With the correct musl library installed, the native library overflows musl's default thread stack. The process segfaults in Decrypt, and sometimes in Setup.

Problem 1: install-native installs the glibc build on musl

assetNameForPlatform always returns libasherah-<arch>.so, even though every release also publishes libasherah-<arch>-musl.so. On Alpine the library then fails to load:

Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by libasherah_ffi.so)

docs/troubleshooting.md ("Alpine/musl LoadError") says musl is selected automatically, but nothing in the tool detects it.

Fix (e561b53):

  • install-native detects musl by checking for /lib/ld-musl-*.so.1, and installs libasherah-<arch>-musl.so when it's found.
  • A new flag, --libc=auto|gnu|musl, covers builds where the build and runtime images differ.
  • The troubleshooting doc and README describe the actual behaviour.

Problem 2: SIGSEGV on musl threads

musl gives every thread except the main one a 128 KiB stack. Go callers using purego run FFI calls on whichever runtime thread the goroutine is on. Building a factory with AWS KMS and DynamoDB, and the AWS calls behind encrypt and decrypt, need more stack than that:

SIGSEGV: segmentation violation  sigcode=2
signal arrived during cgo execution
asherah-go.(*Session).Decrypt  factory.go:174
asherah-go.NewFactory          factory.go:39   (from Setup)

Request goroutines don't run on the main thread, so Decrypt crashes every time. Setup only crashes on starts where it doesn't happen to run on the main thread. glibc threads get 8 MiB by default, so glibc isn't affected.

Fix (051d695):

  • The factory, session, encrypt and decrypt FFI entry points are wrapped in stacker::maybe_grow. When less than 1 MiB of stack is left, the call runs on a separately allocated 8 MiB stack. Otherwise it runs in place, so glibc threads and the musl main thread only pay a stack-pointer check.
  • asherah-go/cmd/musl-stack-repro is new: a small repro that runs Setup, encrypt and decrypt on threads other than the main one.

Most of the asherah-ffi/src/lib.rs diff is rustfmt re-indenting the wrapped bodies. The actual change is the with_stack helper and a wrapper around each of the 7 entry points.

The *_async entry points aren't wrapped: they hand the work to tokio, whose worker threads get a 2 MiB stack on every libc.

Testing

All tests ran on Alpine (x86_64), with the library built from this branch using cargo build --release -p asherah-ffi and -crt-static disabled.

Library musl-stack-repro aws (KMS + DynamoDB clients, thread other than the main one), 3 runs musl-stack-repro (memory metastore, static KMS)
Released v1.0.0 musl SIGSEGV every time not run
This branch No crash, every time; returns the expected AWS error (fake credentials) Setup and 32 encrypt/decrypt round trips OK

install-native:

  • On golang:1.26-alpine: installs libasherah-x64-musl.so, checksum verified.
  • On golang:1.26 (Debian): installs libasherah-x64.so, checksum verified.
  • The new platform_test.go tests pass.

Downstream: rdproxy (Go, Alpine), with the library built from this branch and its own PT_GNU_STACK stack workaround removed, starts, passes its Asherah encrypt/decrypt startup check, and decrypts stored values without crashing.

Not tested:

  • arm64 builds.
  • Encrypt and decrypt against real AWS KMS and DynamoDB from the repro. The repro only uses fake credentials.
  • Log and metrics hooks firing during a call that has switched to the grown stack.
  • Benchmarks. On musl threads every wrapped call is below the 1 MiB threshold, so it allocates a new 8 MiB stack.

Notes

  • New dependency: stacker in asherah-ffi, plus its dependency psm and the build-time crates object and ar_archive_writer. Cargo.lock only adds these four.
  • libgcc_s dependency: the musl .so built from this branch links libgcc_s.so.1, so Alpine runtime images need apk add libgcc. The v1.0.0 release didn't need this. Worth checking whether the release workflow's cargo-zigbuild build has the same dependency.

🤖 Generated with Claude Code

awankhede-godaddy and others added 2 commits November 23, 2026 19:11
assetNameForPlatform always returned the glibc asset (libasherah-<arch>.so),
so on Alpine install-native downloaded a library that fails to load with
"Error loading shared library ld-linux-x86-64.so.2". The troubleshooting doc
claimed musl was selected automatically, but nothing detected it.

Detect musl by the presence of /lib/ld-musl-*.so.1 and install
libasherah-<arch>-musl.so, which every release already publishes. Add
--libc=auto|gnu|musl for builds whose build and runtime images differ.

[ai assisted]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On musl every thread except the main one gets a 128 KiB stack. Go callers
using purego run FFI calls on arbitrary runtime threads, and building a
factory with AWS KMS + DynamoDB (and the AWS calls behind encrypt/decrypt)
needs more than that, so asherah-go segfaulted on Alpine (SIGSEGV,
sigcode=2, "signal arrived during cgo execution") in Setup and Decrypt.

Wrap the factory, session, encrypt and decrypt entry points in
stacker::maybe_grow: when less than 1 MiB of stack is left the call runs
on a separately allocated 8 MiB stack, otherwise in place, so glibc
threads and the musl main thread only pay a stack-pointer check.

asherah-go/cmd/musl-stack-repro reproduces it: on Alpine with the v1.0.0
musl library, `musl-stack-repro aws` crashes in Setup on a non-main
thread; with this change it returns the expected AWS error.

[ai assisted]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@awankhede-godaddy
awankhede-godaddy force-pushed the fix/musl-support branch 2 times, most recently from 0feeaf9 to 051d695 Compare November 23, 2026 11:21
@awankhede-godaddy
awankhede-godaddy marked this pull request as ready for review November 23, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant