Skip to content

Hook cheat sheet: OCaml ​

How to add a vulnboxd runtime hook to a OCaml 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.

OCaml ​

SetupUse this whenChangeHub automation
vulnboxd hook for cohttp / Dream (OCaml)Depends on cohttp-lwt-unix, cohttp-eio, dream, lwt or eio (in its manifest)source, rebuildpartly

With source access ​

These change the service's code or Dockerfile, then it is rebuilt.

vulnboxd hook for cohttp / Dream (OCaml) ​

  • Use this when: OCaml service that depends on cohttp-lwt-unix, cohttp-eio, dream, lwt or eio (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 vbxtrace dune library and wraps the cohttp-lwt-unix server callback (exact per-connection request index from cohttp's connection id) or adds the Dream middleware (request index unknown: Dream exposes no connection identity).

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. copy /usr/local/lib/vulnboxd/trace/native/ocaml/vbxtrace into <service>/vendor/vbxtrace
  2. copy native/c/vbxhook.c, native/c/vbxhook.h and include/vbxtrace.h from the hook directory into <service>/vendor/vbxtrace/src/
  3. cohttp-lwt-unix: add vbxtrace.cohttp-lwt-unix to the executable's libraries and replace Cohttp_lwt_unix.Server.make with Vbxtrace_cohttp_lwt_unix.make (or wrap callback and conn_closed with Vbxtrace_cohttp_lwt_unix.wrap)
  4. Dream: add vbxtrace.dream to the libraries and put Vbxtrace_dream.middleware ~port:<port> first in Dream.run's middleware chain
  5. optional: report spawned processes (Vbxtrace.spawned), outgoing sockets (Vbxtrace.connected_fd) and opened files (Vbxtrace.openfile) with Vbxtrace_lwt.current ()
  6. mount the hook directory read-only at /opt/vbxtrace in the service's compose entry

Example change on a sample service (hooks/native/c4/proposals/ocaml-cohttp.diff):

diff
# Example: a cohttp-lwt-unix service (bin/dune, bin/main.ml) after vendoring vendor/vbxtrace.
--- a/bin/dune
+++ b/bin/dune
@@ -1,3 +1,3 @@
 (executable
  (name main)
- (libraries cohttp-lwt-unix lwt.unix))
+ (libraries cohttp-lwt-unix lwt.unix vbxtrace.cohttp-lwt-unix))
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -12,3 +12,3 @@
 let () =
   Lwt_main.run
-    (Cohttp_lwt_unix.Server.create ~mode:(`TCP (`Port 8080)) (Cohttp_lwt_unix.Server.make ~callback ()))
+    (Cohttp_lwt_unix.Server.create ~mode:(`TCP (`Port 8080)) (Vbxtrace_cohttp_lwt_unix.make ~callback ()))
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,2 +3,4 @@ services:
   <service>:
     build: .
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro

Example change on a sample service (hooks/native/c4/proposals/ocaml-dream.diff):

diff
# Example: a Dream service (bin/dune, bin/main.ml) after vendoring vendor/vbxtrace.
--- a/bin/dune
+++ b/bin/dune
@@ -1,3 +1,3 @@
 (executable
  (name main)
- (libraries dream))
+ (libraries dream vbxtrace.dream))
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -1,4 +1,5 @@
 let () =
   Dream.run ~interface:"0.0.0.0" ~port:8080
+  @@ Vbxtrace_dream.middleware ~port:8080
   @@ Dream.logger
   @@ Dream.router [ Dream.get "/" (fun _ -> Dream.html "hello") ]
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,2 +3,4 @@ services:
   <service>:
     build: .
+    volumes:
+      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
Exact detection rules

Package C4 (Haskell, OCaml, Julia, R, Scala Native), proposal C4:ocaml, defined in hooks/native/c4/proposals/ocaml.toml.

  • Image matches (regex): ^ocaml/opam, ^ocurrent/opam
  • Dependency on: cohttp-lwt-unix, cohttp-eio, dream, lwt, eio
  • Files: dune-project, dune, *.opam

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.