Skip to content

Fall back to NTLM in managed SPNEGO when Kerberos credentials are missing on Unix - #131195

Merged
rzikm merged 1 commit into
dotnet:mainfrom
rzikm:ntlm-ubuntu-noble
Jul 24, 2026
Merged

rzikm merged 1 commit into
dotnet:mainfrom
rzikm:ntlm-ubuntu-noble

Conversation

@rzikm

@rzikm rzikm commented Jul 22, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #122895.

When the managed Negotiate (SPNEGO) implementation offers Kerberos as the optimistic mechanism and no usable KDC/TGT is available, the GSS-API reports GSS_S_NO_CRED, which maps to NegotiateAuthenticationStatusCode.UnknownCredentials. Previously this status was only treated as a recoverable optimistic failure on OpenBSD (Heimdal). On Linux (MIT krb5) the same condition aborted the whole exchange instead of falling back to NTLM, so managed NTLM over HTTP (e.g. EWS/Exchange with AppContext.SetSwitch("System.Net.Security.UseManagedNtlm", true)) failed with 401 whenever a Kerberos mechanism was advertised but no KDC was reachable — the exact scenario in #122895 on the Ubuntu Noble .NET 10 image with gss-ntlmssp installed.

This change treats UnknownCredentials as a recoverable optimistic failure on all Unix platforms, so SPNEGO falls back from Kerberos to NTLM. NTLM remains the last offered mechanism and is guarded by canFallBackToNextMechanism, so a genuine UnknownCredentials originating from NTLM itself (e.g. missing default credentials, see #91160) still propagates unchanged.

Why the previously merged change (#123003) was not enough

#123003 made HasSystemNetSecurityNative (and thus supportKerberos) true whenever the native GSS-API library loads. That does not address the reporter's configuration (with gss-ntlmssp installed, supportKerberos was already true), and on its own it regresses the gss-ntlmssp-absent configuration, because Kerberos then starts being offered optimistically and hits the same missing-fallback path. The real fix is the fallback change here; combined with #123003 both configurations work.

Empirical validation

Because a full test host could not be built in the dev environment (an unrelated clang internal compiler crash), the fix was validated by building real System.Net.Security.dll variants and running an NTLM/Negotiate HTTP handshake against a reference server on a private copy of the 10.0.9 shared framework:

Variant UseManagedNtlm=true, gss-ntlmssp absent UseManagedNtlm=true, gss-ntlmssp present (reporter)
baseline (no fix) 200 401 (bug)
#123003 only 401 (regression) 401
fallback fix (this PR) 200 200
#123003 + fallback fix 200 200

Notes

  • The managed unit test DefaultNetworkCredentials_NTLM_DoesNotThrow (which asserts UnknownCredentials for the direct NTLM package) is unaffected: this change only alters non-NTLM optimistic mechanisms in the SPNEGO path.
  • Consider backporting to release/10.0 together with Fix detection of System.Net.Security.Native #123003.

Note

This pull request was authored by GitHub Copilot (CLI).

…sing on Unix

When the managed Negotiate (SPNEGO) implementation offers Kerberos as the
optimistic mechanism and no usable KDC/TGT is available, the GSS-API reports
GSS_S_NO_CRED, which maps to NegotiateAuthenticationStatusCode.UnknownCredentials.
Previously this status was only treated as a recoverable optimistic failure on
OpenBSD (Heimdal). On Linux (MIT krb5) the same condition aborted the whole
exchange instead of falling back to NTLM, so managed NTLM over HTTP failed with
401 whenever a Kerberos mechanism was advertised but no KDC was reachable.

Treat UnknownCredentials as a recoverable optimistic failure on all Unix
platforms so SPNEGO falls back from Kerberos to NTLM. NTLM remains the last
offered mechanism and is guarded by canFallBackToNextMechanism, so a genuine
UnknownCredentials from NTLM itself (e.g. missing default credentials) still
propagates unchanged.

Fixes dotnet#122895

Co-authored-by: Copilot <[email protected]>
Copilot-Session: aef7f3d9-743f-4149-acd6-ad2bc62c12d2
Copilot AI review requested due to automatic review settings July 22, 2026 08:46
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@rzikm
rzikm requested review from a team and filipnavara July 22, 2026 08:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the managed SPNEGO (Negotiate) optimistic mechanism selection on Unix so that when an initial Kerberos attempt fails with NegotiateAuthenticationStatusCode.UnknownCredentials, the implementation treats it as a recoverable optimistic failure and continues to the next mechanism (NTLM), rather than aborting the whole exchange.

Changes:

  • Treat UnknownCredentials as a recoverable optimistic failure for non-NTLM mechanisms on Unix, enabling Kerberos → NTLM fallback in managed SPNEGO.
  • Update the in-code comment describing the rationale for the fallback behavior.

@filipnavara filipnavara left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should unconditionally fallback on NegotiateAuthenticationStatusCode.UnknownCredentials. This should only be done if _clientOptions.Credential were the default credentials. If the client sent their own password credentials and Kerberos rejected them then presumably we should still fail instead of trying to fallback to NTLM.

UPD: On a second thought, we would not succeed with the default credentials in managed NTLM either, so fallback in that case doesn't make sense to me either.

Okay, I think I understand the motivation now. If no Kerberos server can be reached with password based credentials and the native implementation returns GSS_S_NO_CRED then possibly we can make the fallback. I'm just not sure if there's any devil in the details where the error could be returned after some Kerberos validation, which could possibly be a downgrade attack.

@rzikm

rzikm commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

I'm just not sure if there's any devil in the details where the error could be returned after some Kerberos validation, which could possibly be a downgrade attack.

A downgrade is possible if attacker can prevent clients from reaching the KDC, that is a really strong precondition, so I don't think it is interesting. Besides if downgrade to NTLM is undesired, then server should be configured to refuse it anyway.

@wfurt

wfurt commented Jul 24, 2026

Copy link
Copy Markdown
Member

while NTLM is discouraged in general, I' wonder if we have something exploit in our documentation ... and if not, should we add some note?

@rzikm
rzikm merged commit 8642df8 into dotnet:main Jul 24, 2026
86 checks passed
hez2010 pushed a commit to hez2010/runtime that referenced this pull request Jul 26, 2026
…sing on Unix (dotnet#131195)

## Summary

Fixes dotnet#122895.

When the **managed** Negotiate (SPNEGO) implementation offers Kerberos
as the optimistic mechanism and no usable KDC/TGT is available, the
GSS-API reports `GSS_S_NO_CRED`, which maps to
`NegotiateAuthenticationStatusCode.UnknownCredentials`. Previously this
status was only treated as a *recoverable* optimistic failure on
**OpenBSD** (Heimdal). On **Linux (MIT krb5)** the same condition
aborted the whole exchange instead of falling back to NTLM, so managed
NTLM over HTTP (e.g. EWS/Exchange with
`AppContext.SetSwitch("System.Net.Security.UseManagedNtlm", true)`)
failed with **401** whenever a Kerberos mechanism was advertised but no
KDC was reachable — the exact scenario in dotnet#122895 on the Ubuntu Noble
.NET 10 image with `gss-ntlmssp` installed.

This change treats `UnknownCredentials` as a recoverable optimistic
failure on **all Unix** platforms, so SPNEGO falls back from Kerberos to
NTLM. NTLM remains the last offered mechanism and is guarded by
`canFallBackToNextMechanism`, so a genuine `UnknownCredentials`
originating from NTLM itself (e.g. missing default credentials, see
dotnet#91160) still propagates unchanged.

## Why the previously merged change (dotnet#123003) was not enough

dotnet#123003 made `HasSystemNetSecurityNative` (and thus `supportKerberos`)
`true` whenever the native GSS-API library loads. That does **not**
address the reporter's configuration (with `gss-ntlmssp` installed,
`supportKerberos` was already `true`), and on its own it *regresses* the
`gss-ntlmssp`-absent configuration, because Kerberos then starts being
offered optimistically and hits the same missing-fallback path. The real
fix is the fallback change here; combined with dotnet#123003 both
configurations work.

## Empirical validation

Because a full test host could not be built in the dev environment (an
unrelated clang internal compiler crash), the fix was validated by
building real `System.Net.Security.dll` variants and running an
NTLM/Negotiate HTTP handshake against a reference server on a private
copy of the 10.0.9 shared framework:

| Variant | `UseManagedNtlm=true`, gss-ntlmssp **absent** |
`UseManagedNtlm=true`, gss-ntlmssp **present** (reporter) |
|---|---|---|
| baseline (no fix) | 200 | **401 (bug)** |
| dotnet#123003 only | **401 (regression)** | 401 |
| **fallback fix (this PR)** | 200 | **200** |
| dotnet#123003 + fallback fix | 200 | 200 |

## Notes

- The managed unit test `DefaultNetworkCredentials_NTLM_DoesNotThrow`
(which asserts `UnknownCredentials` for the direct `NTLM` package) is
unaffected: this change only alters non-NTLM optimistic mechanisms in
the SPNEGO path.
- Consider backporting to `release/10.0` together with dotnet#123003.

> [!NOTE]
> This pull request was authored by GitHub Copilot (CLI).

Co-authored-by: Copilot <[email protected]>
Copilot-Session: aef7f3d9-743f-4149-acd6-ad2bc62c12d2
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 26, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NTLM issues in .NET 10 Ubuntu noble Docker image

4 participants