|
| 1 | +# Docker Socket Proxy Security |
| 2 | + |
| 3 | +This dev container uses [Tecnativa's docker-socket-proxy](https://github.com/Tecnativa/docker-socket-proxy) to provide secure, filtered access to the Docker daemon. |
| 4 | + |
| 5 | +## Why Socket Proxy? |
| 6 | + |
| 7 | +### The Problem |
| 8 | +Direct access to `/var/run/docker.sock` gives near-root access to the host system: |
| 9 | +- Can mount any host directory |
| 10 | +- Can run privileged containers |
| 11 | +- Can escape container isolation |
| 12 | +- Supply chain attacks can exploit this access |
| 13 | + |
| 14 | +### The Solution: API-Level Filtering |
| 15 | +Instead of mounting the Docker socket directly, we use a proxy container: |
| 16 | + |
| 17 | +``` |
| 18 | +Dev Container → TCP:2375 → docker-socket-proxy → /var/run/docker.sock → Docker Daemon |
| 19 | + (filtered) (direct access) |
| 20 | +``` |
| 21 | + |
| 22 | +## Security Features |
| 23 | + |
| 24 | +### ✅ What's Allowed |
| 25 | + |
| 26 | +**Read Operations** (completely safe): |
| 27 | +- `docker ps`, `docker images`, `docker logs` |
| 28 | +- `docker inspect`, `docker version`, `docker info` |
| 29 | +- Viewing events and monitoring |
| 30 | + |
| 31 | +**Build Operations** (needed for development): |
| 32 | +- `docker build` - Building images |
| 33 | +- `docker commit` - Committing container changes |
| 34 | + |
| 35 | +**Container Operations** (standard dev work): |
| 36 | +- `docker run` - Starting containers |
| 37 | +- `docker create` - Creating containers |
| 38 | +- `docker exec` - Executing commands in containers |
| 39 | +- `docker stop`, `docker start`, `docker restart` |
| 40 | + |
| 41 | +**Network & Volume Operations** (needed for docker-compose): |
| 42 | +- Creating and managing networks |
| 43 | +- Creating and managing volumes |
| 44 | + |
| 45 | +### ❌ What's Blocked |
| 46 | + |
| 47 | +**Dangerous Operations** (blocked at API level): |
| 48 | +- `docker run --privileged` - **BLOCKED** by proxy |
| 49 | +- Host namespace modes require privileged - **BLOCKED** |
| 50 | +- Device access requires privileged - **BLOCKED** |
| 51 | + |
| 52 | +**Mount Restrictions** (client-side validation): |
| 53 | +- Mounting paths outside `/workspace` - **BLOCKED** by validator |
| 54 | +- System directories (`/`, `/etc`, `/home`, etc.) - **BLOCKED** |
| 55 | +- Only workspace paths allowed for security |
| 56 | + |
| 57 | +## Configuration |
| 58 | + |
| 59 | +### Socket Proxy (API-Level Filtering) |
| 60 | + |
| 61 | +The proxy is configured via environment variables in `docker-compose.yml`: |
| 62 | + |
| 63 | +```yaml |
| 64 | +environment: |
| 65 | + # Read operations - SAFE |
| 66 | + EVENTS: 1 |
| 67 | + PING: 1 |
| 68 | + VERSION: 1 |
| 69 | + IMAGES: 1 |
| 70 | + INFO: 1 |
| 71 | + CONTAINERS: 1 |
| 72 | + |
| 73 | + # Write operations - NEEDED FOR DEV |
| 74 | + POST: 1 # Create containers |
| 75 | + BUILD: 1 # Build images |
| 76 | + EXEC: 1 # Execute in containers |
| 77 | + NETWORKS: 1 # Manage networks |
| 78 | + VOLUMES: 1 # Manage volumes |
| 79 | + |
| 80 | + # Dangerous operations - DISABLED |
| 81 | + ALLOW_PRIVILEGED: 0 # Block --privileged |
| 82 | +``` |
| 83 | +
|
| 84 | +### Mount Validator (Client-Side Filtering) |
| 85 | +
|
| 86 | +Additional validation is performed client-side before API calls: |
| 87 | +
|
| 88 | +```bash |
| 89 | +# Configured via environment variables |
| 90 | +DOCKER_MOUNT_VALIDATION=on # Enable validation |
| 91 | +WORKSPACE_ROOT=/workspace # Allowed mount prefix |
| 92 | + |
| 93 | +# Applied via alias |
| 94 | +alias docker='/usr/local/share/dev-scripts/docker-mount-validator.sh' |
| 95 | +``` |
| 96 | + |
| 97 | +**Why Both Layers?** |
| 98 | +- **Socket Proxy**: Blocks privileged operations (cannot bypass) |
| 99 | +- **Mount Validator**: Blocks dangerous mounts (adds convenience layer) |
| 100 | +- **Defense in Depth**: Multiple security layers |
| 101 | + |
| 102 | +## How It Works |
| 103 | + |
| 104 | +### 1. Proxy Container Startup |
| 105 | +The proxy starts automatically with docker-compose when you open the dev container. |
| 106 | + |
| 107 | +### 2. Dev Container Connection |
| 108 | +```bash |
| 109 | +# Dev container uses TCP to connect to proxy via localhost |
| 110 | +export DOCKER_HOST=tcp://127.0.0.1:2375 |
| 111 | +docker ps # Proxied through security layer |
| 112 | +``` |
| 113 | + |
| 114 | +### 3. API Filtering |
| 115 | +```bash |
| 116 | +# Allowed operation |
| 117 | +docker run ubuntu echo "hello" |
| 118 | +# → API call permitted → executes |
| 119 | + |
| 120 | +# Blocked operation |
| 121 | +docker run --privileged ubuntu |
| 122 | +# → API call blocked by proxy → error returned |
| 123 | +``` |
| 124 | + |
| 125 | +## Usage Examples |
| 126 | + |
| 127 | +### Normal Development (All Work Normally) |
| 128 | + |
| 129 | +```bash |
| 130 | +# Build images |
| 131 | +docker build -t myapp . |
| 132 | + |
| 133 | +# Run containers |
| 134 | +docker run -d --name myapp -p 8080:80 myapp |
| 135 | + |
| 136 | +# Debug containers |
| 137 | +docker exec -it myapp bash |
| 138 | +docker logs myapp |
| 139 | + |
| 140 | +# Use docker-compose |
| 141 | +docker-compose up -d |
| 142 | + |
| 143 | +# Manage resources |
| 144 | +docker images |
| 145 | +docker ps -a |
| 146 | +docker volume ls |
| 147 | +``` |
| 148 | + |
| 149 | +### What Gets Blocked |
| 150 | + |
| 151 | +```bash |
| 152 | +# Privileged container - BLOCKED by proxy |
| 153 | +docker run --privileged ubuntu |
| 154 | +# Error: API call rejected by proxy |
| 155 | + |
| 156 | +# System directory mounts - BLOCKED by validator |
| 157 | +docker run -v /etc:/etc ubuntu |
| 158 | +# ❌ MOUNT VALIDATION FAILED: |
| 159 | +# Attempting to mount: /etc |
| 160 | +# Only /workspace paths are allowed for security. |
| 161 | + |
| 162 | +# Workspace mounts - ALLOWED |
| 163 | +docker run -v /workspace/data:/data ubuntu |
| 164 | +# ✅ Works fine |
| 165 | +``` |
| 166 | + |
| 167 | +## Advantages |
| 168 | + |
| 169 | +### 1. **No Bypass Possible** |
| 170 | +- Security enforced at network/API level |
| 171 | +- Cannot circumvent by calling different binary |
| 172 | +- Cannot disable with environment variable |
| 173 | +- True defense in depth |
| 174 | + |
| 175 | +### 2. **Battle-Tested** |
| 176 | +- Used in production by thousands of projects |
| 177 | +- Active development and security updates |
| 178 | +- Well-documented and community-supported |
| 179 | + |
| 180 | +### 3. **Granular Control** |
| 181 | +- Enable only needed API endpoints |
| 182 | +- Adjust permissions per environment |
| 183 | +- Clear security policy via configuration |
| 184 | + |
| 185 | +### 4. **True Privileged Blocking** |
| 186 | +Unlike wrappers that just warn, this actually BLOCKS: |
| 187 | +```bash |
| 188 | +docker run --privileged ubuntu |
| 189 | +# Error: Forbidden - privileged mode is not allowed |
| 190 | +``` |
| 191 | + |
| 192 | +## Configuration Options |
| 193 | + |
| 194 | +### Stricter Security (Limit More) |
| 195 | + |
| 196 | +Edit `docker-compose.yml`: |
| 197 | +```yaml |
| 198 | +environment: |
| 199 | + CONTAINERS: 1 # Read only |
| 200 | + POST: 0 # Block creating containers |
| 201 | + BUILD: 1 # Allow builds only |
| 202 | + EXEC: 0 # Block exec |
| 203 | +``` |
| 204 | +
|
| 205 | +### More Permissive (Enable More) |
| 206 | +
|
| 207 | +```yaml |
| 208 | +environment: |
| 209 | + # Enable Docker Swarm |
| 210 | + SERVICES: 1 |
| 211 | + SWARM: 1 |
| 212 | + NODES: 1 |
| 213 | + |
| 214 | + # Enable secrets |
| 215 | + SECRETS: 1 |
| 216 | +``` |
| 217 | +
|
| 218 | +## Troubleshooting |
| 219 | +
|
| 220 | +### "Cannot connect to Docker daemon" |
| 221 | +
|
| 222 | +Check proxy is running: |
| 223 | +```bash |
| 224 | +docker ps | grep cloudharness-docker-proxy |
| 225 | +``` |
| 226 | + |
| 227 | +Check environment variable: |
| 228 | +```bash |
| 229 | +echo $DOCKER_HOST |
| 230 | +# Should show: tcp://127.0.0.1:2375 |
| 231 | +``` |
| 232 | + |
| 233 | +### "Forbidden" or "Permission Denied" |
| 234 | + |
| 235 | +The proxy is blocking the operation (e.g., `--privileged`). This is intentional for security. |
| 236 | + |
| 237 | +To allow specific operations, edit `docker-compose.yml` and restart the proxy. |
| 238 | + |
| 239 | +### "MOUNT VALIDATION FAILED" |
| 240 | + |
| 241 | +The mount validator is blocking paths outside `/workspace`. This is intentional for security. |
| 242 | + |
| 243 | +To bypass temporarily: |
| 244 | +```bash |
| 245 | +DOCKER_MOUNT_VALIDATION=off docker run -v /path:/path ubuntu |
| 246 | +``` |
| 247 | + |
| 248 | +To allow specific paths, edit `.devcontainer/dev-scripts/docker-mount-validator.sh`: |
| 249 | +```bash |
| 250 | +ALLOWED_MOUNT_PATHS=( |
| 251 | + "/workspace" |
| 252 | + "/your/custom/path" # Add here |
| 253 | +) |
| 254 | +``` |
| 255 | + |
| 256 | +## Monitoring |
| 257 | + |
| 258 | +### View Proxy Logs |
| 259 | +```bash |
| 260 | +# See what API calls are being made |
| 261 | +docker logs -f cloudharness-docker-proxy |
| 262 | + |
| 263 | +# Look for blocked operations |
| 264 | +docker logs cloudharness-docker-proxy | grep -i forbidden |
| 265 | +``` |
| 266 | + |
| 267 | +## Security Checklist |
| 268 | + |
| 269 | +- [x] Docker socket not mounted in dev container |
| 270 | +- [x] Proxy has read-only access to socket |
| 271 | +- [x] Privileged mode blocked (`ALLOW_PRIVILEGED=0`) |
| 272 | +- [x] Mount validator restricts paths to /workspace only |
| 273 | +- [x] Proxy runs on localhost only (`127.0.0.1:2375`) |
| 274 | +- [x] Minimal API endpoints enabled |
| 275 | +- [x] Proxy logs available for auditing |
| 276 | +- [x] Proxy automatically starts with dev container |
| 277 | +- [x] Client-side mount validation enabled |
| 278 | + |
| 279 | +## Additional Resources |
| 280 | + |
| 281 | +- [Tecnativa docker-socket-proxy](https://github.com/Tecnativa/docker-socket-proxy) |
| 282 | +- [Docker Socket Security](https://docs.docker.com/engine/security/) |
| 283 | +- [Container Security Best Practices](https://cheatsheetseries.owasp.org/cheatsheets/Docker_Security_Cheat_Sheet.html) |
| 284 | + |
| 285 | +--- |
| 286 | + |
| 287 | +**Remember**: This provides defense-in-depth security. The proxy adds a strong security layer, but always follow best practices: use trusted images, scan for vulnerabilities, and monitor container activity. |
0 commit comments