~aleteoryx/muditaos

fd5bef0ef2dacc2365ca04c6315c1cb8664c533d — Pawel Olejniczak 3 years ago f777101 pure_1.4.0-rc.1
[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.
M module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp => module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpointCommon.cpp +1 -1
@@ 169,6 169,6 @@ namespace sdesktop::endpoints
            totalDeviceSpaceMiB += totalSpace;
        }

        return {totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB};
        return {totalDeviceSpaceMiB, reservedSystemSpaceMiB, usedUserSpaceMiB + purefs::userSpaceOffset};
    }
} // namespace sdesktop::endpoints

M module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp => module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp +26 -0
@@ 9,6 9,7 @@
#include <endpoints/message/Sender.hpp>
#include <purefs/filesystem_paths.hpp>

#include <sys/statvfs.h>
#include <filesystem>

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<struct statvfs> vfstat = std::make_unique<struct statvfs>();
        if (statvfs(userDiskPath.c_str(), vfstat.get()) < 0) {
            return 0;
        }

        unsigned long freeMbytes =
            (static_cast<std::uint64_t>(vfstat->f_bfree) * static_cast<std::uint64_t>(vfstat->f_bsize)) /
                (1024LLU * 1024LLU) -
            purefs::userSpaceOffset;
        return freeMbytes;
    }
} // namespace sdesktop::endpoints

M module-services/service-desktop/endpoints/include/endpoints/HttpEnums.hpp => module-services/service-desktop/endpoints/include/endpoints/HttpEnums.hpp +3 -2
@@ 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.

M module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp => module-services/service-desktop/endpoints/include/endpoints/filesystem/FS_Helper.hpp +3 -1
@@ 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;


M module-vfs/paths/include/purefs/filesystem_paths.hpp => module-vfs/paths/include/purefs/filesystem_paths.hpp +2 -0
@@ 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
    {