From a38d94fee3c18d5dc709c3d9a5b9b62d6c797eee Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 5 Feb 2023 22:43:08 -0700 Subject: [PATCH] Address some analyzer findings --- src/LibHac/Boot/Package1.cs | 4 +- src/LibHac/Common/Utilities.cs | 6 +-- src/LibHac/FsSrv/FileSystemServer.cs | 1 - src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs | 15 +++--- .../FsSystem/Buffers/FileSystemBuddyHeap.cs | 2 +- src/LibHac/FsSystem/NcaStructs.cs | 47 ------------------- src/LibHac/Tools/Es/Ticket.cs | 2 +- src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs | 2 +- .../Tools/FsSystem/NcaUtils/NcaExtensions.cs | 1 - .../Tools/FsSystem/NcaUtils/NcaHeader.cs | 1 - .../Tools/FsSystem/NcaUtils/NcaStructs.cs | 46 ++++++++++++++++++ .../FsSystem/PartitionFileSystemBuilder.cs | 4 +- .../Tools/FsSystem/Save/SaveDataFileSystem.cs | 8 +++- src/LibHac/Tools/FsSystem/StreamStorage.cs | 4 +- src/hactoolnet/EnumStrings.cs | 2 +- src/hactoolnet/ProcessAppFs.cs | 1 - src/hactoolnet/ProcessSwitchFs.cs | 1 + 17 files changed, 75 insertions(+), 72 deletions(-) delete mode 100644 src/LibHac/FsSystem/NcaStructs.cs diff --git a/src/LibHac/Boot/Package1.cs b/src/LibHac/Boot/Package1.cs index 3dab974e..d75dc763 100644 --- a/src/LibHac/Boot/Package1.cs +++ b/src/LibHac/Boot/Package1.cs @@ -229,7 +229,7 @@ public class Package1 } // If encrypted, check if the body can be decrypted - Crypto.Aes.DecryptCbc128(metaData2, metaData2, KeySet.MarikoBek, _metaData.Iv); + Aes.DecryptCbc128(metaData2, metaData2, KeySet.MarikoBek, _metaData.Iv); IsDecrypted = metaData2.SequenceEqual(SpanHelpers.AsByteSpan(ref _metaData)); // Get a decrypted body storage if we have the correct key @@ -344,7 +344,7 @@ public class Package1 int start = IsModern ? 6 : 0; int end = IsModern ? 0x20 : 6; - Decryptor decryptor = IsModern ? Crypto.Aes.DecryptCbc128 : Crypto.Aes.DecryptCtr128; + Decryptor decryptor = IsModern ? Aes.DecryptCbc128 : Aes.DecryptCtr128; for (int i = start; i < end; i++) { diff --git a/src/LibHac/Common/Utilities.cs b/src/LibHac/Common/Utilities.cs index e2116d7a..65b08210 100644 --- a/src/LibHac/Common/Utilities.cs +++ b/src/LibHac/Common/Utilities.cs @@ -161,15 +161,15 @@ public static class Utilities return text; } - public static void WriteUTF8(this BinaryWriter writer, string value) + public static void WriteUtf8(this BinaryWriter writer, string value) { byte[] text = Encoding.UTF8.GetBytes(value); writer.Write(text); } - public static void WriteUTF8Z(this BinaryWriter writer, string value) + public static void WriteUtf8Z(this BinaryWriter writer, string value) { - writer.WriteUTF8(value); + writer.WriteUtf8(value); writer.Write((byte)0); } diff --git a/src/LibHac/FsSrv/FileSystemServer.cs b/src/LibHac/FsSrv/FileSystemServer.cs index 4eaeaec9..b2080640 100644 --- a/src/LibHac/FsSrv/FileSystemServer.cs +++ b/src/LibHac/FsSrv/FileSystemServer.cs @@ -26,7 +26,6 @@ public class FileSystemServer : IDisposable public void Dispose() { Globals.Dispose(); - Globals = default; } } diff --git a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs index 7f588217..d239daac 100644 --- a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs @@ -15,6 +15,7 @@ using LibHac.Tools.FsSystem.NcaUtils; using LibHac.Util; using static LibHac.Fs.Impl.CommonMountNames; using NcaFsHeader = LibHac.Tools.FsSystem.NcaUtils.NcaFsHeader; +using NcaHeader = LibHac.FsSystem.NcaHeader; using RightsId = LibHac.Fs.RightsId; using Utility = LibHac.FsSystem.Utility; @@ -698,7 +699,7 @@ public class NcaFileSystemServiceImpl { UnsafeHelpers.SkipParamInit(out fsType); - NcaContentType contentType = nca.Header.ContentType; + NcaHeader.ContentType contentType = (NcaHeader.ContentType)nca.Header.ContentType; switch (fsProxyType) { @@ -706,31 +707,31 @@ public class NcaFileSystemServiceImpl case FileSystemProxyType.Rom: case FileSystemProxyType.Logo: case FileSystemProxyType.RegisteredUpdate: - if (contentType != NcaContentType.Program) + if (contentType != NcaHeader.ContentType.Program) return ResultFs.PreconditionViolation.Log(); break; case FileSystemProxyType.Control: - if (contentType != NcaContentType.Control) + if (contentType != NcaHeader.ContentType.Control) return ResultFs.PreconditionViolation.Log(); break; case FileSystemProxyType.Manual: - if (contentType != NcaContentType.Manual) + if (contentType != NcaHeader.ContentType.Manual) return ResultFs.PreconditionViolation.Log(); break; case FileSystemProxyType.Meta: - if (contentType != NcaContentType.Meta) + if (contentType != NcaHeader.ContentType.Meta) return ResultFs.PreconditionViolation.Log(); break; case FileSystemProxyType.Data: - if (contentType != NcaContentType.Data && contentType != NcaContentType.PublicData) + if (contentType != NcaHeader.ContentType.Data && contentType != NcaHeader.ContentType.PublicData) return ResultFs.PreconditionViolation.Log(); - if (contentType == NcaContentType.Data && !canMountSystemDataPrivate) + if (contentType == NcaHeader.ContentType.Data && !canMountSystemDataPrivate) return ResultFs.PermissionDenied.Log(); break; diff --git a/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs b/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs index 64d620d1..8b294a10 100644 --- a/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs +++ b/src/LibHac/FsSystem/Buffers/FileSystemBuddyHeap.cs @@ -239,7 +239,7 @@ public unsafe class FileSystemBuddyHeap : IDisposable // Determine page sizes nuint maxPageSize = BlockSize << OrderMax; - nuint maxPageCount = (nuint)Alignment.AlignUp(HeapSize, (uint)maxPageSize) / maxPageSize; + nuint maxPageCount = Alignment.AlignUp(HeapSize, (uint)maxPageSize) / maxPageSize; Assert.SdkGreater(maxPageCount, nuint.Zero); // Setup the free lists diff --git a/src/LibHac/FsSystem/NcaStructs.cs b/src/LibHac/FsSystem/NcaStructs.cs deleted file mode 100644 index 206295ee..00000000 --- a/src/LibHac/FsSystem/NcaStructs.cs +++ /dev/null @@ -1,47 +0,0 @@ -namespace LibHac.FsSystem; - -public enum NcaSectionType -{ - Code, - Data, - Logo -} - -public enum NcaContentType -{ - Program, - Meta, - Control, - Manual, - Data, - PublicData -} - -public enum DistributionType -{ - Download, - GameCard -} - -public enum NcaEncryptionType -{ - Auto, - None, - XTS, - AesCtr, - AesCtrEx -} - -public enum NcaHashType -{ - Auto, - None, - Sha256, - Ivfc -} - -public enum NcaFormatType -{ - Romfs, - Pfs0 -} \ No newline at end of file diff --git a/src/LibHac/Tools/Es/Ticket.cs b/src/LibHac/Tools/Es/Ticket.cs index 37552769..0dfe13fd 100644 --- a/src/LibHac/Tools/Es/Ticket.cs +++ b/src/LibHac/Tools/Es/Ticket.cs @@ -126,7 +126,7 @@ public class Ticket } stream.Position = bodyStart; - if (Issuer != null) writer.WriteUTF8(Issuer); + if (Issuer != null) writer.WriteUtf8(Issuer); stream.Position = bodyStart + 0x40; if (TitleKeyBlock?.Length <= 0x100) writer.Write(TitleKeyBlock); stream.Position = bodyStart + 0x140; diff --git a/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs b/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs index b4765e8d..3bfd7dff 100644 --- a/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs +++ b/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs @@ -229,7 +229,7 @@ public class Nca { case NcaEncryptionType.None: return baseStorage; - case NcaEncryptionType.XTS: + case NcaEncryptionType.AesXts: return OpenAesXtsStorage(baseStorage, index, decrypting); case NcaEncryptionType.AesCtr: return OpenAesCtrStorage(baseStorage, index, Header.GetSectionStartOffset(index), header.Counter); diff --git a/src/LibHac/Tools/FsSystem/NcaUtils/NcaExtensions.cs b/src/LibHac/Tools/FsSystem/NcaUtils/NcaExtensions.cs index b85e66ee..072ba52e 100644 --- a/src/LibHac/Tools/FsSystem/NcaUtils/NcaExtensions.cs +++ b/src/LibHac/Tools/FsSystem/NcaUtils/NcaExtensions.cs @@ -5,7 +5,6 @@ using LibHac.Common; using LibHac.Crypto; using LibHac.Fs; using LibHac.Fs.Fsa; -using LibHac.FsSystem; namespace LibHac.Tools.FsSystem.NcaUtils; diff --git a/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs b/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs index 5c93eac3..8dc466d7 100644 --- a/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs +++ b/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs @@ -7,7 +7,6 @@ using LibHac.Common.Keys; using LibHac.Crypto; using LibHac.Diag; using LibHac.Fs; -using LibHac.FsSystem; using LibHac.Tools.Crypto; using LibHac.Util; diff --git a/src/LibHac/Tools/FsSystem/NcaUtils/NcaStructs.cs b/src/LibHac/Tools/FsSystem/NcaUtils/NcaStructs.cs index 429f9d11..083bf97a 100644 --- a/src/LibHac/Tools/FsSystem/NcaUtils/NcaStructs.cs +++ b/src/LibHac/Tools/FsSystem/NcaUtils/NcaStructs.cs @@ -32,4 +32,50 @@ public class TitleVersion { return $"{Major}.{Minor}.{Patch}.{Revision}"; } +} + +public enum NcaSectionType +{ + Code, + Data, + Logo +} + +public enum NcaContentType +{ + Program, + Meta, + Control, + Manual, + Data, + PublicData +} + +public enum DistributionType +{ + Download, + GameCard +} + +public enum NcaEncryptionType +{ + Auto, + None, + AesXts, + AesCtr, + AesCtrEx +} + +public enum NcaHashType +{ + Auto, + None, + Sha256, + Ivfc +} + +public enum NcaFormatType +{ + Romfs, + Pfs0 } \ No newline at end of file diff --git a/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs b/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs index e7f25c37..60986ee0 100644 --- a/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs +++ b/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs @@ -82,7 +82,7 @@ public class PartitionFileSystemBuilder byte[] metaData = new byte[metaDataSize]; var writer = new BinaryWriter(new MemoryStream(metaData)); - writer.WriteUTF8(GetMagicValue(type)); + writer.WriteUtf8(GetMagicValue(type)); writer.Write(Entries.Count); writer.Write(stringTableSize); writer.Write(0); @@ -111,7 +111,7 @@ public class PartitionFileSystemBuilder foreach (Entry entry in Entries) { - writer.WriteUTF8Z(entry.Name); + writer.WriteUtf8Z(entry.Name); } return metaData; diff --git a/src/LibHac/Tools/FsSystem/Save/SaveDataFileSystem.cs b/src/LibHac/Tools/FsSystem/Save/SaveDataFileSystem.cs index 4d8c50ee..62c1829e 100644 --- a/src/LibHac/Tools/FsSystem/Save/SaveDataFileSystem.cs +++ b/src/LibHac/Tools/FsSystem/Save/SaveDataFileSystem.cs @@ -264,7 +264,9 @@ public class SaveDataFileSystem : IFileSystem byte[] hashData = new byte[0x3d00]; headerStream.Position = 0x300; - headerStream.Read(hashData, 0, hashData.Length); + int bytesRead = headerStream.Read(hashData, 0, hashData.Length); + if (bytesRead != hashData.Length) + return ResultFs.OutOfRange.Log(); byte[] hash = new byte[Sha256.DigestSize]; Sha256.GenerateSha256Hash(hashData, hash); @@ -278,7 +280,9 @@ public class SaveDataFileSystem : IFileSystem byte[] cmac = new byte[0x10]; headerStream.Position = 0x100; - headerStream.Read(cmacData, 0, 0x200); + bytesRead = headerStream.Read(cmacData, 0, cmacData.Length); + if (bytesRead != cmacData.Length) + return ResultFs.OutOfRange.Log(); Aes.CalculateCmac(cmac, cmacData, keySet.DeviceUniqueSaveMacKeys[0]); diff --git a/src/LibHac/Tools/FsSystem/StreamStorage.cs b/src/LibHac/Tools/FsSystem/StreamStorage.cs index ae71b992..88cad095 100644 --- a/src/LibHac/Tools/FsSystem/StreamStorage.cs +++ b/src/LibHac/Tools/FsSystem/StreamStorage.cs @@ -29,7 +29,9 @@ public class StreamStorage : IStorage BaseStream.Position = offset; } - BaseStream.Read(destination); + int bytesRead = BaseStream.Read(destination); + if (bytesRead != destination.Length) + return ResultFs.OutOfRange.Log(); } return Result.Success; diff --git a/src/hactoolnet/EnumStrings.cs b/src/hactoolnet/EnumStrings.cs index 4d70ee0a..14e62a90 100644 --- a/src/hactoolnet/EnumStrings.cs +++ b/src/hactoolnet/EnumStrings.cs @@ -1,8 +1,8 @@ using LibHac.Common; using LibHac.Fs; -using LibHac.FsSystem; using LibHac.Ncm; using LibHac.Tools.Fs; +using LibHac.Tools.FsSystem.NcaUtils; using ContentType = LibHac.Ncm.ContentType; namespace hactoolnet; diff --git a/src/hactoolnet/ProcessAppFs.cs b/src/hactoolnet/ProcessAppFs.cs index 9976c17e..5da99077 100644 --- a/src/hactoolnet/ProcessAppFs.cs +++ b/src/hactoolnet/ProcessAppFs.cs @@ -2,7 +2,6 @@ using LibHac.Common; using LibHac.Fs; using LibHac.Fs.Fsa; -using LibHac.FsSystem; using LibHac.Ncm; using LibHac.Spl; using LibHac.Tools.Es; diff --git a/src/hactoolnet/ProcessSwitchFs.cs b/src/hactoolnet/ProcessSwitchFs.cs index 27a751a8..4a2aa3a8 100644 --- a/src/hactoolnet/ProcessSwitchFs.cs +++ b/src/hactoolnet/ProcessSwitchFs.cs @@ -11,6 +11,7 @@ using LibHac.FsSystem; using LibHac.Ns; using LibHac.Tools.Fs; using LibHac.Tools.FsSystem; +using LibHac.Tools.FsSystem.NcaUtils; using LibHac.Tools.FsSystem.Save; using Path = System.IO.Path;