mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-12-01 04:38:35 -05:00
fix: make a crashing process non-dumpable when ZM_DUMP_CORES is off refs #5215
A zero RLIMIT_CORE stops the kernel writing a core file, but when core_pattern pipes to a program such as systemd-coredump or apport the kernel hands it the dump anyway and leaves the limit to that program. With ZM_DUMP_CORES off, the die handler now also calls prctl(PR_SET_DUMPABLE, 0) when it starts reporting, so the kernel skips the dump unless fs.suid_dumpable is 2, in which case the limit still applies. It is done only at crash time because it also stops ptrace and makes root the owner of the process's /proc files. Linux only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dab97e4730
commit
6fd53c3db2
2 files changed
+61
No files matched your search
@@ -26,6 +26,9 @@
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <memory>
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
#include <sys/resource.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
@@ -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<bool> crash_no_core{false};
|
||||
|
||||
// The signal being reported, for the timeout to die of.
|
||||
std::atomic<int> 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);
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,9 @@
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <string>
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
#include <sys/resource.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
@@ -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<char>('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
|
||||
Reference in new issue
Block a user