GT-3400 Fix divide by zero in Ext4FileSystem, charset also. #1342

Value read from disk was being used to calculate another value without
being validated first.

Also tweaked charset usage.

Fixes github issue #1342.
This commit is contained in:
dev747368 2019-12-16 12:06:14 -05:00
parent c75dcfd1f5
commit e5a59b9def
2 changed files with 10 additions and 3 deletions

View file

@ -16,6 +16,8 @@
package ghidra.file.formats.ext4; package ghidra.file.formats.ext4;
import java.io.*; import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import ghidra.app.util.bin.BinaryReader; import ghidra.app.util.bin.BinaryReader;
@ -29,6 +31,8 @@ import ghidra.util.task.TaskMonitor;
@FileSystemInfo(type = "ext4", description = "EXT4", factory = Ext4FileSystemFactory.class) @FileSystemInfo(type = "ext4", description = "EXT4", factory = Ext4FileSystemFactory.class)
public class Ext4FileSystem implements GFileSystem { public class Ext4FileSystem implements GFileSystem {
public static final Charset EXT4_DEFAULT_CHARSET = StandardCharsets.UTF_8;
private FileSystemIndexHelper<Ext4File> fsih; private FileSystemIndexHelper<Ext4File> fsih;
private FileSystemRefManager refManager = new FileSystemRefManager(this); private FileSystemRefManager refManager = new FileSystemRefManager(this);
private FSRLRoot fsrl; private FSRLRoot fsrl;
@ -57,6 +61,9 @@ public class Ext4FileSystem implements GFileSystem {
blockSize = (int) Math.pow(2, (10 + s_log_block_size)); blockSize = (int) Math.pow(2, (10 + s_log_block_size));
int groupSize = blockSize * superBlock.getS_blocks_per_group(); int groupSize = blockSize * superBlock.getS_blocks_per_group();
if (groupSize <= 0) {
throw new IOException("Invalid groupSize: " + groupSize);
}
int numGroups = (int) (provider.length() / groupSize); int numGroups = (int) (provider.length() / groupSize);
if (provider.length() % groupSize != 0) { if (provider.length() % groupSize != 0) {
numGroups++; numGroups++;

View file

@ -15,12 +15,12 @@
*/ */
package ghidra.file.formats.ext4; package ghidra.file.formats.ext4;
import java.io.IOException;
import ghidra.app.util.bin.*; import ghidra.app.util.bin.*;
import ghidra.program.model.data.*; import ghidra.program.model.data.*;
import ghidra.util.exception.DuplicateNameException; import ghidra.util.exception.DuplicateNameException;
import java.io.IOException;
public class Ext4SuperBlock implements StructConverter { public class Ext4SuperBlock implements StructConverter {
private int s_inodes_count; private int s_inodes_count;
@ -355,7 +355,7 @@ public class Ext4SuperBlock implements StructConverter {
while (i < s_volume_name.length && s_volume_name[i] != '\0') { while (i < s_volume_name.length && s_volume_name[i] != '\0') {
i++; i++;
} }
return new String(s_volume_name, 0, i); return new String(s_volume_name, 0, i, Ext4FileSystem.EXT4_DEFAULT_CHARSET);
} }
public byte[] getS_last_mounted() { public byte[] getS_last_mounted() {