Skip to content

Commit

Permalink
GP-3543 additional vaidity checks for ELF Android relocation processing
Browse files Browse the repository at this point in the history
and corrected related markup data
  • Loading branch information
ghidra1 committed Jun 14, 2023
1 parent 3736ae0 commit 6928935
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Expand Up @@ -15,9 +15,8 @@
*/
package ghidra.app.util.bin.format.elf;

import java.util.ArrayList;

import java.io.IOException;
import java.util.ArrayList;

import javax.help.UnsupportedOperationException;

Expand Down Expand Up @@ -177,7 +176,7 @@ long getLastRelocationOffset(WrappedMemBuffer buf) {
WrappedMemBuffer cbuf = new WrappedMemBuffer(buf, comps[2].getOffset());
s = (Scalar) comps[2].getDataType().getValue(cbuf, null, comps[2].getLength());
long groupOffsetDelta = s.getValue();
return baseRelocOffset + ((groupSize - 1) * groupOffsetDelta);
return baseRelocOffset + (groupSize * groupOffsetDelta);
}

if (lastDtc.getFieldName().startsWith("group_")) {
Expand Down
Expand Up @@ -229,23 +229,28 @@ private List<ElfRelocation> parseAndroidRelocations(BinaryReader reader)
long groupRInfo = groupedByInfo ? reader.readNext(LEB128::signed) : 0;

if (groupHasAddend && groupedByAddend) {
if (!addendTypeReloc) {
elfHeader.logError(
"ELF Android Relocation processing failed. Unexpected r_addend in android.rel section");
return List.of();
}
// group_addend (optional)
addend += reader.readNext(LEB128::signed);
}
else if (!groupHasAddend) {
addend = 0;
}

// Process all group entries
for (int i = 0; i < groupSize; i++) {
// reloc_offset (optional)
offset +=
groupedByDelta ? groupOffsetDelta : reader.readNext(LEB128::signed);
offset += groupedByDelta ? groupOffsetDelta : reader.readNext(LEB128::signed);

// reloc_info (optional)
long info = groupedByInfo ? groupRInfo : reader.readNext(LEB128::signed);

long rAddend = 0;
if (groupHasAddend) {
if (addendTypeReloc && groupHasAddend) {
if (!groupedByAddend) {
// reloc_addend (optional)
addend += reader.readNext(LEB128::signed);
Expand Down
Expand Up @@ -95,7 +95,7 @@ public Object getValue(MemBuffer buf, Settings settings, int maxLength) {
}

// approximate bitLength from storage byte length
int bitLength = Math.max(64, len * 7);
int bitLength = Math.min(64, len * 7);
int mod = bitLength % 8;
if (mod != 0) {
bitLength += (8 - mod);
Expand Down

0 comments on commit 6928935

Please sign in to comment.