Files
EchoionUpdater/ReleaseTreeSecurityTests.cpp
T
faumaray 0fd1cef78b update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:44:48 +03:00

283 lines
7.6 KiB
C++

#include "ReleaseTree.hpp"
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
namespace
{
namespace fs = std::filesystem;
class FileDescriptor final
{
public:
explicit FileDescriptor(const int descriptor) noexcept
: descriptor_{descriptor}
{
}
~FileDescriptor()
{
if (descriptor_ >= 0) {
::close(descriptor_);
}
}
FileDescriptor(const FileDescriptor&) = delete;
FileDescriptor& operator=(const FileDescriptor&) = delete;
FileDescriptor(FileDescriptor&&) = delete;
FileDescriptor& operator=(FileDescriptor&&) = delete;
[[nodiscard]] int get() const noexcept { return descriptor_; }
private:
int descriptor_;
};
class TemporaryDirectory final
{
public:
TemporaryDirectory()
{
const std::string value =
(fs::temp_directory_path() / "echoion-release-tree.XXXXXX")
.string();
std::vector<char> pattern{value.begin(), value.end()};
pattern.push_back('\0');
if (::mkdtemp(pattern.data()) == nullptr) {
throw std::system_error{
errno, std::generic_category(),
"cannot create temporary directory"};
}
path_ = pattern.data();
}
~TemporaryDirectory()
{
std::error_code ignored;
fs::remove_all(path_, ignored);
}
TemporaryDirectory(const TemporaryDirectory&) = delete;
TemporaryDirectory& operator=(const TemporaryDirectory&) = delete;
TemporaryDirectory(TemporaryDirectory&&) = delete;
TemporaryDirectory& operator=(TemporaryDirectory&&) = delete;
[[nodiscard]] const fs::path& path() const noexcept
{
return path_;
}
private:
fs::path path_;
};
void setMode(const fs::path& path, const mode_t mode)
{
if (::chmod(path.c_str(), mode) != 0) {
throw std::system_error{errno, std::generic_category(),
"cannot set test path mode"};
}
}
void createDirectory(const fs::path& path)
{
fs::create_directory(path);
setMode(path, 0755);
}
void createFile(const fs::path& path, const std::string_view bytes)
{
std::ofstream output{path, std::ios::binary | std::ios::trunc};
output.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
if (!output) {
throw std::runtime_error{"cannot create test file"};
}
output.close();
setMode(path, 0644);
}
[[nodiscard]] FileDescriptor openDirectory(const fs::path& path)
{
const int descriptor = ::open(
path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
if (descriptor < 0) {
throw std::system_error{errno, std::generic_category(),
"cannot open test directory"};
}
return FileDescriptor{descriptor};
}
void expectFailure(const std::function<void()>& operation,
const std::string_view expected)
{
try {
operation();
} catch (const std::exception& error) {
if (std::string_view{error.what()}.find(expected) ==
std::string_view::npos) {
throw std::runtime_error{"unexpected failure: " +
std::string{error.what()}};
}
return;
}
throw std::runtime_error{"operation unexpectedly succeeded"};
}
[[nodiscard]] Echoion::Updater::RuntimeActivation activation()
{
return {.stagingId = "security-test",
.version = "security-test",
.manifestSha256 = {}};
}
void testOversizedFileRejectedBeforeCopy()
{
TemporaryDirectory temporary;
const fs::path source = temporary.path() / "source";
const fs::path versions = temporary.path() / "versions";
createDirectory(source);
createDirectory(versions);
const fs::path manifest = source / "manifest.json";
FileDescriptor file{::open(
manifest.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0644)};
if (file.get() < 0 ||
::ftruncate(
file.get(),
static_cast<off_t>(3ULL * 1024ULL * 1024ULL * 1024ULL)) != 0) {
throw std::system_error{errno, std::generic_category(),
"cannot create sparse oversized file"};
}
const FileDescriptor sourceDescriptor = openDirectory(source);
const fs::path replacement = temporary.path() / "replacement";
createDirectory(replacement);
createFile(replacement / "manifest.json", "replacement\n");
fs::rename(source, temporary.path() / "pinned-source");
fs::create_directory_symlink(replacement, source);
const fs::path destination = versions / ".incoming-oversized";
expectFailure(
[&] {
Echoion::Updater::copyAndValidateRelease(
sourceDescriptor.get(), destination, activation());
},
"file exceeds size limit");
if (fs::symlink_status(destination).type() !=
fs::file_type::not_found) {
throw std::runtime_error{
"failed oversized copy left a destination tree"};
}
}
void testManifestSymlinkIsNeverFollowed()
{
TemporaryDirectory temporary;
const fs::path source = temporary.path() / "source";
const fs::path versions = temporary.path() / "versions";
const fs::path secret = temporary.path() / "secret";
createDirectory(source);
createDirectory(versions);
createFile(secret, "must-not-be-copied\n");
fs::create_symlink(secret, source / "manifest.json");
const FileDescriptor sourceDescriptor = openDirectory(source);
const fs::path destination = versions / ".incoming-symlink";
expectFailure(
[&] {
Echoion::Updater::copyAndValidateRelease(
sourceDescriptor.get(), destination, activation());
},
"cannot open staged release file");
if (fs::symlink_status(destination).type() !=
fs::file_type::not_found) {
throw std::runtime_error{
"failed symlink copy left a destination tree"};
}
}
void testExcessiveDepthRejected()
{
TemporaryDirectory temporary;
const fs::path source = temporary.path() / "source";
const fs::path versions = temporary.path() / "versions";
createDirectory(source);
createDirectory(versions);
createFile(source / "manifest.json", "{}\n");
for (const std::string_view root : {"bin", "lib", "service", "share"}) {
createDirectory(source / root);
}
fs::path nested = source / "bin";
for (std::size_t depth{}; depth < 70U; ++depth) {
nested /= "d";
createDirectory(nested);
}
const FileDescriptor sourceDescriptor = openDirectory(source);
const fs::path destination = versions / ".incoming-deep";
expectFailure(
[&] {
Echoion::Updater::copyAndValidateRelease(
sourceDescriptor.get(), destination, activation());
},
"maximum directory depth");
if (fs::symlink_status(destination).type() !=
fs::file_type::not_found) {
throw std::runtime_error{
"failed deep copy left a destination tree"};
}
}
void testDescriptorRelativeCleanupDoesNotFollowSymlink()
{
TemporaryDirectory temporary;
const fs::path parent = temporary.path() / "parent";
const fs::path victim = temporary.path() / "victim";
createDirectory(parent);
createDirectory(victim);
createFile(victim / "sentinel", "unchanged\n");
fs::create_symlink(victim, parent / "candidate");
const FileDescriptor parentDescriptor = openDirectory(parent);
expectFailure(
[&] {
Echoion::Updater::removeReleaseTreeAt(parentDescriptor.get(),
"../victim");
},
"unsafe path component");
Echoion::Updater::removeReleaseTreeAt(parentDescriptor.get(),
"candidate");
if (fs::symlink_status(parent / "candidate").type() !=
fs::file_type::not_found ||
!fs::is_regular_file(victim / "sentinel")) {
throw std::runtime_error{
"descriptor-relative cleanup followed a symlink"};
}
}
} // namespace
int main()
{
testOversizedFileRejectedBeforeCopy();
testManifestSymlinkIsNeverFollowed();
testExcessiveDepthRejected();
testDescriptorRelativeCleanupDoesNotFollowSymlink();
}