Repository navigation
spnego_sspi: pass channel bindings on initial context - #22537
felix314159 wants to merge 3 commits into
Conversation
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
This comment was marked as outdated.
This comment was marked as outdated.
jay
left a comment
There was a problem hiding this comment.
LGTM but let's wait and see what the reporter has to say before landing.
|
For testing this is the link to the Windows x64 binary generated by the current latest CI run of this PR: (Note, the binary has the NTLM patch from master, but it's built with LibreSSL, not Schannel, in case this matters for reproduction.) |
|
Small thing: I think this widens an existing tiny leak. |
|
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 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 |
|
Yeah, both. The SSP overwrites |
There was a problem hiding this comment.
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
SecBufferDescindependently of whether a server challenge token is present. - Adds the Schannel endpoint bindings
SecBufferwhenever an SSL (Schannel) context is available, including the initialInitializeSecurityContext()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.
That seems pedantic, we call FreeContextBuffer like we call free, the pointer may be null. |
|
Overly cautious, maybe. |
|
@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)
|
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. |
|
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 |
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
|
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. |
|
Thanks |
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.htmlThe 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
InitializeSecurityContextwhen 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
InitializeSecurityContextcalls.Verification
Tested in a three-VM AD/Kerberos lab using Windows 11 25H2, Windows Server 2022 AD DS, and IIS 10.
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