From fd5bef0ef2dacc2365ca04c6315c1cb8664c533d Mon Sep 17 00:00:00 2001 From: Pawel Olejniczak Date: Tue, 27 Sep 2022 12:39:27 +0200 Subject: [PATCH] [CP-1536] Fix storage memory overflow The user was able to upload more files than there was free space. In addition, the difference between the declared free space through MTP and MC has been corrected. --- .../deviceInfo/DeviceInfoEndpointCommon.cpp | 2 +- .../endpoints/filesystem/FS_Helper.cpp | 26 +++++++++++++++++++ .../endpoints/include/endpoints/HttpEnums.hpp | 5 ++-- .../endpoints/filesystem/FS_Helper.hpp | 4 ++- .../paths/include/purefs/filesystem_paths.hpp | 2 ++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp index bba85eb6f6888a9154279af5753b9683d2570c38..c0f90df2da48e30a2136ab5dc68dae303b662fef 100644 --- a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp +++ b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp @@ -169,6 +169,6 @@ namespace sdesktop::endpoints totalDeviceSpaceMiB += totalSpace; } - return {totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB}; + return {totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB + purefs::userSpaceOffset}; } } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp index 2bab0c03c357a5d2b923c6a919f52384a8958700..e6ccb52d82755a8bdd5326f1aebbaf84e2d40aa8 100644 --- a/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp +++ b/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace sdesktop::endpoints @@ -208,6 +209,15 @@ namespace sdesktop::endpoints return ResponseContext{.status = code}; } + auto freeSpaceLeftForUserFilesMiB = getFreeSpaceForUserFilesMiB(); + constexpr auto bytesInMebibyte = 1048576; + + if (fileSize / bytesInMebibyte > freeSpaceLeftForUserFilesMiB) { + LOG_ERROR("Not enough space left on device!"); + code = http::Code::InsufficientStorage; + return ResponseContext{.status = code}; + } + if (!std::filesystem::exists(filePath)) { LOG_DEBUG("Creating file %s", filePath.c_str()); @@ -331,4 +341,20 @@ namespace sdesktop::endpoints json11::Json::object response({{directory, jsonArr}}); return ResponseContext{.status = http::Code::OK, .body = response}; } + + auto FS_Helper::getFreeSpaceForUserFilesMiB() const -> unsigned long + { + const auto userDiskPath = purefs::dir::getUserDiskPath(); + + std::unique_ptr vfstat = std::make_unique(); + if (statvfs(userDiskPath.c_str(), vfstat.get()) < 0) { + return 0; + } + + unsigned long freeMbytes = + (static_cast(vfstat->f_bfree) * static_cast(vfstat->f_bsize)) / + (1024LLU * 1024LLU) - + purefs::userSpaceOffset; + return freeMbytes; + } } // namespace sdesktop::endpoints diff --git a/module-services/service-desktop/endpoints/include/endpoints/HttpEnums.hpp b/module-services/service-desktop/endpoints/include/endpoints/HttpEnums.hpp index 8059a1791608cd1e716e807d640c7dd43174301b..8563b3cad6702db761f6185ac9008a54bbe9ed1b 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/HttpEnums.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/HttpEnums.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -23,7 +23,8 @@ namespace sdesktop::endpoints::http Conflict = 409, UnprocessableEntity = 422, InternalServerError = 500, - NotImplemented = 501 + NotImplemented = 501, + InsufficientStorage = 507 }; /*! Enum class for the HTTP methods. diff --git a/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp b/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp index 7b7cc45873a5f840efceebb3806ce0aeade0568f..226d464a27c49645f3c4f95698a9a02363736dc7 100644 --- a/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp +++ b/module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -41,6 +41,8 @@ namespace sdesktop::endpoints auto startGetFile(Context &context) const -> ResponseContext; auto getFileChunk(Context &context) const -> ResponseContext; + auto getFreeSpaceForUserFilesMiB() const -> unsigned long; + auto startSendFile(Context &context) const -> ResponseContext; auto sendFileChunk(Context &context) const -> ResponseContext; diff --git a/module-vfs/paths/include/purefs/filesystem_paths.hpp b/module-vfs/paths/include/purefs/filesystem_paths.hpp index 23cf80392a5555eeeb7aea4c7724ab2b1f67cca1..52cf2223f6c659335267c48a44672b749e387aee 100644 --- a/module-vfs/paths/include/purefs/filesystem_paths.hpp +++ b/module-vfs/paths/include/purefs/filesystem_paths.hpp @@ -8,6 +8,8 @@ namespace purefs { std::filesystem::path createPath(const std::string &parent, const std::string &child) noexcept; + constexpr inline auto userSpaceOffset = 1023; // This space is additionally reserved on the user partition, + // and it's excluded from the space that user can use for his files namespace dir {