Hook cheat sheet: Kotlin/Native
How to add a vulnboxd runtime hook to a Kotlin/Native service by hand. Find your setup in the table, then follow its block.
- Without source access: only the compose file changes; redeploy. With source access: you change the code or Dockerfile and rebuild.
- Hub automation says how much the hub's proposals do for you. Fully: approve the proposal in the hub; the block is the fallback when it failed. With conditions: the same, when the conditions listed hold. Partly: the hub pushes the compose part, the rest is yours. Not at all: everything is by hand.
/usr/local/lib/vulnboxd/traceis the hook directory vulnboxd installs on the vulnbox. Compose snippets mount it read-only at/opt/vbxtracein the container.- How to check that the hook works and undo it is at the end of the page.
- Runtime hooks explains what a hook does.
Kotlin/Native
| Setup | Use this when | Change | Hub automation |
|---|---|---|---|
| Add the vulnboxd hook to a Kotlin/Native Ktor service | Depends on io.ktor:ktor-server-cio or ktor-server-cio-linuxx64 (in its manifest) | source, rebuild | partly |
With source access
These change the service's code or Dockerfile, then it is rebuilt.
Add the vulnboxd hook to a Kotlin/Native Ktor service
- Use this when: Kotlin/Native service that depends on
io.ktor:ktor-server-cioorktor-server-cio-linuxx64(in its manifest). - Hub automation: partly. The hub pushes the compose lines; the source change is yours.
- Check and undo: hook mounted into the container
Vendors the Kotlin/Native binding into vendor/vbxtrace (cinterop def + vbxhook.c/.h + vbxtrace.h + Kotlin sources), adds the cinterop and source dir to build.gradle.kts, installs VbxTracePlugin first in the Ktor module, and reports posix_spawn / open / ktor-network connects through VbxTrace; mounts the hook directory read-only.
1. Compose: merge into the service (app is its name) and redeploy:
services:
app:
volumes:
- /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro2. Source change, then rebuild:
- vendor: run
/usr/local/lib/vulnboxd/trace/native/kotlin/vendor.sh <service-dir>(copiesvbxhook.def,vbxhook.c/.h,vbxtrace.handkotlin/VbxTrace.kt,kotlin/VbxTraceKtor.kttovendor/vbxtrace) build.gradle.kts, in thelinuxX64(orlinuxArm64) target:compilations.getByName("main") { cinterops { create("vbxhook") { defFile(project.file("vendor/vbxtrace/vbxhook.def")); includeDirs(project.file("vendor/vbxtrace")) } } }; in the native main source set:kotlin.srcDir("vendor/vbxtrace/kotlin")- Ktor module:
install(VbxTracePlugin)before any other plugin (import vbxtrace.VbxTracePlugin) - inside handlers:
val ctx = VbxTrace.current(); afterposix_spawn(platform.linux.posix_spawn, notplatform.posix) orfork:VbxTrace.spawned(ctx, pid);open(...)→VbxTrace.open(ctx, path, flags, mode);aSocket(selector).tcp().connect(h, p)→VbxTrace.connect(selector, h, p)(orVbxTrace.connected(socket)after any connect) - work moved to a Worker / pthread / C callback:
val child = VbxTrace.handoff(ctx), thenVbxTrace.runHandedOff(child) { ... }there - make sure the Dockerfile copies
vendor/before the Gradle build
Example change on a sample service (hooks/native/c3/proposals/kotlin-native-ktor.diff):
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -8,11 +8,20 @@ repositories {
kotlin {
linuxX64 {
+ compilations.getByName("main") {
+ cinterops {
+ create("vbxhook") {
+ defFile(project.file("vendor/vbxtrace/vbxhook.def"))
+ includeDirs(project.file("vendor/vbxtrace"))
+ }
+ }
+ }
binaries {
executable { entryPoint = "main" }
}
}
sourceSets {
val linuxX64Main by getting {
+ kotlin.srcDir("vendor/vbxtrace/kotlin")
dependencies {
implementation("io.ktor:ktor-server-cio:3.1.3")
}
--- a/src/linuxX64Main/kotlin/Main.kt
+++ b/src/linuxX64Main/kotlin/Main.kt
@@ -1,20 +1,25 @@
import io.ktor.server.application.install
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import platform.posix.O_RDONLY
-import platform.posix.open
import platform.posix.close
+import vbxtrace.VbxTrace
+import vbxtrace.VbxTracePlugin
fun main() {
embeddedServer(CIO, port = 8080) {
+ install(VbxTracePlugin)
install(CallLogging)
routing {
get("/flag/{id}") {
- val fd = open("/data/" + call.parameters["id"], O_RDONLY)
+ val fd = VbxTrace.open(VbxTrace.current(), "/data/" + call.parameters["id"], O_RDONLY)
if (fd < 0) return@get call.respondText("no", status = io.ktor.http.HttpStatusCode.NotFound)
close(fd)
call.respondText("ok")
}
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,6 +2,7 @@ FROM gradle:8.14-jdk21 AS build
WORKDIR /app
COPY settings.gradle.kts build.gradle.kts ./
+COPY vendor ./vendor
COPY src ./src
RUN gradle --no-daemon linkReleaseExecutableLinuxX64Exact detection rules
Package C3 (Swift, Dart, Kotlin/Native), proposal C3:kotlin-native, defined in hooks/native/c3/proposals/kotlin-native.toml.
- Image matches (regex):
^(docker\.io/)?(library/)?gradle:,^(docker\.io/)?(library/)?(debian|ubuntu)(:|$) - Command matches (regex):
\.kexe\b,linkReleaseExecutableLinux - Dependency on:
io.ktor:ktor-server-cio,ktor-server-cio-linuxx64 - Files:
build.gradle.kts,settings.gradle.kts,src/linuxX64Main/kotlin/*.kt,src/nativeMain/kotlin/*.kt - Sources match (regex):
\blinuxX64\s*\(,\blinuxArm64\s*\(
Check that it works, and undo
Each block links the variant it needs.
Hook mounted into the container
Check: After the redeploy, this lists the processes that loaded the hook (empty: it is not loaded):
docker compose exec app sh -c 'grep -l vbxtrace /proc/[0-9]*/maps'The hub's Trace tab then shows links whose method starts with hook:.
Undo: Set VBXTRACE: "0" in the service's environment and restart it, or revert the commit (the hub's proposal screen has a revert per container). Remove the mount of /opt/vbxtrace only together with what loads from it: a runtime told to preload a missing file may refuse to start.