From c5584599c06e023898c9650ea40b6b51e01a5304 Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Thu, 9 Mar 2023 11:09:01 -0500 Subject: [PATCH] GP-3054: Preventing relative file paths from ending up in an FSRL --- .../ghidra/app/util/headless/AnalyzeHeadless.java | 8 ++++---- .../formats/gfilesystem/LocalFileSystem.java | 12 ++++++++---- .../ghidra/program/flatapi/FlatProgramAPI.java | 15 ++++++--------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/AnalyzeHeadless.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/AnalyzeHeadless.java index 3ed273a01f..5b44ba640a 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/AnalyzeHeadless.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/AnalyzeHeadless.java @@ -212,10 +212,10 @@ public class AnalyzeHeadless implements GhidraLaunchable { options.setPropertiesFileDirectories(args[++argi]); } else if (checkArgument("-import", args, argi)) { - File inputFile = new File(args[++argi]); + File inputFile = new File(args[++argi]).getAbsoluteFile(); if (!inputFile.isDirectory() && !inputFile.isFile()) { throw new InvalidInputException( - inputFile.getAbsolutePath() + " is not a valid directory or file."); + inputFile + " is not a valid directory or file."); } HeadlessAnalyzer.checkValidFilename(inputFile); @@ -234,10 +234,10 @@ public class AnalyzeHeadless implements GhidraLaunchable { break; } - File otherFile = new File(nextArg); + File otherFile = new File(nextArg).getAbsoluteFile(); if (!otherFile.isFile() && !otherFile.isDirectory()) { throw new InvalidInputException( - otherFile.getAbsolutePath() + " is not a valid directory or file."); + otherFile + " is not a valid directory or file."); } HeadlessAnalyzer.checkValidFilename(otherFile); diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/LocalFileSystem.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/LocalFileSystem.java index 253e4f3e0c..81878c5471 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/LocalFileSystem.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/LocalFileSystem.java @@ -115,13 +115,17 @@ public class LocalFileSystem implements GFileSystem, GFileHashProvider { /** * Converts a {@link File} into a {@link FSRL}. + *

+ * NOTE: The given {@link File}'s absolute path will be used. * - * @param f {@link File} - * @return {@link FSRL} + * @param f The {@link File} to convert to an {@link FSRL} + * @return The {@link FSRL} */ public FSRL getLocalFSRL(File f) { - return fsFSRL - .withPath(FSUtilities.appendPath("/", FilenameUtils.separatorsToUnix(f.getPath()))); + // We prepend a "/" to ensure that Windows-style paths (i.e. C:\) start with a "/". For + // unix-style paths, this redundant "/" will be dropped. + return fsFSRL.withPath( + FSUtilities.appendPath("/", FilenameUtils.separatorsToUnix(f.getAbsolutePath()))); } @Override diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java b/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java index b5d5f5cc96..0a507be8ea 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java @@ -43,7 +43,6 @@ import ghidra.program.model.symbol.*; import ghidra.program.model.util.CodeUnitInsertionException; import ghidra.program.util.AddressEvaluator; import ghidra.program.util.string.*; -import ghidra.util.Conv; import ghidra.util.ascii.AsciiCharSetRecognizer; import ghidra.util.datastruct.Accumulator; import ghidra.util.datastruct.ListAccumulator; @@ -154,16 +153,14 @@ public class FlatProgramAPI { } /** - * Returns the path to the program's executable file. + * Returns the {@link File} that the program was originally imported from. It does not + * necessarily still exist on the file system. + *

* For example, c:\temp\test.exe. - * @return path to program's executable file + * @return the {@link File} that the program was originally imported from */ public final File getProgramFile() { - File f = new File(currentProgram.getExecutablePath()); - if (f.exists()) { - return f; - } - return null; + return new File(currentProgram.getExecutablePath()); } /** @@ -1915,7 +1912,7 @@ public class FlatProgramAPI { * @return a new address with the specified offset in the default address space */ public final Address toAddr(int offset) { - return toAddr(offset & Conv.INT_MASK); + return toAddr(Integer.toUnsignedLong(offset)); } /**