Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.StampedLock;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -97,6 +98,13 @@ private static class CtxAndAddresses {
final Collection<Address<?>> addressesOfInterest;
final WafHandle ctx;

/**
* Read-held while creating a context from {@link #ctx}; write-held while closing it. Keep the
* protected section callback-free: StampedLock is not reentrant. This protects context creation
* until libddwaf-java locks and checks the handle itself.
*/
final StampedLock handleLock = new StampedLock();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small thought: since StampedLock is not reentrant, it seems that if something inside the read-locked section ever re-entered doRunWaf on the same thread while the RC thread is waiting for the write lock, it could deadlock. As far as I can see this can't happen today, because the section only builds a WafContext under synchronized(reqCtx). Do you think it would be worth adding a short note to the Javadoc saying this section should stay callback-free? Totally optional, just in case it helps future readers.


private CtxAndAddresses(Collection<Address<?>> addressesOfInterest, WafHandle ctx) {
this.addressesOfInterest = addressesOfInterest;
this.ctx = ctx;
Expand Down Expand Up @@ -208,7 +216,13 @@ private void initOrUpdateWafHandle(AppSecModuleConfigurer.Reconfiguration reconf
}

if (prevContextAndAddresses != null) {
prevContextAndAddresses.ctx.close();
// Context creation must finish acquiring native ruleset ownership before retiring the handle.
long stamp = prevContextAndAddresses.handleLock.writeLock();
try {
prevContextAndAddresses.ctx.close();
} finally {
prevContextAndAddresses.handleLock.unlockWrite(stamp);
}
}

reconf.reloadSubscriptions();
Expand Down Expand Up @@ -568,8 +582,30 @@ private Waf.ResultWithData doRunWaf(
CtxAndAddresses ctxAndAddr,
GatewayContext gwCtx)
throws AbstractWafException {
WafContext wafContext =
reqCtx.getOrCreateWafContext(ctxAndAddr.ctx, wafMetricsEnabled, gwCtx.isRasp);
// Existing contexts own their native ruleset, so using one needs no lock.
WafContext wafContext = reqCtx.getWafContextIfReady(wafMetricsEnabled, gwCtx.isRasp);
if (wafContext == null) {
for (; ; ) {
long stamp = ctxAndAddr.handleLock.tryReadLock();
if (stamp == 0L) {
// Avoid waiting for destruction of a retired handle; retry with the current snapshot.
ctxAndAddr = ctxAndAddresses.get();
continue;
}
try {
// A callback may have captured this snapshot before remote config replaced it; never
// create a context from a retired handle.
if (ctxAndAddr == ctxAndAddresses.get()) {
wafContext =
reqCtx.getOrCreateWafContext(ctxAndAddr.ctx, wafMetricsEnabled, gwCtx.isRasp);
break;
}
} finally {
ctxAndAddr.handleLock.unlockRead(stamp);
}
ctxAndAddr = ctxAndAddresses.get();
}
}
if (wafContext == null) {
// Context closed concurrently with the isWafContextClosed() check in onDataAvailable; skip
// (APPSEC-69085).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public WafContext getOrCreateWafContext(
if (wafContextClosed) {
return null;
}
if (createMetrics) {
if (!metricsReady(createMetrics, isRasp)) {
if (wafMetrics == null) {
this.wafMetrics = new WafMetrics();
}
Expand All @@ -410,6 +410,24 @@ public WafContext getOrCreateWafContext(
}
}

/**
* Returns the request's open {@link WafContext} without locking, or {@code null} when it does not
* exist yet, is closed, or still lacks metrics that {@link #getOrCreateWafContext} would create.
* On {@code null}, callers fall back to {@link #getOrCreateWafContext}.
*/
public WafContext getWafContextIfReady(boolean createMetrics, boolean isRasp) {
WafContext curWafContext = this.wafContext;
if (curWafContext == null || wafContextClosed) {
return null;
}
return metricsReady(createMetrics, isRasp) ? curWafContext : null;
}

/** Whether the metrics a WAF call with these flags needs already exist. */
private boolean metricsReady(boolean createMetrics, boolean isRasp) {
return !createMetrics || (wafMetrics != null && (!isRasp || raspMetrics != null));
}

public void closeWafContext() {
if (wafContextClosed) {
// Fast path for the common case of redundant close() calls (e.g. the generic fallback
Expand Down
Loading
Loading