Skip to content

Commit 416f288

Browse files
committed
Fix security analysis warnings (CMemory, CAssemblyModule)
1 parent eda026e commit 416f288

7 files changed

Lines changed: 97 additions & 34 deletions

File tree

include/dynlibutils/memaddr.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ class CMemory
108108
constexpr operator std::uintptr_t() const noexcept { return GetAddr(); }
109109

110110
/// Compare operators.
111-
bool operator==(const CMemory right) const noexcept { return m_addr == right.m_addr; }
112-
bool operator!=(const CMemory right) const noexcept { return !operator==(right); }
113-
bool operator<(const CMemory right) const noexcept { return m_addr < right.m_addr; }
111+
bool operator==(const CMemory& right) const noexcept { return m_addr == right.m_addr; }
112+
bool operator!=(const CMemory& right) const noexcept { return !operator==(right); }
113+
bool operator<(const CMemory& right) const noexcept { return m_addr < right.m_addr; }
114114

115115
// Addition and subtraction operators.
116116
CMemory operator+(const std::size_t right) const noexcept { return Offset(right); }
117117
CMemory operator-(const std::size_t right) const noexcept { return Offset(-static_cast<std::ptrdiff_t>(right)); }
118118
CMemory operator+(const std::ptrdiff_t right) const noexcept { return Offset(right); }
119119
CMemory operator-(const std::ptrdiff_t right) const noexcept { return Offset(-right); }
120-
CMemory operator+(const CMemory right) const noexcept { return Offset(static_cast<std::ptrdiff_t>(right.m_addr)); }
121-
CMemory operator-(const CMemory right) const noexcept { return Offset(static_cast<std::ptrdiff_t>(right.m_addr)); }
120+
CMemory operator+(const CMemory& right) const noexcept { return Offset(static_cast<std::ptrdiff_t>(right.m_addr)); }
121+
CMemory operator-(const CMemory& right) const noexcept { return Offset(-static_cast<std::ptrdiff_t>(right.m_addr)); }
122122

123123
/// Cast methods.
124124
template<typename PTR> constexpr PTR CCast() const noexcept { return (PTR)m_addr; }
@@ -263,20 +263,20 @@ class CMemoryView : public CMemory
263263
using CThis = CMemoryView<T>;
264264

265265
// Addition and subtraction operators (view ones).
266-
CMemory operator+(const std::size_t right) const noexcept { return Offset(right); }
267-
CMemory operator-(const std::size_t right) const noexcept { return Offset(-right); }
268-
CMemory operator+(const std::ptrdiff_t right) const noexcept { return Offset(right); }
269-
CMemory operator-(const std::ptrdiff_t right) const noexcept { return Offset(-right); }
270-
CMemory operator+(const CMemory right) const noexcept { return Offset(static_cast<std::ptrdiff_t>(right.GetAddr())); }
271-
CMemory operator-(const CMemory right) const noexcept { return Offset(static_cast<std::ptrdiff_t>(right.GetAddr())); }
266+
CMemoryView operator+(const std::size_t right) const noexcept { return Offset(right); }
267+
CMemoryView operator-(const std::size_t right) const noexcept { return Offset(-right); }
268+
CMemoryView operator+(const std::ptrdiff_t right) const noexcept { return Offset(right); }
269+
CMemoryView operator-(const std::ptrdiff_t right) const noexcept { return Offset(-right); }
270+
CMemoryView operator+(const CMemory& right) const noexcept { return Offset(static_cast<std::ptrdiff_t>(right.GetAddr())); }
271+
CMemoryView operator-(const CMemory& right) const noexcept { return Offset(-static_cast<std::ptrdiff_t>(right.GetAddr())); }
272272

273273
/// Cast methods (view ones).
274274
constexpr T* CCastView() const noexcept { return CBase::CCast<T*>(); }
275275
constexpr T* RCastView() const noexcept { return CBase::RCast<T*>(); }
276276
constexpr T* UCastView() const noexcept { return CBase::UCast<T*>(); }
277277

278278
/// Access methods (view ones).
279-
constexpr T* GetPtr() const noexcept { return CBase::RCast<T*>(); }
279+
constexpr T* GetPtr() const noexcept { return RCastView(); }
280280
constexpr std::uintptr_t GetAddr() const noexcept { return CBase::RCast<T*>(); }
281281
constexpr T& GetRef() const noexcept { return *GetPtr(); }
282282
constexpr T Get() const { return GetRef(); }

include/dynlibutils/module.hpp

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,30 @@ struct Pattern_t
6262
static constexpr std::size_t sm_nMaxSize = SIZE;
6363

6464
// Constructors.
65-
constexpr Pattern_t(const Pattern_t<SIZE>& copyFrom) noexcept : m_nSize(copyFrom.m_nSize), m_aBytes(copyFrom.m_aBytes), m_aMask(copyFrom.m_aMask) {}
66-
constexpr Pattern_t(Pattern_t<SIZE>&& moveFrom) noexcept : m_nSize(std::move(moveFrom.m_nSize)), m_aBytes(std::move(moveFrom.m_aBytes)), m_aMask(std::move(moveFrom.m_aMask)) {}
65+
constexpr Pattern_t(const Pattern_t<SIZE>& copyFrom) noexcept { CopyFrom(copyFrom); }
66+
constexpr Pattern_t(Pattern_t<SIZE>&& moveFrom) noexcept { MoveFrom(std::move(moveFrom)); }
6767
constexpr Pattern_t(std::size_t size = 0, const std::array<uint8_t, SIZE>& bytes = {}, const std::array<char, SIZE>& mask = {}) noexcept : m_nSize(size), m_aBytes(bytes), m_aMask(mask) {} // Default one.
6868
constexpr Pattern_t(std::size_t &&size, std::array<uint8_t, SIZE>&& bytes, const std::array<char, SIZE>&& mask) noexcept : m_nSize(std::move(size)), m_aBytes(std::move(bytes)), m_aMask(std::move(mask)) {}
69+
Pattern_t& operator=(const Pattern_t<SIZE>& copyFrom) { return CopyFrom(); }
70+
Pattern_t& operator=(Pattern_t<SIZE>&& moveFrom) { return MoveFrom(std::move(moveFrom)); }
71+
72+
Pattern_t& CopyFrom(const Pattern_t<SIZE>& other)
73+
{
74+
m_nSize = other.m_nSize;
75+
m_aBytes = other.m_aBytes;
76+
m_aMask = other.m_aMask;
77+
78+
return *this;
79+
}
80+
81+
Pattern_t& MoveFrom(Pattern_t<SIZE>&& other)
82+
{
83+
m_nSize = other.m_nSize;
84+
m_aBytes = std::move(other.m_aBytes);
85+
m_aMask = std::move(other.m_aMask);
86+
87+
return *this;
88+
}
6989

7090
// Fields. Available to anyone (so structure).
7191
std::size_t m_nSize;
@@ -290,7 +310,7 @@ struct CCache
290310
CCache(
291311
const volatile std::uint8_t* pPatternMem,
292312
const size_t nSize,
293-
const CMemory pStartAddress = nullptr,
313+
const CMemory& pStartAddress = nullptr,
294314
const Section_t* pModuleSection = nullptr
295315
)
296316
: m_svPattern(pPatternMem, pPatternMem + nSize)
@@ -321,13 +341,14 @@ struct CCache
321341

322342
struct CHash
323343
{
344+
static constexpr std::size_t m_nGoldenRatio = 0x9e3779b9u;
345+
324346
std::size_t operator()(const CCache& k) const noexcept
325347
{
326-
static constexpr std::size_t golden_ratio = 0x9e3779b9u;
327348
std::size_t h = std::hash<std::string>()(k.m_svPattern);
328-
h ^= std::hash<uintptr_t>()(k.m_nStart) + golden_ratio + (h << 6) + (h >> 2);
329-
h ^= std::hash<uintptr_t>()(k.m_pSectionAddr) + golden_ratio + (h << 6) + (h >> 2);
330-
h ^= std::hash<size_t>()(k.m_nSectionSize) + golden_ratio + (h << 6) + (h >> 2);
349+
h ^= std::hash<uintptr_t>()(k.m_nStart) + m_nGoldenRatio + (h << 6) + (h >> 2);
350+
h ^= std::hash<uintptr_t>()(k.m_pSectionAddr) + m_nGoldenRatio + (h << 6) + (h >> 2);
351+
h ^= std::hash<size_t>()(k.m_nSectionSize) + m_nGoldenRatio + (h << 6) + (h >> 2);
331352
return h;
332353
}
333354
};
@@ -359,24 +380,42 @@ class CAssemblyModule : public CMemory
359380

360381
public:
361382
constexpr CSignatureView() : m_pModule(nullptr) {}
362-
constexpr CSignatureView(CSignatureView&& moveFrom) : Base_t(std::move(moveFrom)), m_pModule(std::move(moveFrom.m_pModule)) {}
383+
constexpr CSignatureView(const CSignatureView& copyFrom) { CopyFrom(copyFrom); }
384+
constexpr CSignatureView(CSignatureView&& moveFrom) { MoveFrom(std::move(moveFrom)); }
363385
constexpr CSignatureView(const Base_t& pattern, CAssemblyModule* module) : Base_t(pattern), m_pModule(module) {}
364386
constexpr CSignatureView(Base_t&& pattern, CAssemblyModule* module) : Base_t(std::move(pattern)), m_pModule(module) {}
387+
CSignatureView& operator=(const CSignatureView& copyFrom) { return CopyFrom(copyFrom); }
388+
CSignatureView& operator=(CSignatureView&& moveFrom) { return MoveFrom(std::move(moveFrom)); }
389+
390+
CSignatureView& CopyFrom(const CSignatureView& other)
391+
{
392+
Base_t::CopyFrom(other);
393+
m_pModule = other.m_pModule;
394+
395+
return *this;
396+
}
397+
CSignatureView& MoveFrom(CSignatureView&& other)
398+
{
399+
Base_t::MoveFrom(std::move(other));
400+
m_pModule = std::exchange(other.m_pModule, nullptr);
401+
402+
return *this;
403+
}
365404

366405
bool IsValid() const { return m_pModule && m_pModule->IsValid(); }
367406

368407
[[nodiscard]]
369-
CMemory operator()(const CMemory pStart = nullptr, const Section_t* pSection = nullptr) const
408+
CMemory operator()(const CMemory& pStart = nullptr, const Section_t* pSection = nullptr) const
370409
{
371410
return Find(pStart, pSection);
372411
}
373412

374-
[[nodiscard]] CMemory Find(const CMemory pStart, const Section_t* pSection = nullptr) const
413+
[[nodiscard]] CMemory Find(const CMemory& pStart, const Section_t* pSection = nullptr) const
375414
{
376415
return m_pModule->FindPattern<SIZE>(CMemory(Base_t::m_aBytes.data()), std::string_view(Base_t::m_aMask.data(), Base_t::m_nSize), pStart, pSection);
377416
}
378417
[[nodiscard]] CMemory OffsetAndFind(const std::ptrdiff_t offset, CMemory pStart, const Section_t* pSection = nullptr) const { return Find(pStart + offset, pSection); }
379-
[[nodiscard]] CMemory OffsetFromSelfAndFind(const CMemory pStart, const Section_t* pSection = nullptr) const { return OffsetAndFind(Base_t::m_nSize, pStart, pSection); }
418+
[[nodiscard]] CMemory OffsetFromSelfAndFind(const CMemory& pStart, const Section_t* pSection = nullptr) const { return OffsetAndFind(Base_t::m_nSize, pStart, pSection); }
380419
[[nodiscard]] CMemory DerefAndFind(const std::uintptr_t deref, CMemory pStart, const Section_t* pSection = nullptr) const { return Find(pStart.Deref(deref), pSection); }
381420
}; // class CSignatureView<SIZE>
382421

@@ -399,18 +438,30 @@ class CAssemblyModule : public CMemory
399438
~CAssemblyModule();
400439

401440
CAssemblyModule(const CAssemblyModule&) = delete;
402-
CAssemblyModule& operator=(const CAssemblyModule&) = delete;
403-
CAssemblyModule(CAssemblyModule&& other) noexcept : CMemory(std::exchange(static_cast<CMemory &>(other), DYNLIB_INVALID_MEMORY)), m_sPath(std::move(other.m_sPath)), m_vecSections(std::move(other.m_vecSections)), m_pExecutableSection(std::move(other.m_pExecutableSection)) {}
404-
CAssemblyModule(const CMemory pModuleMemory);
441+
CAssemblyModule(CAssemblyModule&& moveFrom) noexcept { MoveFrom(std::move(moveFrom)); }
442+
CAssemblyModule(const CMemory& pModuleMemory);
405443
explicit CAssemblyModule(const std::string_view svModuleName);
406444
explicit CAssemblyModule(const char* pszModuleName) : CAssemblyModule(std::string_view(pszModuleName)) {}
407445
explicit CAssemblyModule(const std::string& sModuleName) : CAssemblyModule(std::string_view(sModuleName)) {}
446+
CAssemblyModule &operator=(const CAssemblyModule&) = delete;
447+
CAssemblyModule &operator=(CAssemblyModule&& moveFrom) { return MoveFrom(std::move(moveFrom)); }
448+
449+
CAssemblyModule &CopyFrom(const CAssemblyModule&) = delete;
450+
CAssemblyModule &MoveFrom(CAssemblyModule&& other)
451+
{
452+
*static_cast<CMemory *>(this) = std::exchange(static_cast<CMemory &>(other), DYNLIB_INVALID_MEMORY);
453+
m_sPath = std::move(other.m_sPath);
454+
m_vecSections = std::move(other.m_vecSections);
455+
m_pExecutableSection = std::move(other.m_pExecutableSection);
456+
457+
return *this;
458+
}
408459

409460
bool LoadFromPath(const std::string_view svModelePath, int flags);
410461
bool LoadFromPath(const std::string_view svModelePath);
411462

412463
bool InitFromName(const std::string_view svModuleName, bool bExtension = false);
413-
bool InitFromMemory(const CMemory pModuleMemory, bool bForce = true);
464+
bool InitFromMemory(const CMemory& pModuleMemory, bool bForce = true);
414465

415466
template<std::size_t N>
416467
[[nodiscard]]
@@ -438,7 +489,7 @@ class CAssemblyModule : public CMemory
438489
// *pModuleSection
439490
// Output : CMemory
440491
//-----------------------------------------------------------------------------
441-
CMemory FindPattern(const CMemoryView<std::uint8_t> pPatternMem, const std::string_view svMask, const CMemory pStartAddress, const Section_t* pModuleSection) const;
492+
CMemory FindPattern(const CMemoryView<std::uint8_t>& pPatternMem, const std::string_view svMask, const CMemory& pStartAddress, const Section_t* pModuleSection) const;
442493

443494
template<std::size_t SIZE>
444495
[[nodiscard]]

include/dynlibutils/vthook.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CVTHook : public CMemory
4747

4848
CVTHook() = default;
4949
CVTHook(const CVTHook &other) = delete;
50-
CVTHook(CVTHook &&other) : CMemory(std::exchange(static_cast<CMemory &>(other), DYNLIB_INVALID_MEMORY)), m_pOriginalFn(std::exchange(static_cast<CMemory &>(other.m_pOriginalFn), DYNLIB_INVALID_MEMORY)) {}
50+
CVTHook(CVTHook &&other) { MoveFrom(std::move(other)); }
5151
~CVTHook()
5252
{
5353
if (IsHooked())
@@ -56,6 +56,16 @@ class CVTHook : public CMemory
5656
}
5757
}
5858

59+
CVTHook &CopyFrom(const CVTHook &other) = delete;
60+
CVTHook &MoveFrom(CVTHook &&other)
61+
{
62+
*static_cast<CMemory *>(this) = std::exchange(static_cast<CMemory &>(other), DYNLIB_INVALID_MEMORY);
63+
m_pOriginalFn = std::exchange(static_cast<CMemory &>(other.m_pOriginalFn), DYNLIB_INVALID_MEMORY);
64+
65+
return *this;
66+
}
67+
68+
5969
bool IsHooked() const noexcept { return IsValid(); } // Returns true if a hook is currently installed (i.e., we have a valid vtable slot pointer).
6070
void Clear() noexcept { SetPtr(nullptr); m_pOriginalFn = nullptr; }
6171

src/apple/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ bool CAssemblyModule<Mutex>::InitFromName(const std::string_view svModuleName, b
6969
// Output : bool
7070
//-----------------------------------------------------------------------------
7171
template<typename Mutex>
72-
bool CAssemblyModule<Mutex>::InitFromMemory(const CMemory pModuleMemory, bool bForce)
72+
bool CAssemblyModule<Mutex>::InitFromMemory(const CMemory& pModuleMemory, bool bForce)
7373
{
7474
if (IsValid() && !bForce)
7575
return false;

src/linux/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool CAssemblyModule<Mutex>::InitFromName(const std::string_view svModuleName, b
7474
// Output : bool
7575
//-----------------------------------------------------------------------------
7676
template<typename Mutex>
77-
bool CAssemblyModule<Mutex>::InitFromMemory(const CMemory pModuleMemory, bool bForce)
77+
bool CAssemblyModule<Mutex>::InitFromMemory(const CMemory& pModuleMemory, bool bForce)
7878
{
7979
if (IsValid() && !bForce)
8080
return false;

src/module.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ template<typename Mutex>
1717
CAssemblyModule<Mutex>::CAssemblyModule(const std::string_view szModuleName)
1818
{
1919
InitFromName(szModuleName);
20+
m_pExecutableSection = nullptr;
2021
}
2122

2223
//-----------------------------------------------------------------------------
2324
// Purpose: constructor
2425
// Input : pModuleMemory
2526
//-----------------------------------------------------------------------------
2627
template<typename Mutex>
27-
CAssemblyModule<Mutex>::CAssemblyModule(const CMemory pModuleMemory)
28+
CAssemblyModule<Mutex>::CAssemblyModule(const CMemory& pModuleMemory)
2829
{
2930
InitFromMemory(pModuleMemory);
31+
m_pExecutableSection = nullptr;
3032
}
3133

3234
template<typename Mutex>
@@ -74,7 +76,7 @@ CMemory CAssemblyModule<Mutex>::GetAddress(const CCache& hKey) const noexcept
7476
}
7577

7678
template<typename Mutex>
77-
CMemory CAssemblyModule<Mutex>::FindPattern(const CMemoryView<std::uint8_t> pPatternMem, const std::string_view svMask, const CMemory pStartAddress, const Section_t* pModuleSection) const
79+
CMemory CAssemblyModule<Mutex>::FindPattern(const CMemoryView<std::uint8_t>& pPatternMem, const std::string_view svMask, const CMemory& pStartAddress, const Section_t* pModuleSection) const
7880
{
7981
const auto* pPattern = pPatternMem.RCastView();
8082

src/windows/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bool CAssemblyModule<Mutex>::InitFromName(const std::string_view svModuleName, b
8484
// Output : bool
8585
//-----------------------------------------------------------------------------
8686
template<typename Mutex>
87-
bool CAssemblyModule<Mutex>::InitFromMemory(const CMemory pModuleMemory, bool bForce)
87+
bool CAssemblyModule<Mutex>::InitFromMemory(const CMemory& pModuleMemory, bool bForce)
8888
{
8989
if (IsValid() && !bForce)
9090
return false;

0 commit comments

Comments
 (0)