diff --git a/DesktopEditor/common/File.cpp b/DesktopEditor/common/File.cpp index 176b97b53f..c05f2a65af 100644 --- a/DesktopEditor/common/File.cpp +++ b/DesktopEditor/common/File.cpp @@ -1591,25 +1591,31 @@ namespace NSFile std::wstring CFileBinary::CreateTempFileWithUniqueName(const std::wstring& strFolderPathRoot, const std::wstring& Prefix) { #if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) - wchar_t pBuffer[MAX_PATH + 1]; - ::GetTempFileNameW(strFolderPathRoot.c_str(), Prefix.c_str(), 0, pBuffer); - std::wstring sRet(pBuffer); - return sRet; + wchar_t pBuffer[MAX_PATH + 1] = {0}; + if (0 == ::GetTempFileNameW(strFolderPathRoot.c_str(), Prefix.c_str(), 0, pBuffer)) + return std::wstring(); + return std::wstring(pBuffer); #else - char pcRes[MAX_PATH]; - BYTE* pData = (BYTE*)pcRes; - std::wstring sPrefix = strFolderPathRoot + L"/" + Prefix + L"_XXXXXX"; + LONG lLen = 0; - NSFile::CUtf8Converter::GetUtf8StringFromUnicode(sPrefix.c_str(), (LONG)sPrefix.length(), pData, lLen); - pcRes[lLen] = '\0'; + BYTE* pcRes = NULL; + NSFile::CUtf8Converter::GetUtf8StringFromUnicode(sPrefix.c_str(), (LONG)sPrefix.length(), pcRes, lLen); - int res = mkstemp(pcRes); + if (lLen <= 0) + { + delete[] pcRes; + return std::wstring(); + } + + int res = mkstemp((char*)pcRes); if (-1 != res) close(res); - std::string sRes = pcRes; - return NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sRes.c_str(), sRes.length()); + std::wstring sResult = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)pcRes, (LONG)lLen); + + delete[] pcRes; + return sResult; #endif } bool CFileBinary::OpenTempFile(std::wstring *pwsName, FILE **ppFile, wchar_t *wsMode, wchar_t *wsExt, wchar_t *wsFolder, wchar_t* /*wsName*/) diff --git a/MsBinaryFile/XlsFile/Converter/XlsConverter.cpp b/MsBinaryFile/XlsFile/Converter/XlsConverter.cpp index 722c515a4f..71ce26fbcb 100644 --- a/MsBinaryFile/XlsFile/Converter/XlsConverter.cpp +++ b/MsBinaryFile/XlsFile/Converter/XlsConverter.cpp @@ -2522,7 +2522,7 @@ void XlsConverter::convert(XLS::Obj * obj) std::wstring objectId_bin = xlsx_context->get_mediaitems().add_control_activeX(target_bin); NSFile::CFileBinary file; - if(xls_global_info->controls_data.second >= obj->pictFmla.lPosInCtlStm + obj->pictFmla.cbBufInCtlStm) + if (obj->pictFmla.lPosInCtlStm <= xls_global_info->controls_data.second && obj->pictFmla.cbBufInCtlStm <= xls_global_info->controls_data.second - obj->pictFmla.lPosInCtlStm) { if ( file.CreateFileW(xlsx_context->get_mediaitems().activeX_path() + target_bin) ) { diff --git a/OFDFile/OFDFile.pro b/OFDFile/OFDFile.pro index 56ef2fa41e..476d3602f6 100644 --- a/OFDFile/OFDFile.pro +++ b/OFDFile/OFDFile.pro @@ -49,7 +49,8 @@ PWD_ROOT_DIR = $$PWD include($$CORE_ROOT_DIR/Common/base.pri) include($$CORE_ROOT_DIR/Common/3dParty/boost/boost.pri) -DEFINES += OFD_USE_DYNAMIC_LIBRARY +DEFINES += OFD_USE_DYNAMIC_LIBRARY \ + CRYPTOPP_DISABLE_ASM ADD_DEPENDENCY(graphics, kernel, UnicodeConverter, PdfFile) diff --git a/OOXML/Base/Rand.h b/OOXML/Base/Rand.h new file mode 100644 index 0000000000..eadca684e9 --- /dev/null +++ b/OOXML/Base/Rand.h @@ -0,0 +1,200 @@ +/* + * Copyright (C) Ascensio System SIA, 2009-2026 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation, together with the + * additional terms provided in the LICENSE file. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA by email at info@onlyoffice.com + * or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga, + * LV-1050, Latvia, European Union. + * + * The interactive user interfaces in modified versions of the Program + * are required to display Appropriate Legal Notices in accordance with + * Section 5 of the GNU AGPL version 3. + * + * No trademark rights are granted under this License. + * + * All non-code elements of the Product, including illustrations, + * icon sets, and technical writing content, are licensed under the + * Creative Commons Attribution-ShareAlike 4.0 International License: + * https://creativecommons.org/licenses/by-sa/4.0/legalcode + * + * This license applies only to such non-code elements and does not + * modify or replace the licensing terms applicable to the Program's + * source code, which remains licensed under the GNU Affero General + * Public License v3. + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#ifndef SECURE_RANDOM_BYTES_H +#define SECURE_RANDOM_BYTES_H + +#include +#include +#include +#include +#include + +#if defined(_WIN32) || defined(_WIN64) +#include + +#if defined(WIN_XP_OR_VISTA) + +// RtlGenRandom (a.k.a. SystemFunction036 in advapi32.dll). +// Available since Windows XP, cryptographically secure, +// does not open key containers — stable under load. +extern "C" BOOLEAN NTAPI SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength); +#pragma comment(lib, "advapi32.lib") + +#else + +#include +#pragma comment(lib, "bcrypt.lib") +#endif + +#elif defined(_MAC) + +#include + +#elif defined(_LINUX) + +#include +#include +#include + +// Fallback syscall numbers for systems where SYS_getrandom is not in headers. +// getrandom() syscall added in Linux 3.17 (October 2014). + +#ifndef SYS_getrandom + +#if defined(__x86_64__) +#define SYS_getrandom 318 +#elif defined(__i386__) +#define SYS_getrandom 355 +#elif defined(__aarch64__) +#define SYS_getrandom 278 +#elif defined(__arm__) +#define SYS_getrandom 384 +#elif defined(__powerpc64__) +#define SYS_getrandom 359 +#elif defined(__riscv) +#define SYS_getrandom 278 +#endif + +#endif + +#endif + + +// Fills `buf` with `len` cryptographically secure random bytes. +// Returns true on success, false if every available source failed. +static bool SecureRandomBytes(unsigned char* buf, size_t len) +{ + if (len == 0) + return true; + if (buf == nullptr) + return false; + + size_t filled = 0; + +#if defined(_WIN32) || defined(_WIN64) + +#if defined(WIN_XP_OR_VISTA) + while (filled < len) + { + ULONG chunk = (len - filled > 0xFFFFFF00u) ? 0xFFFFFF00u : static_cast(len - filled); + if (!::SystemFunction036(buf + filled, chunk)) + break; + filled += chunk; + } + if (filled == len) + return true; +#else + while (filled < len) + { + ULONG chunk = (len - filled > 0xFFFFFF00u) ? 0xFFFFFF00u : static_cast(len - filled); + NTSTATUS status = ::BCryptGenRandom(nullptr, buf + filled, chunk, BCRYPT_USE_SYSTEM_PREFERRED_RNG); + if (status != 0) // STATUS_SUCCESS + break; + filled += chunk; + } + if (filled == len) + return true; +#endif + +#elif defined(_MAC) + if (::SecRandomCopyBytes(kSecRandomDefault, len, buf) == errSecSuccess) + return true; + +#elif defined(__ANDROID__) || defined(__linux__) + // ===== Linux / Android ===== + // Direct syscall — does not depend on glibc version or headers. + // On kernels older than 3.17 syscall returns ENOSYS and we fall through. +#ifdef SYS_getrandom + while (filled < len) { + size_t chunk = (len - filled > 256) ? 256 : (len - filled); + long ret = ::syscall(SYS_getrandom, buf + filled, chunk, 0); + if (ret < 0) { + if (errno == EINTR) continue; + break; // ENOSYS or other error — fall through to /dev/urandom + } + filled += static_cast(ret); + } + if (filled == len) return true; +#endif +#endif + + // POSIX fallback: /dev/urandom +#if !defined(_WIN32) && !defined(_WIN64) + { + FILE* f = std::fopen("/dev/urandom", "rb"); + if (f) { + while (filled < len) { + size_t n = std::fread(buf + filled, 1, len - filled, f); + if (n == 0) break; + filled += n; + } + std::fclose(f); + if (filled == len) return true; + } + } +#endif + + // Final fallback: std::random_device + try + { + static std::mutex m; + static std::random_device rd; + std::lock_guard lock(m); + while (filled < len) + { + unsigned int r = rd(); + size_t to_copy = (len - filled < sizeof(r)) ? (len - filled) : sizeof(r); + std::memcpy(buf + filled, &r, to_copy); + filled += to_copy; + } + return true; + } + catch (...) { + return false; + } + + return false; +} + +// Returns a cryptographically secure random unsigned int. +static unsigned int RandUInt() +{ + unsigned int result = 0; + SecureRandomBytes(reinterpret_cast(&result), sizeof(result)); + return result; +} + +#endif // SECURE_RANDOM_BYTES_H diff --git a/OOXML/Base/Unit.cpp b/OOXML/Base/Unit.cpp index 0f71fb0f13..405a35f199 100644 --- a/OOXML/Base/Unit.cpp +++ b/OOXML/Base/Unit.cpp @@ -40,7 +40,6 @@ #include "Unit.h" #include - double Cm_To_Mm (const double &dValue) { return dValue * 10; @@ -759,43 +758,6 @@ namespace XmlUtils return sstream.str(); } - int Rand() - { - //rand returns integral value range between 0 and RAND_MAX.(RAND_MAX at least 32767.) - static bool init = false; /* ensure different random header each time */ - if (!init) - { - init = true; - srand((unsigned int) time(nullptr)); - } - return std::rand(); - } - int GenerateInt() - { - //todo c++11 - return ((Rand() & 0x7FFF) | ((Rand() & 0x7FFF) << 15) | ((Rand() & 0x3) << 30)); - } - - std::wstring GenerateGuid() - { - std::wstring result; - //#if defined (_WIN32) || defined(_WIN64) - // GUID guid; - // CoCreateGuid(&guid); - // - // OLECHAR* guidString; - // StringFromCLSID(guid, &guidString); - // - // result = std::wstring(guidString); - // - // CoTaskMemFree(guidString); - //#else - std::wstringstream sstream; - sstream << boost::wformat(L"%04X%04X-%04X-%04X-%04X-%04X%04X%04X") % (Rand() & 0xff) % (Rand() & 0xff) % (Rand() & 0xff) % ((Rand() & 0x0fff) | 0x4000) % ((Rand() % 0x3fff) + 0x8000) % (Rand() & 0xff) % (Rand() & 0xff) % (Rand() & 0xff); - result = sstream.str(); - //#endif - return result; - } std::wstring DoubleToString( double value, wchar_t* format ) { if ( format == nullptr ) return L""; @@ -1056,3 +1018,31 @@ namespace XmlUtils return buffer; } } + +#include "./Rand.h" +namespace XmlUtils +{ + int GenerateInt() + { + return static_cast(RandUInt()); + } + + std::wstring GenerateGuid() + { + std::wstring result; + + std::wstringstream sstream; + sstream << boost::wformat(L"%04X%04X-%04X-%04X-%04X-%04X%04X%04X") + % (RandUInt() & 0xffff) + % (RandUInt() & 0xffff) + % (RandUInt() & 0xffff) + % ((RandUInt() & 0x0fff) | 0x4000) + % ((RandUInt() & 0x3fff) | 0x8000) + % (RandUInt() & 0xffff) + % (RandUInt() & 0xffff) + % (RandUInt() & 0xffff); + result = sstream.str(); + + return result; + } +} diff --git a/OOXML/Base/Unit.h b/OOXML/Base/Unit.h index a54936288e..bb9fc6d4e9 100644 --- a/OOXML/Base/Unit.h +++ b/OOXML/Base/Unit.h @@ -32,6 +32,11 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ + +/* + * SPDX-FileCopyrightText: Euro-Office contributors + * Modified by Euro-Office contributors, see MODIFICATIONS.txt + */ #pragma once #include "Base.h" @@ -172,7 +177,6 @@ namespace XmlUtils std::wstring ToString(double value, const wchar_t* format); std::string ToString(double value, const char* format); - int Rand(); int GenerateInt(); std::wstring GenerateGuid(); diff --git a/OOXML/Projects/Linux/DocxFormatLib/DocxFormatLib.pro b/OOXML/Projects/Linux/DocxFormatLib/DocxFormatLib.pro index ee2dac147b..2ccf9a08b1 100644 --- a/OOXML/Projects/Linux/DocxFormatLib/DocxFormatLib.pro +++ b/OOXML/Projects/Linux/DocxFormatLib/DocxFormatLib.pro @@ -47,7 +47,8 @@ CONFIG += core_x2t include(../../../../Common/base.pri) DEFINES += UNICODE _UNICODE \ - DONT_WRITE_EMBEDDED_FONTS + DONT_WRITE_EMBEDDED_FONTS \ + CRYPTOPP_DISABLE_ASM #BOOST include($$PWD/../../../../Common/3dParty/boost/boost.pri) diff --git a/OOXML/XlsxFormat/Comments/ThreadedComments.cpp b/OOXML/XlsxFormat/Comments/ThreadedComments.cpp index 0daefc1061..936ca598b6 100644 --- a/OOXML/XlsxFormat/Comments/ThreadedComments.cpp +++ b/OOXML/XlsxFormat/Comments/ThreadedComments.cpp @@ -67,7 +67,7 @@ namespace OOX writer.WriteString(L""); } diff --git a/OdfFile/Reader/Converter/StarMath2OOXML/StarMath2OOXML.pro b/OdfFile/Reader/Converter/StarMath2OOXML/StarMath2OOXML.pro index 33df4b7d4f..046f36df92 100644 --- a/OdfFile/Reader/Converter/StarMath2OOXML/StarMath2OOXML.pro +++ b/OdfFile/Reader/Converter/StarMath2OOXML/StarMath2OOXML.pro @@ -66,3 +66,9 @@ HEADERS += \ $$PWD/typeConversion.h \ $$PWD/typeselements.h \ $$PWD/TFormulaSize.h + +core_mac | core_ios { + LIBS += -framework Security +} + + diff --git a/PdfFile/PdfFile.pro b/PdfFile/PdfFile.pro index 25ac45efa2..66c21f9995 100644 --- a/PdfFile/PdfFile.pro +++ b/PdfFile/PdfFile.pro @@ -245,3 +245,7 @@ SOURCES += PdfFile.cpp \ PdfReader.cpp \ PdfEditor.cpp \ OnlineOfficeBinToPdf.cpp + +core_mac | core_ios { + LIBS += -framework Security +} diff --git a/PdfFile/SrcWriter/FontOTWriter.cpp b/PdfFile/SrcWriter/FontOTWriter.cpp index 3d661a434b..8d14df7038 100644 --- a/PdfFile/SrcWriter/FontOTWriter.cpp +++ b/PdfFile/SrcWriter/FontOTWriter.cpp @@ -43,6 +43,7 @@ #include #include #include +#include "./../../OOXML/Base/Rand.h" namespace PdfWriter { @@ -3677,7 +3678,7 @@ namespace PdfWriter CharStringOperand newOperand; newOperand.IsInteger = false; - newOperand.RealValue = ((double)rand() + 1) / ((double)RAND_MAX + 1); + newOperand.RealValue = ((double)RandUInt() + 1) / ((double)RAND_MAX + 1); mOperandStack.push_back(newOperand); return inProgramCounter; diff --git a/X2tConverter/build/Qt/X2tConverter.pro b/X2tConverter/build/Qt/X2tConverter.pro index 33f972acda..4094ba285f 100644 --- a/X2tConverter/build/Qt/X2tConverter.pro +++ b/X2tConverter/build/Qt/X2tConverter.pro @@ -79,3 +79,7 @@ include(X2tConverter.pri) HEADERS += ../../src/dylib/x2t.h SOURCES += ../../src/dylib/x2t.cpp } + +core_mac | core_ios { + LIBS += -framework Security +}