Skip to content

Commit

Permalink
Resolve relative paths before testing if the path exists.
Browse files Browse the repository at this point in the history
Trying to go beyond the root dir with '..' should just go back to the root dir. We need to resolve that before mapping to the host's filesystem.
  • Loading branch information
jpd002 committed Dec 13, 2023
1 parent 7a2f40a commit c81c7eb
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions Source/iop/Iop_McServ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,21 @@ std::string CMcServ::MakeAbsolutePath(const std::string& inputPath)
{
auto frags = StringUtils::Split(inputPath, '/', true);
std::vector<std::string> newFrags;
for(auto fragIterator = std::rbegin(frags);
fragIterator != std::rend(frags); fragIterator++)
for(const auto& frag : frags)
{
if(*fragIterator == "..")
if(frag.empty()) continue;
if(frag == "..")
{
fragIterator++;
fragIterator++;
if(newFrags.empty())
{
//Going up too much, don't bother
continue;
}
newFrags.pop_back();
}
else
{
newFrags.insert(std::begin(newFrags), *fragIterator);
newFrags.push_back(frag);
}
}
auto outputPath = std::string();
Expand Down Expand Up @@ -711,19 +715,20 @@ void CMcServ::ChDir(uint32* args, uint32 argsSize, uint32* ret, uint32 retSize,
newCurrentDirectory = currentDirectory + SEPARATOR_CHAR + requestedDirectory;
}

newCurrentDirectory = MakeAbsolutePath(newCurrentDirectory);

auto mcPath = CAppConfig::GetInstance().GetPreferencePath(m_mcPathPreference[cmd->port]);
auto hostPath = Iop::PathUtils::MakeHostPath(mcPath, newCurrentDirectory.c_str());

if(!Iop::PathUtils::IsInsideBasePath(mcPath, hostPath))
{
//Some games (EA games) will try to ChDir('..') from the MC's root
//Kim Possible: What's the Switch will also attempt this and rely on the result
//to consider other MC operations to be successes
result = 0;
//This shouldn't happen, but fail just in case.
assert(false);
result = RET_NO_ENTRY;
}
else if(fs::exists(hostPath) && fs::is_directory(hostPath))
{
currentDirectory = MakeAbsolutePath(newCurrentDirectory);
currentDirectory = newCurrentDirectory;
result = 0;
}
else
Expand Down

0 comments on commit c81c7eb

Please sign in to comment.