Archived
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).
This commit is contained in:
1 parent
d893fd05d1
commit
3bf9dc9593
1 file changed
+12
-14
+12
-14
@@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
#include "build/startup.h"
|
#include "build/startup.h"
|
||||||
#include "device/device.h"
|
#include "device/device.h"
|
||||||
|
#include "place/xstats.h"
|
||||||
#include "server/adapters.h"
|
#include "server/adapters.h"
|
||||||
#include "server/sink.h"
|
#include "server/sink.h"
|
||||||
|
|
||||||
@@ -693,21 +694,18 @@ int Engine::run_diagnose() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fs::path dir(cfg_.diagnose);
|
const fs::path dir(cfg_.diagnose);
|
||||||
{
|
/* Both reports go through the expert-stats writer's discipline (xstats.h): a temp file and a
|
||||||
std::ofstream f(dir / "diagnose.json");
|
* rename, the link not followed. A report dir is shared in practice -- /srv/reports, a peer's
|
||||||
f << rep.dump(2) << "\n";
|
* mount -- where a plain ofstream let a pre-created symlink make the run overwrite some other
|
||||||
if (!f) {
|
* file, and a reader polling the dir saw half a report. */
|
||||||
RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.json").c_str());
|
if (const int rc = xs_write_atomic((dir / "diagnose.json").string(), rep.dump(2) + "\n")) {
|
||||||
return RAD_E_IO;
|
RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.json").c_str());
|
||||||
}
|
return rc;
|
||||||
}
|
}
|
||||||
{
|
if (const int rc =
|
||||||
std::ofstream f(dir / "diagnose.md");
|
xs_write_atomic((dir / "diagnose.md").string(), diag::render_markdown(rep))) {
|
||||||
f << diag::render_markdown(rep);
|
RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.md").c_str());
|
||||||
if (!f) {
|
return rc;
|
||||||
RAD_ERR("diagnose: cannot write %s", (dir / "diagnose.md").c_str());
|
|
||||||
return RAD_E_IO;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
RAD_INFO("diagnose: report in %s", (dir / "diagnose.md").c_str());
|
RAD_INFO("diagnose: report in %s", (dir / "diagnose.md").c_str());
|
||||||
return problems.empty() && parity_ok ? RAD_OK : RAD_E_STATE;
|
return problems.empty() && parity_ok ? RAD_OK : RAD_E_STATE;
|
||||||
|
|||||||
Reference in new issue
Block a user