Dockerã³ã³ãããŒã¯ãæãäžè¬çãªã³ã³ãããŒåãã¯ãããžãŒã§ãã åœåã¯ãäž»ã«éçºç°å¢ãšãã¹ãç°å¢ã§äœ¿çšãããŠããŸããããåŸã ã«æ¬çªç°å¢ã«ç§»è¡ããŸããã éšã®åŸã®ããã³ã®ããã«ãçç£ç°å¢ã§Dockerã³ã³ãããå¢ãå§ããŸãããããã®æè¡ã䜿çšãã人ã®ã»ãšãã©ã¯ãDockerã³ã³ãããå®å šã«å ¬éããæ¹æ³ã«ã€ããŠèããŸããã§ããã
OWASPã«åºã¥ããŠãDockerã³ã³ããäžã«æ§ç¯ãããç°å¢ãå€§å¹ ã«ä¿è·ããã«ãŒã«ã®ãªã¹ããæºåããŸããã
ã«ãŒã«0
ãã¹ããã·ã³ãšDockerã«ã¯ãçŸåšã®ãã¹ãŠã®æŽæ°ãå«ãŸããŠããå¿ èŠããããŸãã
ã³ã³ãããŒç°å¢ãããã¹ãã·ã¹ãã ãžã®ãšã¹ã±ãŒãã«ã€ãªããæ¢ç¥ã®è匱æ§ïŒéåžžã¯ãã¹ãã·ã¹ãã ã§ã®æš©éææ Œã«ã€ãªããïŒããä¿è·ããã«ã¯ããã¹ãOSãDocker Engineãããã³Docker Machineã®ãã¹ãŠã®ããããã€ã³ã¹ããŒã«ããããšãéåžžã«éèŠã§ãã
ããã«ãã³ã³ããã¯ïŒä»®æ³ãã·ã³ãšã¯ç°ãªãïŒã«ãŒãã«ããã¹ããšå ±æãããããã³ã³ããå ã§å®è¡ãããã«ãŒãã«ãšã¯ã¹ããã€ãã¯ãã¹ãã«ãŒãã«ã§çŽæ¥å®è¡ãããŸãã ããšãã°ãååã«éé¢ãããã³ã³ããå ã§å®è¡ãããã«ãŒãã«æš©éææ Œã®æªçšïŒDirty COWãªã©ïŒã¯ããã¹ãäžã®ã«ãŒãã¢ã¯ã»ã¹ã«ã€ãªãããŸãã
ã«ãŒã«1
DockerããŒã¢ã³ã®ãœã±ãããžã®ã¢ã¯ã»ã¹ãèš±å¯ããªãã§ãã ãã
DockerãµãŒãã¹ïŒããŒã¢ã³ïŒã¯ãçä¿¡APIæ¥ç¶ã«UNIXãœã±ãã/var/run/docker.sockã䜿çšããŸãã ãã®ãªãœãŒã¹ã®ææè ã¯ãrootãŠãŒã¶ãŒã§ãªããã°ãªããŸããã ãããŠä»ã®æ¹æ³ã¯ãããŸããã ãã®ãœã±ãããžã®ã¢ã¯ã»ã¹æš©ãå€æŽããããšã¯ããã¹ãã·ã¹ãã ãžã®ã«ãŒãã¢ã¯ã»ã¹ãèš±å¯ããããšãšæ¬è³ªçã«åçã§ãã
ãŸãã/ var / run / docker.sockãœã±ãããã³ã³ãããŒã§æäœããªãã§ãã ãããã³ã³ãããŒã䜿çšããªãå Žåã¯ãã³ã³ãããŒå ã®ãµãŒãã¹ã䟵害ãããšãã¹ãã·ã¹ãã ãå®å šã«å¶åŸ¡ã§ããããã§ãã ãã®ãããªãã®ã䜿çšããã³ã³ãããããå ŽåïŒ
-v /var/run/docker.sock://var/run/docker.sock
ãŸãã¯docker-composeã®å ŽåïŒ
volumes: - "/var/run/docker.sock:/var/run/docker.sock"
ãããæ©æ¥ã«å€æŽããå¿ èŠããããŸãã
ãããŠæåŸã«ãç¹ã«è¿œå ã®ä¿è·æ¹æ³ïŒå°ãªããšãèªèšŒïŒã䜿çšããã«ãå¿ èŠãªçµ¶å¯Ÿçãªç¢ºå®æ§ããªãéããDocker TCPãœã±ããã䜿çšããªãã§ãã ããã ããã©ã«ãã§ã¯ãDocker TCPãœã±ããã¯å€éšã€ã³ã¿ãŒãã§ãŒã¹0.0.0.0:2375ïŒHTTPã®å Žåã¯2376ïŒã§ããŒããéããã³ã³ãããŒãå®å šã«å¶åŸ¡ããæœåšçã«ãã¹ãã·ã¹ãã ã䜿çšã§ããããã«ããŸãã
ã«ãŒã«2
ã³ã³ããå ã§éç¹æš©ãŠãŒã¶ãŒãæ§æãã
ç¹æš©ã®ãªããŠãŒã¶ãŒã䜿çšããããã«ã³ã³ãããæ§æããããšã¯ãç¹æš©ã®ææ Œæ»æãåé¿ããããã®æè¯ã®æ¹æ³ã§ãã ããã¯ããŸããŸãªæ¹æ³ã§å®è¡ã§ããŸãã
1.ãdocker runãã³ãã³ãã®ã-uããªãã·ã§ã³ã䜿çšããŸãã
docker run -u 4000 alpine
2.ã€ã¡ãŒãžã®ãã«ãäžïŒ
FROM alpine RUN groupadd -r myuser && useradd -r -g myuser myuser < root-, , > USER myuser
3. DockerããŒã¢ã³ã§ããŠãŒã¶ãŒåå空éãïŒãŠãŒã¶ãŒç°å¢ïŒã®ãµããŒããæå¹ã«ããŸãã
--userns-remap=default
詳现ã«ã€ããŠã¯ã å ¬åŒããã¥ã¡ã³ããã芧ãã ããã
Kubernetesã§ã¯ãåŸè ã¯runAsNonRootãªãã·ã§ã³ãä»ããŠã»ãã¥ãªãã£ã³ã³ããã¹ãã§æ§æãããŸãã
kind: ... apiVersion: ... metadata: name: ... spec: ... containers: - name: ... image: .... securityContext: ... runAsNonRoot: true ...
ã«ãŒã«3
ã³ã³ããã®æ©èœãå¶éãã
Linuxã§ã¯ãã«ãŒãã«2.2以éã Linuxã«ãŒãã«æ©èœãšåŒã°ããç¹æš©ããã»ã¹ã®æ©èœãå¶åŸ¡ããæ¹æ³ããããŸãïŒè©³çŽ°ã«ã€ããŠã¯ãªã³ã¯ãåç §ããŠãã ããïŒã
Dockerã¯ãããã©ã«ãã§ãããã®ã«ãŒãã«æ©èœã®äºåå®çŸ©ãããã»ããã䜿çšããŸãã ãããŠã次ã®ã³ãã³ãã䜿çšããŠãã®ã»ãããå€æŽã§ããŸãã
--cap-drop â --cap-add â
æé©ãªã»ãã¥ãªãã£èšå®ã¯ãæåã«ãã¹ãŠã®æ©èœãç¡å¹ã«ãïŒ--cap-drop allïŒã次ã«å¿ èŠãªæ©èœã®ã¿ãæ¥ç¶ããããšã§ãã ããšãã°ã次ã®ããã«ïŒ
docker run --cap-drop all --cap-add CHOWN alpine
ãããŠæãéèŠãªïŒïŒïŒïŒâprivilegedãã©ã°ã䜿çšããŠã³ã³ãããŒãå®è¡ããªãã§ãã ãã!!!
Kubernetesã§ã¯ãLinux Kernel Capabilitieså¶çŽã¯ãæ©èœãªãã·ã§ã³ãä»ããŠã»ãã¥ãªãã£ã³ã³ããã¹ãã§æ§æãããŸãã
kind: ... apiVersion: ... metadata: name: ... spec: ... containers: - name: ... image: .... securityContext: ... capabilities: drop: - all add: - CHOWN ...
ã«ãŒã«4
no-new-privilegesãã©ã°ã䜿çšããŸã
ã³ã³ãããèµ·åãããšããã³ã³ããå ã§ã®æš©éææ Œãé²ã--security-opt = no-new-privilegesãã©ã°ã䜿çšãããšäŸ¿å©ã§ãã
Kubernetesã§ã¯ãLinux Kernel Capabilitieså¶çŽã¯ãallowPrivilegeEscalationãªãã·ã§ã³ãä»ããŠã»ãã¥ãªãã£ã³ã³ããã¹ãã§æ§æãããŸãã
kind: ... apiVersion: ... metadata: name: ... spec: ... containers: - name: ... image: .... securityContext: ... allowPrivilegeEscalation: false ...
ã«ãŒã«5
ã³ã³ãããŒééä¿¡ããªãã«ãã
ããã©ã«ãã§ã¯ãã³ã³ããééä¿¡ã¯Dockerã§æå¹ã«ãªã£ãŠããŸããããã¯ããã¹ãŠã®ã³ã³ãããïŒdocker0ãããã¯ãŒã¯ã䜿çšããŠïŒçžäºã«éä¿¡ã§ããããšãæå³ããŸãã ãã®æ©èœãç¡å¹ã«ããã«ã¯ãâicc = falseãã©ã°ãæå®ããŠDockerãµãŒãã¹ãå®è¡ããŸãã
ã«ãŒã«6
Linuxã»ãã¥ãªãã£ã¢ãžã¥ãŒã«ã䜿çšããïŒLinuxã»ãã¥ãªãã£ã¢ãžã¥ãŒã«-seccompãAppArmorãSELinuxïŒ
ããã©ã«ãã§ã¯ãDockerã¯ãã§ã«Linuxã»ãã¥ãªãã£ã¢ãžã¥ãŒã«ã®ãããã¡ã€ã«ã䜿çšããŠããŸãã ãããã£ãŠã ã»ãã¥ãªãã£ãããã¡ã€ã«ãç¡å¹ã«ããªãã§ãã ããã ãããã§ã§ããæ倧ã®ããšã¯ãã«ãŒã«ã匷åããããšã§ãã
seccompã®ããã©ã«ããããã¡ã€ã«ã¯ã ããããå ¥æã§ããŸã ã
ãŸããDockerã¯ä¿è·ã®ããã«AppArmorã䜿çšããDocker Engineèªäœãã³ã³ãããŒã®èµ·åæã«AppArmorã®ããã©ã«ããããã¡ã€ã«ãçæããŸãã ã€ãŸãã次ã®ä»£ããã«ïŒ
$ docker run --rm -it hello-world
èµ·åããŸãïŒ
$ docker run --rm -it --security-opt apparmor=docker-default hello-world
ããã¥ã¡ã³ãã«ã¯ãnginxã®AppArmorãããã¡ã€ã«ã®äŸãèšèŒãããŠããŸãããããã¯éåžžã«å¯èœã§ãïŒå¿ èŠã§ãïŒïŒã
#include <tunables/global> profile docker-nginx flags=(attach_disconnected,mediate_deleted) { #include <abstractions/base> network inet tcp, network inet udp, network inet icmp, deny network raw, deny network packet, file, umount, deny /bin/** wl, deny /boot/** wl, deny /dev/** wl, deny /etc/** wl, deny /home/** wl, deny /lib/** wl, deny /lib64/** wl, deny /media/** wl, deny /mnt/** wl, deny /opt/** wl, deny /proc/** wl, deny /root/** wl, deny /sbin/** wl, deny /srv/** wl, deny /tmp/** wl, deny /sys/** wl, deny /usr/** wl, audit /** w, /var/run/nginx.pid w, /usr/sbin/nginx ix, deny /bin/dash mrwklx, deny /bin/sh mrwklx, deny /usr/bin/top mrwklx, capability chown, capability dac_override, capability setuid, capability setgid, capability net_bind_service, deny @{PROC}/* w, # deny write for all files directly in /proc (not in a subdir) # deny write to files not in /proc/<number>/** or /proc/sys/** deny @{PROC}/{[^1-9],[^1-9][^0-9],[^1-9s][^0-9y][^0-9s],[^1-9][^0-9][^0-9][^0-9]*}/** w, deny @{PROC}/sys/[^k]** w, # deny /proc/sys except /proc/sys/k* (effectively /proc/sys/kernel) deny @{PROC}/sys/kernel/{?,??,[^s][^h][^m]**} w, # deny everything except shm* in /proc/sys/kernel/ deny @{PROC}/sysrq-trigger rwklx, deny @{PROC}/mem rwklx, deny @{PROC}/kmem rwklx, deny @{PROC}/kcore rwklx, deny mount, deny /sys/[^f]*/** wklx, deny /sys/f[^s]*/** wklx, deny /sys/fs/[^c]*/** wklx, deny /sys/fs/c[^g]*/** wklx, deny /sys/fs/cg[^r]*/** wklx, deny /sys/firmware/** rwklx, deny /sys/kernel/security/** rwklx, }
ã«ãŒã«7
ã³ã³ãããªãœãŒã¹ãå¶éãã
ãã®ã«ãŒã«ã¯éåžžã«åçŽã§ãã次ã®DoS / DDoSæ»æäžã«ã³ã³ããããã¹ãŠã®ãµãŒããŒãªãœãŒã¹ã䜿ãæããã®ãé²ãããã«ãåã³ã³ããã®ã¡ã¢ãªäœ¿çšå¶éãåå¥ã«èšå®ã§ããŸãã å¶éã§ããã®ã¯ãã¡ã¢ãªã®éãCPUãã³ã³ããã®åèµ·åã®åæ°ã§ãã
é çªã«è¡ããŸãããã
èšæ¶
-mãŸãã¯--memoryãªãã·ã§ã³
ã³ã³ããã䜿çšã§ããã¡ã¢ãªã®æ倧éã æå°å€ã¯4mïŒ4ã¡ã¬ãã€ãïŒã§ãã
ãªãã·ã§ã³--memory-swap
ã¹ã¯ãããæ§æãããªãã·ã§ã³ïŒã¹ã¯ãããã¡ã€ã«ïŒã å·§åŠã«èšå®ïŒ
- --memory-swap> 0ã®å Žåã-memoryãã©ã°ãèšå®ããå¿ èŠããããŸãã ãã®å Žåãmemory-swapã¯ãã¹ã¯ããã«å ããŠã³ã³ããã§äœ¿çšå¯èœãªåèšã¡ã¢ãªéã瀺ããŸãã
- ããç°¡åãªäŸã --memory = "300m"ãããã³--memory-swap = "1g"ã®å Žåãã³ã³ããã¯300MBã®ã¡ã¢ãªãš700MBã®ã¹ã¯ããïŒ1g-300mïŒã䜿çšã§ããŸãã
- --memory-swap = 0ã®å Žåãèšå®ã¯ç¡èŠãããŸãã
- --memory-swapã--memoryãšåãå€ã«èšå®ãããŠããå Žåãã³ã³ããã«ã¯ã¹ã¯ããããããŸããã
- å€--memory-swapãèšå®ãããŠããªããã-memoryãèšå®ãããŠããå Žåãã¹ã¯ããã®æ°ã¯æå®ãããã¡ã¢ãªã®éã®2åã«çãããªããŸãã ããšãã°ã-memory = "300m"ã§ã-memory-swapãèšå®ãããŠããªãå Žåãã³ã³ããã¯300MBã®ã¡ã¢ãªãš600MBã®ã¹ã¯ããã䜿çšããŸãã
- --memory-swap = -1ã®å Žåãã³ã³ããã¯ãã¹ãã·ã¹ãã ã§å¯èœãªãã¹ãŠã®ã¹ã¯ããã䜿çšããŸãã
ãã¹ãã¹ãžã®æ³šæïŒã³ã³ããå ã§èµ·åãããç¡æã®ãŠãŒãã£ãªãã£ã¯ãã³ã³ããã§å©çšå¯èœãªã¹ã¯ããã®å®éã®å€ã§ã¯ãªãããã¹ãã¹ã¯ããã®æ°ã衚瀺ããŸãã
ãªãã·ã§ã³--oom-kill-disable
OOMïŒã¡ã¢ãªäžè¶³ïŒãã©ãŒãæå¹ãŸãã¯ç¡å¹ã«ã§ããŸãã
泚æïŒ OOM Killerããªãã«ã§ããã®ã¯--memoryãªãã·ã§ã³ãèšå®ãããŠããå Žåã®ã¿ã§ããããããªããšãã³ã³ããå ã®ã¡ã¢ãªäžè¶³ã«ãããã«ãŒãã«ããã¹ãã·ã¹ãã ããã»ã¹ã®åŒ·å¶çµäºãéå§ããå¯èœæ§ããããŸãã
--memory-swappinessã-memory-reservationã-kernel-memoryãªã©ã®ä»ã®ã¡ã¢ãªç®¡çèšå®ãªãã·ã§ã³ã¯ãã³ã³ããã®ããã©ãŒãã³ã¹ã調æŽããããã®ãã®ã§ãã
CPU
ãªãã·ã§ã³--cpus
ãã®ãªãã·ã§ã³ã¯ãã³ã³ããã䜿çšã§ãã䜿çšå¯èœãªããã»ããµãªãœãŒã¹ã®éãèšå®ããŸãã ããšãã°ã2ã€ã®CPUãæã€ãã¹ããããã-cpus = "1.5"ãèšå®ããå Žåãã³ã³ããã¯1.5ããã»ããµã䜿çšããããšãä¿èšŒãããŸãã
ãªãã·ã§ã³--cpuset-cpus
ç¹å®ã®ã³ã¢ãŸãã¯CPUã®äœ¿çšãæ§æããŸãã å€ã¯ããã€ãã³ãŸãã¯ã³ã³ãã§æå®ã§ããŸãã æåã®ã±ãŒã¹ã§ã¯ãèš±å¯ãããã³ã¢ã®ç¯å²ã瀺ããã2çªç®ã®ã±ãŒã¹ã§ã¯ç¹å®ã®ã³ã¢ã瀺ãããŸãã
ã³ã³ããã®åèµ·ååæ°
--restart=on-failure:<number_of_restarts>
ãã®èšå®ã¯ãã³ã³ãããäºæããã¯ã©ãã·ã¥ããå Žåã«Dockerãã³ã³ããã®åèµ·åãè©Šè¡ããåæ°ãèšå®ããŸãã ã³ã³ããã®ç¶æ ãå®è¡äžã«å€æŽããããšãã«ãŠã³ã¿ãŒã¯ãŒãã«ãªã»ãããããŸãã
皌åããŠããªããµãŒãã¹ã®ç¡éã®åèµ·åãåé¿ããããã«ã5ãªã©ã®å°ããªæ£ã®æ°ãèšå®ããããšããå§ãããŸãã
ã«ãŒã«8
èªã¿åãå°çšã®ãã¡ã€ã«ã·ã¹ãã ãšããªã¥ãŒã ã䜿çšãã
ã³ã³ãããã©ããã«äœããæžã蟌ãå¿ èŠããªãå Žåã¯ãå¯èœãªéãèªã¿åãå°çšãã¡ã€ã«ã·ã¹ãã ã䜿çšããå¿ èŠããããŸãã ããã¯ãæœåšçãªäŸµå ¥è ã®ç掻ã倧ããè€éã«ããŸãã
èªã¿åãå°çšãã¡ã€ã«ã·ã¹ãã ã§ã³ã³ãããèµ·åããäŸïŒ
docker run --read-only alpine
èªã¿åãå°çšã¢ãŒãã§ããªã¥ãŒã ãæ¥ç¶ããäŸïŒ
docker run -v volume-name:/path/in/container:ro alpine
ã«ãŒã«9
ã³ã³ããã®å®å šæ§åæããŒã«ã䜿çšãã
æ¢ç¥ã®è匱æ§ãæã€ã³ã³ãããæ€åºããã«ã¯ãããŒã«ã䜿çšããå¿ èŠããããŸãã ãããã®å€ãã¯ãŸã ãããŸãããããããã¯ïŒ
â¢ç¡æïŒ
â¢ã³ããŒã·ã£ã«ïŒ
- Snyk ïŒç¡æçããããŸãïŒ;
- anchore ïŒç¡æçããããŸãïŒ;
- JFrog XRay ;
- Qualys ã
ãŸããKubernetesã«ã¯ãæ§æãšã©ãŒãæ€åºããããã®ããŒã«ããããŸãã
- kubeaudit ;
- kubesec.io ;
- ãã¥ãŒããã³ã ã