Make sure we can't ChDir out of the memory card's path.

This commit is contained in:
Jean-Philip Desjardins 2020-05-04 11:41:35 -04:00
parent 0a0571a37b
commit b8e537c9e7
3 changed files with 15 additions and 1 deletions

View file

@ -508,7 +508,12 @@ void CMcServ::ChDir(uint32* args, uint32 argsSize, uint32* ret, uint32 retSize,
auto mcPath = CAppConfig::GetInstance().GetPreferencePath(m_mcPathPreference[cmd->port]);
auto hostPath = Iop::PathUtils::MakeHostPath(mcPath, newCurrentDirectory.c_str());
if(fs::exists(hostPath) && fs::is_directory(hostPath))
if(!Iop::PathUtils::IsInsideBasePath(mcPath, hostPath))
{
//Some games (EA games) will try to ChDir('..') from the MC's root
result = RET_NO_ENTRY;
}
else if(fs::exists(hostPath) && fs::is_directory(hostPath))
{
m_currentDirectory = newCurrentDirectory;
result = 0;

View file

@ -15,3 +15,11 @@ fs::path Iop::PathUtils::MakeHostPath(const fs::path& baseHostPath, const char*
result.concat(guestPath);
return result;
}
bool Iop::PathUtils::IsInsideBasePath(const fs::path& basePath, const fs::path& targetPath)
{
auto canonicalBasePath = fs::weakly_canonical(basePath);
auto canonicalTargetPath = fs::weakly_canonical(targetPath);
//If we find the full base path inside the target path, we're in
return canonicalTargetPath.native().find(canonicalBasePath.native()) == 0;
}

View file

@ -7,5 +7,6 @@ namespace Iop
namespace PathUtils
{
fs::path MakeHostPath(const fs::path&, const char*);
bool IsInsideBasePath(const fs::path&, const fs::path&);
}
}