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>
The die handler's 5 second report timeout replaced the child's alarm()
watchdog and ends a stuck report on the original signal, so a test whose
report stalled still passed. run_crashing_child() now waits in the parent
with its own deadline, 4 seconds by default, and kills the child and
fails the test once it passes. The blocked-stderr test, which is meant to
reach the timeout, gets 8 seconds; the other-thread test, which ends
through its 3 second wait, gets 4.5.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The die handler is installed only when ZM_DUMP_CORES is off. It used to end
the process with exit(), so no core was written; it now re-raises the fault
with the default action, which dumps core wherever the core size limit
allows. zmSetDefaultDieHandler() now lowers the soft RLIMIT_CORE to 0 in
that case. The hard limit is left as it was, and with ZM_DUMP_CORES on
nothing changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only zms handles SIGPIPE; in zmc, zma and zmu it is fatal. A crash report
written to a stderr pipe whose reader had gone raised SIGPIPE, which killed
the process before the report reached the log file or syslog and replaced
the fault as the cause of death. SIGPIPE is now blocked while the die
handler runs, so the write fails with EPIPE and the report goes on.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A write in the die handler that never completes, such as to a stderr pipe
nobody reads when a daemon is run by hand, left the process hung with the
fault never reaching zmdc. The handler now sets a 5 second alarm when it
starts reporting; if it fires, a SIGALRM handler ends the process on the
signal that was being reported. alarm() and sigaction() are
async-signal-safe and nothing else in ZoneMinder uses SIGALRM.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The crash socket was blocking, so a full syslog queue could leave the
crashed process waiting in send() instead of dying. It is now non-blocking
and a line that does not fit is dropped.
- zmSetCrashSyslog() rewrote the ident in place while another thread could
be reading it in the handler. It now publishes a new, never-modified
target (socket path and ident) through an atomic pointer.
- The socket was connected once, so a syslog daemon that restarted and made
a new socket no longer got reports. It is left unconnected and each line
is sent with sendto() to the stored path.
- The default path was /dev/log everywhere; it is now _PATH_LOG, which is
/var/run/syslog on macOS and /var/run/log on FreeBSD.
Tests: the fork helper fails a test whose child the watchdog had to kill;
new cases cover a recreated syslog socket and a full syslog queue (filled
from several senders, as Linux caps what one sender may queue). The
worker-thread overflow test accepts SIGBUS, which is what macOS raises.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-ups to the async-signal-safe die handler:
- The log file descriptor the handler wrote to could be closed by a log
rotation after the handler loaded it, and its number reused by an
unrelated file or socket. zmSetCrashLogFile() now keeps one descriptor
that is never closed and dup2()s each new log file onto it, so it only
ever refers to a log file.
- Lines were formatted with snprintf(), which is not async-signal-safe. A
small fixed-buffer CrashLine appends strings, decimals and hex instead.
- glibc backtrace() loads libgcc_s on first use, which allocates. It is now
called once when the handler is installed, and the comment that called it
safe is corrected.
- zmdc points daemons' stderr at /dev/null, so with file logging off the
report went nowhere. The handler now also sends each line to syslog over
its own datagram socket, connected by zmSetCrashSyslog() when the Logger
opens syslog, without calling syslog(3).
- A second signal arriving during the report was blocked when re-raised, so
the process exited 255 instead of dying of it. Both paths now go through
die_of(), which unblocks the signal.
- The shared volatile flag did not stop two threads that fault together from
both reporting. A lock-free atomic_flag picks the reporting thread; a fault
on another thread waits for it to end the process, and a fault inside the
handler on the same thread dies at once.
- sigaltstack() only covers the calling thread. zmSetThreadAltStack() gives
a thread its own stack, freed when it exits; main, the decoder and the
analysis threads call it.
Tests fork a child per case: report formatting, the rotated log file, the
syslog datagram, a second signal mid-report, a fault on another thread
mid-report, and a stack overflow on a worker thread.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
exportEvents() copies each event file with exec($cmd, $output, $return)
and only put $return into a ZM\Debug line, so a failed cp never reached
the user and the export still handed back an archive missing files - the
"fails silently" half of #5074. $output was also never reset between
iterations, and exec() appends to a non-empty array, so that Debug line
accumulated every line of output from every file in the export.
Reset $output before each copy, and on a non-zero return log a ZM\Error
and mark the export failed; after the loop, return false when any copy
failed so ajax/event.php reports "Export Failed" instead of serving an
incomplete archive, matching how the tar/zip step already handles its
own exec() status.
tests/php/test_export_copy_failure.php extracts the exportEvents() body
from the real source and checks the reset and the failure handling are
present; it fails against the pre-fix source.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
zm_die_handler logged through Error()/Fatal() and, on the second signal,
called Fatal() -> logPrint(), which from inside the handler stopped the
database queue, closed the MySQL handle, deleted the Logger and called
exit(): it joined and freed objects the decoder, analysis and stream
threads were still using, ran atexit handlers and static destructors (the
libcrypto/MariaDB signal-unsafe-call floods seen under ThreadSanitizer),
and could deadlock on the log mutex. The crash report it wrote to the Logs
table was sometimes cut short or lost, and the process could hang instead
of exiting.
Emit the crash report (signal, fault reason, fault and instruction
address, backtrace) with async-signal-safe write(2) only, to stderr and,
when open, the per-process log file, then restore the default disposition
and re-raise the signal so the process dies of the actual fault and can
leave a core dump. Nothing touches the database, the log queue or the
Logger from the handler.
- The Logger publishes its log-file descriptor in zm_crash_log_fd when it
opens the file and clears it on close, so the handler can write to the
log file without going through the Logger.
- The glibc path uses backtrace_symbols_fd(), which does not allocate,
instead of backtrace_symbols(); the libunwind path writes fixed buffers.
- Install the handler on a dedicated sigaltstack so a stack-overflow fault
can still be reported, and guard against faulting again while handling.
The crash detail now lands in the log file and syslog rather than the Logs
table; zmdc restarts the daemon and records the abnormal exit.
tests/zm_signal.cpp forks a child, installs the handler, raises SIGSEGV
and checks the process dies of the signal rather than exiting from the
handler, and that the report reached stderr and the published log fd.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
CMake looked only for libmysqlclient and include/mysql/mysql.h, the names
MySQL and MariaDB's compatibility package install. MariaDB Connector/C on
its own installs libmariadb.so and include/mariadb/mysql.h (Gentoo's
dev-db/mariadb-connector-c, Fedora's mariadb-connector-c-devel, Debian's
libmariadb-dev without libmariadb-dev-compat), so configure stopped with
"ZoneMinder requires mysqlclient but it was not found on your system".
find_library now also accepts the name mariadb and find_path the mariadb
include directory; zm_db.h includes mysql.h and mysqld_error.h through
that directory instead of the mysql/ prefix. Systems with libmysqlclient
keep it: mysqlclient is tried first in every directory, and its headers
sit in include/mysql, which is still the first suffix searched.
fixes#5494
Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
web/skins/classic/views/plugin.php queried a PluginsConfig table that no
schema or migration creates and read ZM_PLUGINS_CONFIG_PATH, which is not a
ConfigData option, so ?view=plugin could never run. Nothing links to the
view, no menu or router entry references it, and its history shows only
sweeping maintenance edits, never plugin-feature work. It is a vestige of
the old 1.2x plugin experiment.
Remove the view and its per-view skin assets (js/plugin.js and the base,
classic and dark plugin.css). PluginsConfig and ZM_PLUGINS_CONFIG_PATH
appear nowhere else in the tree.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The export-generation path sanitized the user-supplied export directory name with
preg_replace('/[^\w\-.]/', '', ...), a whitelist that keeps dots, so a bare '..'
survived and $export_root became the parent of the export root. exportEvents()
then chmod'd, wrote into, and tarred that parent, and the archive was downloadable
through the safeExportComponent() read guard because it genuinely sat in the
exports dir. downloadEvents() had the same shape via DIR_EXPORTS_DOWNLOAD
(refs GHSA-72h2-rvhv-wr5g).
These are residual siblings of GHSA-fvx4-86xp-9v22, which hardened the download/
read views but left the write path. Refuse a '.'/'..' component in exportEvents(),
and route downloadEvents()'s name through the existing safeExportComponent().
Add tests/php/test_export_path_traversal.php.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Follow-up to the numeric field-name guard. FilterComponent::buildFilter() takes
an optional $validFields allow-list, but only EventsController's search index
and MonitorsController (via qualifyFields) used it; the other index/search/
consoleEvents call sites passed none, so a field name was checked only by the
character class, not against a known column set.
Pass array_keys($this->Model->schema()) - the pattern EventsController already
uses - at the remaining sites across Configs, EventData, Events (three), Frames,
Groups, ServerStats, Snapshots, Storage, Tags and UserPreference, so a field
must be a real column of the queried model and an unknown field is a 400 rather
than a MySQL 500. The injection itself is already closed by the numeric
field-name guard; this is defense in depth and consistent error handling.
ServerStatsController's commented-out permission check is left as-is (clients
need the data to render monitors); only the allow-list is added there.
Add tests/php/test_api_filter_fields_enforced.php.
Reported by arpitjain099.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A named parameter written `0:<payload>` reaches FilterComponent::buildFilter()
as an integer array key, and the field regex `[a-z0-9.]+` matched the all-digit
name, so `$conditions["0"] = $value` cast the key to an int. CakePHP 2.x renders
an integer-keyed condition as a raw, unescaped SQL fragment, so the value went
into the WHERE clause verbatim (UNION exfiltration, ACL bypass via a trailing
comment, blind boolean oracle) at the index() call sites that pass no
$validFields allow-list.
Refuse an all-digit field name in buildFilter(), so every key that reaches
$conditions contains a letter or a dot and stays a string key that CakePHP
quotes. Add tests/php/test_api_filter_numeric_field.php.
Reported by arpitjain099.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
When the next event the page sent has been deleted, zms stops at the end of
the current event. The page only asked again about a 0 boundary, so it kept the
deleted neighbour and zms stayed paused. Ask again every BOUNDARY_REFRESH_MS
while zms reports paused; a different answer is sent and resumes it through
HintResumes. A user's pause is unaffected, since zms only resumes after
stopping at the end of the filter.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On the zms path the page can move on to another monitor's event without a page
load, but its elements and PanZoom are built for the monitor it was loaded for.
Looking them up by eventData.MonitorId found nothing after such a change: the
scale timer threw on panZoom[id].getScale(), the zoom buttons stopped showing,
and the stream element was not found. Look them up by domMonitorId(), the
monitor the page was loaded for.
The image also kept the first monitor's size and zms its scale. When the new
event's frames are another size, run changeScale() again. The zones drawn are
the page's monitor's, so hide them and disable the toggle over another
monitor's event, and put them back as they were on returning.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
restartZmsStream reloaded the stream image's original URL, which names the
event the page was opened on. zms moves on by itself, now across monitors, so
recovering from a broken stream started playback over from that first event.
Restart on the last event zms reported, without the first event's frame.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The MPEG encoder's size is set by the stream's first frame. When playback moved
on to an event from a monitor with another aspect ratio, PackImage scaled its
frames to that size and distorted them. Scale them to fit inside it, centred on
black, with even sizes and offsets so subsampled chroma stays aligned. The black
fill is done by hand because av_image_fill_black needs FFmpeg 4.2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A lookup given up on after LOOKUP_TIMEOUT_MS is retried while the first may
still answer. If the first answered last, its older neighbours replaced the
retry's: a stale 0 disabled Next and sent zms a boundary. Number the lookups
and apply only the reply to the last one; only its completion ends the wait.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A packed planar frame whose width is a multiple of 32, such as 32x16 YUV420P,
has the same luma stride as the 32-byte layout, so PackImage picked that layout
and then refused the image because it needs more bytes than the frame has. The
frame was dropped. Accept a layout only if it also fits Size(), so the packed
one is tried next.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each hint is its own request, so two sends for the same event, such as a
boundary 0 and then the event found by the refresh, could reach zms out of
order and leave it with the older one. The page now waits for the replies to
one send before starting the next; a zms restart drops that wait, since the
old commands went to the old process.
When zms stopped at the end of an event because the filter had nothing
further, or the hint came too late, a hint arriving afterwards was stored but
playback stayed paused, so an event recorded since was never played. A
SET_EVENT that names an event in the direction we stopped in now resumes
through the end-of-event wait, unless a user command came in between.
Give up on an unanswered neighbour lookup after 5s rather than 10s, so a
retry still arrives within zms's 10s wait.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
prepareImage keeps last_crop in the pixels of the frame it was computed for.
When event playback moves to a monitor with another resolution, the old crop
was applied to the new frame, so the zoomed area jumped or Crop refused it.
Record the frame size the crop belongs to and rescale the crop when it changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PackImage assumed every Image has 32-byte-aligned rows. An Image that wraps a
caller's buffer is packed (width * colours), and one wrapping an AVFrame keeps
the frame's stride, so mapping them on a 32-byte stride read the wrong rows
and ran past the end of the buffer. Use LineSize() for packed formats, match it
against the 32-byte or packed layout for planar ones, and refuse a mapping
larger than Size().
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replies to CMD_SET_EVENT named only the source event, so after a boundary
refresh or a zms restart the replies to an earlier send for the same event
could mark the later one delivered and stop its retries. Number each send and
match replies by that number; reset() drops the number so replies to the old
zms no longer count.
If the neighbour lookup after an event change failed, the page kept the
previous event's neighbours and never asked again, so zms waited at the end
of the event and stopped. Track the lookup: look up the playing event's
neighbours when there are none for it, one request at a time, retrying a
failed one after 2s and giving up on an unanswered one after 10s. This also
replaces the separate boundary refresh bookkeeping.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>