Skip to content

Commit

Permalink
Forbidden filesystem opcodes from accessing outside game locations.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDMC committed Jul 27, 2024
1 parent 16afb29 commit 4c0546f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- new opcode **2102 ([log_to_file](https://library.sannybuilder.com/#/sa/debug/2102))**
- implemented support of opcodes **0662**, **0663** and **0664** (original Rockstar's script debugging opcodes. See DebugUtils.ini)
- new [FileSystemOperations](https://github.com/cleolibrary/CLEO5/tree/master/cleo_plugins/FileSystemOperations) plugin
- forbidden scripts from accessing and changing any files outside game root or game settings directory
- file related opcodes moved from CLEO core into separated plugin
- opcode **0A9E ([write_to_file](https://library.sannybuilder.com/#/sa/file/0A9E))** now supports literal numbers and strings
- fixed bug causing file stream opcodes not working correctly when read-write modes are used
Expand Down
24 changes: 24 additions & 0 deletions cleo_plugins/FileSystemOperations/FileSystemOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ using namespace plugin;
#define OPCODE_READ_PARAM_FILE_HANDLE(handle) auto handle = (DWORD)OPCODE_READ_PARAM_PTR(); \
if(m_hFiles.find(handle) == m_hFiles.end()) { auto info = ScriptInfoStr(thread); SHOW_ERROR("Invalid or already closed '0x%X' file handle param in script %s \nScript suspended.", handle, info.c_str()); return thread->Suspend(); }

const char* Gta_User_Dir_Path = (char*)0x00C92368;
#define PATH_SECURITY_CHECK(path) if(_strnicmp(path, CFileMgr::ms_rootDirName, strlen(CFileMgr::ms_rootDirName)) != 0 && _strnicmp(path, Gta_User_Dir_Path, strlen(Gta_User_Dir_Path)) != 0) { SHOW_ERROR("Forbidden access to file path '%s' outside game directory in script %s \nScript suspended.", path, ScriptInfoStr(thread).c_str()); return thread->Suspend(); }


class FileSystemOperations
{
public:
Expand Down Expand Up @@ -114,6 +118,7 @@ class FileSystemOperations
static OpcodeResult WINAPI opcode_0A9A(CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);

char mode[16];
auto paramType = thread->PeekDataType();
Expand Down Expand Up @@ -262,6 +267,7 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_FileExists(CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);

DWORD fAttr = GetFileAttributes(filename);
bool exists = (fAttr != INVALID_FILE_ATTRIBUTES) && !(fAttr & FILE_ATTRIBUTE_DIRECTORY);
Expand Down Expand Up @@ -382,6 +388,7 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_DirectoryExists(CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);

DWORD fAttr = GetFileAttributes(filename);
bool exists = (fAttr != INVALID_FILE_ATTRIBUTES) && (fAttr & FILE_ATTRIBUTE_DIRECTORY);
Expand All @@ -394,6 +401,7 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_CreateDirectory(CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);

bool result = CreateDirectory(filename, NULL) != 0;

Expand All @@ -405,6 +413,7 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_FindFirstFile(CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);

WIN32_FIND_DATA ffd = { 0 };
HANDLE handle = FindFirstFile(filename, &ffd);
Expand Down Expand Up @@ -471,6 +480,7 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_DeleteFile(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);

auto success = DeleteFile(filename);

Expand Down Expand Up @@ -527,6 +537,7 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_DeleteDirectory(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filename);
PATH_SECURITY_CHECK(filename);
auto deleteContents = OPCODE_READ_PARAM_BOOL();

BOOL result;
Expand All @@ -549,7 +560,10 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_MoveFile(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filepath);
PATH_SECURITY_CHECK(filepath);

OPCODE_READ_PARAM_FILEPATH(newFilepath);
PATH_SECURITY_CHECK(newFilepath);

BOOL result = GetFileAttributes(filepath) & FILE_ATTRIBUTE_DIRECTORY;
if (!result)
Expand All @@ -563,7 +577,10 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_MoveDir(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filepath);
PATH_SECURITY_CHECK(filepath);

OPCODE_READ_PARAM_FILEPATH(newFilepath);
PATH_SECURITY_CHECK(newFilepath);

BOOL result = GetFileAttributes(filepath) & FILE_ATTRIBUTE_DIRECTORY;
if (result)
Expand All @@ -577,7 +594,10 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_CopyFile(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filepath);
PATH_SECURITY_CHECK(filepath);

OPCODE_READ_PARAM_FILEPATH(newFilepath);
PATH_SECURITY_CHECK(newFilepath);

BOOL result = CopyFile(filepath, newFilepath, FALSE);
if (result)
Expand Down Expand Up @@ -646,7 +666,10 @@ class FileSystemOperations
static OpcodeResult WINAPI Script_FS_CopyDir(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filepath);
PATH_SECURITY_CHECK(filepath);

OPCODE_READ_PARAM_FILEPATH(newFilepath);
PATH_SECURITY_CHECK(newFilepath);

BOOL result = CopyDir(filepath, newFilepath);

Expand Down Expand Up @@ -773,6 +796,7 @@ class FileSystemOperations
static OpcodeResult __stdcall opcode_2305(CRunningScript* thread)
{
OPCODE_READ_PARAM_FILEPATH(path);
PATH_SECURITY_CHECK(path);

HANDLE file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (file == INVALID_HANDLE_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ if defined GTA_SA_DIR (
</ResourceCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="$(PLUGIN_SDK_DIR)\plugin_sa\game_sa\CFileMgr.cpp" />
<ClCompile Include="FileSystemOperations.cpp" />
<ClCompile Include="FileUtils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\cleo_sdk\CLEO.h" />
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h" />
<ClInclude Include="$(PLUGIN_SDK_DIR)\plugin_sa\game_sa\CFileMgr.h" />
<ClInclude Include="FileUtils.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<ItemGroup>
<ClCompile Include="FileSystemOperations.cpp" />
<ClCompile Include="FileUtils.cpp" />
<ClCompile Include="$(PLUGIN_SDK_DIR)\plugin_sa\game_sa\CFileMgr.cpp">
<Filter>plugin_sdk</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="FileUtils.h" />
Expand All @@ -13,11 +16,17 @@
<ClInclude Include="..\..\cleo_sdk\CLEO_Utils.h">
<Filter>cleo_sdk</Filter>
</ClInclude>
<ClInclude Include="$(PLUGIN_SDK_DIR)\plugin_sa\game_sa\CFileMgr.h">
<Filter>plugin_sdk</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="cleo_sdk">
<UniqueIdentifier>{a2c39c52-f49e-4ffe-bb0a-661ab07131b9}</UniqueIdentifier>
</Filter>
<Filter Include="plugin_sdk">
<UniqueIdentifier>{e9854345-7dae-4c93-a8be-6bd77536c7cd}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\Resource.rc" />
Expand Down

0 comments on commit 4c0546f

Please sign in to comment.