Ghidra is a software reverse engineering (SRE) framework
GP-7251 getCodePoint range check
caheckmane37c57f3d9c2
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.ccindex 7f43d290c7..b96e226e74 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/printlanguage.cc@@ -506,7 +506,7 @@ bool PrintLanguage::escapeCharacterData(ostream &s,const uint1 *buf,int4 count,iint4 skip = charsize;int4 codepoint = 0;while(i<count) {- codepoint = StringManager::getCodepoint(buf+i,charsize,bigend,skip);+ codepoint = StringManager::getCodepoint(buf+i,count-i,charsize,bigend,skip);if (codepoint == 0 || codepoint == -1) break;printUnicode(s,codepoint);i += skip;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.ccindex 142773975d..0f75a0346e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.cc@@ -40,7 +40,7 @@ bool StringManager::writeUnicode(ostream &s,const uint1 *buffer,int4 size,int4 cint4 count=0;int4 skip = charsize;while(i<size) {- int4 codepoint = getCodepoint(buffer+i,charsize,bigend,skip);+ int4 codepoint = getCodepoint(buffer+i,size-i,charsize,bigend,skip);if (codepoint < 0) return false;if (codepoint == 0) break; // TerminatorwriteUtf8(s, codepoint);@@ -329,7 +329,7 @@ int4 StringManager::checkCharacters(const uint1 *buf,int4 size,int4 charsize,booint4 count=0;int4 skip = charsize;while(i<size) {- int4 codepoint = getCodepoint(buf+i,charsize,bigend,skip);+ int4 codepoint = getCodepoint(buf+i,size-i,charsize,bigend,skip);if (codepoint < 0) return -1;if (codepoint == 0) break;count += 1;@@ -340,19 +340,23 @@ int4 StringManager::checkCharacters(const uint1 *buf,int4 size,int4 charsize,boo/// One or more bytes is consumed from the array, and the number of bytes used is passed back./// \param buf is a pointer to the bytes in the character array+/// \param len is the number of bytes in the array/// \param charsize is 1 for UTF8, 2 for UTF16, or 4 for UTF32/// \param bigend is \b true for big endian encoding of the UTF element/// \param skip is a reference for passing back the number of bytes consumed/// \return the codepoint or -1 if the encoding is invalid-int4 StringManager::getCodepoint(const uint1 *buf,int4 charsize,bool bigend,int4 &skip)+int4 StringManager::getCodepoint(const uint1 *buf,int4 len,int4 charsize,bool bigend,int4 &skip){+ if (len < charsize)+ return -1;int4 codepoint;int4 sk = 0;if (charsize==2) { // UTF-16codepoint = readUtf16(buf,bigend);sk += 2;if ((codepoint>=0xD800)&&(codepoint<=0xDBFF)) { // high surrogate+ if (len < 4) return -1;int4 trail=readUtf16(buf+2,bigend);sk += 2;if ((trail<0xDC00)||(trail>0xDFFF)) return -1; // Bad trail@@ -367,12 +371,14 @@ int4 StringManager::getCodepoint(const uint1 *buf,int4 charsize,bool bigend,int4sk = 1;}else if ((val&0xe0)==0xc0) {+ if (len < 2) return -1;int4 val2 = buf[1];sk = 2;if ((val2&0xc0)!=0x80) return -1; // Not a valid UTF8-encodingcodepoint = ((val&0x1f)<<6) | (val2 & 0x3f);}else if ((val&0xf0)==0xe0) {+ if (len < 3) return -1;int4 val2 = buf[1];int4 val3 = buf[2];sk = 3;@@ -380,6 +386,7 @@ int4 StringManager::getCodepoint(const uint1 *buf,int4 charsize,bool bigend,int4codepoint = ((val&0xf)<<12) | ((val2&0x3f)<<6) | (val3 & 0x3f);}else if ((val&0xf8)==0xf0) {+ if (len < 4) return -1;int4 val2 = buf[1];int4 val3 = buf[2];int4 val4 = buf[3];diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.hhindex e4c1c4e32e..ff25714814 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/stringmanage.hh@@ -76,7 +76,7 @@ public:static int4 readUtf16(const uint1 *buf,bool bigend); ///< Read a UTF16 code point from a byte arraystatic void writeUtf8(ostream &s,int4 codepoint); ///< Write unicode character to stream in UTF8 encodingstatic int4 checkCharacters(const uint1 *buf,int4 size,int4 charsize,bool bigend);- static int4 getCodepoint(const uint1 *buf,int4 charsize,bool bigend,int4 &skip); ///< Extract next \e unicode \e codepoint+ static int4 getCodepoint(const uint1 *buf,int4 len,int4 charsize,bool bigend,int4 &skip); ///< Extract next \e unicode \e codepoint};/// \brief An implementation of StringManager that understands terminated unicode strings