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/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.
OCaml
| Setup | Use this when | Change | Hub automation |
|---|---|---|---|
| vulnboxd hook for cohttp / Dream (OCaml) | Depends on cohttp-lwt-unix, cohttp-eio, dream, lwt or eio (in its manifest) | source, rebuild | partly |
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,lwtoreio(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:
services:
app:
volumes:
- /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro2. Source change, then rebuild:
- copy
/usr/local/lib/vulnboxd/trace/native/ocaml/vbxtraceinto<service>/vendor/vbxtrace - copy
native/c/vbxhook.c,native/c/vbxhook.handinclude/vbxtrace.hfrom the hook directory into<service>/vendor/vbxtrace/src/ - cohttp-lwt-unix: add
vbxtrace.cohttp-lwt-unixto the executable's libraries and replaceCohttp_lwt_unix.Server.makewithVbxtrace_cohttp_lwt_unix.make(or wrapcallbackandconn_closedwithVbxtrace_cohttp_lwt_unix.wrap) - Dream: add
vbxtrace.dreamto the libraries and putVbxtrace_dream.middleware ~port:<port>first inDream.run's middleware chain - optional: report spawned processes (
Vbxtrace.spawned), outgoing sockets (Vbxtrace.connected_fd) and opened files (Vbxtrace.openfile) withVbxtrace_lwt.current () - mount the hook directory read-only at
/opt/vbxtracein the service's compose entry
Example change on a sample service (hooks/native/c4/proposals/ocaml-cohttp.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:roExample change on a sample service (hooks/native/c4/proposals/ocaml-dream.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:roExact 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):
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.