Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper input validation for readdir policy #1239

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/fuse_readdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ FUSE::ReadDir::from_string(std::string const &str_)
{
std::lock_guard<std::mutex> lg(_mutex);

if(!FUSE::ReadDirFactory::valid(str_))
return -EINVAL;

_type = str_;
}

Expand Down
33 changes: 28 additions & 5 deletions src/fuse_readdir_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <set>


namespace l
{
static
void
read_cfg(std::string const cfgstr_,
read_cfg(std::string const str_,
std::string &type_,
int &concurrency_)
{
char type[16];
int concurrency;

concurrency = 0;
std::sscanf(cfgstr_.c_str(),"%15[a-z]:%u",type,&concurrency);
std::sscanf(str_.c_str(),"%15[a-z]:%d",type,&concurrency);

if(concurrency == 0)
concurrency = std::thread::hardware_concurrency();
Expand All @@ -55,14 +56,36 @@ namespace l
}
}

bool
FUSE::ReadDirFactory::valid(std::string const str_)
{
int concurrency;
std::string type;
static const std::set<std::string> types =
{
"seq", "cosr", "cor"
};

l::read_cfg(str_,type,concurrency);

if(types.find(type) == types.end())
return false;
if(concurrency <= 0)
return false;

return true;
}

std::shared_ptr<FUSE::ReadDirBase>
FUSE::ReadDirFactory::make(std::string const cfgstr_)
FUSE::ReadDirFactory::make(std::string const str_)
{
int concurrency;
std::string type;

l::read_cfg(cfgstr_,type,concurrency);
assert(concurrency);
if(!valid(str_))
return {};

l::read_cfg(str_,type,concurrency);

if(type == "seq")
return std::make_shared<FUSE::ReadDirSeq>();
Expand Down
3 changes: 2 additions & 1 deletion src/fuse_readdir_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace FUSE
class ReadDirFactory
{
public:
static std::shared_ptr<ReadDirBase> make(std::string const cfgstr);
static bool valid(std::string str);
static std::shared_ptr<ReadDirBase> make(std::string const str);
};
}