Fix Off-By-One Stack Buffer Overflows in XML Parser#1717
Fix Off-By-One Stack Buffer Overflows in XML Parser#1717S4N-T0S wants to merge 2 commits intoveracrypt:masterfrom
Conversation
|
@S4N-T0S The proposed
I would classify the impact as local memory corruption and possible crash from user-controlled XML, with security relevance when the file is parsed by an elevated VeraCrypt process, but not as a demonstrated privilege escalation. Can you please update the PR with the broader parser hardening described above? I can also amend this before merging while preserving attribution. |
There was a problem hiding this comment.
Hopefully the commit I have just pushed will suffice in terms of tests and the parser hardening. Here is a quick breakdown of how the latest changes address your feedback:
- Checking for the closing quote & current tag bounds (
XmlGetAttributeText):
I introduced anodeEndpointer that locates the>character of the current tag. The function now explicitly looks for both the opening (quote1) and closing (quote2) quotes and verifies that neither isNULLand that both appear beforenodeEnd. This prevents it from computing invalid lengths or falsely grabbing a quote from a subsequent XML tag. - Bounding the decoded output index (
XmlGetNodeText):
I removed the raw length check prior to the loop and instead added aif (j >= xmlTextSize - 1)check inside the decodingwhileloop. This ensures we are strictly bounding against the final decoded string length (safely preserving 1 byte for the NUL terminator) without prematurely rejecting valid escaped text where the raw length exceeds the buffer but the decoded length fits. - General robustness:
Added<= 0early-exit checks for the passed buffer sizes in both functions just to be perfectly safe. - Boundary Tests (
Tests.c/Tests.h):
Added a newXmlTest()suite covering all the requested scenarios:- 1 & 5:
size - 1lengths accepted for both attributes and nodes. - 2 & 6:
sizelengths correctly rejected (preventing the original off-by-one). - 3: Malformed tags with missing quotes returning
NULL. - 4: Cross-tag quote constraints (closing quote belonging to a later tag).
- 7 & 8: Escaped text handling, ensuring decoded output is checked correctly against the buffer bounds.
- 1 & 5:
|
@S4N-T0S I found two issues that still need to be fixed before merge:
After those two changes and successful checks, I think this will be ready to merge. |
@idrassi |
|
@S4N-T0S Please structure it like this:
So the flow would be: BOOL testsPassed = AutoTestAlgorithms();
#if !defined(TC_WINDOWS_DRIVER) && !defined(_UEFI)
if (testsPassed && !XmlTest())
testsPassed = FALSE;
#endifThis keeps XML testing in a user-mode test path. Please also add the sentinel test discussed earlier: when |
Bug Description:
The functions XmlGetAttributeText and XmlGetNodeText in Xml.c contain off-by-one buffer overflows. They check if the parsed length l is strictly greater than size (l > size). If l == size, the functions execute buffer[l] = 0;, which writes a null byte exactly one byte past the allocated bounds of the array.
Security Impact:
Because VeraCrypt parses Configuration.xml and History.xml from the user-controlled %APPDATA% folder, a low-privileged malware could place a malicious XML file there. If VeraCrypt is later launched with Administrative privileges (e.g., via UAC for device mounting), this memory corruption occurs in an elevated context. Of course this would be difficult to exploit because of Stack Canaries etc in Windows, either way no reason not to fix. At the very least it results in an application crash.
Fix:
Changed the boundary limits from > to >= to ensure 1 byte is always cleanly reserved for the null terminator.