Skip to content

Commit 7f2e394

Browse files
committed
Started whitelist code
1 parent 963d53a commit 7f2e394

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

projects/premake5.lua

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ newoption({
44
value = "path to garrysmod_common directory"
55
})
66

7+
newoption({
8+
trigger = "whitelist",
9+
description = "Enables wrapping of getaddrinfo and a whitelist file to filter valid addresses and ports",
10+
value = "0 or 1"
11+
})
12+
713
local gmcommon = _OPTIONS.gmcommon or os.getenv("GARRYSMOD_COMMON")
14+
local whitelist = _OPTIONS.whitelist == "1"
815
if gmcommon == nil then
916
error("you didn't provide a path to your garrysmod_common (https://github.com/danielga/garrysmod_common) directory")
1017
end
@@ -16,6 +23,7 @@ local LUASOCKET_FOLDER = "../luasocket/src"
1623
CreateWorkspace({name = "socket.core"})
1724
CreateProject({serverside = true, manual_files = true})
1825
files("../source/socket.cpp")
26+
if whitelist then files("../source/whitelist.cpp") end
1927
IncludeLuaShared()
2028
links("socket")
2129

@@ -24,6 +32,7 @@ CreateWorkspace({name = "socket.core"})
2432

2533
CreateProject({serverside = false, manual_files = true})
2634
files("../source/socket.cpp")
35+
if whitelist then files("../source/whitelist.cpp") end
2736
IncludeLuaShared()
2837
links("socket")
2938

@@ -52,19 +61,24 @@ CreateWorkspace({name = "socket.core"})
5261
IncludeLuaShared()
5362

5463
filter("system:windows")
55-
defines({
64+
local windowsdefines = {
5665
"LUASOCKET_API=__declspec(dllexport)",
57-
"MIME_API=__declspec(dllexport)"
58-
})
66+
"MIME_API=__declspec(dllexport)",
67+
"getaddrinfo=__wrap_getaddrinfo"
68+
}
69+
if whitelist then windowsdefines[#windowsdefines+1] = "getaddrinfo=__wrap_getaddrinfo" end
70+
defines(windowsdefines)
5971
files(LUASOCKET_FOLDER .. "/wsocket.c")
6072
links("ws2_32")
6173

6274
filter("system:not windows")
63-
defines({
75+
local unixdefines = {
6476
"LUASOCKET_API=''",
6577
"UNIX_API=''",
6678
"MIME_API=''"
67-
})
79+
}
80+
if whitelist then unixdefines[#unixdefines+1] = "getaddrinfo=__wrap_getaddrinfo" end
81+
defines(unixdefines)
6882
files(LUASOCKET_FOLDER .. "/usocket.c")
6983

7084
CreateWorkspace({name = "mime.core"})

source/whitelist.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#undef getaddrinfo
2+
3+
#include "socket.h"
4+
#include <vector>
5+
6+
std::vector<std::pair<std::string, std::string> > whitelist;
7+
8+
void parseWhitelist()
9+
{
10+
}
11+
12+
bool isSafe(unsigned char* pNodeName, unsigned char* pServiceName)
13+
{
14+
for(auto i = whitelist.begin(), end = whitelist.end(); i!=end; ++i)
15+
{
16+
if(i->first == NodeStr && i->second == pServiceName)
17+
{
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
extern "C" {
25+
26+
WINSOCK_API_LINKAGE INT WSAAPI __wrap_getaddrinfo(
27+
_In_opt_ PCSTR pNodeName,
28+
_In_opt_ PCSTR pServiceName,
29+
_In_opt_ const ADDRINFOA * pHints,
30+
_Outptr_ PADDRINFOA * ppResult
31+
)
32+
{
33+
if(isSafe(pNodeName, pServiceName))
34+
{
35+
return getaddrinfo(pNodeName, pServiceName, pHints, ppResult);
36+
}
37+
else
38+
{
39+
return EAI_FAIL;
40+
}
41+
}
42+
43+
}

0 commit comments

Comments
 (0)