Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

feat(container): restrict sockets and process tracing in Compose (#160)

MauricioPerera23c37b9c9881
diff --git a/docker/README.md b/docker/README.md
index a9241d51c..8c99ddad2 100644
--- a/docker/README.md
+++ b/docker/README.md
@@ -228,6 +228,16 @@ fallback is no longer supported. The entrypoint preserves Codex sandbox settings
Source inspection needs a host that supports Bubblewrap; do not disable sandboxing
to work around host restrictions.
+The bundled Compose seccomp profile allows direct socket calls for Unix sockets,
+IPv4, IPv6, and the route netlink sockets Bubblewrap uses to configure loopback.
+It blocks the legacy IPv4 packet socket type and limits direct socket pairs to
+Unix sockets. Normal IP networking remains available. Use a network policy to
+restrict outbound destinations. On amd64, 32-bit i386 programs retain a legacy
+socket interface whose arguments seccomp cannot filter.
+
+The profile also blocks `ptrace`, `process_vm_readv`, and `process_vm_writev`.
+Debuggers and other tools that rely on these calls may fail inside the container.
+
`run --rm` removes only the finished runner container. Preserve its host mounts
for later stages and retries; use the same image version and source paths.
Stop active runners before backing up the entire results and state directories,
diff --git a/docker/codex-security-seccomp.json b/docker/codex-security-seccomp.json
index 9b281782c..07b63b252 100644
--- a/docker/codex-security-seccomp.json
+++ b/docker/codex-security-seccomp.json
@@ -324,7 +324,6 @@
"sigprocmask",
"sigreturn",
"socketcall",
- "socketpair",
"splice",
"stat",
"stat64",
@@ -378,24 +377,32 @@
"action": "SCMP_ACT_ALLOW"
},
{
- "names": ["process_vm_readv", "process_vm_writev", "ptrace"],
+ "names": ["socket", "socketpair"],
"action": "SCMP_ACT_ALLOW",
- "includes": { "minKernel": "4.8" }
+ "args": [{ "index": 0, "value": 1, "op": "SCMP_CMP_EQ" }]
},
{
"names": ["socket"],
"action": "SCMP_ACT_ALLOW",
- "args": [{ "index": 0, "value": 38, "op": "SCMP_CMP_LT" }]
+ "comment": "IPv4 SOCK_PACKET (10) opens a packet socket. Other assigned socket types and the CLOEXEC and NONBLOCK flags leave bit 0x8 clear.",
+ "args": [
+ { "index": 0, "value": 2, "op": "SCMP_CMP_EQ" },
+ { "index": 1, "value": 8, "valueTwo": 0, "op": "SCMP_CMP_MASKED_EQ" }
+ ]
},
{
"names": ["socket"],
"action": "SCMP_ACT_ALLOW",
- "args": [{ "index": 0, "value": 39, "op": "SCMP_CMP_EQ" }]
+ "args": [{ "index": 0, "value": 10, "op": "SCMP_CMP_EQ" }]
},
{
"names": ["socket"],
"action": "SCMP_ACT_ALLOW",
- "args": [{ "index": 0, "value": 40, "op": "SCMP_CMP_GT" }]
+ "comment": "Bubblewrap uses NETLINK_ROUTE to configure loopback in a new network namespace.",
+ "args": [
+ { "index": 0, "value": 16, "op": "SCMP_CMP_EQ" },
+ { "index": 2, "value": 0, "op": "SCMP_CMP_EQ" }
+ ]
},
{
"names": ["personality"],
diff --git a/docker/verify-container-compose.sh b/docker/verify-container-compose.sh
index 5a3679d06..5d271451f 100644
--- a/docker/verify-container-compose.sh
+++ b/docker/verify-container-compose.sh
@@ -2,6 +2,13 @@
set -euo pipefail
+# Keep the legacy i386 socket interface available on amd64.
+jq -e '
+ any(.archMap[]; .architecture == "SCMP_ARCH_X86_64" and (.subArchitectures | index("SCMP_ARCH_X86"))) and
+ any(.syscalls[]; (.names | index("socketcall")) and .action == "SCMP_ACT_ALLOW" and
+ all(.args, .includes, .excludes; length == 0))
+' docker/codex-security-seccomp.json > /dev/null
+
# Resolve the environment on the host; these checks need no running container.
for compose_file in compose.yaml compose.runner.yaml; do
endpoint="https://embeddings.example.com/custom/v1/embeddings?api-version=synthetic"
diff --git a/docker/verify-container-sandbox.sh b/docker/verify-container-sandbox.sh
index f8e24ba75..618662909 100644
--- a/docker/verify-container-sandbox.sh
+++ b/docker/verify-container-sandbox.sh
@@ -18,12 +18,45 @@ if docker info --format '{{json .SecurityOptions}}' | grep -Fq '"name=apparmor"'
fi
fi
-docker run --rm \
+docker run --rm -i \
--cap-drop ALL \
--security-opt no-new-privileges \
--security-opt "seccomp=$repository_root/docker/codex-security-seccomp.json" \
"${apparmor_options[@]}" \
- --entrypoint /bin/sh "$image" -ec '
- cd "$(dirname "$(readlink -f /usr/local/bin/codex-security)")"
- exec npm exec --offline -- codex sandbox /usr/bin/true
- '
+ --entrypoint /bin/sh "$image" -es <<'SH'
+unshare --user --map-root-user --net python3 - <<'PYTHON'
+import ctypes
+import errno
+import os
+import socket
+from unittest import TestCase
+
+check = TestCase()
+socket.socket(socket.AF_INET).close()
+try:
+ socket.socket(socket.AF_INET6).close()
+except OSError as exc:
+ if exc.errno != errno.EAFNOSUPPORT:
+ raise
+# A fresh user and network namespace grants CAP_NET_RAW only in this isolated namespace.
+socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP).close()
+packet_type = 10 | socket.SOCK_NONBLOCK | socket.SOCK_CLOEXEC # Linux SOCK_PACKET with flags
+check.assertRaises(PermissionError, socket.socket, socket.AF_INET, packet_type, 0)
+check.assertRaises(PermissionError, socket.socket, socket.AF_PACKET, socket.SOCK_DGRAM)
+for endpoint in socket.socketpair():
+ endpoint.close()
+socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE).close()
+
+check.assertRaises(PermissionError, socket.socket, socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_USERSOCK)
+check.assertRaises(PermissionError, socket.socketpair, socket.AF_TIPC, socket.SOCK_SEQPACKET)
+
+libc = ctypes.CDLL(None, use_errno=True)
+result = libc.ptrace(0, 0, None, None) # PTRACE_TRACEME operates on the calling process.
+check.assertEqual((result, ctypes.get_errno()), (-1, errno.EPERM), "ptrace")
+for call in (libc.process_vm_readv, libc.process_vm_writev):
+ result = call(os.getpid(), None, 0, None, 0, 0)
+ check.assertEqual((result, ctypes.get_errno()), (-1, errno.EPERM), call.__name__)
+PYTHON
+cd "$(dirname "$(readlink -f /usr/local/bin/codex-security)")"
+exec npm exec --offline -- codex sandbox /usr/bin/true
+SH