Skip to content

Keep slf4j names out of the native logging init (APPSEC-71631) - #222

Merged
jandro996 merged 13 commits into
masterfrom
alejandro.gonzalez/APPSEC-71631-slf4j-table-by-ctor
Nov 18, 2026
Merged

jandro996 merged 13 commits into
masterfrom
alejandro.gonzalez/APPSEC-71631-slf4j-table-by-ctor

Conversation

@jandro996

@jandro996 jandro996 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Moves every slf4j call of the native logging init (java_log_init in src/main/c/logging.c) into com.datadog.ddwaf.logging.InfoToDebugLogger, behind JNI entry points whose signatures only use JDK types. The native code no longer names any slf4j class, so the binding initializes correctly however slf4j is relocated (datadog.slf4j in dd-java-agent, any other prefix, or not relocated), and no matter which other copy of slf4j is visible from its class loader. Adds three tests that run against relocated copies of the binding.

Motivation

APPSEC-71631: with dd-java-agent, AppSec never starts when slf4j-api is resolved in the JVM boot layer (Mule 4.x on JDK 11+, modular apps launched with --module-path/-m, --add-modules org.slf4j, jlink images that include slf4j):

WARN com.datadog.appsec.ddwaf.WafInitialization - Error initializing WAF library
java.lang.RuntimeException: Could not find method com/datadog/ddwaf/logging/InfoToDebugLogger.<init>(Lorg/slf4j/Logger;)V during native function java_meth_init_checked, file java_call.c, line 52
Caused by: java.lang.NoSuchMethodError
  • dd-java-agent relocates org.slf4j to datadog.slf4j, so the shaded InfoToDebugLogger only has <init>(Ldatadog/slf4j/Logger;)V.
  • In JNI_OnLoad, FindClass resolves through the agent class loader, which is parented on the platform loader. The JDK builtin loaders share one package-to-module map, so the platform loader also resolves packages of named modules defined to the app loader: org.slf4j.event.Level is visible whenever slf4j-api is a named or automatic module in the boot layer.
  • java_log_init had both slf4j signature tables hardcoded (org/slf4j and datadog/slf4j). It probed org/slf4j/event/Level first and, when it was found, used the org table, then failed on the InfoToDebugLogger constructor lookup.

This probe order dates from 2021, so every libsqreen version is affected. Class path launches are not.

Jira ticket: APPSEC-71631

Description of the Change

  • InfoToDebugLogger: new JNI entry points with no slf4j types in their signatures: static jniCreate(String) (new InfoToDebugLogger(LoggerFactory.getLogger(name))), jniLog(int, Throwable, String, Object[]) and jniIsLoggable(int), where the int is the DDWAF_LOG_LEVEL value, mapped to Level in Java (unknown values map to DEBUG, as the native side did). The existing public constructor and log(Level, ...)/isLoggable(Level) methods are unchanged. All slf4j references now live in this class's bytecode, which any shading tool relocates with any prefix.
  • logging.c: removes both signature tables, the Level/LoggerFactory lookups and the five weak global refs to the Level constants. java_log_init now only looks up InfoToDebugLogger.jniCreate, jniLog and jniIsLoggable, and the log callbacks pass the level as a jint. No slf4j name is left in the native code or in the built library.
  • build.gradle:
    • relocatedBindingJar, relocatedBindingArbitraryPrefixJar and relocatedSlf4jJar: ShadowJar tasks that build the binding (main classes and resources, including native_libs/, plus slf4j-api) relocated to datadog.slf4j and to shaded.org.slf4j, and slf4j-api alone relocated to datadog.slf4j. They are written to build/relocation-test-libs and are not part of assemble or the publication.
    • The shadow plugin is com.github.johnrengelman.shadow 8.1.1 declared with apply false. Applying it would add a variant to components.java and change the published artifact. 8.1.1 is used because some CI jobs run Gradle on JDK 8, and newer Shadow releases need Java 11.
    • testRelocation source set and testSlf4jRelocation task, wired into check. Its classpath has junit, hamcrest and the unrelocated slf4j-api/slf4j-simple, but not the main output; the jar and directory paths are passed as system properties. forkEvery = 1 because the native init state is per process. Jacoco is disabled on it because the classes under test are relocated copies.
  • Tests (each asserts first that the parent loader cannot load com.datadog.ddwaf.Waf and that InfoToDebugLogger is defined by the binding loader with the expected constructor, then requires Waf.initialize(false) to succeed):
    • RelocatedBindingInitTest (regression): binding relocated to datadog.slf4j, with the unrelocated slf4j-api visible from its loader. The native decision only depends on what is visible from the binding loader, so this reproduces the bug without JPMS and runs on every JVM of the matrix, JDK 8 included.
    • ArbitraryPrefixRelocatedBindingInitTest: same, relocated to shaded.org.slf4j.
    • UnrelocatedBindingWithRelocatedSlf4jVisibleTest: unrelocated binding whose loader also sees a relocated datadog/slf4j, as an unshaded binding does in an application with dd-java-agent attached (the agent puts datadog/slf4j on the bootstrap class path).
  • Native log forwarding is tested in the relocated scenarios: slf4j-simple is shaded into both relocated binding jars (Shadow relocates its property keys too, so the tests set <prefix>.simpleLogger.*), and the relocated tests assert that the relocated LoggerFactory is not NOP and that the native DEBUG ddwaf_native - Deinitializing JNI library line from Waf.deinitialize() is forwarded. InfoToDebugLoggerTest (Groovy, no native) covers the level mapping, jniIsLoggable and jniLog.
  • logging.c: a _Static_assert checks that DDWAF_LOG_LEVEL still has the values InfoToDebugLogger mirrors (TRACE=0 .. ERROR=4).
  • AGENTS.md: documents the new task and the "no slf4j names in native code" rule.

No public Java API change. dd-trace-java only needs the version bump.

Additional Notes

The first version of this PR kept both tables in C and chose one by probing which InfoToDebugLogger constructor exists. That fixed dd-java-agent, but any other relocation prefix would still have failed, hence the move to Java. Moving only the strings out of the binary was not enough: Shadow only rewrites string constants that are a whole class name, not method descriptors.

CI evidence that the relocation tests fail without this change: run 43861442545, on a temporary commit (since dropped) that put back master's logging.c and InfoToDebugLogger.java:

Job Result with master's native logging init
Development tests (ubuntu-22.04) :testSlf4jRelocation fails: RelocatedBindingInitTest and ArbitraryPrefixRelocatedBindingInitTest fail, UnrelocatedBindingWithRelocatedSlf4jVisibleTest passes (3 tests, 2 failed)
Development tests (macos-15) same
Coverage same
ASAN on Linux same

The Test matrix did not run on that commit because it depends on the jobs above. The failure is the production error, raised from Waf.initialize after all the preconditions passed:

RelocatedBindingInitTest > initializesWithOrgSlf4jVisibleFromBindingLoader FAILED
java.lang.AssertionError: Waf.initialize failed with the relocated binding
Caused by: java.lang.RuntimeException: Could not find method com/datadog/ddwaf/logging/InfoToDebugLogger.<init>(Lorg/slf4j/Logger;)V during native function java_meth_init_checked, file java_call.c, line 52
Caused by: java.lang.NoSuchMethodError: Lcom/datadog/ddwaf/logging/InfoToDebugLogger;.<init>(Lorg/slf4j/Logger;)V

CI with this change: run 44094333869 on 7a232fa (final head) is fully green, including Coverage, ASAN on Linux and the whole Test matrix on the release binaries (HotSpot 8/11/17/21/25, Semeru/OpenJ9 8/11/17/21, glibc and musl, CentOS 6/7, macOS and Windows).

ArbitraryPrefixRelocatedBindingInitTest with the first version of this PR (constructor probe, local run swapping only logging.c and InfoToDebugLogger.java): fails with Could not find a constructor com/datadog/ddwaf/logging/InfoToDebugLogger.<init> taking either (Ldatadog/slf4j/Logger;)V or (Lorg/slf4j/Logger;)V, so that version only fixed the datadog.slf4j prefix.

Local results with this PR (macOS arm64):

Check Result
./gradlew check jacocoTestReport (JDK 11) test 207 tests, 0 failures (3 skipped); testgc 1/0; testSlf4jRelocation 3/0; no -Xcheck:jni warnings
ddwaf_native log lines reaching slf4j in the test suite present (2014 lines)
./gradlew help with Gradle on JDK 8 OK
clang-format 18 clean

End to end, with a dd-java-agent built from the libsqreen 17.7.0 bump (DataDog/dd-trace-java#13369) plus this change (7431e4b): AppSec is fully enabled for --module-path launches on JDK 11/17/21 with slf4j 2.0.16 and 1.7.36, for --add-modules org.slf4j, for class path launches, and on a stock Mule 4.9.0 CE (JPMS launch, default domain deployed). With -Ddd.trace.debug=true, the ddwaf_native logs go through the agent logger. The unpatched agent still fails on a module path launch with the original error.

Not covered by these tests: a real JPMS boot layer (the end-to-end guard belongs in a dd-trace-java smoke test).

Released in libsqreen 17.7.1 (#223). Follow-up: bump libsqreen in dd-trace-java.

java_log_init picked the org/slf4j or datadog/slf4j signature table based
on whether org/slf4j/event/Level was visible from the binding's class
loader. When the binding is relocated to datadog/slf4j (as in
dd-java-agent), org/slf4j can still be visible, e.g. through slf4j as a
named module in the JPMS boot layer. The org table was then chosen and
native init failed looking up InfoToDebugLogger.<init>(Lorg/slf4j/Logger;)V.

Detect the table from the constructor InfoToDebugLogger actually has,
trying the datadog descriptor first and then the org one.

APPSEC-71631
Add relocatedBindingJar (main classes and resources, including
native_libs/, plus slf4j-api) and relocatedSlf4jJar (slf4j-api only),
both with org.slf4j relocated to datadog.slf4j. They reproduce how
dd-java-agent shades this binding, so tests can load it the way the
agent does.

The shadow plugin is pinned to 8.1.1, the last line that runs on the
JDK 8 Gradle daemon used in CI, and declared with apply false: only the
ShadowJar task type is used, so components.java and the published
artifact do not change.

APPSEC-71631
Add the testRelocation source set and the testSlf4jRelocation task, wired
into check. The task classpath holds only JUnit, hamcrest and unrelocated
slf4j, never the binding itself; tests get the relocated jars and the main
output dirs as system properties and build their own class loaders. One
JVM per test class, since the native init state is per process.

APPSEC-71631
Initialize the binding relocated to datadog.slf4j, as dd-java-agent ships it, from a
class loader that can also see an unrelated org.slf4j. Preconditions assert the
scenario: the parent cannot load the binding, org.slf4j.event.Level is visible, and
InfoToDebugLogger only has the datadog.slf4j constructor.

APPSEC-71631
An unshaded binding running next to dd-java-agent sees both org.slf4j and
the relocated datadog.slf4j. The native init must still pick the org.slf4j
signatures from the InfoToDebugLogger constructor, so this guards against
choosing the table by probing datadog.slf4j first.

APPSEC-71631
- Disable jacoco on testSlf4jRelocation: the classes are relocated copies and
  jacocoTestReport read its exec file without a declared dependency
- Write the relocated test jars outside build/libs, which the release
  packaging step copies with libsqreen-*.jar
- Skip the relocation tests when the Gradle-provided properties are missing
- Check that native_libs/ exists before building the mirror test loader
- Assign slf4j_active only after the table detection succeeds
- Drop redundant comments and use the current year in new file headers

APPSEC-71631
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Pipelines

🎯 Code Coverage (details)
• Patch Coverage: 68.18%
• Overall Coverage: 57.30% (+0.79%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 7a232fa | Docs | Give us feedback!

@jandro996
jandro996 force-pushed the alejandro.gonzalez/APPSEC-71631-slf4j-table-by-ctor branch from 3fcf6c7 to 7e6207c Compare November 12, 2026 10:26
@jandro996
jandro996 marked this pull request as ready for review November 12, 2026 11:16
@jandro996
jandro996 requested a review from a team as a code owner November 12, 2026 11:16
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T11:17:47.754823Z 7e6207c Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Move every slf4j touchpoint into InfoToDebugLogger. It gets JNI entry
points whose signatures use only JDK types and primitives: a static
create(String) factory, log(int, ...) and isLoggable(int), where the int
is the DDWAF_LOG_LEVEL value mapped to an slf4j Level in Java.

The native side now references no slf4j class or descriptor, so a
shading tool relocates all slf4j usage in the bytecode with any prefix.
This replaces the choice between two hardcoded org/datadog tables based
on the InfoToDebugLogger constructor, which still broke for any other
relocation prefix. It also drops the Level/LoggerFactory lookups and the
weak global refs to the Level constants.

APPSEC-71631
Relocate org.slf4j to shaded.org.slf4j and initialize the binding from a
loader that also sees the unrelocated org.slf4j. Native code names no
slf4j class, so any relocation prefix must work, not only the
datadog.slf4j prefix used by dd-java-agent.

APPSEC-71631
@jandro996 jandro996 changed the title Choose the slf4j signature table from the InfoToDebugLogger constructor (APPSEC-71631) Keep slf4j names out of the native logging init (APPSEC-71631) Nov 16, 2026

@cataphract cataphract left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, although this could be generalized for every relocation, either by moving the class name to a Java class so it's automatically relocated by shadow or by removing references in JNI code to the (possibly) relocated slf4j in C code. See alejandro.gonzalez/APPSEC-71631-slf4j-table-by-ctor...glopes/APPSEC-71631-slf4j-table-by-ctor

Use distinct names (jniCreate, jniLog, jniIsLoggable) instead of int
overloads of the public log/isLoggable methods, and update the test
javadocs that still described the constructor-probe approach.
Suggested in review.

APPSEC-71631
@jandro996
jandro996 force-pushed the alejandro.gonzalez/APPSEC-71631-slf4j-table-by-ctor branch from 91d01cf to a45ea62 Compare November 16, 2026 12:22
@jandro996

Copy link
Copy Markdown
Member Author

@cataphract thanks! We converged on the same approach. Since your review (on 7e6207c), the branch has:

  • d15fe8b: removes every slf4j reference from the C code. All slf4j calls now go through InfoToDebugLogger entry points that only take JDK types, so shading relocates them with any prefix. No slf4j string is left in the built library.
  • f8c41f8: adds ArbitraryPrefixRelocatedBindingInitTest, which relocates the binding to shaded.org.slf4j. It fails on master and on the previous constructor-probe version, and passes now.
  • a45ea62: takes the jni* names (jniCreate, jniLog, jniIsLoggable) and the test javadoc wording from your branch.

One difference from your branch: unknown levels map to DEBUG, as the native side did before (_lvl_api_to_java), and the levels are named constants that mirror DDWAF_LOG_LEVEL.

Evidence:

  • With master's logging.c and InfoToDebugLogger.java put back, RelocatedBindingInitTest and ArbitraryPrefixRelocatedBindingInitTest fail in CI with the production error (run).
  • CI is fully green on a45ea62 (run), including the Semeru jobs.
  • I re-ran the end to end check with a dd-java-agent built from this branch: AppSec is fully enabled with module path launches (JDK 11/17/21, slf4j 2.0.16 and 1.7.36), with --add-modules org.slf4j, with class path launches and on stock Mule 4.9.0 CE.

Your approval was on the previous version, so would you mind taking another look?

@jandro996
jandro996 requested a review from cataphract November 17, 2026 08:37
Comment thread build.gradle Resolved
jniLog and jniIsLoggable receive the DDWAF_LOG_LEVEL value as a jint and
InfoToDebugLogger maps it with hand-copied constants. A _Static_assert in
logging.c turns a change of the enum values into a native build error.

APPSEC-71631
The relocated binding jars shipped slf4j-api without a binding, so the
relocated LoggerFactory returned a NOPLogger and no native log was ever
forwarded in the relocation tests. Shade slf4j-simple into them, log at
trace to a temp file and check that the logger is not a NOPLogger and that
the native DEBUG line of Waf.deinitialize arrives.

Add a unit test for the JNI entry points of InfoToDebugLogger: level
mapping (INFO and unknown levels as DEBUG), formatting, throwable and
disabled levels.

APPSEC-71631
@jandro996
jandro996 merged commit bb01f4e into master Nov 18, 2026
42 checks passed
@jandro996
jandro996 deleted the alejandro.gonzalez/APPSEC-71631-slf4j-table-by-ctor branch November 18, 2026 11:31
jandro996 added a commit to DataDog/dd-trace-java that referenced this pull request Nov 24, 2026
libsqreen 17.7.1 keeps slf4j class names out of the native logging
init (DataDog/libddwaf-java#222), so AppSec starts when slf4j-api is a
named module in the JPMS boot layer (e.g. Mule 4.x on JDK 11+). Same
libddwaf 2.2.1 and Java API as 17.7.0.

Also updates the libsqreen line of the gradle.lockfile files that pin
it at 17.7.0, one line per file.
gh-worker-dd-mergequeue-cf854d Bot pushed a commit to DataDog/dd-trace-java that referenced this pull request Nov 24, 2026
Bump io.sqreen:libsqreen to 17.7.1

libsqreen 17.7.1 keeps slf4j class names out of the native logging
init (DataDog/libddwaf-java#222), so AppSec starts when slf4j-api is a
named module in the JPMS boot layer (e.g. Mule 4.x on JDK 11+). Same
libddwaf 2.2.1 and Java API as 17.7.0.

Also updates the libsqreen line of the gradle.lockfile files that pin
it at 17.7.0, one line per file.

Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
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.

3 participants