Skip to content

Ghidra is a software reverse engineering (SRE) framework

Fixes a stack trace caused by a (passing) unit test from GP-330

jmlagor7a81692a1099
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStrings.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStrings.java
index a98c02e4fe..7fbca10984 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStrings.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStrings.java
@@ -16,11 +16,10 @@
package ghidra.app.util.bin.format.pe.cli.streams;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
import ghidra.app.util.bin.BinaryReader;
-import ghidra.app.util.bin.format.pe.cli.CliMetadataRoot;
import ghidra.app.util.bin.format.pe.cli.CliStreamHeader;
import ghidra.program.model.data.*;
import ghidra.util.exception.DuplicateNameException;
@@ -64,15 +63,26 @@ public class CliStreamStrings extends CliAbstractStream {
public boolean parse() throws IOException {
reader.setPointerIndex(offset);
- // First byte is always 0x00
- reader.readNextByte();
+ int bytesRead = 0;
+ int stringLength = 0;
+ int prevOffset = 0;
- int bytesRead = 1;
+ // Loop through the data looking for NULL terminators
while (bytesRead < header.getSize()) {
- String str = reader.readNextAsciiString();
- stringIndexes.add(bytesRead);
- stringSizes.add(str.length() + 1);
- bytesRead += str.length() + 1;
+ int currentByte = reader.readNextUnsignedByte();
+ stringLength++;
+ bytesRead++;
+
+ if (currentByte == 0) {
+ stringIndexes.add(prevOffset);
+ prevOffset = bytesRead;
+
+ // Record the length of the UTF-8 string including the NULL terminator
+ stringSizes.add(stringLength);
+
+ // We're moving on to the next string so reset to 0
+ stringLength = 0;
+ }
}
return true;
@@ -87,6 +97,10 @@ public class CliStreamStrings extends CliAbstractStream {
*/
public String getString(int index) {
+ if (stringIndexes.size() == 0 || stringSizes.size() == 0) {
+ return null;
+ }
+
int lastIndex = stringIndexes.get(stringIndexes.size() - 1);
int lastSize = stringSizes.get(stringSizes.size() - 1);
@@ -94,8 +108,24 @@ public class CliStreamStrings extends CliAbstractStream {
return null;
}
+ int stringLength = 0;
+ int stringLengthIndex = Collections.binarySearch(stringIndexes, index);
+ if (stringLengthIndex >= 0) {
+ stringLength = stringSizes.get(stringLengthIndex);
+ }
+ else {
+ // Reinterpret to the closest offset of a complete string prior
+ // to the offset, then get the remainder string length
+ stringLengthIndex = (-stringLengthIndex - 1) - 1;
+ stringLength =
+ stringSizes.get(stringLengthIndex) - (index - stringIndexes.get(stringLengthIndex));
+ }
+
try {
- return reader.readAsciiString(offset + index);
+ // Grab an array of bytes at the index and convert to UTF-8, and don't
+ // include the NULL terminator
+ return new String(reader.readByteArray(offset + index, stringLength - 1),
+ StandardCharsets.UTF_8);
}
catch (IOException e) {
return null;
@@ -105,7 +135,7 @@ public class CliStreamStrings extends CliAbstractStream {
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
Structure struct = new StructureDataType(new CategoryPath(PATH), header.getName(), 0);
- struct.add(BYTE, "Reserved", "Always 0");
+
for (int i = 0; i < stringSizes.size(); i++) {
struct.add(UTF8, stringSizes.get(i),
"[" + Integer.toHexString(stringIndexes.get(i)) + "]", null);
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStringsTest.java b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStringsTest.java
new file mode 100644
index 0000000000..4f418ccff1
--- /dev/null
+++ b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/pe/cli/streams/CliStreamStringsTest.java
@@ -0,0 +1,182 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.util.bin.format.pe.cli.streams;
+
+import static org.junit.Assert.*;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.Test;
+
+import generic.test.AbstractGTest;
+import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.ByteArrayProvider;
+import ghidra.app.util.bin.format.pe.cli.CliStreamHeader;
+import ghidra.util.LittleEndianDataConverter;
+
+public class CliStreamStringsTest extends AbstractGTest {
+
+ private CliStreamStrings initCliStreamStrings() throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ baos.write(new byte[] { 0, 0, 0, 0 }); // offset
+ baos.write(new byte[] { 0, 0, 0, 0 }); // size
+ baos.write(new byte[] { (byte) 'a', (byte) 'b', (byte) 'c', 0 }); // name
+ int offset = baos.size();
+ PrintWriter pw = new PrintWriter(baos, false, StandardCharsets.UTF_8);
+ pw.write("\0");
+ pw.write("test1\0");
+ pw.write("test2\0");
+ pw.write("ab\ucc01\u1202ab\0");
+ pw.write("last\0");
+ pw.flush();
+ int size = baos.size() - offset;
+ byte[] bytes = baos.toByteArray();
+
+ LittleEndianDataConverter.INSTANCE.putInt(bytes, 0, offset);
+ LittleEndianDataConverter.INSTANCE.putInt(bytes, 4, size);
+
+ ByteArrayProvider bap = new ByteArrayProvider(bytes);
+ BinaryReader br = new BinaryReader(bap, true);
+
+ CliStreamHeader csh = new CliStreamHeader(null, br);
+ CliStreamStrings css = new CliStreamStrings(csh, offset, 0, br);
+
+ return css;
+ }
+
+ private CliStreamStrings initCliStreamStringsEmpty() throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ baos.write(new byte[] { 0, 0, 0, 0 }); // offset
+ baos.write(new byte[] { 0, 0, 0, 0 }); // size
+ baos.write(new byte[] { (byte) 'a', (byte) 'b', (byte) 'c', 0 }); // name
+ int offset = baos.size();
+ PrintWriter pw = new PrintWriter(baos, false, StandardCharsets.UTF_8);
+ pw.write("\0");
+ pw.flush();
+ int size = baos.size() - offset;
+ byte[] bytes = baos.toByteArray();
+
+ LittleEndianDataConverter.INSTANCE.putInt(bytes, 0, offset);
+ LittleEndianDataConverter.INSTANCE.putInt(bytes, 4, size);
+
+ ByteArrayProvider bap = new ByteArrayProvider(bytes);
+ BinaryReader br = new BinaryReader(bap, true);
+
+ CliStreamHeader csh = new CliStreamHeader(null, br);
+ CliStreamStrings css = new CliStreamStrings(csh, offset, 0, br);
+
+ return css;
+ }
+
+ private CliStreamStrings initCliStreamStringsHeaderOnly() throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ baos.write(new byte[] { 0, 0, 0, 0 }); // offset
+ baos.write(new byte[] { 0, 0, 0, 0 }); // size
+ baos.write(new byte[] { (byte) 'a', (byte) 'b', (byte) 'c', 0 }); // name
+ int offset = baos.size();
+ PrintWriter pw = new PrintWriter(baos, false, StandardCharsets.UTF_8);
+ pw.flush();
+ int size = baos.size() - offset;
+ byte[] bytes = baos.toByteArray();
+
+ LittleEndianDataConverter.INSTANCE.putInt(bytes, 0, offset);
+ LittleEndianDataConverter.INSTANCE.putInt(bytes, 4, size);
+
+ ByteArrayProvider bap = new ByteArrayProvider(bytes);
+ BinaryReader br = new BinaryReader(bap, true);
+
+ CliStreamHeader csh = new CliStreamHeader(null, br);
+ CliStreamStrings css = new CliStreamStrings(csh, offset, 0, br);
+
+ return css;
+ }
+
+ @Test
+ public void testParse() throws IOException {
+ CliStreamStrings css = initCliStreamStrings();
+ assertEquals(css.parse(), true);
+ }
+
+ @Test
+ public void testGetString() throws IOException {
+ // Test a normally formed blob of UTF-8 strings
+ CliStreamStrings css = initCliStreamStrings();
+ css.parse();
+
+ assertEquals(css.getString(-1), null);
+ assertEquals(css.getString(0), "");
+ assertEquals(css.getString(1), "test1");
+ assertEquals(css.getString(2), "est1");
+ assertEquals(css.getString(3), "st1");
+ assertEquals(css.getString(4), "t1");
+ assertEquals(css.getString(5), "1");
+ assertEquals(css.getString(6), "");
+ assertEquals(css.getString(7), "test2");
+ assertEquals(css.getString(8), "est2");
+ assertEquals(css.getString(9), "st2");
+ assertEquals(css.getString(10), "t2");
+ assertEquals(css.getString(11), "2");
+ assertEquals(css.getString(12), "");
+ assertEquals(css.getString(13), "ab\ucc01\u1202ab");
+ assertEquals(css.getString(14), "b\ucc01\u1202ab");
+ assertEquals(css.getString(15), "\ucc01\u1202ab");
+
+ // Invalid conversions -> Unicode Replacement characters
+ assertEquals(css.getString(16), "\ufffd\ufffd\u1202ab");
+ assertEquals(css.getString(17), "\ufffd\u1202ab");
+
+ assertEquals(css.getString(18), "\u1202ab");
+
+ // Invalid conversions -> Unicode Replacement characters
+ assertEquals(css.getString(19), "\ufffd\ufffdab");
+ assertEquals(css.getString(20), "\ufffdab");
+
+ assertEquals(css.getString(21), "ab");
+ assertEquals(css.getString(22), "b");
+ assertEquals(css.getString(23), "");
+ assertEquals(css.getString(24), "last");
+ assertEquals(css.getString(25), "ast");
+ assertEquals(css.getString(26), "st");
+ assertEquals(css.getString(27), "t");
+ assertEquals(css.getString(28), "");
+ assertEquals(css.getString(29), null);
+ }
+
+ @Test
+ public void testGetStringEmpty() throws IOException {
+ // Test a blob that only includes the mandatory single
+ // NULL string
+ CliStreamStrings css = initCliStreamStringsEmpty();
+ css.parse();
+
+ assertEquals(css.getString(0), "");
+ assertEquals(css.getString(1), null);
+ assertEquals(css.getString(2), null);
+ }
+
+ @Test
+ public void testGetStringHeaderOnly() throws IOException {
+ // Test a blob that for some reason includes the header
+ // only and not the mandatory NULL string
+ CliStreamStrings css = initCliStreamStringsHeaderOnly();
+ css.parse();
+
+ assertEquals(css.getString(0), null);
+ assertEquals(css.getString(1), null);
+ assertEquals(css.getString(2), null);
+ }
+}