Skip to content

Commit aace44d

Browse files
authored
Add files via upload
1 parent cbe6d78 commit aace44d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+33795
-0
lines changed

x86/lib/Security.hpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#pragma once
2+
#include <Windows.h>
3+
#include <string>
4+
#include <accctrl.h>
5+
#include <aclapi.h>
6+
#include <bcrypt.h>
7+
8+
// code submitted in pull request from https://github.com/sbtoonz, authored by KeePassXC https://github.com/keepassxreboot/keepassxc/blob/dab7047113c4ad4ffead944d5c4ebfb648c1d0b0/src/core/Bootstrap.cpp#L121
9+
inline bool LockMemAccess()
10+
{
11+
bool bSuccess = false;
12+
// Process token and user
13+
HANDLE hToken = nullptr;
14+
PTOKEN_USER pTokenUser = nullptr;
15+
DWORD cbBufferSize = 0;
16+
17+
// Access control list
18+
PACL pACL = nullptr;
19+
DWORD cbACL = 0;
20+
21+
// Open the access token associated with the calling process
22+
if (!OpenProcessToken(
23+
GetCurrentProcess(),
24+
TOKEN_QUERY,
25+
&hToken
26+
)) {
27+
goto Cleanup;
28+
}
29+
30+
// Retrieve the token information in a TOKEN_USER structure
31+
GetTokenInformation(
32+
hToken,
33+
TokenUser, // request for a TOKEN_USER structure
34+
nullptr,
35+
0,
36+
&cbBufferSize
37+
);
38+
39+
pTokenUser = static_cast<PTOKEN_USER>(malloc(cbBufferSize));
40+
if (pTokenUser == nullptr) {
41+
goto Cleanup;
42+
}
43+
44+
if (!GetTokenInformation(
45+
hToken,
46+
TokenUser,
47+
pTokenUser,
48+
cbBufferSize,
49+
&cbBufferSize
50+
)) {
51+
goto Cleanup;
52+
}
53+
54+
if (!IsValidSid(pTokenUser->User.Sid)) {
55+
goto Cleanup;
56+
}
57+
58+
// Calculate the amount of memory that must be allocated for the DACL
59+
cbACL = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pTokenUser->User.Sid);
60+
61+
// Create and initialize an ACL
62+
pACL = static_cast<PACL>(malloc(cbACL));
63+
if (pACL == nullptr) {
64+
goto Cleanup;
65+
}
66+
67+
if (!InitializeAcl(pACL, cbACL, ACL_REVISION)) {
68+
goto Cleanup;
69+
}
70+
71+
// Add allowed access control entries, everything else is denied
72+
if (!AddAccessAllowedAce(
73+
pACL,
74+
ACL_REVISION,
75+
SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE, // same as protected process
76+
pTokenUser->User.Sid // pointer to the trustee's SID
77+
)) {
78+
goto Cleanup;
79+
}
80+
81+
// Set discretionary access control list
82+
bSuccess = ERROR_SUCCESS == SetSecurityInfo(
83+
GetCurrentProcess(), // object handle
84+
SE_KERNEL_OBJECT, // type of object
85+
DACL_SECURITY_INFORMATION, // change only the objects DACL
86+
nullptr, nullptr, // do not change owner or group
87+
pACL, // DACL specified
88+
nullptr // do not change SACL
89+
);
90+
91+
Cleanup:
92+
93+
if (pACL != nullptr) {
94+
free(pACL);
95+
96+
}
97+
if (pTokenUser != nullptr) {
98+
free(pTokenUser);
99+
100+
}
101+
if (hToken != nullptr) {
102+
CloseHandle(hToken);
103+
104+
}
105+
return bSuccess;
106+
}

0 commit comments

Comments
 (0)