commit
0fd1cef78b
5 files changed
+3920
No files matched your search
@@ -0,0 +1,33 @@
|
||||
find_package(OpenSSL REQUIRED COMPONENTS Crypto)
|
||||
|
||||
add_executable(EchoionUpdater echoion-updater.cpp ReleaseTree.cpp)
|
||||
target_link_libraries(EchoionUpdater PRIVATE OpenSSL::Crypto Void::Update)
|
||||
set_target_properties(EchoionUpdater PROPERTIES
|
||||
OUTPUT_NAME echoion-updater
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
set_target_properties(EchoionUpdater PROPERTIES
|
||||
INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
|
||||
INSTALL_RPATH_USE_LINK_PATH FALSE)
|
||||
endif()
|
||||
|
||||
install(TARGETS EchoionUpdater
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
COMPONENT Runtime)
|
||||
|
||||
if(BUILD_TESTS)
|
||||
add_executable(EchoionReleaseTreeSecurityTests
|
||||
ReleaseTreeSecurityTests.cpp
|
||||
ReleaseTree.cpp)
|
||||
target_link_libraries(EchoionReleaseTreeSecurityTests PRIVATE OpenSSL::Crypto)
|
||||
|
||||
add_test(NAME EchoionReleaseTreeSecurity
|
||||
COMMAND EchoionReleaseTreeSecurityTests)
|
||||
|
||||
add_test(NAME EchoionUpdaterLifecycle
|
||||
COMMAND bash
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../tests/updater_test.sh"
|
||||
"$<TARGET_FILE:EchoionUpdater>"
|
||||
"${CMAKE_SOURCE_DIR}")
|
||||
endif()
|
||||
+1562
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
|
||||
namespace Echoion::Updater
|
||||
{
|
||||
|
||||
struct RuntimeActivation {
|
||||
std::string stagingId;
|
||||
std::string version;
|
||||
std::string manifestSha256;
|
||||
std::uint32_t retainHealthyVersions = 2;
|
||||
};
|
||||
|
||||
struct RuntimeRollback {
|
||||
std::string version;
|
||||
std::uint32_t retainHealthyVersions = 2;
|
||||
};
|
||||
|
||||
struct RuntimeCommit {
|
||||
std::string version;
|
||||
std::uint32_t retainHealthyVersions = 2;
|
||||
};
|
||||
|
||||
struct PendingRuntime {
|
||||
std::string candidateId;
|
||||
std::string version;
|
||||
std::string previousVersion;
|
||||
std::string manifestSha256;
|
||||
std::string activatedAt;
|
||||
std::uint32_t retainHealthyVersions = 2;
|
||||
};
|
||||
|
||||
using PrivilegedRequest =
|
||||
std::variant<RuntimeActivation, RuntimeRollback, RuntimeCommit>;
|
||||
|
||||
[[nodiscard]] RuntimeActivation
|
||||
parseActivationRequest(std::string_view json);
|
||||
[[nodiscard]] PrivilegedRequest
|
||||
parsePrivilegedRequest(std::string_view json);
|
||||
[[nodiscard]] PendingRuntime parsePendingRuntime(std::string_view json);
|
||||
|
||||
[[nodiscard]] std::string sha256File(const std::filesystem::path& path);
|
||||
|
||||
void copyAndValidateRelease(int sourceDirectoryDescriptor,
|
||||
const std::filesystem::path& destination,
|
||||
const RuntimeActivation& activation);
|
||||
void removeReleaseTreeAt(int parentDirectoryDescriptor,
|
||||
std::string_view name);
|
||||
|
||||
void validateRelease(const std::filesystem::path& release,
|
||||
const std::string& expectedVersion,
|
||||
const std::string& expectedManifestSha256 = {});
|
||||
|
||||
void syncReleaseTree(const std::filesystem::path& root);
|
||||
|
||||
} // namespace Echoion::Updater
|
||||
@@ -0,0 +1,282 @@
|
||||
#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();
|
||||
}
|
||||
+1982
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user