Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7249: Improved parsing of DEX string_data_item and debug_info_item

Ryan Kurtz76024497afd4
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/analyzer/DexHeaderFormatMarkup.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/analyzer/DexHeaderFormatMarkup.java
index ca121da8f3..488109e5ec 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/analyzer/DexHeaderFormatMarkup.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/analyzer/DexHeaderFormatMarkup.java
@@ -4,9 +4,9 @@
* 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.
@@ -315,9 +315,9 @@ public class DexHeaderFormatMarkup {
private String getParameterName(DexHeader header, CodeItem codeItem, int parameterOrdinal) {
try {
DebugInfoItem debugInfo = codeItem.getDebugInfo();
- int[] debugParameterNames = debugInfo.getParameterNames();
+ List<Integer> debugParameterNames = debugInfo.getParameterNames();
List<StringIDItem> strings = header.getStrings();
- StringIDItem stringIDItem = strings.get(debugParameterNames[parameterOrdinal]);
+ StringIDItem stringIDItem = strings.get(debugParameterNames.get(parameterOrdinal));
StringDataItem stringDataItem = stringIDItem.getStringDataItem();
return stringDataItem.getString();
}
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java
index 2ceab22bcf..e78be26b8e 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java
@@ -4,9 +4,9 @@
* 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.
@@ -16,6 +16,8 @@
package ghidra.file.formats.android.dex.format;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import ghidra.app.util.bin.*;
import ghidra.program.model.data.*;
@@ -27,8 +29,8 @@ public class DebugInfoItem implements StructConverter {
private int lineStartLength;// in bytes
private int parametersSize;
private int parametersSizeLength;// in bytes
- private int[] parameterNames;
- private int[] parameterNamesLengths;
+ private List<Integer> parameterNames = new ArrayList<>();
+ private List<Integer> parameterNamesLengths = new ArrayList<>();
private byte[] stateMachineOpcodes;
public DebugInfoItem(BinaryReader reader) throws IOException {
@@ -40,14 +42,11 @@ public class DebugInfoItem implements StructConverter {
parametersSize = leb128.asUInt32();
parametersSizeLength = leb128.getLength();
- parameterNames = new int[parametersSize];
- parameterNamesLengths = new int[parametersSize];
-
for (int i = 0; i < parametersSize; ++i) {
leb128 = reader.readNext(LEB128Info::unsigned);
- parameterNames[i] = leb128.asUInt32() - 1;// uleb128p1
- parameterNamesLengths[i] = leb128.getLength();
+ parameterNames.add(leb128.asUInt32() - 1);// uleb128p1
+ parameterNamesLengths.add(leb128.getLength());
}
int count = DebugInfoStateMachineReader.computeLength(reader.clone());
@@ -55,33 +54,30 @@ public class DebugInfoItem implements StructConverter {
}
/**
- * <pre>
- * The initial value for the state machine's line register.
+ * {@return the initial value for the state machine's line register}
+ * <p>
* Does not represent an actual positions entry.
- * </pre>
*/
public int getLineStart() {
return lineStart;
}
/**
- * <pre>
- * The number of parameter names that are encoded.
+ * {@return the number of parameter names that are encoded}
+ * <p>
* There should be one per method parameter, excluding an instance method's this, if any.
- * </pre>
*/
public int getParametersSize() {
return parametersSize;
}
/**
- * <pre>
- * String index of the method parameter name.
- * An encoded value of NO_INDEX indicates that no name is available for the associated parameter.
- * The type descriptor and signature are implied from the method descriptor and signature.
- * </pre>
+ * {@return a {@link List} of String indexes of the method parameter name}
+ * <p>
+ * An encoded value of NO_INDEX indicates that no name is available for the associated
+ * parameter. The type descriptor and signature are implied from the method descriptor and signature.
*/
- public int[] getParameterNames() {
+ public List<Integer> getParameterNames() {
return parameterNames;
}
@@ -104,8 +100,8 @@ public class DebugInfoItem implements StructConverter {
structure.add(ULEB128, parametersSizeLength, "parameters_size", null);
for (int i = 0; i < parametersSize; ++i) {
- structure.add(ULEB128, parameterNamesLengths[i], "parameter_" + i, null);
- builder.append("%d".formatted(parameterNamesLengths[i]));
+ structure.add(ULEB128, parameterNamesLengths.get(i), "parameter_" + i, null);
+ builder.append("%d".formatted(parameterNamesLengths.get(i)));
}
ArrayDataType stateMachineArray =
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ModifiedUTF8.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ModifiedUTF8.java
index c3298a4519..576bd89be0 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ModifiedUTF8.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ModifiedUTF8.java
@@ -4,9 +4,9 @@
* 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.
@@ -26,24 +26,33 @@ import java.io.*;
*/
public final class ModifiedUTF8 {
- public final static String decode(InputStream in, char[] out)
- throws UTFDataFormatException, IOException {
- int s = 0;
+ /**
+ * Decodes a null-terminated MUTF-8 string from the given stream.
+ * <p>
+ * The decoded string grows as bytes are consumed, so its length is bounded by the number of
+ * bytes available in the stream rather than by any (possibly untrusted) declared length.
+ *
+ * @param in the stream to read MUTF-8 bytes from
+ * @return the decoded string
+ * @throws UTFDataFormatException if the bytes are not valid MUTF-8
+ * @throws IOException if an IO-related error occurred
+ */
+ public final static String decode(InputStream in) throws UTFDataFormatException, IOException {
+ StringBuilder sb = new StringBuilder();
while (true) {
char a = (char) (in.read() & 0xff);
if (a == 0) {
- return new String(out, 0, s);
+ return sb.toString();
}
- out[s] = a;
if (a < '\u0080') {
- s++;
+ sb.append(a);
}
else if ((a & 0xe0) == 0xc0) {
int b = in.read() & 0xff;
if ((b & 0xC0) != 0x80) {
throw new UTFDataFormatException("bad second byte");
}
- out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
+ sb.append((char) (((a & 0x1F) << 6) | (b & 0x3F)));
}
else if ((a & 0xf0) == 0xe0) {
int b = in.read() & 0xff;
@@ -51,7 +60,7 @@ public final class ModifiedUTF8 {
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
throw new UTFDataFormatException("bad second or third byte");
}
- out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
+ sb.append((char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)));
}
else {
throw new UTFDataFormatException("bad byte");
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java
index 1ca764a37c..fc8f01bef6 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java
@@ -4,9 +4,9 @@
* 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.
@@ -30,7 +30,6 @@ import ghidra.util.exception.DuplicateNameException;
public class StringDataItem implements StructConverter {
private static final int MAX_STRING_LEN = 0x200000; // 2Mb'ish
- private int stringLength;
private int lebLength;
private int actualLength;
private String string;
@@ -41,7 +40,6 @@ public class StringDataItem implements StructConverter {
reader = reader.clone(DexUtil.adjustOffset(stringItem.getStringDataOffset(), dexHeader));
LEB128Info leb128 = reader.readNext(LEB128Info::unsigned);
- stringLength = leb128.asUInt32();
lebLength = leb128.getLength();
long nullTermIndex =
@@ -49,11 +47,7 @@ public class StringDataItem implements StructConverter {
actualLength = (int) (nullTermIndex - reader.getPointerIndex() + 1);
byte[] stringBytes = reader.readNextByteArray(actualLength);
- ByteArrayInputStream in = new ByteArrayInputStream(stringBytes);
-
- char[] out = new char[stringLength];
-
- string = ModifiedUTF8.decode(in, out);
+ string = ModifiedUTF8.decode(new ByteArrayInputStream(stringBytes));
}
/**