From 7237a15098aa7985bae8fa2e0fb29ffec85114ba Mon Sep 17 00:00:00 2001 From: jakcron Date: Fri, 11 May 2018 23:47:31 +0800 Subject: [PATCH] [nstool] Added PfsProcess struct check before using struct. --- programs/nstool/source/PfsProcess.cpp | 9 +++++++++ programs/nstool/source/PfsProcess.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/programs/nstool/source/PfsProcess.cpp b/programs/nstool/source/PfsProcess.cpp index fa04bd3..452f38a 100644 --- a/programs/nstool/source/PfsProcess.cpp +++ b/programs/nstool/source/PfsProcess.cpp @@ -42,6 +42,11 @@ size_t PfsProcess::determineHeaderSize(const nx::sPfsHeader* hdr) return sizeof(nx::sPfsHeader) + hdr->file_num.get() * fileEntrySize + hdr->name_table_size.get(); } +bool PfsProcess::validateHeaderMagic(const nx::sPfsHeader* hdr) +{ + return std::string(hdr->signature, 4) == nx::pfs::kPfsSig || std::string(hdr->signature, 4) == nx::pfs::kHashedPfsSig; +} + void PfsProcess::validateHfs() { fnd::MemoryBlob scratch; @@ -122,6 +127,10 @@ void PfsProcess::process() // open minimum header to get full header size scratch.alloc(sizeof(nx::sPfsHeader)); mReader->read(scratch.getBytes(), mOffset, scratch.getSize()); + if (validateHeaderMagic(((nx::sPfsHeader*)scratch.getBytes())) == false) + { + throw fnd::Exception(kModuleName, "Corrupt Header"); + } size_t pfsHeaderSize = determineHeaderSize(((nx::sPfsHeader*)scratch.getBytes())); // open minimum header to get full header size diff --git a/programs/nstool/source/PfsProcess.h b/programs/nstool/source/PfsProcess.h index bf062c0..d55324a 100644 --- a/programs/nstool/source/PfsProcess.h +++ b/programs/nstool/source/PfsProcess.h @@ -48,6 +48,8 @@ private: void displayHeader(); void displayFs(); size_t determineHeaderSize(const nx::sPfsHeader* hdr); + bool validateHeaderMagic(const nx::sPfsHeader* hdr); void validateHfs(); void extractFs(); + }; \ No newline at end of file