Skip to content

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/trace is the hook directory vulnboxd installs on the vulnbox. Compose snippets mount it read-only at /opt/vbxtrace in 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 ​

SetupUse this whenChangeHub automation
Add the vulnboxd hook to a Kotlin/Native Ktor serviceDepends on io.ktor:ktor-server-cio or ktor-server-cio-linuxx64 (in its manifest)source, rebuildpartly

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-cio or ktor-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:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro

2. Source change, then rebuild:

  1. vendor: run /usr/local/lib/vulnboxd/trace/native/kotlin/vendor.sh <service-dir> (copies vbxhook.def, vbxhook.c/.h, vbxtrace.h and kotlin/VbxTrace.kt, kotlin/VbxTraceKtor.kt to vendor/vbxtrace)
  2. build.gradle.kts, in the linuxX64 (or linuxArm64) 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")
  3. Ktor module: install(VbxTracePlugin) before any other plugin (import vbxtrace.VbxTracePlugin)
  4. inside handlers: val ctx = VbxTrace.current(); after posix_spawn (platform.linux.posix_spawn, not platform.posix) or fork: VbxTrace.spawned(ctx, pid); open(...) → VbxTrace.open(ctx, path, flags, mode); aSocket(selector).tcp().connect(h, p) → VbxTrace.connect(selector, h, p) (or VbxTrace.connected(socket) after any connect)
  5. work moved to a Worker / pthread / C callback: val child = VbxTrace.handoff(ctx), then VbxTrace.runHandedOff(child) { ... } there
  6. make sure the Dockerfile copies vendor/ before the Gradle build

Example change on a sample service (hooks/native/c3/proposals/kotlin-native-ktor.diff):

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 linkReleaseExecutableLinuxX64
Exact 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):

sh
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.