Blame SOURCES/xerces-c-CVE-2016-0729.patch

95d693
diff -urNp xerces-c-3.1.1-patched/src/xercesc/internal/XMLReader.cpp xerces-c-3.1.1-patched-modified/src/xercesc/internal/XMLReader.cpp
95d693
--- xerces-c-3.1.1-patched/src/xercesc/internal/XMLReader.cpp	2016-03-03 11:14:06.855482706 -0500
95d693
+++ xerces-c-3.1.1-patched-modified/src/xercesc/internal/XMLReader.cpp	2016-03-03 11:46:23.305567446 -0500
95d693
@@ -1460,8 +1460,30 @@ void XMLReader::doInitDecode()
95d693
 
95d693
             while (fRawBufIndex < fRawBytesAvail)
95d693
             {
95d693
-                // Security fix: make sure there are at least sizeof(UCS4Ch) bytes to consume.
95d693
+                // Make sure there are at least sizeof(UCS4Ch) bytes to consume.
95d693
                 if (fRawBufIndex + sizeof(UCS4Ch) > fRawBytesAvail) {
95d693
+                    fCharsAvail = 0;
95d693
+                    fRawBufIndex = 0;
95d693
+                    fMemoryManager->deallocate(fPublicId);
95d693
+                    fMemoryManager->deallocate(fEncodingStr);
95d693
+                    ArrayJanitor<XMLCh> janValue(fSystemId, fMemoryManager);
95d693
+                    ThrowXMLwithMemMgr1
95d693
+                    (
95d693
+                        TranscodingException
95d693
+                        , XMLExcepts::Reader_CouldNotDecodeFirstLine
95d693
+                        , fSystemId
95d693
+                        , fMemoryManager
95d693
+                    );
95d693
+                }
95d693
+
95d693
+                // Make sure we don't exhaust the limited prolog buffer size.
95d693
+                // Leave room for a space added at the end of this function.
95d693
+                if (fCharsAvail == kCharBufSize - 1) {
95d693
+                    fCharsAvail = 0;
95d693
+                    fRawBufIndex = 0;
95d693
+                    fMemoryManager->deallocate(fPublicId);
95d693
+                    fMemoryManager->deallocate(fEncodingStr);
95d693
+                    ArrayJanitor<XMLCh> janValue(fSystemId, fMemoryManager);
95d693
                     ThrowXMLwithMemMgr1
95d693
                     (
95d693
                         TranscodingException
95d693
@@ -1547,6 +1569,23 @@ void XMLReader::doInitDecode()
95d693
                 const char curCh = *asChars++;
95d693
                 fRawBufIndex++;
95d693
 
95d693
+                // Make sure we don't exhaust the limited prolog buffer size.
95d693
+                // Leave room for a space added at the end of this function.
95d693
+                if (fCharsAvail == kCharBufSize - 1) {
95d693
+                    fCharsAvail = 0;
95d693
+                    fRawBufIndex = 0;
95d693
+                    fMemoryManager->deallocate(fPublicId);
95d693
+                    fMemoryManager->deallocate(fEncodingStr);
95d693
+                    ArrayJanitor<XMLCh> janValue(fSystemId, fMemoryManager);
95d693
+                    ThrowXMLwithMemMgr1
95d693
+                    (
95d693
+                        TranscodingException
95d693
+                        , XMLExcepts::Reader_CouldNotDecodeFirstLine
95d693
+                        , fSystemId
95d693
+                        , fMemoryManager
95d693
+                    );
95d693
+                }
95d693
+
95d693
                 // Looks ok, so store it
95d693
                 fCharSizeBuf[fCharsAvail] = 1;
95d693
                 fCharBuf[fCharsAvail++] = XMLCh(curCh);
95d693
@@ -1630,8 +1669,30 @@ void XMLReader::doInitDecode()
95d693
 
95d693
             while (fRawBufIndex < fRawBytesAvail)
95d693
             {
95d693
-                // Security fix: make sure there are at least sizeof(UTF16Ch) bytes to consume.
95d693
+                // Make sure there are at least sizeof(UTF16Ch) bytes to consume.
95d693
                 if (fRawBufIndex + sizeof(UTF16Ch) > fRawBytesAvail) {
95d693
+                    fCharsAvail = 0;
95d693
+                    fRawBufIndex = 0;
95d693
+                    fMemoryManager->deallocate(fPublicId);
95d693
+                    fMemoryManager->deallocate(fEncodingStr);
95d693
+                    ArrayJanitor<XMLCh> janValue(fSystemId, fMemoryManager);
95d693
+                    ThrowXMLwithMemMgr1
95d693
+                    (
95d693
+                        TranscodingException
95d693
+                        , XMLExcepts::Reader_CouldNotDecodeFirstLine
95d693
+                        , fSystemId
95d693
+                        , fMemoryManager
95d693
+                    );
95d693
+                }
95d693
+
95d693
+                // Make sure we don't exhaust the limited prolog buffer size.
95d693
+                // Leave room for a space added at the end of this function.
95d693
+                if (fCharsAvail == kCharBufSize - 1) {
95d693
+                    fCharsAvail = 0;
95d693
+                    fRawBufIndex = 0;
95d693
+                    fMemoryManager->deallocate(fPublicId);
95d693
+                    fMemoryManager->deallocate(fEncodingStr);
95d693
+                    ArrayJanitor<XMLCh> janValue(fSystemId, fMemoryManager);
95d693
                     ThrowXMLwithMemMgr1
95d693
                     (
95d693
                         TranscodingException
95d693
@@ -1676,6 +1737,24 @@ void XMLReader::doInitDecode()
95d693
                 const XMLCh chCur = XMLEBCDICTranscoder::xlatThisOne(*srcPtr++);
95d693
                 fRawBufIndex++;
95d693
 
95d693
+                // Make sure we don't exhaust the limited prolog buffer size.
95d693
+                // Leave room for a space added at the end of this function.
95d693
+                if (fCharsAvail == kCharBufSize - 1) {
95d693
+                    fCharsAvail = 0;
95d693
+                    fRawBufIndex = 0;
95d693
+                    fMemoryManager->deallocate(fPublicId);
95d693
+                    fMemoryManager->deallocate(fEncodingStr);
95d693
+                    ArrayJanitor<XMLCh> janValue(fSystemId, fMemoryManager);
95d693
+                    ThrowXMLwithMemMgr1
95d693
+                    (
95d693
+                        TranscodingException
95d693
+                        , XMLExcepts::Reader_CouldNotDecodeFirstLine
95d693
+                        , fSystemId
95d693
+                        , fMemoryManager
95d693
+                    );
95d693
+                }
95d693
+
95d693
+
95d693
                 //
95d693
                 //  And put it into the character buffer. This stuff has to
95d693
                 //  look like it was normally transcoded.
95d693
@@ -1730,7 +1809,7 @@ void XMLReader::doInitDecode()
95d693
 //
95d693
 void XMLReader::refreshRawBuffer()
95d693
 {
95d693
-    // Security fix: make sure we don't underflow on the subtraction.
95d693
+    // Make sure we don't underflow on the subtraction.
95d693
     if (fRawBufIndex > fRawBytesAvail) {
95d693
         ThrowXMLwithMemMgr1
95d693
         (
95d693
diff -urNp xerces-c-3.1.1-patched/src/xercesc/util/XMLUri.cpp xerces-c-3.1.1-patched-modified/src/xercesc/util/XMLUri.cpp
95d693
--- xerces-c-3.1.1-patched/src/xercesc/util/XMLUri.cpp	2016-03-03 11:13:54.871619517 -0500
95d693
+++ xerces-c-3.1.1-patched-modified/src/xercesc/util/XMLUri.cpp	2016-03-03 11:30:38.593240739 -0500
95d693
@@ -875,11 +875,21 @@ void XMLUri::initializePath(const XMLCh*
95d693
                 // check for valid escape sequence
95d693
                 if (testChar == chPercent)
95d693
                 {
95d693
-                    if (index+2 >= end ||
95d693
-                        !XMLString::isHex(uriSpec[index+1]) ||
95d693
-                        !XMLString::isHex(uriSpec[index+2]))
95d693
+                    if (index + 2 >= end)
95d693
                     {
95d693
-                        XMLCh value1[BUF_LEN+1];
95d693
+                        XMLCh value1[3];
95d693
+                        value1[1] = chNull;
95d693
+                        value1[2] = chNull;
95d693
+                        XMLString::moveChars(value1, &(uriSpec[index]), (index + 1 >= end ? 1 : 2));
95d693
+                        ThrowXMLwithMemMgr2(MalformedURLException
95d693
+                            , XMLExcepts::XMLNUM_URI_Component_Invalid_EscapeSequence
95d693
+                            , errMsg_PATH
95d693
+                            , value1
95d693
+                            , fMemoryManager);
95d693
+                    }
95d693
+                    else if (!XMLString::isHex(uriSpec[index+1]) || !XMLString::isHex(uriSpec[index+2]))
95d693
+                    {
95d693
+                        XMLCh value1[4];
95d693
                         XMLString::moveChars(value1, &(uriSpec[index]), 3);
95d693
                         value1[3] = chNull;
95d693
                         ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -892,7 +902,7 @@ void XMLUri::initializePath(const XMLCh*
95d693
                 else if (!isUnreservedCharacter(testChar) &&
95d693
                          !isPathCharacter(testChar))
95d693
                 {
95d693
-                    XMLCh value1[BUF_LEN+1];
95d693
+                    XMLCh value1[2];
95d693
                     value1[0] = testChar;
95d693
                     value1[1] = chNull;
95d693
                     ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -920,11 +930,21 @@ void XMLUri::initializePath(const XMLCh*
95d693
                 // check for valid escape sequence
95d693
                 if (testChar == chPercent)
95d693
                 {
95d693
-                    if (index+2 >= end ||
95d693
-                        !XMLString::isHex(uriSpec[index+1]) ||
95d693
-                        !XMLString::isHex(uriSpec[index+2]))
95d693
+                    if (index + 2 >= end)
95d693
                     {
95d693
-                        XMLCh value1[BUF_LEN+1];
95d693
+                        XMLCh value1[3];
95d693
+                        value1[1] = chNull;
95d693
+                        value1[2] = chNull;
95d693
+                        XMLString::moveChars(value1, &(uriSpec[index]), (index + 1 >= end ? 1 : 2));
95d693
+                        ThrowXMLwithMemMgr2(MalformedURLException
95d693
+                            , XMLExcepts::XMLNUM_URI_Component_Invalid_EscapeSequence
95d693
+                            , errMsg_PATH
95d693
+                            , value1
95d693
+                            , fMemoryManager);
95d693
+                    }
95d693
+                    else if (!XMLString::isHex(uriSpec[index+1]) || !XMLString::isHex(uriSpec[index+2]))
95d693
+                    {
95d693
+                        XMLCh value1[4];
95d693
                         XMLString::moveChars(value1, &(uriSpec[index]), 3);
95d693
                         value1[3] = chNull;
95d693
                         ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -941,7 +961,7 @@ void XMLUri::initializePath(const XMLCh*
95d693
                 // contains '[' and ']'.
95d693
                 else if (!isReservedOrUnreservedCharacter(testChar))
95d693
                 {
95d693
-                    XMLCh value1[BUF_LEN+1];
95d693
+                    XMLCh value1[2];
95d693
                     value1[0] = testChar;
95d693
                     value1[1] = chNull;
95d693
                     ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -979,11 +999,21 @@ void XMLUri::initializePath(const XMLCh*
95d693
 
95d693
             if (testChar == chPercent)
95d693
             {
95d693
-                if (index+2 >= end ||
95d693
-                    !XMLString::isHex(uriSpec[index+1]) ||
95d693
-                    !XMLString::isHex(uriSpec[index+2]))
95d693
+                if (index + 2 >= end)
95d693
+                {
95d693
+                    XMLCh value1[3];
95d693
+                    value1[1] = chNull;
95d693
+                    value1[2] = chNull;
95d693
+                    XMLString::moveChars(value1, &(uriSpec[index]), (index + 1 >= end ? 1 : 2));
95d693
+                    ThrowXMLwithMemMgr2(MalformedURLException
95d693
+                        , XMLExcepts::XMLNUM_URI_Component_Invalid_EscapeSequence
95d693
+                        , errMsg_QUERY
95d693
+                        , value1
95d693
+                        , fMemoryManager);
95d693
+                }
95d693
+                if (!XMLString::isHex(uriSpec[index+1]) || !XMLString::isHex(uriSpec[index+2]))
95d693
                 {
95d693
-                    XMLCh value1[BUF_LEN+1];
95d693
+                    XMLCh value1[4];
95d693
                     XMLString::moveChars(value1, &(uriSpec[index]), 3);
95d693
                     value1[3] = chNull;
95d693
                     ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -995,7 +1025,7 @@ void XMLUri::initializePath(const XMLCh*
95d693
             }
95d693
             else if (!isReservedOrUnreservedCharacter(testChar))
95d693
             {
95d693
-                XMLCh value1[BUF_LEN+1];
95d693
+                XMLCh value1[2];
95d693
                 value1[0] = testChar;
95d693
                 value1[1] = chNull;
95d693
                 ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -1030,11 +1060,21 @@ void XMLUri::initializePath(const XMLCh*
95d693
 
95d693
             if (testChar == chPercent)
95d693
             {
95d693
-                if (index+2 >= end ||
95d693
-                    !XMLString::isHex(uriSpec[index+1]) ||
95d693
-                    !XMLString::isHex(uriSpec[index+2]))
95d693
+                if (index + 2 >= end)
95d693
+                {
95d693
+                    XMLCh value1[3];
95d693
+                    value1[1] = chNull;
95d693
+                    value1[2] = chNull;
95d693
+                    XMLString::moveChars(value1, &(uriSpec[index]), (index + 1 >= end ? 1 : 2));
95d693
+                    ThrowXMLwithMemMgr2(MalformedURLException
95d693
+                        , XMLExcepts::XMLNUM_URI_Component_Invalid_EscapeSequence
95d693
+                        , errMsg_FRAGMENT
95d693
+                        , value1
95d693
+                        , fMemoryManager);
95d693
+                }
95d693
+                if (!XMLString::isHex(uriSpec[index+1]) || !XMLString::isHex(uriSpec[index+2]))
95d693
                 {
95d693
-                    XMLCh value1[BUF_LEN+1];
95d693
+                    XMLCh value1[4];
95d693
                     XMLString::moveChars(value1, &(uriSpec[index]), 3);
95d693
                     value1[3] = chNull;
95d693
                     ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -1046,7 +1086,7 @@ void XMLUri::initializePath(const XMLCh*
95d693
             }
95d693
             else if (!isReservedOrUnreservedCharacter(testChar))
95d693
             {
95d693
-                XMLCh value1[BUF_LEN+1];
95d693
+                XMLCh value1[2];
95d693
                 value1[0] = testChar;
95d693
                 value1[1] = chNull;
95d693
                 ThrowXMLwithMemMgr2(MalformedURLException
95d693
@@ -1410,14 +1450,15 @@ void XMLUri::isConformantUserInfo(const
95d693
         }
95d693
         else if (*tmpStr == chPercent)               // '%'
95d693
         {
95d693
-            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex
95d693
-                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex
95d693
+            if (XMLString::stringLen(tmpStr) >= 3
95d693
+                && XMLString::isHex(*(tmpStr+1))     // 1st hex
95d693
+                && XMLString::isHex(*(tmpStr+2))  )  // 2nd hex
95d693
             {
95d693
                 tmpStr+=3;
95d693
             }
95d693
             else
95d693
             {
95d693
-                XMLCh value1[BUF_LEN+1];
95d693
+                XMLCh value1[4];
95d693
                 value1[0] = chPercent;
95d693
                 value1[1] = *(tmpStr+1);
95d693
                 value1[2] = *(tmpStr+2);
95d693
@@ -1468,8 +1509,9 @@ bool XMLUri::isValidServerBasedAuthority
95d693
         }
95d693
         else if (userinfo[index] == chPercent)               // '%'
95d693
         {
95d693
-            if (XMLString::isHex(userinfo[index+1]) &&     // 1st hex
95d693
-                XMLString::isHex(userinfo[index+2])  )     // 2nd hex
95d693
+            if (index + 2 < userLen
95d693
+                && XMLString::isHex(userinfo[index+1])       // 1st hex
95d693
+                && XMLString::isHex(userinfo[index+2])  )    // 2nd hex
95d693
                 index +=3;
95d693
             else
95d693
                 return false;
95d693
@@ -1508,8 +1550,9 @@ bool XMLUri::isValidServerBasedAuthority
95d693
         }
95d693
         else if (*tmpStr == chPercent)               // '%'
95d693
         {
95d693
-            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex
95d693
-                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex
95d693
+            if (XMLString::stringLen(tmpStr) >= 3
95d693
+                && XMLString::isHex(*(tmpStr+1))     // 1st hex
95d693
+                && XMLString::isHex(*(tmpStr+2))  )  // 2nd hex
95d693
             {
95d693
                 tmpStr+=3;
95d693
             }
95d693
@@ -1537,8 +1580,9 @@ bool XMLUri::isValidRegistryBasedAuthori
95d693
         }
95d693
         else if (authority[index] == chPercent)               // '%'
95d693
         {
95d693
-            if (XMLString::isHex(authority[index+1]) &&     // 1st hex
95d693
-                XMLString::isHex(authority[index+2])  )     // 2nd hex
95d693
+            if (index + 2 < authLen
95d693
+                && XMLString::isHex(authority[index+1])       // 1st hex
95d693
+                && XMLString::isHex(authority[index+2])  )    // 2nd hex
95d693
                 index +=3;
95d693
             else
95d693
                 return false;
95d693
@@ -1566,8 +1610,9 @@ bool XMLUri::isValidRegistryBasedAuthori
95d693
         }
95d693
         else if (*tmpStr == chPercent)               // '%'
95d693
         {
95d693
-            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex
95d693
-                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex
95d693
+            if (XMLString::stringLen(tmpStr) >= 3
95d693
+                && XMLString::isHex(*(tmpStr + 1))     // 1st hex
95d693
+                && XMLString::isHex(*(tmpStr + 2)))  // 2nd hex
95d693
             {
95d693
                 tmpStr+=3;
95d693
             }
95d693
@@ -1602,8 +1647,9 @@ bool XMLUri::isURIString(const XMLCh* co
95d693
         }
95d693
         else if (*tmpStr == chPercent)               // '%'
95d693
         {
95d693
-            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex
95d693
-                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex
95d693
+            if (XMLString::stringLen(tmpStr) >=3
95d693
+                && XMLString::isHex(*(tmpStr+1))     // 1st hex
95d693
+                && XMLString::isHex(*(tmpStr+2))  )  // 2nd hex
95d693
             {
95d693
                 tmpStr+=3;
95d693
             }
95d693
diff -urNp xerces-c-3.1.1-patched/src/xercesc/util/XMLURL.cpp xerces-c-3.1.1-patched-modified/src/xercesc/util/XMLURL.cpp
95d693
--- xerces-c-3.1.1-patched/src/xercesc/util/XMLURL.cpp	2016-03-03 11:13:54.871619517 -0500
95d693
+++ xerces-c-3.1.1-patched-modified/src/xercesc/util/XMLURL.cpp	2016-03-03 11:44:08.553089142 -0500
95d693
@@ -117,8 +117,8 @@ static const XMLCh gListSix[]    = { chP
95d693
 static bool isHexDigit(const XMLCh toCheck)
95d693
 {
95d693
     if (((toCheck >= chDigit_0) && (toCheck <= chDigit_9))
95d693
-    ||  ((toCheck >= chLatin_A) && (toCheck <= chLatin_Z))
95d693
-    ||  ((toCheck >= chLatin_a) && (toCheck <= chLatin_z)))
95d693
+    ||  ((toCheck >= chLatin_A) && (toCheck <= chLatin_F))
95d693
+    ||  ((toCheck >= chLatin_a) && (toCheck <= chLatin_f)))
95d693
     {
95d693
         return true;
95d693
     }
95d693
@@ -130,7 +130,7 @@ static unsigned int xlatHexDigit(const X
95d693
     if ((toXlat >= chDigit_0) && (toXlat <= chDigit_9))
95d693
         return (unsigned int)(toXlat - chDigit_0);
95d693
 
95d693
-    if ((toXlat >= chLatin_A) && (toXlat <= chLatin_Z))
95d693
+    if ((toXlat >= chLatin_A) && (toXlat <= chLatin_F))
95d693
         return (unsigned int)(toXlat - chLatin_A) + 10;
95d693
 
95d693
     return (unsigned int)(toXlat - chLatin_a) + 10;
95d693
@@ -611,9 +611,20 @@ BinInputStream* XMLURL::makeNewStream()
95d693
 
95d693
             while (percentIndex != -1) {
95d693
 
95d693
-                if (percentIndex+2 >= (int)end ||
95d693
-                    !isHexDigit(realPath[percentIndex+1]) ||
95d693
-                    !isHexDigit(realPath[percentIndex+2]))
95d693
+            	// Isolate the length/boundary check so we don't try and copy off the end.
95d693
+                if (percentIndex+2 >= (int)end)
95d693
+                {
95d693
+                    XMLCh value1[3];
95d693
+                    value1[1] = chNull;
95d693
+                    value1[2] = chNull;
95d693
+					XMLString::moveChars(value1, &(realPath[percentIndex]), (percentIndex + 1 >= (int)end ? 1 : 2));
95d693
+                    ThrowXMLwithMemMgr2(MalformedURLException
95d693
+                            , XMLExcepts::XMLNUM_URI_Component_Invalid_EscapeSequence
95d693
+                            , realPath
95d693
+                            , value1
95d693
+                            , fMemoryManager);
95d693
+                }
95d693
+                else if (!isHexDigit(realPath[percentIndex+1]) || !isHexDigit(realPath[percentIndex+2]))
95d693
                 {
95d693
                     XMLCh value1[4];
95d693
                     XMLString::moveChars(value1, &(realPath[percentIndex]), 3);