Files
moloapp/audit/MOLO-DAV-001/reproduce.cpp
T

63 lines
2.2 KiB
C++

// MOLO-DAV-001: WebDAV COPY into the source's own subtree never
// terminates and grows memory without bound. See README.md.
//
// ResourceTree is pure in-memory state (no I/O, no server loop), so this
// check drives it directly rather than needing a server: no COPY/MOVE
// route is wired to a live binary in example/ today, but this is the
// same ResourceTree::copy every real COPY handler calls
// (copy_move_handler.cpp).
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <unistd.h>
#include "realtime/webdav/server/resource_tree.h"
using molo::realtime::webdav::server::ResourceTree;
namespace {
constexpr unsigned kWatchdogSecs = 10;
void on_alarm(int) {
// _exit, not exit: the process may be mid-way through growing a
// std::map without bound; do not run destructors over it.
const char msg[] =
"FAIL: ResourceTree::copy(\"/dav/a\", \"/dav/a/b\", ...) did not return within "
"10s; it inserts into entries_ (a std::map) while range-iterating that same "
"map (resource_tree.cpp), so copying a collection into its own subtree "
"(dst inside src) keeps discovering newly-inserted entries ahead of the "
"iterator and never converges (CWE-674, uncontrolled recursion/expansion; "
"unbounded memory growth, CWE-400)\n";
::write(2, msg, sizeof(msg) - 1);
_exit(1);
}
} // namespace
int main() {
std::signal(SIGALRM, on_alarm);
::alarm(kWatchdogSecs);
ResourceTree tree("/dav");
if (!tree.mkcol("/dav/a")) {
std::printf("SKIP: could not set up /dav/a (unexpected ResourceTree API change)\n");
return 0;
}
std::vector<uint8_t> body{'h', 'i'};
tree.put("/dav/a/file.txt", body);
bool created = false;
// Destination is INSIDE the source collection: exactly the shape RFC 4918
// 9.8.3/9.9.3 says a server must reject with 403 before doing anything.
bool ok = tree.copy("/dav/a", "/dav/a/b", /*overwrite=*/true, created);
// If we get here at all, the loop terminated. Whether copy() returned
// true or false, check it did not blow up the tree size either.
std::printf("copy() returned %s\n", ok ? "true" : "false");
std::printf(
"PASS: ResourceTree::copy into its own subtree returned (did not hang)\n");
return 0;
}