From 8569e8d5770d65f1d74ca9eefb7e5067c54341c9 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 25 Oct 2026 20:10:50 +0100 Subject: [PATCH] fix(runtime): walk every path component with O_NOFOLLOW in DiskProvider openat(root_fd_, path, O_NOFOLLOW) only blocks the kernel from dereferencing a symlink at the FINAL component of path; an earlier component that is a symlink pointing outside the jail is still followed while resolving the rest of the path, so a strict (follow_symlinks=false) DiskProvider served files outside its configured root via a symlinked intermediate directory (CWE-61, MOLO-STATIC-001). Walk the path one component at a time from root_fd_ instead, applying O_NOFOLLOW at every level, so a symlink anywhere in the path is rejected when follow_symlinks_ is false. The follow_symlinks_=true path (realpath containment fallback) is unchanged. Co-Authored-By: Claude Sonnet 5 --- src/runtime/static/disk_provider.cpp | 53 ++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/runtime/static/disk_provider.cpp b/src/runtime/static/disk_provider.cpp index a679604..9c24bf9 100644 --- a/src/runtime/static/disk_provider.cpp +++ b/src/runtime/static/disk_provider.cpp @@ -6,8 +6,49 @@ #include #include +#include + namespace molo::runtime::static_ { +namespace { + +// openat() with O_NOFOLLOW on the full relative path only blocks the +// kernel from dereferencing a symlink at the FINAL component; an earlier +// component that is a symlink is still followed transparently while the +// rest of the path resolves, escaping root_fd_'s jail. Walk the path one +// component at a time instead, applying O_NOFOLLOW at every single level +// (every intermediate directory, then the final entry), so a symlink +// anywhere in the path is rejected, not only at the end. Returns a valid +// fd, or -1 (errno set) the moment any component fails to open this way. +int OpenNoFollowWalk(int root_fd, const std::string& path) { + std::vector segs; + std::size_t i = 0; + while (i <= path.size()) { + std::size_t slash = path.find('/', i); + if (slash == std::string::npos) slash = path.size(); + if (slash == i) return -1; // empty segment: "//" or a trailing "/" + segs.emplace_back(path, i, slash - i); + i = slash + 1; + } + if (segs.empty()) return -1; + int dir_fd = root_fd; + bool owns_dir_fd = false; + for (std::size_t k = 0; k + 1 < segs.size(); ++k) { + int next = openat(dir_fd, segs[k].c_str(), + O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + if (owns_dir_fd) close(dir_fd); + if (next < 0) return -1; + dir_fd = next; + owns_dir_fd = true; + } + int fd = openat(dir_fd, segs.back().c_str(), + O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + if (owns_dir_fd) close(dir_fd); + return fd; +} + +} // namespace + DiskProvider::DiskProvider(std::string root, bool follow_symlinks, bool allow_dotfiles) : root_(std::move(root)), @@ -37,8 +78,16 @@ std::optional DiskProvider::open(std::string_view rel) { i = slash + 1; } } - int fd = openat(root_fd_, path.c_str(), - O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + int fd; + if (follow_symlinks_) { + // Unchanged: single openat, with the realpath containment fallback + // below for when a symlink (anywhere in the path) makes it fail. + fd = openat(root_fd_, path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC); + } else { + // Strict default: reject a symlink in ANY component, not just the + // final one. + fd = OpenNoFollowWalk(root_fd_, path); + } if (fd < 0 && follow_symlinks_) { // Resolve through realpath and verify containment (v1: small TOCTOU // window, documented; strict deployments keep follow_symlinks off).