Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7398 fix ext4 extent count logic, suggested by PR #9806

dev747368f24262389f53
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4Extent.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4Extent.java
index 93b247122a..85e3a94771 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4Extent.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4Extent.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.
@@ -22,6 +22,7 @@ import ghidra.program.model.data.*;
import ghidra.util.exception.DuplicateNameException;
public class Ext4Extent implements StructConverter {
+ private static final int EE_LEN_MAX_WRITTEN = 0x8000;
private int ee_block;
private short ee_len;
@@ -39,29 +40,28 @@ public class Ext4Extent implements StructConverter {
ee_start_lo = reader.readNextInt();
}
- public int getEe_block() {
- return ee_block;
- }
-
- public short getEe_len() {
- return ee_len;
- }
-
- public short getEe_start_hi() {
- return ee_start_hi;
- }
-
- public int getEe_start_lo() {
- return ee_start_lo;
- }
-
/**
* Returns the number of blocks this extent contains.
+ * <p>
+ * Max 32768 (0x8000) if written, 32767 (0x7fff) if unwritten.
*
* @return number of blocks in this extent
*/
public int getExtentBlockCount() {
- return Short.toUnsignedInt(ee_len);
+ int rawLen = Short.toUnsignedInt(ee_len);
+
+ // not a simple written/unwritten bit flag, 0x8000 is considered part of the 'written' range
+ return rawLen <= EE_LEN_MAX_WRITTEN
+ ? rawLen
+ : rawLen - EE_LEN_MAX_WRITTEN;
+ }
+
+ /**
+ * {@return boolean true if this extent is unwritten, false if written.}
+ */
+ public boolean isUnWritten() {
+ int rawLen = Short.toUnsignedInt(ee_len);
+ return rawLen > EE_LEN_MAX_WRITTEN;
}
/**
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4ExtentsHelper.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4ExtentsHelper.java
index 3c5972b3fc..9b30d97682 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4ExtentsHelper.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/Ext4ExtentsHelper.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.
@@ -74,6 +74,12 @@ public class Ext4ExtentsHelper {
extentLen = expectedLength - ebp.length();
}
+ if (extent.isUnWritten()) {
+ // treat the range as sparse, which results in 00's, because the bytes on disk
+ // have not been initialized yet
+ providerOfs = -1;
+ }
+
ebp.addRange(providerOfs, extentLen);
}
}
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/NewExt4Analyzer.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/NewExt4Analyzer.java
index 1beed26cb5..adac2086f7 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/NewExt4Analyzer.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ext4/NewExt4Analyzer.java
@@ -16,7 +16,6 @@
package ghidra.file.formats.ext4;
import java.io.IOException;
-import java.util.List;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.plugin.core.analysis.AutoAnalysisManager;
@@ -335,67 +334,67 @@ public class NewExt4Analyzer extends FileFormatAnalyzer {
}
}
- private void processIBlock(Program program, BinaryReader reader, boolean isDirEntry2,
- Ext4IBlock i_block, TaskMonitor monitor) throws Exception {
-
- Ext4ExtentHeader header = i_block.getHeader();
- if (header.getEh_depth() == 0) {//leaf node....
- short numEntries = header.getEh_entries();
- List<Ext4Extent> entries = i_block.getExtentEntries();
- for (int i = 0; i < numEntries; i++) {
- Ext4Extent extent = entries.get(i);
- long offset = extent.getExtentStartBlockNumber() * blockSize;
- reader.setPointerIndex(offset);
- Address address = toAddr(program, offset);
- if (isDirEntry2) {
- while ((reader.getPointerIndex() - offset) < (extent.getEe_len() * blockSize)) {
- Ext4DirEntry2 dirEnt2 = Ext4DirEntry2.read(reader);
- DataType dataType = dirEnt2.toDataType();
- createData(program, address, dataType);
- String comment = "Name: " + dirEnt2.getName() + "\n";
- if (dirEnt2.getFile_type() == Ext4Constants.FILE_TYPE_REGULAR_FILE) {
- comment += "Type: REGULAR FILE" + "\n";
- }
- else if (dirEnt2.getFile_type() == Ext4Constants.FILE_TYPE_DIRECTORY) {
- comment += "Type: DIRECTORY" + "\n";
- }
- comment += "Type: INODE_0x" + Integer.toHexString(dirEnt2.getInode());
- //TODO: add link to iNode using {@program program_name.exe@symbol_name}
- //TODO: add parent path to comment
- setPlateComment(program, address, comment);
- address = address.add(dataType.getLength());
- }
- }
- else {
- throw new RuntimeException("TODO: support old style dir entry");
- }
- }
- }
- else {//interior node....
- short numEntries = header.getEh_entries();
- List<Ext4ExtentIdx> entries = i_block.getIndexEntries();
- for (int i = 0; i < numEntries; i++) {
- monitor.checkCancelled();
-
- Ext4ExtentIdx extentIndex = entries.get(i);
- long lo = extentIndex.getEi_leaf_lo();
- long hi = extentIndex.getEi_leaf_hi();
- long physicalBlockOfNextLevel = (hi << 32) | lo;
- long offset = physicalBlockOfNextLevel * blockSize;
-
- Address address = toAddr(program, offset);
- setPlateComment(program, address, "TODO Ext4ExtentIdx / ext4_fsblk_t ???");
-
- reader.setPointerIndex(offset);
-
- Ext4IBlock iBlock = new Ext4IBlock(reader, true);
- DataType dataType = iBlock.toDataType();
- createData(program, address, dataType);
-
- processIBlock(program, reader, isDirEntry2, iBlock, monitor);
- }
- }
- }
+// private void processIBlock(Program program, BinaryReader reader, boolean isDirEntry2,
+// Ext4IBlock i_block, TaskMonitor monitor) throws Exception {
+//
+// Ext4ExtentHeader header = i_block.getHeader();
+// if (header.getEh_depth() == 0) {//leaf node....
+// short numEntries = header.getEh_entries();
+// List<Ext4Extent> entries = i_block.getExtentEntries();
+// for (int i = 0; i < numEntries; i++) {
+// Ext4Extent extent = entries.get(i);
+// long offset = extent.getExtentStartBlockNumber() * blockSize;
+// reader.setPointerIndex(offset);
+// Address address = toAddr(program, offset);
+// if (isDirEntry2) {
+// while ((reader.getPointerIndex() - offset) < (extent.getEe_len() * blockSize)) {
+// Ext4DirEntry2 dirEnt2 = Ext4DirEntry2.read(reader);
+// DataType dataType = dirEnt2.toDataType();
+// createData(program, address, dataType);
+// String comment = "Name: " + dirEnt2.getName() + "\n";
+// if (dirEnt2.getFile_type() == Ext4Constants.FILE_TYPE_REGULAR_FILE) {
+// comment += "Type: REGULAR FILE" + "\n";
+// }
+// else if (dirEnt2.getFile_type() == Ext4Constants.FILE_TYPE_DIRECTORY) {
+// comment += "Type: DIRECTORY" + "\n";
+// }
+// comment += "Type: INODE_0x" + Integer.toHexString(dirEnt2.getInode());
+// //TODO: add link to iNode using {@program program_name.exe@symbol_name}
+// //TODO: add parent path to comment
+// setPlateComment(program, address, comment);
+// address = address.add(dataType.getLength());
+// }
+// }
+// else {
+// throw new RuntimeException("TODO: support old style dir entry");
+// }
+// }
+// }
+// else {//interior node....
+// short numEntries = header.getEh_entries();
+// List<Ext4ExtentIdx> entries = i_block.getIndexEntries();
+// for (int i = 0; i < numEntries; i++) {
+// monitor.checkCancelled();
+//
+// Ext4ExtentIdx extentIndex = entries.get(i);
+// long lo = extentIndex.getEi_leaf_lo();
+// long hi = extentIndex.getEi_leaf_hi();
+// long physicalBlockOfNextLevel = (hi << 32) | lo;
+// long offset = physicalBlockOfNextLevel * blockSize;
+//
+// Address address = toAddr(program, offset);
+// setPlateComment(program, address, "TODO Ext4ExtentIdx / ext4_fsblk_t ???");
+//
+// reader.setPointerIndex(offset);
+//
+// Ext4IBlock iBlock = new Ext4IBlock(reader, true);
+// DataType dataType = iBlock.toDataType();
+// createData(program, address, dataType);
+//
+// processIBlock(program, reader, isDirEntry2, iBlock, monitor);
+// }
+// }
+// }
private void processHashTreeDirectory(Program program, BinaryReader reader,
Ext4SuperBlock superBlock, Ext4Inode inode, TaskMonitor monitor) {