Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

More lenient AXML parsing: allow for non-standard attributes sizes and avoid index exceptions when decoding some strings #2210

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions jadx-core/src/main/java/jadx/core/xmlgen/BinaryXMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,10 @@ private void parseElement() throws IOException {
die("startNS's attributeStart is not 0x14");
}
int attributeSize = is.readInt16();
if (attributeSize != 0x14) {
die("startNS's attributeSize is not 0x14");
if (attributeSize < 0x14) {
die("startNS's attributeSize is less than 0x14");
}

int attributeCount = is.readInt16();
int idIndex = is.readInt16();
int classIndex = is.readInt16();
Expand All @@ -289,22 +290,24 @@ private void parseElement() throws IOException {
Set<String> attrCache = new HashSet<>();
boolean attrNewLine = attributeCount != 1 && this.attrNewLine;
for (int i = 0; i < attributeCount; i++) {
parseAttribute(i, attrNewLine, attrCache);
parseAttribute(i, attrNewLine, attrCache, attributeSize);
}
long endPos = is.getPos();
if (endPos - startPos + 0x4 < elementSize) {
is.skip(elementSize - (endPos - startPos + 0x4));
}
}

private void parseAttribute(int i, boolean newLine, Set<String> attrCache) throws IOException {
private void parseAttribute(int i, boolean newLine, Set<String> attrCache, int attributeSize) throws IOException {
int attributeNS = is.readInt32();
int attributeName = is.readInt32();
int attributeRawValue = is.readInt32();
is.skip(3);
int attrValDataType = is.readInt8();
int attrValData = is.readInt32();

is.skip(attributeSize - 0x14);

String shortNsName = null;
if (attributeNS != -1) {
shortNsName = getAttributeNS(attributeNS, newLine);
Expand Down
11 changes: 10 additions & 1 deletion jadx-core/src/main/java/jadx/core/xmlgen/BinaryXMLStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;

public class BinaryXMLStrings {
public static final String INVALID_STRING_PLACEHOLDER = "⟨STRING_DECODE_ERROR⟩";
private final int stringCount;

private final long stringsStart;
Expand Down Expand Up @@ -40,6 +41,10 @@ public String get(int id) {
return cached;
}

if (id * 4 >= buffer.limit() - 3) {
return INVALID_STRING_PLACEHOLDER;
}

long offset = stringsStart + buffer.getInt(id * 4);
String extracted;
if (isUtf8) {
Expand All @@ -63,7 +68,7 @@ public int size() {

private static String extractString8(byte[] strArray, int offset) {
if (offset >= strArray.length) {
return "STRING_DECODE_ERROR";
return INVALID_STRING_PLACEHOLDER;
}
int start = offset + skipStrLen8(strArray, offset);
int len = strArray[start++];
Expand All @@ -78,6 +83,10 @@ private static String extractString8(byte[] strArray, int offset) {
}

private static String extractString16(byte[] strArray, int offset) {
if (offset + 2 >= strArray.length) {
return INVALID_STRING_PLACEHOLDER;
}

int len = strArray.length;
int start = offset + skipStrLen16(strArray, offset);
int end = start;
Expand Down
Loading