From 029bec3e2db93d46681986b33b4ad69846d81bbd Mon Sep 17 00:00:00 2001 From: aevolution-gitops-bot Date: Mon, 5 Oct 2026 10:44:21 -0400 Subject: [PATCH] Use private memfd for PostgreSQL passfile --- deploy/kubernetes/region/base/deployment.yaml | 4 +- src/persistence/postgres.cpp | 65 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/deploy/kubernetes/region/base/deployment.yaml b/deploy/kubernetes/region/base/deployment.yaml index d161e94..14ed70f 100644 --- a/deploy/kubernetes/region/base/deployment.yaml +++ b/deploy/kubernetes/region/base/deployment.yaml @@ -67,10 +67,10 @@ spec: exec /usr/local/bin/evo-server --profile lineage "${retained[@]}" "${inputs[@]}" \ "${m10[@]}" \ --world "$REGION_WORLD" --region "$REGION_ID" --frame "$REGION_FRAME" \ - --pg-service /run/postgres/pg_service.conf --pg-passfile /dev/stdin \ + --pg-service /run/postgres/pg_service.conf --pg-passfile /run/postgres/pgpass \ --objects /objects --objects-reserve-mib "$REGION_RESERVE_MIB" \ --build "$REGION_BUILD" --lease-profile default \ - --flush-ms 1000 --drain-ms 20000 --ops-listen 0.0.0.0:9100 < /run/postgres/pgpass + --flush-ms 1000 --drain-ms 20000 --ops-listen 0.0.0.0:9100 env: - {name: REGION_WORLD, value: set-by-overlay} - {name: REGION_ID, value: set-by-overlay} diff --git a/src/persistence/postgres.cpp b/src/persistence/postgres.cpp index fe8bfe9..5226afe 100644 --- a/src/persistence/postgres.cpp +++ b/src/persistence/postgres.cpp @@ -6,7 +6,12 @@ #include #include #include +#include #include +#include +#include +#include +#include #include namespace evo::persistence { @@ -31,6 +36,56 @@ struct Clear { }; using Connection = std::unique_ptr; using Result = std::unique_ptr; +struct PrivatePassfile { + int fd{-1}; + explicit PrivatePassfile(int value) : fd(value) {} + ~PrivatePassfile() { + if (fd >= 0) + ::close(fd); + } + PrivatePassfile(const PrivatePassfile&) = delete; + PrivatePassfile& operator=(const PrivatePassfile&) = delete; + PrivatePassfile(PrivatePassfile&& other) noexcept : fd(std::exchange(other.fd, -1)) {} + PrivatePassfile& operator=(PrivatePassfile&& other) noexcept { + if (this != &other) { + if (fd >= 0) + ::close(fd); + fd = std::exchange(other.fd, -1); + } + return *this; + } + [[nodiscard]] std::string path() const { + return "/proc/self/fd/" + std::to_string(fd); + } +}; +std::expected private_passfile(const std::string& source) { + std::ifstream in(source, std::ios::binary); + if (!in) + return std::unexpected{PgError::config}; + std::string contents(4097, '\0'); + in.read(contents.data(), static_cast(contents.size())); + contents.resize(static_cast(in.gcount())); + if (contents.empty() || contents.size() > 4096 || contents.find('\0') != std::string::npos) + return std::unexpected{PgError::config}; + const int fd = static_cast(::syscall(SYS_memfd_create, "evo-pgpass", MFD_CLOEXEC)); + if (fd < 0 || ::fchmod(fd, S_IRUSR | S_IWUSR) != 0) { + if (fd >= 0) + ::close(fd); + return std::unexpected{PgError::config}; + } + std::size_t written{}; + while (written < contents.size()) { + const auto count = ::write(fd, contents.data() + written, contents.size() - written); + if (count <= 0) { + ::close(fd); + return std::unexpected{PgError::config}; + } + written += static_cast(count); + } + if (::lseek(fd, 0, SEEK_SET) < 0) + return std::unexpected{PgError::config}; + return PrivatePassfile{fd}; +} bool wait(PGconn* c, short events, Clock::time_point deadline) { for (;;) { const auto left = @@ -150,6 +205,14 @@ Postgres::connect(const PgOptions& o, std::chrono::milliseconds timeout) { inet_pton(AF_INET6, o.hostaddr.c_str(), address.data()) != 1))) || (local && !o.hostaddr.empty())) return std::unexpected{fail(PgError::config)}; + std::optional private_file; + if (!o.password_file.empty()) { + auto made = private_passfile(o.password_file); + if (!made) + return std::unexpected{fail(made.error())}; + private_file.emplace(std::move(*made)); + } + const std::string passfile = private_file ? private_file->path() : std::string{}; const char* keys[] = {"host", "hostaddr", "port", "dbname", "user", "passfile", "sslrootcert", "sslmode", "gssencmode", "options", nullptr}; const char* values[] = {o.host.c_str(), @@ -157,7 +220,7 @@ Postgres::connect(const PgOptions& o, std::chrono::milliseconds timeout) { o.port.c_str(), o.database.c_str(), o.user.c_str(), - o.password_file.c_str(), + passfile.c_str(), o.ca_file.c_str(), local ? "disable" : "verify-full", "disable",