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 <noreply@anthropic.com>
134 lines
4.4 KiB
C++
134 lines
4.4 KiB
C++
// Spec: plans/01-runtime.md 6.7. Jailed disk provider.
|
|
#include "disk_provider.h"
|
|
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include <vector>
|
|
|
|
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<std::string> 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)),
|
|
follow_symlinks_(follow_symlinks),
|
|
allow_dotfiles_(allow_dotfiles) {
|
|
root_fd_ = ::open(root_.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
}
|
|
|
|
DiskProvider::~DiskProvider() {
|
|
if (root_fd_ >= 0) close(root_fd_);
|
|
}
|
|
|
|
std::optional<Asset> DiskProvider::open(std::string_view rel) {
|
|
if (root_fd_ < 0) return std::nullopt;
|
|
std::string path(rel);
|
|
if (path.empty()) return std::nullopt;
|
|
if (path.size() > 4096) return std::nullopt;
|
|
if (path[0] == '/') return std::nullopt;
|
|
if (path.find('\0') != std::string::npos) return std::nullopt;
|
|
if (!allow_dotfiles_) {
|
|
// Any dotfile segment denies the request.
|
|
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 && path[i] == '.') return std::nullopt;
|
|
i = slash + 1;
|
|
}
|
|
}
|
|
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).
|
|
std::string full = root_ + "/" + path;
|
|
char* resolved = realpath(full.c_str(), nullptr);
|
|
if (resolved == nullptr) return std::nullopt;
|
|
std::string r(resolved);
|
|
free(resolved);
|
|
char* root_resolved = realpath(root_.c_str(), nullptr);
|
|
if (root_resolved == nullptr) return std::nullopt;
|
|
std::string rr(root_resolved);
|
|
free(root_resolved);
|
|
if (r != rr && (r.size() <= rr.size() ||
|
|
r.compare(0, rr.size(), rr) != 0 ||
|
|
r[rr.size()] != '/')) {
|
|
return std::nullopt;
|
|
}
|
|
fd = ::open(r.c_str(), O_RDONLY | O_CLOEXEC);
|
|
if (fd < 0) return std::nullopt;
|
|
}
|
|
if (fd < 0) return std::nullopt;
|
|
struct stat st;
|
|
if (fstat(fd, &st) != 0) {
|
|
close(fd);
|
|
return std::nullopt;
|
|
}
|
|
AssetMeta meta;
|
|
meta.mtime = (int64_t)st.st_mtime;
|
|
if (S_ISDIR(st.st_mode)) {
|
|
close(fd);
|
|
meta.is_dir = true;
|
|
return Asset::ForDir(meta);
|
|
}
|
|
if (!S_ISREG(st.st_mode)) {
|
|
close(fd);
|
|
return std::nullopt;
|
|
}
|
|
meta.length = (uint64_t)st.st_size;
|
|
meta.is_file = true;
|
|
return Asset::ForFile(fd, meta);
|
|
}
|
|
|
|
} // namespace molo::runtime::static_
|