Skip to content

cachedb_nats: drop RTLD_GLOBAL (#4501); cachedb_nats_fts parses with cJSON - #4510

Open
NormB wants to merge 2 commits into
OpenSIPS:masterfrom
NormB:fix/cachedb-nats-rtld-global
Open

NormB wants to merge 2 commits into
OpenSIPS:masterfrom
NormB:fix/cachedb-nats-rtld-global

Conversation

@NormB

@NormB NormB commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #4501. cachedb_nats no longer asks for RTLD_GLOBAL, so none of its symbols reach the global scope. cachedb_nats_fts, which needed that flag to use the JSON scanners in cachedb_nats, now parses documents with the core's cJSON (lib/cJSON.h). With rtpengine loaded after cachedb_nats, the "RTPEngine notification receiver" is forked again.

Details

cachedb_nats was opened RTLD_NOW | RTLD_GLOBAL so that cachedb_nats_fts could resolve cdbn_skip_ws, cdbn_parse_json_string and cdbn_skip_json_value. The flag also put struct module_exports exports in the global scope. In a module loaded after it, a reference to its own exports bound to the one in cachedb_nats. rtpengine's notification_sock setter therefore wrote cachedb_nats's exports.procs, and its own process was never forked.

Solution

  • cachedb_nats uses DEFAULT_DLFLAGS.
  • cachedb_nats_fts parses with cJSON and uses nothing from cachedb_nats's JSON code.
  • Field names and values are indexed decoded.
  • A document is not indexed, and a warning is logged, if it:
    • is not a single JSON object
    • is malformed
    • contains a NUL
    • exceeds the new limits
  • New cachedb_nats_fts parameters:
    • json_max_depth (default 64, range 1–1024)
    • json_max_bytes (default 1048576, at least 2)
  • cachedb_nats already refused documents it reads back if they were nested deeper than 64 levels or larger than 1 MiB. Those limits were fixed at compile time; they are now parameters of the same names and defaults. A warning is logged at startup if json_max_bytes is below max_value_size.
  • An out-of-range value is rejected when the configuration is read, so opensips -C reports it.

Compatibility

  • Script functions, MI commands and processes are unchanged. There are two new optional parameters in each module, and their defaults keep the current behaviour.
  • Values containing JSON escapes are now indexed in decoded form.

How it was tested

  • make test-cachedb_nats test-cachedb_nats_fts passes 1248/1248 and 242/242 under:
    • Debian trixie with gcc 14.2 and clang 19.1 (-Werror)
    • the CI images for Ubuntu 20.04 (clang-9, gcc-9), 22.04 (gcc-11) and 26.04 (clang-20)
  • Under ASan and UBSan, there are no reports from the changed code.
  • With NATS 2.15.2 JetStream, using the cachedb_nats: RTLD_GLOBAL puts its exports in the global scope; rtpengine loaded after it never forks its notification receiver #4501 configuration:
    • the RTPEngine notification receiver is forked and bound
    • documents are indexed or refused at the configured limits, both live and on the startup rebuild
    • cachedb_nats reads are refused at its configured limits

Fixes #4501

@NormB
NormB force-pushed the fix/cachedb-nats-rtld-global branch from 8b18a9d to 9a25d67 Compare November 27, 2026 03:37
@NormB
NormB force-pushed the fix/cachedb-nats-rtld-global branch from 9a25d67 to 3505e4d Compare November 27, 2026 04:54
@NormB NormB changed the title cachedb_nats: drop RTLD_GLOBAL, bind the JSON walkers for cachedb_nats_fts cachedb_nats: drop RTLD_GLOBAL (#4501); cachedb_nats_fts parses with cJSON Dec 3, 2026
@NormB
NormB force-pushed the fix/cachedb-nats-rtld-global branch from 44a45eb to 295f66d Compare December 3, 2026 23:19
NormB added 2 commits December 4, 2026 00:56
cachedb_nats was opened RTLD_NOW | RTLD_GLOBAL so that cachedb_nats_fts
could resolve its hand-written JSON scanners as symbols.  RTLD_GLOBAL
also put every other global symbol of cachedb_nats.so in the global
scope, its "struct module_exports exports" included.  In a module loaded
after it, a reference to its own "exports" then bound to cachedb_nats's:
rtpengine's notification_sock setter wrote cachedb_nats's exports.procs,
so the "RTPEngine notification receiver" process was never forked.
cachedb_nats_fts's own nats_api bound to cachedb_nats's the same way.

Open cachedb_nats with DEFAULT_DLFLAGS, and have cachedb_nats_fts parse
documents with the core's cJSON (lib/cJSON.h), so that it needs nothing
of cachedb_nats's JSON code.
cachedb_nats_fts therefore no longer has to be loaded after cachedb_nats
(before, loading it first failed on an undefined symbol); the nats and
cachedb_nats_fts READMEs stop saying so.

Field names and values are now indexed decoded ("a\"b" as a"b,
"é" as UTF-8).  A document is refused whole, with a warning, when
it is not one JSON object (trailing text included), is malformed, holds
a NUL (raw or \u0000), or exceeds the limits below.  cJSON allocates
with the system malloc here, as the index is also used from threads and
pkg memory is not thread-safe.

New cachedb_nats_fts modparams, checked when the config is read (so
opensips -C reports a bad value) and again in mod_init:
  json_max_depth  nesting limit, default 64, 1..1024 (cJSON recurses
                  once per level)
  json_max_bytes  size limit, default 1048576, at least 2

test_index_partial_snapshot passed lengths that counted the C string
terminator; the new parser refuses the NUL, as cachedb_nats's own reader
already does, so the lengths are corrected.

Tests: test_global_scope.c (cachedb_nats) checks that the dlopen flags
carry no RTLD_GLOBAL and that none of the module's symbols resolve in
the global scope; test_json_parse.c (cachedb_nats_fts) covers decoding,
special characters, refused documents, the limits and their setters,
the 1024-level ceiling on the main stack and in a thread, concurrent
indexing, the module's own scope and the README's parameter defaults.

Fixes OpenSIPS#4501
The guard run on every JSON document cachedb_nats reads back (query
rows and nats_reg_show, before cJSON) refused documents nested deeper
than 64 levels or larger than 1 MiB, both fixed at compile time.  Make
them modparams, as in cachedb_nats_fts:
  json_max_depth  default 64, 1..1024 (cJSON recurses once per level)
  json_max_bytes  default 1048576, at least 2
checked when the config is read (so opensips -C reports a bad value)
and again in mod_init.  A json_max_bytes below max_value_size logs a
warning at startup: such a usrloc row is written, then refused on every
read.

The parse copy is allocated with a size_t length, as data_len may now
reach INT_MAX.

README: both parameters, the Limitations entry, a cross-reference from
max_value_size, and the difference from cachedb_nats_fts's limits of the
same name.  test_json_limits.c covers the defaults, the setters, the
limits applied by the guard, the 1024-level ceiling, the max_value_size
warning and the README sections.
@NormB
NormB force-pushed the fix/cachedb-nats-rtld-global branch from 295f66d to 3a665c2 Compare December 4, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cachedb_nats: RTLD_GLOBAL puts its exports in the global scope; rtpengine loaded after it never forks its notification receiver

2 participants