Homebrew / homebrew-core

🍻 Default formulae for the missing package manager for macOS (or Linux)
https://brew.sh
BSD 2-Clause "Simplified" License
13.81k stars 12.48k forks source link

psql segfaults from brew linked krb5 #173362

Closed acole42 closed 5 months ago

acole42 commented 6 months ago

brew gist-logs <formula> link OR brew config AND brew doctor output

❯ brew config && brew doctor
HOMEBREW_VERSION: 4.3.2-56-gfe16e9d
ORIGIN: https://github.com/Homebrew/brew
HEAD: fe16e9d7ef393fdcf353a3c3eba73d0299b744cc
Last commit: 56 minutes ago
Core tap HEAD: dcaff8104d9caafa565d01817a99f122dfb5534c
Core tap last commit: 7 minutes ago
Core tap JSON: 31 May 19:43 UTC
Core cask tap HEAD: c0ea0f5f3b8f5d5d27fd2365c68f98dd3ec04762
Core cask tap last commit: 33 minutes ago
Core cask tap JSON: 31 May 19:43 UTC
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 10
HOMEBREW_SORBET_RUNTIME: set
Homebrew Ruby: 3.3.2 => /opt/homebrew/Library/Homebrew/vendor/portable-ruby/3.3.2/bin/ruby
CPU: 10-core 64-bit arm_firestorm_icestorm
Clang: 15.0.0 build 1500
Git: 2.45.1 => /opt/homebrew/bin/git
Curl: 8.4.0 => /usr/bin/curl
macOS: 14.2.1-arm64
CLT: 15.1.0.0.1.1700200546
Xcode: 15.2
Rosetta 2: false
Your system is ready to brew.

Verification

What were you trying to do (and why)?

Run psql to connect to a postgres DB using TCP.

What happened (include all command output)?

Segfaults when trying to connect to a postgres DB with psql using TCP using the linked krb5. Seems similar to #135170 but I'm unable to post to that issue since it was marked stale.

❯ lldb /opt/homebrew/bin/psql
(lldb) target create "/opt/homebrew/bin/psql"
Current executable set to '/opt/homebrew/bin/psql' (arm64).
(lldb) r -h 127.0.0.1 -d postgres
Process 19762 launched: '/opt/homebrew/bin/psql' (arm64)
Process 19762 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x6b59f969d05a0098)
    frame #0: 0x000000010085ad44 libkrb5.3.3.dylib`krb5_free_principal + 20 
libkrb5.3.3.dylib`krb5_free_principal:
->  0x10085ad44 <+20>: ldr    x0, [x1, #0x18]
    0x10085ad48 <+24>: cbz    x0, 0x10085ad84           ; <+84>
    0x10085ad4c <+28>: ldr    w8, [x19, #0x20]
    0x10085ad50 <+32>: cmp    w8, #0x1
Target 0: (psql) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x6b59f969d05a0098)
  * frame #0: 0x000000010085ad44 libkrb5.3.3.dylib`krb5_free_principal + 20
    frame #1: 0x000000010083dbf8 libkrb5.3.3.dylib`krb5_cccol_have_content + 188
    frame #2: 0x00000001005e5a2c libgssapi_krb5.2.2.dylib`acquire_cred_context + 1740
    frame #3: 0x00000001005e52e8 libgssapi_krb5.2.2.dylib`acquire_cred_from + 688
    frame #4: 0x00000001005d7340 libgssapi_krb5.2.2.dylib`gss_add_cred_from + 628
    frame #5: 0x00000001005d6f84 libgssapi_krb5.2.2.dylib`gss_acquire_cred_from + 400
    frame #6: 0x00000001005d6de8 libgssapi_krb5.2.2.dylib`gss_acquire_cred + 36
    frame #7: 0x00000001005480e8 libpq.5.dylib`pg_GSS_have_cred_cache + 60
    frame #8: 0x0000000100536d80 libpq.5.dylib`PQconnectPoll + 4468
    frame #9: 0x00000001005345e4 libpq.5.dylib`connectDBComplete + 276
    frame #10: 0x00000001005341e0 libpq.5.dylib`PQconnectdbParams + 44
    frame #11: 0x00000001000226f8 psql`main + 2764
    frame #12: 0x000000018111d0e0 dyld`start + 2360
❯ DYLD_PRINT_LIBRARIES=1  psql -h 127.0.0.1  -d postgres 2>&1 | egrep "krb5|libpq"
dyld[41206]: <15D9A190-BFBD-395C-8EA0-8ACFABB691DF> /opt/homebrew/Cellar/postgresql@14/14.12/lib/postgresql@14/libpq.5.14.dylib
dyld[41206]: <2A2C9A6C-42A6-3F85-926A-A2B69C339240> /opt/homebrew/Cellar/krb5/1.21.2/lib/libgssapi_krb5.2.2.dylib
dyld[41206]: <A56A7A6C-A898-3A74-B08C-762AC0CF1D4C> /opt/homebrew/Cellar/krb5/1.21.2/lib/libkrb5.3.3.dylib
dyld[41206]: <465EB15D-68A6-3B8C-9EBC-08022A12C96C> /opt/homebrew/Cellar/krb5/1.21.2/lib/libk5crypto.3.1.dylib
dyld[41206]: <332CB287-287D-3AE1-AF49-83850734B47A> /opt/homebrew/Cellar/krb5/1.21.2/lib/libcom_err.3.0.dylib
dyld[41206]: <FC3FF0A4-DAA1-324B-B588-32686F6D2D7F> /opt/homebrew/Cellar/krb5/1.21.2/lib/libkrb5support.1.1.dylib
[1]    41206 segmentation fault  DYLD_PRINT_LIBRARIES=1 psql -h 127.0.0.1 -d postgres 2>&1
❯ psql -d postgres -h 127.0.0.1
[1]    32129 segmentation fault  psql  -h 127.0.0.1 -d postgres

What did you expect to happen?

Connect to the database without segfaulting.

Step-by-step reproduction instructions (by running brew commands)

1. `brew install postgresql@14`
2. `brew services start postgresql@14`
3. `psql -h 127.0.0.1 -d postgres`
acole42 commented 6 months ago

What's also odd is if I used either the arm64_ventura bottle of krb5 or brew install --build-from-source krb5 I run into the segfault issue. Yet if I download krb5-1.21.2 and compile the libraries separately using the same flags as brew and inject the compiled libraries into DYLD_LIBRARY_PATH I don't run into the segfault issue.


~/Projects/krb5-1.21.2/src ❯ ./configure
--disable-debug \
--disable-dependency-tracking \
--prefix=/opt/homebrew/Cellar/krb5/1.21.2 \
--libdir=/opt/homebrew/Cellar/krb5/1.21.2/lib \
--disable-nls \
--disable-silent-rules \
--without-system-verto \
--without-keyutils

❯ make
❯ DYLD_LIBRARY_PATH=$HOME/Projects/krb5-1.21.2/src/lib DYLD_PRINT_LIBRARIES=1  psql -h 127.0.0.1  -d postgres 2>&1 | egrep "krb5|libpq"
dyld[64127]: <15D9A190-BFBD-395C-8EA0-8ACFABB691DF> /opt/homebrew/Cellar/postgresql@14/14.12/lib/postgresql@14/libpq.5.14.dylib
dyld[64127]: <E2647C09-805E-348D-B795-4A109272481E> ~/Projects/krb5-1.21.2/src/lib/gssapi/libgssapi_krb5.2.2.dylib
dyld[64127]: <4A29A8C7-9A5B-3FB9-98FB-3DFDBF6EC43E> ~/Projects/krb5-1.21.2/src/lib/krb5/libkrb5.3.3.dylib
dyld[64127]: <3D7EA99D-B924-3082-934F-32667A2271AE> ~/Projects/krb5-1.21.2/src/lib/crypto/libk5crypto.3.1.dylib
dyld[64127]: <A6C7A90F-BF45-32E5-9CF7-9D2940CE025E> ~/Projects/krb5-1.21.2/src/util/et/libcom_err.3.0.dylib
dyld[64127]: <0926D154-AE40-32DD-8043-44096D720683> ~/Projects/krb5-1.21.2/src/util/support/libkrb5support.1.1.dylib

psql (14.12 (Homebrew))
Type "help" for help.

postgres=#
carlocab commented 6 months ago

Could you share the link from doing

brew install --build-from-source krb5
brew gist-logs krb5

?

cho-m commented 6 months ago

Maybe https://krbdev.mit.edu/rt/Ticket/Display.html?id=9103 based on stack trace?

If so, could try https://github.com/krb5/krb5/commit/635c8cca65b745476d07c1f5ff701445db25c10d

acole42 commented 6 months ago

@carlocab Here's the output from the requested commands.

❯ brew install --build-from-source krb5
==> Fetching krb5
==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/f9e9b8fb55aef222c500f8a94a948b3c437afe67/Formula/k/krb5.rb
##################################################################################################################################################################### 100.0%
==> Downloading https://kerberos.org/dist/krb5/1.21/krb5-1.21.2.tar.gz
Already downloaded: ~/Library/Caches/Homebrew/downloads/36ed7af4db98b395a891105a34041859cec149be294a1dc35492d16153b905c9--krb5-1.21.2.tar.gz
==> ./configure --disable-nls --disable-silent-rules --without-system-verto --without-keyutils
==> make
==> make install
==> Caveats
krb5 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have krb5 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/krb5/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/krb5/sbin:$PATH"' >> ~/.zshrc

For compilers to find krb5 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/krb5/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/krb5/include"

For pkg-config to find krb5 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/krb5/lib/pkgconfig"
==> Summary
🍺  /opt/homebrew/Cellar/krb5/1.21.2: 163 files, 5.1MB, built in 49 seconds
==> Running `brew cleanup krb5`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

❯ psql -d postgres -h 127.0.0.1
[1]    49904 segmentation fault  psql -d postgres -h 127.0.0.1
 brew gist-logs krb5

https://gist.github.com/acole42/298db6f4f2f4de36047eb4e4790e51ad

@cho-m I compiled the latest commit from the master branch on krb5/krb5 which included https://github.com/krb5/krb5/commit/635c8cca65b745476d07c1f5ff701445db25c10d. When I injected the compiled libraries in DYLD_LIBRARY_PATH I didn't encounter the segfault. It seems like that commit fixes the bug.

I'm still puzzled why compiling the same version (krb5-1.21.2 ) as the krb5 formula, which doesn't have the referenced fix https://github.com/krb5/krb5/commit/635c8cca65b745476d07c1f5ff701445db25c10d, works fine if I compile it separately without brew but segfaults when I have brew compile the library.

github-actions[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.