Repository navigation
Keep slf4j names out of the native logging init (APPSEC-71631) - #222
Conversation
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
3fcf6c7 to
7e6207c
Compare
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
cataphract
left a comment
There was a problem hiding this comment.
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
91d01cf to
a45ea62
Compare
|
@cataphract thanks! We converged on the same approach. Since your review (on 7e6207c), the branch has:
One difference from your branch: unknown levels map to Evidence:
Your approval was on the previous version, so would you mind taking another look? |
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
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.
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>
What does this PR do?
Moves every slf4j call of the native logging init (
java_log_initinsrc/main/c/logging.c) intocom.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.slf4jin 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):org.slf4jtodatadog.slf4j, so the shadedInfoToDebugLoggeronly has<init>(Ldatadog/slf4j/Logger;)V.JNI_OnLoad,FindClassresolves 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.Levelis visible whenever slf4j-api is a named or automatic module in the boot layer.java_log_inithad both slf4j signature tables hardcoded (org/slf4janddatadog/slf4j). It probedorg/slf4j/event/Levelfirst and, when it was found, used theorgtable, then failed on theInfoToDebugLoggerconstructor 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[])andjniIsLoggable(int), where theintis theDDWAF_LOG_LEVELvalue, mapped toLevelin Java (unknown values map toDEBUG, as the native side did). The existing public constructor andlog(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, theLevel/LoggerFactorylookups and the five weak global refs to theLevelconstants.java_log_initnow only looks upInfoToDebugLogger.jniCreate,jniLogandjniIsLoggable, and the log callbacks pass the level as ajint. No slf4j name is left in the native code or in the built library.build.gradle:relocatedBindingJar,relocatedBindingArbitraryPrefixJarandrelocatedSlf4jJar:ShadowJartasks that build the binding (main classes and resources, includingnative_libs/, plus slf4j-api) relocated todatadog.slf4jand toshaded.org.slf4j, and slf4j-api alone relocated todatadog.slf4j. They are written tobuild/relocation-test-libsand are not part ofassembleor the publication.com.github.johnrengelman.shadow8.1.1 declared withapply false. Applying it would add a variant tocomponents.javaand 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.testRelocationsource set andtestSlf4jRelocationtask, wired intocheck. 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 = 1because the native init state is per process. Jacoco is disabled on it because the classes under test are relocated copies.com.datadog.ddwaf.Wafand thatInfoToDebugLoggeris defined by the binding loader with the expected constructor, then requiresWaf.initialize(false)to succeed):RelocatedBindingInitTest(regression): binding relocated todatadog.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 toshaded.org.slf4j.UnrelocatedBindingWithRelocatedSlf4jVisibleTest: unrelocated binding whose loader also sees a relocateddatadog/slf4j, as an unshaded binding does in an application with dd-java-agent attached (the agent putsdatadog/slf4jon the bootstrap class path).<prefix>.simpleLogger.*), and the relocated tests assert that the relocatedLoggerFactoryis not NOP and that the nativeDEBUG ddwaf_native - Deinitializing JNI libraryline fromWaf.deinitialize()is forwarded.InfoToDebugLoggerTest(Groovy, no native) covers the level mapping,jniIsLoggableandjniLog.logging.c: a_Static_assertchecks thatDDWAF_LOG_LEVELstill has the valuesInfoToDebugLoggermirrors (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
InfoToDebugLoggerconstructor 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.candInfoToDebugLogger.java::testSlf4jRelocationfails:RelocatedBindingInitTestandArbitraryPrefixRelocatedBindingInitTestfail,UnrelocatedBindingWithRelocatedSlf4jVisibleTestpasses (3 tests, 2 failed)The
Testmatrix did not run on that commit because it depends on the jobs above. The failure is the production error, raised fromWaf.initializeafter all the preconditions passed:CI with this change: run 44094333869 on 7a232fa (final head) is fully green, including
Coverage,ASAN on Linuxand the wholeTestmatrix 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).ArbitraryPrefixRelocatedBindingInitTestwith the first version of this PR (constructor probe, local run swapping onlylogging.candInfoToDebugLogger.java): fails withCould 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 thedatadog.slf4jprefix.Local results with this PR (macOS arm64):
./gradlew check jacocoTestReport(JDK 11)test207 tests, 0 failures (3 skipped);testgc1/0;testSlf4jRelocation3/0; no-Xcheck:jniwarningsddwaf_nativelog lines reaching slf4j in thetestsuite./gradlew helpwith Gradle on JDK 8End 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-pathlaunches 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, theddwaf_nativelogs 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.