Skip to content

Question about default GSS encryption mode in Npgsql 10.0 #6360

Closed as not planned

Description

@Varorbc

@vonzshik @roji

Hi Npgsql team,

After upgrading to .NET 10.0 and Npgsql 10.0, I encountered the following error:

Cannot load library libgssapi_krb5.so.2: cannot open shared object file: No such file or directory

I investigated and found that:

  • Since .NET 8.0, Microsoft’s official Debian/Alpine container images no longer include Kerberos libraries (docs).
  • Starting from Npgsql 10.0, the PostgreSQL client defaults to GssEncryptionMode.Prefer (PR #6324).

This combination causes applications running in official .NET container images to fail at startup unless Kerberos libraries are manually installed.

My questions:

  1. Is there any plan to change the default from GssEncryptionMode.Prefer to GssEncryptionMode.Disable, to avoid breaking scenarios where Kerberos is not intended to be used?
  2. If not, should this change be explicitly mentioned in the release notes, so users are aware that Kerberos libraries are now required unless they override the setting?

Thanks for your great work on Npgsql!

Activity

  1. vonzshik commented on Dec 1, 2025

    @vonzshik
    Contributor

    Is there a stacktrace for the exception? We do have try/catch around gss encryption and ideally should have retried without it in case it fails.

    try
    {
    await conn.SetupEncryption(sslMode, gssEncMode, timeout, async, cancellationToken).ConfigureAwait(false);
    timeout.CheckAndApply(conn);
    conn.WriteStartupMessage(username);
    await conn.Flush(async, cancellationToken).ConfigureAwait(false);
    using var cancellationRegistration = conn.StartCancellableOperation(cancellationToken, attemptPgCancellation: false);
    await conn.Authenticate(username, timeout, async, cancellationToken).ConfigureAwait(false);
    }
    // We handle any exception here because on Windows while receiving a response from Postgres
    // We might hit connection reset, in which case the actual error will be lost
    // And we only read some IO error
    // In addition, this behavior mimics libpq, where it retries as long as GssEncryptionMode and SslMode allows it
    catch (Exception e) when
    // We might also get here OperationCancelledException/TimeoutException
    // But it's fine to fall down and retry because we'll immediately exit with the exact same exception
    //
    // Any error after trying with GSS encryption
    (gssEncMode == GssEncryptionMode.Prefer ||
    // Auth error with/without SSL
    (sslMode == SslMode.Prefer && conn.IsSslEncrypted || sslMode == SslMode.Allow && !conn.IsSslEncrypted))
    {
    if (gssEncMode == GssEncryptionMode.Prefer)
    {
    conn.ConnectionLogger.LogTrace(e, "Error while opening physical connection with GSS encryption, retrying without it");
    gssEncMode = GssEncryptionMode.Disable;
    }

  2. Varorbc commented on Dec 1, 2025

    @Varorbc
    Author

    I’ve investigated the exception further and found that it originates in the .NET runtime’s native interop layer (System.Net.Security.Native/pal_gssapi.c). When libgssapi_krb5.so.2 is missing, the runtime’s native code attempts to call into GSSAPI and fails. This failure is not surfaced as a typical managed DllNotFoundException or EntryPointNotFoundException. Instead, the native layer returns an error code which is then propagated up, and the managed fallback logic in Npgsql never gets a chance to intercept it.

    This explains why the exception cannot be caught either by my application code or by Npgsql’s internal try/catch. The error is raised at the runtime boundary, outside of Npgsql’s control.

    So while the application continues to work fine once Kerberos/GSSAPI is unavailable (it falls back to password-based authentication), the uncaught exception message is confusing for users. It looks like a hard failure even though functionality is not actually impacted.

    https://github.com/dotnet/runtime/blob/3b637728da9d5dc1dc625640bb5d7e436cf0f12a/src/native/libs/System.Net.Security.Native/pal_gssapi.c#L120-L144

  3. roji commented on Dec 1, 2025

    @roji
    Member

    @Varorbc can you please post the full exception and stack trace as requested?

  4. Varorbc commented on Dec 1, 2025

    @Varorbc
    Author

    @roji I can’t provide a managed stack trace because the error originates in the .NET runtime’s native GSSAPI layer (pal_gssapi.c). It surfaces as an uncaught message rather than a catchable exception, so neither my code nor Npgsql’s try/catch can intercept it. The application still works, but the error message is confusing for users.

  5. roji commented on Dec 1, 2025

    @roji
    Member
    Cannot load library libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
    

    Is the above the only line you're getting on your application output? If not, please post the entire output so we have the full picture. Even if you don't have a managed stack trace, the full unmanaged error is still important.

  6. Varorbc commented on Dec 1, 2025

    @Varorbc
    Author

    Yes, that's all the information I've seen.

  7. roji commented on Dec 1, 2025

    @roji
    Member

    In the meantime, you can of course specify GSS Encryption Mode=Disable on the connection string to disable GSS encryption and make the error messag go away.

  8. Varorbc commented on Dec 1, 2025

    @Varorbc
    Author

    Yes, that's what I'm doing now, the error message disappeared.

  9. roji commented on Dec 1, 2025

    @roji
    Member

    From an offline conversation, @vonzshik suggests simply reversing the ordering of things in the GSS encryption setup, so that we send the GSS request to PostgreSQL - and read the response - before we actually start using NegotiateAuthentication (which triggers the underlying error, code). This would restrict the error only to cases where PostgreSQL has been configured for GSS-API, eliminating it for the majority case where users haven't set it up.

  10. added theissue type on Dec 1, 2025
  11. added this to the 10.0.1 milestone on Dec 1, 2025
  12. vonzshik commented on Dec 1, 2025

    @vonzshik
    Contributor

    Investigated a bit further, and it might be a not so clear-cut solution.
    Currently, they way GSS encryption setup works is that we first check whether we can initialize GSS API (that's essentially an attempt to retrieve credentials to send to postgres), and then if and only if we're successful, actually send a request to postgres to ask for GSS Encryption. At this point postgres can either agree (and then handshake happens) or say it doesn't support it. This behavior is in line with libpq and it's explicitly documented there as such.
    Now, here is where things are a bit murky. Theoretically we can swap both things around. After all, whether we exit due to postgres not supporting GSS or there being no credentials doesn't matter that much, unless we consider a case where the server does support GSS but the client doesn't, at which point we'll have to drop the connection and recreate it from scratch, but overall that's not the end of the world.
    The problem is that there is a difference in behavior between GSS on windows and GSS on linux. On windows, we can always retrieve credentials (which I assume is due to internal workings of windows and to support SSPI). On linux, that's actually not true. As long as you do not have a kerberos token in local cache (kinit), we can't actually retrieve anything. This means that by changing the order to request->credentials we'll force everyone to send a gss encryption request, no matter whether they actually have kerberos enabled or not. While the roundtrip on physical open by itself is also not the end of the world, I can't say for sure whether cloud providers (amazon, azure, etc) do not have kerberos enabled on their postgres servers (and I suspect they actually do for easier setup). This means that in the worst case scenario by default people wouldn't only pay for a roundtrip during physical open, but because there is no backing out of GSS once it's started we would have to completely scrap the connection and start anew, with GSS encryption disabled.
    To me it doesn't seems to be a good tradeoff, where the issue isn't even breaking the app, but instead adds a confusing error to stderr/logs. I propose we treat it as an environmental issue and as such improve our documentation (including release notes), so people are aware of it and can act accordingly to remedy it.

  13. roji commented on Dec 1, 2025

    @roji
    Member

    Discussed this further internally, and I agree with the above. Our choice here is basically between saving the roundtrip and re-connection on Linux only - when the user isn't actually logged in - and getting rid of the warning where Kerberos isn't installed. Since the former case (connecting from Linux without Kerberos login) seems quite common, it seems preferable to prioritize that over eliminating the warning for when Kerberos isn't installed (the current state of affairs).

    Let's see how many people actually have trouble with this, and see what we do as a function of that.

  14. 2 remaining items

  15. roji commented on Dec 2, 2025

    @roji
    Member

    @Varorbc done in npgsql/doc#437, thanks for flagging this.

  16. mattzink commented on Dec 15, 2025

    @mattzink

    Npgsql internally handles this and falls back to connecting without GSSAPI, so the error is harmless.

    Just wanted to note that the above statement isn't exactly true. Since the error message is logged to stderr, it was causing my xunit integration job to fail since it expects a clean stderr.

  17. removed this from the 10.0.1 milestone on Dec 18, 2025
  18. removed theissue type on Dec 18, 2025
  19. gabynevada commented on Jan 29, 2026

    @gabynevada

    Another issue is that we we're using the runtime-deps:10.0-noble-chiseled-extra images and from v10 they all started failing with this error.

    Had some pain debugging and finding this issue as the images worked fine in previous versions.

  20. jdaigle commented on Mar 9, 2026

    @jdaigle

    Is there any overhead from trying to use GSSAPI and failing? Would we see any performance improvements by setting GSS Encryption Mode=Disable (assuming we still use SSL/TLS)?

  21. mhkolk commented on Mar 16, 2026

    @mhkolk

    This issue should be reopened as we continue to see this error in a context of Dockerized .NET app.

    The error explicitely says

    Cannot load library libgssapi_krb5.so.2 
    Error: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
    

    but this file is only available when Kerberos package is installed in Alpine Linux.

    The new .net10 images are Ubuntu based and we are still seeing this issue. I tried adding the Ubuntu version of the Kerberos by adding the following line to our Dockerfile

    RUN apt update && apt -y upgrade libkrb5-3

    as documented here

    but the same issue/error still appears.

    I should add we do not want to go changing connection strings all over to include GSS Encryption Mode=Disable though it is the only solution that works at the moment.

  22. rowanmoul commented on Mar 23, 2026

    @rowanmoul

    It's great that GSSAPI was added as an option in Npgsql 10, but why was the default changed from Disable to Prefer. That seems to be the issue here. Why is this on by default instead of opt-in?

  23. roji commented on Mar 23, 2026

    @roji
    Member

    @rowanmoul unless I'm mistaken, that's also how libpq behaves - GSSAPI is preferred - and we generally align to libpq. Also, specifically with security-related features, it's generally a good idea to have things on by default, and to offer an explicit opt-out (rather than the other way around).

  24. mus65 commented on Mar 28, 2026

    @mus65
    Contributor

    Maybe the runtime is open to just not logging this error, opened dotnet/runtime#126251.

  25. roji commented on Mar 30, 2026

    @roji
    Member

    @mus65 thanks for opening the issue - it seems like the fix to runtime is about to get merged, and this warning will no longer get logged.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions