From 5e0f4130c4e1e53b9f62bcace63bb11347483d7e Mon Sep 17 00:00:00 2001 From: Adubbz Date: Tue, 6 Aug 2019 21:29:28 +1000 Subject: [PATCH] Manually prevent placeholder/content appending --- stratosphere/ncm/source/ncm_contentstorage.cpp | 16 ++++++++++++++++ stratosphere/ncm/source/ncm_fs.cpp | 2 +- stratosphere/ncm/source/ncm_types.hpp | 14 ++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/stratosphere/ncm/source/ncm_contentstorage.cpp b/stratosphere/ncm/source/ncm_contentstorage.cpp index 96de686db..317ab6a68 100644 --- a/stratosphere/ncm/source/ncm_contentstorage.cpp +++ b/stratosphere/ncm/source/ncm_contentstorage.cpp @@ -157,6 +157,14 @@ namespace sts::ncm { auto file_guard = SCOPE_GUARD { fclose(f); }; + u64 size = 0; + R_TRY(GetSizeFromPlaceHolderId(&size, placeholder_id)); + + /* We can't disable append with stdio, so check this manually. */ + if (offset + data.num_elements > size) { + return ResultFileExtensionWithoutOpenModeAllowAppend; + } + if (fseek(f, offset, SEEK_SET) != 0) { return fsdevGetLastResult(); } @@ -585,6 +593,14 @@ namespace sts::ncm { fclose(f); }; + u64 size = 0; + R_TRY(GetSizeFromContentId(&size, content_id)); + + /* We can't disable append with stdio, so check this manually. */ + if (offset + data.num_elements > size) { + return ResultFileExtensionWithoutOpenModeAllowAppend; + } + if (fseek(f, offset, SEEK_SET) != 0) { return fsdevGetLastResult(); } diff --git a/stratosphere/ncm/source/ncm_fs.cpp b/stratosphere/ncm/source/ncm_fs.cpp index 80b3c37a8..4f675b3d3 100644 --- a/stratosphere/ncm/source/ncm_fs.cpp +++ b/stratosphere/ncm/source/ncm_fs.cpp @@ -301,7 +301,7 @@ namespace sts::ncm { /* We should be given a qualified path. */ if (!unqual_path || unqual_path > path + 0xf) { - return ResultFsUnqualifiedPath; + return ResultInvalidMountName; } strncpy(mount_name.name, path, unqual_path - path); diff --git a/stratosphere/ncm/source/ncm_types.hpp b/stratosphere/ncm/source/ncm_types.hpp index e7d649291..587e59404 100644 --- a/stratosphere/ncm/source/ncm_types.hpp +++ b/stratosphere/ncm/source/ncm_types.hpp @@ -152,11 +152,13 @@ namespace sts::ncm { typedef void (*MakePlaceHolderPathFunc)(char* out, PlaceHolderId placeholder_id, const char* root); // TODO: Move to libstrat - static constexpr Result ResultNcmStoragePathNotFound = MAKERESULT(Module_Ncm, 1); - static constexpr Result ResultNcmInvalidPlaceHolderDirectoryEntry = MAKERESULT(Module_Ncm, 170); - static constexpr Result ResultNcmInvalidContentStorageOperation = MAKERESULT(Module_Ncm, 190); - static constexpr Result ResultNcmStorageRootNotFound = MAKERESULT(Module_Ncm, 310); - static constexpr Result ResultFsUnqualifiedPath = MAKERESULT(Module_Fs, 6065); - static constexpr Result ResultFsMountNameNotFound = MAKERESULT(Module_Fs, 6905); + static constexpr Result ResultNcmStoragePathNotFound = MAKERESULT(Module_Ncm, 1); + static constexpr Result ResultNcmInvalidPlaceHolderDirectoryEntry = MAKERESULT(Module_Ncm, 170); + static constexpr Result ResultNcmInvalidContentStorageOperation = MAKERESULT(Module_Ncm, 190); + static constexpr Result ResultNcmStorageRootNotFound = MAKERESULT(Module_Ncm, 310); + + static constexpr Result ResultFileExtensionWithoutOpenModeAllowAppend = MAKERESULT(Module_Fs, 6201); + static constexpr Result ResultInvalidMountName = MAKERESULT(Module_Fs, 6065); + static constexpr Result ResultFsMountNameNotFound = MAKERESULT(Module_Fs, 6905); } \ No newline at end of file