From 3bf9dc95938567be49d07f5d06c24e603cc56998 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 21:26:43 +0000 Subject: [PATCH] diag: the report writes take the atomic, link-safe path diagnose wrote diagnose.json and diagnose.md with plain ofstreams into the configured report dir. That dir is a shared place in practice -- a /srv/reports, a fleet peer's mount -- and a local user who can write it can pre-create DIR/diagnose.json as a symlink: the serving-user process then truncates and overwrites whatever the link names, with report text whose category strings a corpus file partly shapes. A reader polling the dir also sees a half-written report. This is the hazard xs_write_atomic exists for -- xstats' comment describes exactly this pre-created-symlink story -- and xstats.h is what xstats uses to write the very same kind of file. Both report writes go through it now: temp file, no follow, rename. Found by the 2.1.0 review pass (axis: hostile sockets / runtime). --- core/engine_diagnose.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/core/engine_diagnose.cpp b/core/engine_diagnose.cpp index a795c7e8..d153ef3f 100644 --- a/core/engine_diagnose.cpp +++ b/core/engine_diagnose.cpp @@ -34,6 +34,7 @@ #include "build/startup.h" #include "device/device.h" +#include "place/xstats.h" #include "server/adapters.h" #include "server/sink.h" @@ -693,21 +694,18 @@ int Engine::run_diagnose() { } const fs::path dir(cfg_.diagnose); - { - std::ofstream f(dir / "diagnose.json"); - f << rep.dump(2) << "\n"; - if (!f) { - RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.json").c_str()); - return RAD_E_IO; - } + /* Both reports go through the expert-stats writer's discipline (xstats.h): a temp file and a + * rename, the link not followed. A report dir is shared in practice -- /srv/reports, a peer's + * mount -- where a plain ofstream let a pre-created symlink make the run overwrite some other + * file, and a reader polling the dir saw half a report. */ + if (const int rc = xs_write_atomic((dir / "diagnose.json").string(), rep.dump(2) + "\n")) { + RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.json").c_str()); + return rc; } - { - std::ofstream f(dir / "diagnose.md"); - f << diag::render_markdown(rep); - if (!f) { - RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.md").c_str()); - return RAD_E_IO; - } + if (const int rc = + xs_write_atomic((dir / "diagnose.md").string(), diag::render_markdown(rep))) { + RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.md").c_str()); + return rc; } RAD_INFO("diagnose: report in %s", (dir / "diagnose.md").c_str()); return problems.empty() && parity_ok ? RAD_OK : RAD_E_STATE;