From 8f96bb4b259b96b3ec004a50686c771811827a83 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 8 Apr 2022 10:25:45 +0100 Subject: Fix crash when daemon receives a user or group it can't map --- netfs/daemon/modeCheck.cpp | 14 ++++++++++++-- netfs/unittests/Jamfile.jam | 8 ++++++++ netfs/unittests/testDaemon.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 netfs/unittests/testDaemon.cpp diff --git a/netfs/daemon/modeCheck.cpp b/netfs/daemon/modeCheck.cpp index d63163a..652d8a4 100644 --- a/netfs/daemon/modeCheck.cpp +++ b/netfs/daemon/modeCheck.cpp @@ -1,10 +1,20 @@ #include "modeCheck.h" #include +template +T && +assert_truthy(T && v, ExP &&... p) +{ + if (!v) { + throw Ex {std::forward(p)...}; + } + return std::forward(v); +} + ModeCheck::ModeCheck(const NetFS::ReqEnv & re, const std::filesystem::path & r, const EntryResolver & u, const EntryResolver & g) : - myu(u.getEntry(re.user)->id), - myg(g.getEntry(re.grp)->id), root(r), userLookup(u), groupLookup(g) + myu(assert_truthy(u.getEntry(re.user), EPERM)->id), + myg(assert_truthy(g.getEntry(re.grp), EPERM)->id), root(r), userLookup(u), groupLookup(g) { } diff --git a/netfs/unittests/Jamfile.jam b/netfs/unittests/Jamfile.jam index e811c57..1cd7d84 100644 --- a/netfs/unittests/Jamfile.jam +++ b/netfs/unittests/Jamfile.jam @@ -91,3 +91,11 @@ run testFuse.cpp testCore ; +run testDaemon.cpp ../daemon/modeCheck.cpp + : -- : : + BOOST_TEST_DYN_LINK + boost_utf + testMocks + testCore + ; + diff --git a/netfs/unittests/testDaemon.cpp b/netfs/unittests/testDaemon.cpp new file mode 100644 index 0000000..5c6fbc9 --- /dev/null +++ b/netfs/unittests/testDaemon.cpp @@ -0,0 +1,28 @@ +#define BOOST_TEST_MODULE TestNetFSDaemon +#include +#include + +#include +#include +#include + +struct ModeCheckHelper { + EntryResolverPtr eru = std::make_shared(); + EntryResolverPtr erg = std::make_shared(eru); +}; + +BOOST_FIXTURE_TEST_SUITE(mc, ModeCheckHelper) + +BOOST_AUTO_TEST_CASE(mode_check_valid) +{ + ModeCheck mc {{"root", "root"}, "/tmp", *eru, *erg}; +} + +BOOST_AUTO_TEST_CASE(mode_check_invalid) +{ + BOOST_CHECK_THROW({ ModeCheck({"root", ""}, "/tmp", *eru, *erg); }, NetFS::SystemError); + BOOST_CHECK_THROW({ ModeCheck({"", "root"}, "/tmp", *eru, *erg); }, NetFS::SystemError); + BOOST_CHECK_THROW({ ModeCheck({"", ""}, "/tmp", *eru, *erg); }, NetFS::SystemError); +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3