diff --git a/src/zm_signal.cpp b/src/zm_signal.cpp index b310cf7b68..48118736b6 100644 --- a/src/zm_signal.cpp +++ b/src/zm_signal.cpp @@ -26,6 +26,9 @@ #include #include #include +#ifdef __linux__ +#include +#endif #include #include #include @@ -72,6 +75,9 @@ std::atomic_flag crash_reporting = ATOMIC_FLAG_INIT; // fault unreported to zmdc; when this runs out the process dies of the fault. constexpr unsigned kCrashReportSeconds = 5; +// Set when ZM_DUMP_CORES is off, so the crash leaves no core dump. +std::atomic crash_no_core{false}; + // The signal being reported, for the timeout to die of. std::atomic crash_signal{0}; @@ -325,6 +331,15 @@ RETSIGTYPE zm_die_handler(int signal) die_of(signal); } limit_crash_report(signal); +#ifdef __linux__ + // A zero core size limit stops the kernel writing a core file, but a + // core_pattern that pipes to a program (systemd-coredump, apport) is handed + // the dump anyway, and it is up to that program to honour the limit. A + // process that is not dumpable is not dumped at all, unless fs.suid_dumpable + // is 2. This is done only now, as it also stops ptrace and changes who owns + // the process's /proc files. + if (crash_no_core) prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); +#endif // Everything below is emitted with async-signal-safe write(2) only. We do not // touch the database, the log queue or the Logger from the handler: closing @@ -608,6 +623,7 @@ void zmSetDefaultDieHandler() { core.rlim_cur = 0; setrlimit(RLIMIT_CORE, &core); } + crash_no_core = true; zmSetDieHandler(zm_die_handler); } } diff --git a/tests/zm_signal.cpp b/tests/zm_signal.cpp index 5b95b411a4..3a6fd3b75b 100644 --- a/tests/zm_signal.cpp +++ b/tests/zm_signal.cpp @@ -29,6 +29,9 @@ #include #include #include +#ifdef __linux__ +#include +#endif #include #include #include @@ -465,3 +468,45 @@ TEST_CASE("the core size limit is left alone when ZM_DUMP_CORES is on", "[signal REQUIRE(WIFEXITED(status)); REQUIRE(WEXITSTATUS(status) == 0); } + +#ifdef __linux__ +namespace { + +// Whether the process may dump core while its crash report is under way, as +// another thread sees it. That thread then ends the process, so the stuck +// report does not have to time out. +int dumpable_during_report(bool dump_cores) { + static int result[2]; + REQUIRE(pipe(result) == 0); + std::string captured; + run_crashing_child(captured, [&] { + close(result[0]); + config.dump_cores = dump_cores; + zmSetDefaultDieHandler(); + if (prctl(PR_GET_DUMPABLE) != 1) _exit(43); // Not before the crash. + zmSetCrashLogFile(blocking_full_pipe()); + std::thread([] { + usleep(200 * 1000); + char dumpable = static_cast('0' + prctl(PR_GET_DUMPABLE)); + if (write(result[1], &dumpable, 1) != 1) {} + _exit(0); + }).detach(); + raise(SIGSEGV); + }); + close(result[1]); + char dumpable = '?'; + if (read(result[0], &dumpable, 1) != 1) dumpable = '?'; + close(result[0]); + return dumpable == '?' ? -1 : dumpable - '0'; +} + +} // namespace + +TEST_CASE("a crash with ZM_DUMP_CORES off makes the process not dumpable", "[signal]") { + REQUIRE(dumpable_during_report(false) == 0); +} + +TEST_CASE("a crash with ZM_DUMP_CORES on leaves the process dumpable", "[signal]") { + REQUIRE(dumpable_during_report(true) == 1); +} +#endif