Skip to content

spnego_sspi: pass channel bindings on initial context - #22537

Closed
felix314159 wants to merge 3 commits into
curl:masterfrom
felix314159:issue-22466
Closed

felix314159 wants to merge 3 commits into
curl:masterfrom
felix314159:issue-22466

Conversation

@felix314159

@felix314159 felix314159 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Reproduction of reproducible bug described in #22466

Using a domain-joined Windows 11 25H2 client and Windows Server 2022 IIS with Windows Authentication and Extended Protection for Authentication enabled:

curl.exe -v --negotiate -u : https://iis.example.test/default.html

The client obtains the correct Kerberos service ticket, but IIS returns HTTP 401. With EPA Required, the IIS Security log records Kerberos status 0xC000035B (STATUS_BAD_BINDINGS). Disabling EPA allows the request to succeed.

Root cause

curl sends the initial Kerberos token preemptively, before receiving a server challenge.

The Windows SSPI implementation only added Schannel endpoint bindings to the input passed to InitializeSecurityContext when a challenge token was present. Consequently, the initial Kerberos token lacked the channel binding required by IIS EPA.

Fix

Construct the SSPI input descriptor independently of the challenge token and add Schannel endpoint bindings whenever they are available. This supplies channel bindings on both the initial and subsequent InitializeSecurityContext calls.

Verification

Tested in a three-VM AD/Kerberos lab using Windows 11 25H2, Windows Server 2022 AD DS, and IIS 10.

IIS EPA mode curl 8.21.0 patched curl
Disabled HTTP 200 HTTP 200
Allow HTTP 401 HTTP 200
Require HTTP 401 HTTP 200

The patched Windows UCRT64 Schannel/SSPI build also passed all 78 applicable libcurl and tool unit tests. I am also looking for a new full-time job, I could take care of e.g. any Windows-related issues and feature implementations :)

Fixes #22466

HTTP Negotiate can create and send a Kerberos token before receiving a server challenge. The SSPI backend only supplied Schannel endpoint bindings when a challenge token was present, causing IIS with EPA enabled to reject preemptive Kerberos authentication with STATUS_BAD_BINDINGS.

Build the SSPI input descriptor independently of the challenge token so Schannel endpoint bindings are included on both initial and subsequent InitializeSecurityContext calls.

Fixes curl#22466
@testclutch

This comment was marked as outdated.

Comment thread lib/vauth/spnego_sspi.c Resolved

@jay jay 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.

LGTM but let's wait and see what the reporter has to say before landing.

@jay jay added the Windows Windows-specific label Aug 11, 2026
@vszakats

vszakats commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

For testing this is the link to the Windows x64 binary generated by the current latest CI run of this PR:
https://github.com/curl/curl/actions/runs/31434203926/artifacts/9080398228
via:
https://github.com/curl/curl/actions/runs/31434203926?pr=22537

(Note, the binary has the NTLM patch from master, but it's built with LibreSSL, not Schannel, in case this matters for reproduction.)

@martin-fzi

Copy link
Copy Markdown

Small thing: I think this widens an existing tiny leak. QueryContextAttributes(SECPKG_ATTR_ENDPOINT_BINDINGS) allocates pkgBindings.Bindings, but it isn't freed with FreeContextBuffer() after InitializeSecurityContext() consumes it. The old challenge path already leaked it; this now adds the preemptive path too.

@jay

jay commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

QueryContextAttributes (Schannel) says:

"The caller must allocate the pBuffer structure itself, but the SSP allocates any memory required to hold variable sized members of the pBuffer structure. When you have finished using memory allocated by the SSP, free it by calling the FreeContextBuffer function."

hm. So I guess we have to free the pointers in pBuffer (in this case &pkgBindings which is a pointer to SecPkgContext_Bindings on the stack) instead of pBuffer itself?

  if(nego->sslContext) {
    pkgBindings.Bindings = &channelBindings;
    nego->status = Curl_pSecFn->QueryContextAttributes(
        nego->sslContext,
        SECPKG_ATTR_ENDPOINT_BINDINGS,
        &pkgBindings);
    if(nego->status == SEC_E_OK) {
      SecBuffer *binding_buf = &chlg_buf[chlg_desc.cBuffers++];
      binding_buf->BufferType = SECBUFFER_CHANNEL_BINDINGS;
      binding_buf->cbBuffer   = pkgBindings.BindingsLength;
      binding_buf->pvBuffer   = pkgBindings.Bindings;
    }

If so then it was wrong for us to set the pkgBindings.Bindings pointer since QueryContextAttributes doc seems to say that it's going to allocate the memory? There's two instances of this in the code.

diff --git a/lib/vauth/ntlm_sspi.c b/lib/vauth/ntlm_sspi.c
index 2e98e86..e54d044 100644
--- a/lib/vauth/ntlm_sspi.c
+++ b/lib/vauth/ntlm_sspi.c
@@ -242,6 +242,9 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
   SecBufferDesc type_3_desc;
   SECURITY_STATUS status;
   unsigned long attrs;
+#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
+  SecPkgContext_Bindings pkgBindings = { 0, NULL };
+#endif
 
   (void)creds;
 
@@ -262,9 +265,6 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
    * https://learn.microsoft.com/security-updates/SecurityAdvisories/2009/973811
    */
   if(ntlm->sslContext) {
-    SEC_CHANNEL_BINDINGS channelBindings;
-    SecPkgContext_Bindings pkgBindings;
-    pkgBindings.Bindings = &channelBindings;
     status = Curl_pSecFn->QueryContextAttributes(
       ntlm->sslContext,
       SECPKG_ATTR_ENDPOINT_BINDINGS,
@@ -296,6 +296,11 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
                                                   0, ntlm->context,
                                                   &type_3_desc,
                                                   &attrs, NULL);
+
+#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
+  Curl_pSecFn->FreeContextBuffer(pkgBindings.Bindings);
+#endif
+
   if(status != SEC_E_OK) {
     infof(data, "NTLM handshake failure (type-3 message): Status=0x%08lx",
           (unsigned long)status);
diff --git a/lib/vauth/spnego_sspi.c b/lib/vauth/spnego_sspi.c
index 1e7c009..89e5d1c 100644
--- a/lib/vauth/spnego_sspi.c
+++ b/lib/vauth/spnego_sspi.c
@@ -94,8 +94,7 @@ CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
   SecBufferDesc resp_desc;
   unsigned long attrs;
 #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
-  SEC_CHANNEL_BINDINGS channelBindings;
-  SecPkgContext_Bindings pkgBindings;
+  SecPkgContext_Bindings pkgBindings = { 0, NULL };
 #endif
 
   if(nego->context && nego->status == SEC_E_OK) {
@@ -234,7 +233,6 @@ CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
    * https://learn.microsoft.com/security-updates/SecurityAdvisories/2009/973811
    */
   if(nego->sslContext) {
-    pkgBindings.Bindings = &channelBindings;
     nego->status = Curl_pSecFn->QueryContextAttributes(
         nego->sslContext,
         SECPKG_ATTR_ENDPOINT_BINDINGS,
@@ -273,6 +271,10 @@ CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
                                              &resp_desc, &attrs, NULL);
   }
 
+#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
+  Curl_pSecFn->FreeContextBuffer(pkgBindings.Bindings);
+#endif
+
   /* Free the decoded challenge as it is not required anymore */
   curlx_free(chlg);
 

edit: patch updated to init pkgBindings.Bindings NULL and guard around FreeContextBuffer calls

@martin-fzi

Copy link
Copy Markdown

Yeah, both. The SSP overwrites Bindings with its own allocation, so the stack SEC_CHANNEL_BINDINGS was never used.
The FreeContextBuffer calls should have if(pkgBindings.Bindings) too: the #ifdef only guards compile time, and Bindings stays NULL when sslContext is NULL or the query fails

Copilot AI left a comment

Copy link
Copy Markdown

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 fixes Windows SSPI/SPNEGO (Negotiate) authentication against IIS configurations that require Extended Protection for Authentication (EPA) by ensuring Schannel endpoint/channel bindings are passed to InitializeSecurityContext() even on the initial, preemptive Kerberos token generation.

Changes:

  • Initializes the SSPI input SecBufferDesc independently of whether a server challenge token is present.
  • Adds the Schannel endpoint bindings SecBuffer whenever an SSL (Schannel) context is available, including the initial InitializeSecurityContext() call.
  • Adjusts the InitializeSecurityContext() call to pass a non-NULL input descriptor when at least one input buffer (challenge token and/or channel bindings) is present.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@jay

jay commented Aug 14, 2026

Copy link
Copy Markdown
Member

The FreeContextBuffer calls should have if(pkgBindings.Bindings) too: the #ifdef only guards compile time, and Bindings stays NULL when sslContext is NULL or the query fails

That seems pedantic, we call FreeContextBuffer like we call free, the pointer may be null.

@martin-fzi

Copy link
Copy Markdown

Overly cautious, maybe. NULL isn't documented as valid for FreeContextBuffer, so that relies on implementation behavior.

@tajbender

Copy link
Copy Markdown

@jay is there any need for further investigation for this patch?

if so, let me know. I' lee try to assist. I'm running Win11.

refer to martin-fzi's comments

Ref: curl#22537 (comment)
@jay

jay commented Aug 20, 2026

Copy link
Copy Markdown
Member

Felix I pushed Martin's suggestion to fix the memory leak so we can get a new build from the CI, if you have a better solution feel free to overwrite it.

@felix314159

Copy link
Copy Markdown
Contributor Author

thanks, your commit looks good to me and the CI failures are unrelated

@vszakats

Copy link
Copy Markdown
Member

thanks, your commit looks good to me and the CI failures are unrelated

Yep, those are this issue hitting new jobs as the change reaches them: actions/runner-images#14562

vszakats added a commit that referenced this pull request Aug 21, 2026
As the MSYS2 update propagates into more GitHub runner images, we need
to install Perl to avoid native Windows (aka Strawberry) Perl breaking
jobs that are running tests.

For others jobs, this patch leaves the new default Strawberry Perl. It's
fine to use it a little bit more.

Also:
- libssh 32-bit was dropped upstream, replace with libssh2, and move
  libssh to the 64-bit job running tests.
  msys2/MINGW-packages@f6ae365
  msys2/MINGW-packages#30777

Refs:
actions/runner-images#14562
actions/runner-images#14541 (comment)
msys2/MSYS2-packages@6364893
msys2/MSYS2-packages#6491
msys2/MSYS2-packages#6479

Bug: #22537 (comment)
Follow-up to d854ab4 #22580

Closes #22632
save an artifact for this job:
CM VS2026, Debug, x64, OpenSSL 3.5 + Schannel, Static

based off of WIP in curl#22653
@github-actions github-actions Bot added the CI Continuous Integration label Aug 24, 2026
@jay

jay commented Aug 24, 2026

Copy link
Copy Markdown
Member

I've added a temporary commit, based on #22653, to save an artifact of a curl schannel build so that the reporter can test an schannel build of this branch without having to compile it himself.

@jay jay closed this in 3ca8660 Aug 25, 2026
@jay

jay commented Aug 25, 2026

Copy link
Copy Markdown
Member

Thanks

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

Labels

authentication CI Continuous Integration Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

curl fails to negotiate Kerberos with Extended Protection for Authentication (EPA) enabled on the target URL

7 participants