From 68a9c15d1ba82e550b4621b2d090c83fa806b43c Mon Sep 17 00:00:00 2001 From: SeoJunLee-code <285387336+SeoJunLee-code@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:24:55 +0900 Subject: [PATCH] fix(iis): harden ReadFileChunk, write callbacks and Host parsing - ReadFileChunk: treat a read that returns no more than the page alignment offset as EOF. The old code subtracted without a check, so bytesRead could wrap and memcpy read past the one-page buffer, and a net read of 0 bytes made the loop spin forever. Also clamp bytesRead to what is left in the page. - WriteBodyCallback / WriteResponseCallback: check the IIS and APR allocations. The response callback now allocates before it clears the original body, so on failure the response is left as it was and the caller gets APR_ENOMEM. The request body is already consumed, so the request callback fails the request with E_OUTOFMEMORY. - OnBeginRequest: for an IPv6 literal Host such as [2001:db8::1]:8080, look for the port only after the closing bracket and drop the brackets, as Apache httpd does. - ConvertUTF16ToUTF8: return a pool string instead of the literal "", so r->hostname never points into read-only memory. Drop the dead NULL check and document the contract. Refs #3787 (items 1, 2, 4 and 5; item 3 is not covered). Co-Authored-By: Claude Opus 5.5 --- iis/mymodule.cpp | 83 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/iis/mymodule.cpp b/iis/mymodule.cpp index 6a9e31acc..15cfb206c 100644 --- a/iis/mymodule.cpp +++ b/iis/mymodule.cpp @@ -187,8 +187,9 @@ char *ConvertUTF16ToUTF8( __in const WCHAR * pszTextUTF16, size_t cchUTF16, apr_ // if ( (pszTextUTF16 == NULL) || (*pszTextUTF16 == L'\0') || cchUTF16 == 0 ) { - // Return empty string - return ""; + // Return empty string. It comes from the pool, not a string literal, + // so callers may modify it in place like any other result. + return apr_pstrdup(pool, ""); } // @@ -206,7 +207,7 @@ char *ConvertUTF16ToUTF8( __in const WCHAR * pszTextUTF16, size_t cchUTF16, apr_ if ( cbUTF8 == 0 ) { - return ""; + return apr_pstrdup(pool, ""); } // @@ -391,8 +392,27 @@ HRESULT CMyHttpModule::ReadFileChunk(HTTP_DATA_CHUNK *chunk, char *buf) } } + // + // The read started at the page-aligned offset, so the first + // dwDataStartOffset bytes are before the data we want. If the read + // returned no more than that (the file ended before the requested + // range, e.g. it got shorter after its size was taken), there is + // nothing left to copy: treat it as EOF. Without this check the + // subtraction below wraps around and memcpy reads past pIoBuffer, + // and a net read of 0 bytes would make the loop spin forever. + // + if (bytesRead <= dwDataStartOffset) + { + chunk->FromFileHandle.ByteRange.Length.QuadPart = bytesTotal; + break; + } + bytesRead -= dwDataStartOffset; + if (bytesRead > m_dwPageSize - dwDataStartOffset) + { + bytesRead = m_dwPageSize - dwDataStartOffset; + } if (bytesRead > chunk->FromFileHandle.ByteRange.Length.QuadPart) { bytesRead = (DWORD)chunk->FromFileHandle.ByteRange.Length.QuadPart; @@ -848,7 +868,8 @@ CMyHttpModule::OnBeginRequest( r->hostname = ConvertUTF16ToUTF8(req->CookedUrl.pHost, req->CookedUrl.HostLength / sizeof(WCHAR), r->pool); r->path_info = ConvertUTF16ToUTF8(req->CookedUrl.pAbsPath, req->CookedUrl.AbsPathLength / sizeof(WCHAR), r->pool); - if(r->hostname == NULL || r->hostname[0] == '\0') + // ConvertUTF16ToUTF8() and ZeroTerminate() never return NULL + if(r->hostname[0] == '\0') { if(req->Headers.KnownHeaders[HttpHeaderHost].pRawValue != NULL) r->hostname = ZeroTerminate(req->Headers.KnownHeaders[HttpHeaderHost].pRawValue, @@ -860,11 +881,32 @@ CMyHttpModule::OnBeginRequest( if(r->hostname != NULL) { - int k = 0; char *ptr = (char *)r->hostname; - while(*ptr != 0 && *ptr != ':') - ptr++; + if(*ptr == '[') + { + // IPv6 literal such as [2001:db8::1]:8080. The address itself + // contains ':', so the port separator can only come after the + // closing bracket. Drop the brackets like Apache httpd does. + char *end = strchr(ptr, ']'); + + if(end != NULL) + { + *end = 0; + r->hostname = ptr + 1; + ptr = end + 1; + } + else + { + // malformed, keep the value as it is and do not look for a port + ptr += strlen(ptr); + } + } + else + { + while(*ptr != 0 && *ptr != ':') + ptr++; + } if(*ptr == ':') { @@ -1141,6 +1183,20 @@ apr_status_t WriteBodyCallback(request_rec *r, char *buf, unsigned int length) IHttpContext *pHttpContext = rsc->m_pHttpContext; IHttpRequest *pHttpRequest = pHttpContext->GetRequest(); + // since we clean the APR pool at the end of OnSendRequest, we must get IIS-managed memory chunk + // + void *reqbuf = pHttpContext->AllocateRequestMemory(length); + + if(reqbuf == NULL && length > 0) + { + // The original body has already been read, so it cannot be kept. + // Fail the request instead of crashing on a NULL copy. + if(rsc->m_pProvider != NULL) + rsc->m_pProvider->SetErrorStatus(E_OUTOFMEMORY); + + return APR_ENOMEM; + } + CHAR szLength[21]; //Max length for a 64 bit int is 20 ZeroMemory(szLength, sizeof(szLength)); @@ -1166,10 +1222,6 @@ apr_status_t WriteBodyCallback(request_rec *r, char *buf, unsigned int length) // possible, but there's nothing we can do } - // since we clean the APR pool at the end of OnSendRequest, we must get IIS-managed memory chunk - // - void *reqbuf = pHttpContext->AllocateRequestMemory(length); - memcpy(reqbuf, buf, length); pHttpRequest->InsertEntityBody(reqbuf, length); @@ -1217,12 +1269,17 @@ apr_status_t WriteResponseCallback(request_rec *r, char *buf, unsigned int lengt HTTP_RESPONSE *pRawHttpResponse = pHttpResponse->GetRawHttpResponse(); HTTP_DATA_CHUNK *pDataChunk = (HTTP_DATA_CHUNK *)apr_palloc(rsc->m_pRequestRec->pool, sizeof(HTTP_DATA_CHUNK)); - pRawHttpResponse->EntityChunkCount = 0; - // since we clean the APR pool at the end of OnSendRequest, we must get IIS-managed memory chunk // void *reqbuf = pHttpContext->AllocateRequestMemory(length); + // Allocate before changing the response, so that on failure the + // original body is left untouched and the caller sees the error. + if(pDataChunk == NULL || (reqbuf == NULL && length > 0)) + return APR_ENOMEM; + + pRawHttpResponse->EntityChunkCount = 0; + memcpy(reqbuf, buf, length); pDataChunk->DataChunkType = HttpDataChunkFromMemory;