JVM agent core (J1)
Ready
The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.
J1 is the hook for every service that runs on a JVM: Java, Kotlin, Scala, Clojure, Groovy, whatever framework. It is one Java agent jar. The JVM loads it at startup because the service's environment says so (JAVA_TOOL_OPTIONS), so the image and the source stay exactly as they are. The same jar carries the framework hooks of J2 (servlet containers), J3 (Netty and reactive stacks) and J4 (Kotlin coroutines). J1 itself covers the JDK: child processes, outgoing connections, file opens, the JDK's own HTTP server, and how a request's context follows work across threads (thread pools, CompletableFuture, ForkJoinPool, new threads, virtual threads).
Covers
| Versions | |
|---|---|
| JVM | Java 8–25 (HotSpot / OpenJDK builds: Temurin, Corretto, Zulu, Liberica, Oracle, distro packages); glibc and musl (Alpine) images; x86_64 and aarch64 |
| Core access | Java 8–21: a JNI shim (libvbxjni-<arch>.so, no libc of its own); Java 22+: FFM (no native code besides the core) |
| JDK HTTP server | com.sun.net.httpserver (HttpServer, jwebserver's server) on all of the above |
| Languages | anything that compiles to JVM bytecode |
| Frameworks | see J2, J3, J4 |
What the kernel links without the hook. A JVM serves requests from thread pools. When one pool thread handles one request from start to finish (Tomcat, Jetty and Undertow in their default blocking mode), the kernel's "one live client socket per thread" rule already links that thread's syscalls to the request. It cannot link anything a request hands to another thread (an executor, a CompletableFuture, an event loop, a coroutine dispatcher, a virtual thread moving between carriers), and it cannot tell apart the requests on one keep-alive connection. J1 adds exactly that: request boundaries with the index on the connection, and the request a spawn, connect or file open was done for, on whatever thread.
How it gets in
From a registry (local development, or a build with network; the same release as the hook directory): Maven Central garden.ctf.vulnboxd:vbxtrace-agent:1.0.0: mvn dependency:unpack -Dartifact=garden.ctf.vulnboxd:vbxtrace-agent:1.0.0:zip:dist -DoutputDirectory=target/agent, then -javaagent:target/agent/vbxtrace/jvm/vbxtrace-agent.jar; see Local development.
Compose-only: mount the hook directory read-only and add the agent to JAVA_TOOL_OPTIONS. If the service already sets JAVA_TOOL_OPTIONS, the hub appends to it with a space.
services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ environment:
+ JAVA_TOOL_OPTIONS: "-javaagent:/opt/vbxtrace/jvm/vbxtrace-agent.jar"Every JVM started in that container picks it up, also one started by a shell script, catalina.sh, Gradle or Maven. The JVM prints Picked up JAVA_TOOL_OPTIONS: ... on stderr when it does.
The hub proposes this when it detects: pom.xml, build.gradle(.kts), settings.gradle(.kts), project.clj, deps.edn, build.sbt, a *.jar in the service directory; a base image such as eclipse-temurin, openjdk, amazoncorretto, ibm-semeru-runtimes, sapmachine, azul/zulu, bellsoft/liberica, maven, gradle, tomcat, jetty, clojure, distroless Java, UBI OpenJDK or Quarkus images; java, catalina.sh, lein or sbt in the command; or dependencies on the frameworks of J2–J4. Template: hooks/jvm/j1/proposals/javaagent.toml.
The mount and the variable belong together
A JVM given -javaagent: with a jar that does not exist refuses to start ("Error opening zip file or JAR manifest missing"). That is the JVM's rule, not the agent's. Never add the variable without the mount. If the service breaks after the change, vulnboxd's rollback reverts both.
By hand: add the two blocks above to the service in docker-compose.yml, docker compose up -d <service>. Nothing else changes; the image is not rebuilt.
Files
/opt/vbxtrace/jvm/ (from /usr/local/lib/vulnboxd/trace/jvm/ on the vulnbox):
| File | What |
|---|---|
vbxtrace-agent.jar | the agent: about 300 KB, no dependencies (ASM is relocated into it) |
vbxtrace-ffm.jar | Java 22+: a small named module that links the core through FFM |
libvbxjni-x86_64.so, libvbxjni-aarch64.so | Java 8–21: the JNI shim; it loads ../libvbxtrace-<arch>.so |
Built reproducibly by hooks/jvm/build.sh (a pinned Temurin 25 / Alpine builder container; the same inputs give byte-identical files).
What it records
| Event | Emitted from | Join key |
|---|---|---|
request_begin / request_end | com.sun.net.httpserver: when the request head is parsed (ExchangeImpl created) / when the exchange's run() returns, with the response code. Frameworks: J2, J3 | peer address and port of the accepted socket, request index on that connection |
spawned | ProcessBuilder.start() and Runtime.exec() (both end in ProcessImpl.start), after the child started | child pid |
connected | Socket.connect, SocketChannel.connect / finishConnect (when connected), AsynchronousSocketChannel.connect (when the connection completes) | local port |
opened | FileOutputStream, FileInputStream, RandomAccessFile constructors; every NIO open (Files.newOutputStream, Files.write, Files.newByteChannel, FileChannel.open, … all end in UnixChannelFactory.open) | pid + fd; flags as open(2) got them (O_WRONLY|O_CREAT|O_TRUNC = 0x241, append 0x441, read 0x0, "rw" 0x42) |
context_enter / context_exit | a thread starts / stops running work of a request (see below) | context id |
Every event carries the context of the request current on the calling thread; nothing is emitted for work outside a request.
Context following
The current request is kept in an InheritableThreadLocal. On top of that, the agent follows work that leaves the thread, explicitly and without wrapping anything (the tasks an application submits are never replaced by wrappers, so executor.remove(task), shutdownNow() and instanceof checks keep working):
| Mechanism | How |
|---|---|
ThreadPoolExecutor (and subclasses, ScheduledThreadPoolExecutor, Tomcat's own pool), jboss-threads' EnhancedQueueExecutor | the request is captured with the task (FutureTask at construction, a plain Runnable at execute) and entered on the worker around the task (FutureTask.run/runAndReset, beforeExecute/afterExecute) |
ForkJoinPool, CompletableFuture.*Async, parallel streams' tasks | captured at ForkJoinTask construction (in the thread that creates the task) for one-shot tasks — the JDK's own and RecursiveTask/RecursiveAction/CountedCompleter — and entered around doExec (other ForkJoinTask subclasses, e.g. Akka/Pekko mailboxes, are reused and never capture); CompletableFuture's async tasks also when they run on another executor |
new Thread(r).start() | the thread inherits the request and reports context_enter/context_exit around Thread.run() |
| virtual threads (Java 21+) | the virtual thread inherits the request; every mount on a carrier thread reports context_enter, every unmount context_exit (so the sensor follows it across carriers) |
| pool worker threads | threads a pool creates for itself (ThreadPoolExecutor.addWorker, ForkJoinPool.createWorker, EnhancedQueueExecutor, Netty event loops, kotlinx.coroutines' dispatcher threads) do not inherit the request that happened to trigger their creation; neither do the JDK's process-reaper and virtual-thread scheduler tasks |
The sensor's view of a thread always mirrors the current request: switching from request A to B on a thread reports context_exit A, context_enter B (and back afterwards). handoff is not needed on the JVM: the context id itself travels with the work.
Work a request started keeps its id after the response went out (a background task submitted by the request still runs "for" it).
Known gaps
- Unknown thread pools that start threads lazily while a request runs: their threads inherit that request, so work the pool later does outside any task scope is attributed to it. All JDK pools, Tomcat, Jetty, Netty and kotlinx.coroutines are handled; report others.
- Pools that run tasks without
FutureTask/ForkJoinTaskand withoutThreadPoolExecutor's worker loop (custom executors with their own threads): the request does not follow; such work stays unlinked (never mislinked).jboss-threads'EnhancedQueueExecutor(Quarkus, WildFly) is followed. ThreadPoolExecutorsubclasses overridingbeforeExecute/afterExecutewithout callingsuperlose propagation for plainRunnables (submitted tasks still work).- Native code doing its own I/O (JNI database drivers,
netty-transport-native-epollconnects, SQLite JNI): noopened/connected; still linked by the kernel rule when the thread is unambiguous, or bycontext_enterwhile a context is current on that thread. - UDP (
DatagramSocket,DatagramChannel) sends are not reported asconnectedyet. - OpenJ9 / IBM Semeru: the JDK-internal hook points (
ProcessImpl,UnixChannelFactory,VirtualThread) are OpenJDK's; on OpenJ9 classes whose internals differ are simply left alone. - Java 7 and older are not supported (the agent is Java 8 bytecode); a Java 7 JVM fails to load it and starts without it after printing an error.
Safety
- What is changed: only the bytecode of the listed JDK methods and the framework methods of J2–J4, by adding a static call at entry and/or exit. Arguments, return values and exceptions pass through untouched (an exception leaving a method is rethrown as is). A class whose bytecode the agent cannot rewrite with certainty (unknown class file version, a supertype it cannot resolve without loading classes, a class loader that cannot see the agent) is left unchanged — a missing hook, never a broken class.
- Every hook is guarded: each handler catches everything. Internal errors are counted; after 5 the agent switches itself off for the process. A failing
premainnever stops the JVM (the service starts without the hook) — with the one exception of a missing jar, see above. - No tracer, no work: each request hook first asks the core whether a tracer is attached (
vbx_v1_enabled, a critical FFM downcall or a JNI call); context propagation does nothing while no request context exists, which is always the case without a tracer. - Chaos-tested: with
VBXTRACE_CHAOS=1every call into the core throws inside the agent; the sample services' responses are byte-identical with and without the hook, and without it at all. - Side effects of loading an agent: the JVM prints
Picked up JAVA_TOOL_OPTIONS: …; on Java 11+ it prints "Sharing is only supported for boot loader classes because bootstrap classpath has been appended" (the agent's hook classes must be visible from every class loader, so they go on the bootstrap class path; class-data sharing then covers only JDK classes, which costs some startup time). On Java 22+ native access is granted to the agent's own modulevbxtrace.ffmonly, so there is no restricted-method warning and the service's own native-access settings stay as they are. VBXTRACE=0in the environment: the agent does nothing at all (it returns frompremainbefore touching anything).- Overhead has not been measured yet.
Turning it off
Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service.
To remove it by hand, delete -javaagent:/opt/vbxtrace/jvm/vbxtrace-agent.jar from JAVA_TOOL_OPTIONS (and the variable if nothing else is left in it), then the mount.
Troubleshooting
- Are the probes there?
readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.solists them;sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*'too. - Is the agent loaded? The service's log starts with
Picked up JAVA_TOOL_OPTIONS: -javaagent:/opt/vbxtrace/jvm/vbxtrace-agent.jar. Inside the container,grep vbxtrace /proc/1/mapsshowslibvbxtrace-<arch>.so(and on Java 8–21libvbxjni-<arch>.so) once the agent started.VBXTRACE_DEBUG=1makes the agent print what it instrumented, everyopenedwith the file's path, and any internal error to stderr. - Which links did a flow get? The hub shows each link's method; this package's are
hook:J1(andhook:J2,hook:J3,hook:J4for the framework hooks). - The service does not start any more: the mount is missing or points elsewhere, so the JVM cannot open the jar. Check the volume line; revert the change.
- Nothing is linked:
JAVA_TOOL_OPTIONSis overwritten later (e.g. by an entrypoint script that sets it instead of appending to it), or a launcher clears the environment.jcmd <pid> VM.flagsand thePicked upline show what the JVM got. ClassFormatError/VerifyErrormentioning a hooked class: should never happen; setVBXTRACE=0, and report it with the JDK version.