Slide 1

Slide 1 text

What have syscalls done for you lately? Liz Rice @lizrice Aqua Security

Slide 2

Slide 2 text

Agenda ● What are syscalls? ● Syscalls, seccomp & containers ● Shellshock exploit

Slide 3

Slide 3 text

What is a syscall?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

When do you need syscalls? ● Files ● Devices ● Processes ● Communications ● Time & date And creating containers

Slide 6

Slide 6 text

Let’s see some syscalls strace Using strace

Slide 7

Slide 7 text

How do you make a syscall? Language-specific library ● C - libc ● Golang - syscall package func Write(fd int, p []byte) (n int, err error)

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

~ 330 of them Syscall codes

Slide 10

Slide 10 text

syscall() saves CPU registers before making the system call, restores the registers upon return from the system call, and stores any error code returned by the system call in errno(3) if an error occurs. Making a syscall

Slide 11

Slide 11 text

Syscall parameters x86 64 table from blog.rchapman.org

Slide 12

Slide 12 text

ENTRY (syscall) movq %rdi, %rax /* Syscall number -> rax. */ movq %rsi, %rdi /* shift arg1 - arg5. */ movq %rdx, %rsi movq %rcx, %rdx movq %r8, %r10 movq %r9, %r8 movq 8(%rsp),%r9 /* arg6 is on the stack. */ Syscall /* Do the system call. */ cmpq $-4095, %rax /* Check %rax for error. */ jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ Ret /* Return to caller. */ PSEUDO_END (syscall) Syscall in assembler GNU C library

Slide 13

Slide 13 text

Transition to kernel ● Execute in privileged mode ● Look up kernel code to run ○ syscall code from %rax

Slide 14

Slide 14 text

Portability Different CPUs, same approach

Slide 15

Slide 15 text

vDSO ● Avoid expensive kernel transitions ● Architecture-specific ● Typical: get time, CPU strace(1) and the vDSO When tracing systems calls with strace(1), symbols (system calls) that are exported by the vDSO will not appear in the trace output.

Slide 16

Slide 16 text

Syscalls and seccomp

Slide 17

Slide 17 text

Seccomp { "defaultAction": "SCMP_ACT_ERRNO", "architectures": [ "SCMP_ARCH_X86_64", "SCMP_ARCH_X86", "SCMP_ARCH_X32" ], "syscalls": [ { "name": "accept", "action": "SCMP_ACT_ALLOW", "args": [] }, { "name": "accept4", "action": "SCMP_ACT_ALLOW", "args": [] Restrict the syscalls a process can use

Slide 18

Slide 18 text

Seccomp ... { "names": [ "reboot" ], "action": "SCMP_ACT_ALLOW", "args": [], "comment": "", "includes": { "caps": [ "CAP_SYS_BOOT" ] }, "excludes": {} }, ... Can I reboot the host?

Slide 19

Slide 19 text

Stracing Docker containers

Slide 20

Slide 20 text

Share PID namespace docker run -it --pid=container: \ --cap-add sys_ptrace /bin/bash

Slide 21

Slide 21 text

So you’ve got your syscalls ● Creating a seccomp profile ● Portability? ○ Kernel / architecture

Slide 22

Slide 22 text

AppArmor (Not specifically to do with syscalls)

Slide 23

Slide 23 text

AppArmor profiles Define what a program can do ● File access (read, write, execute…) ● Capabilities ● Network access ● ...

Slide 24

Slide 24 text

Generating AppArmor profiles ● aa-autodep - blank profile ● aa-complain - Generate logs ● aa-logprof - Review logs ● Manual edits? ● Zzzzzzzzz

Slide 25

Slide 25 text

#include /usr/sbin/nginx { #include #include #include capability dac_override, capability dac_read_search, capability net_bind_service, capability setgid, capability setuid, /data/www/safe/* r, deny /data/www/unsafe/* r, /etc/group r, /etc/nginx/conf.d/ r, /etc/nginx/mime.types r, /etc/nginx/nginx.conf r, /etc/nsswitch.conf r, /etc/passwd r, /etc/ssl/openssl.cnf r, /run/nginx.pid rw, /usr/sbin/nginx mr, /var/log/nginx/access.log w, /var/log/nginx/error.log w, } Typical profile

Slide 26

Slide 26 text

Container AppArmor profiles ● Generate profile on host ● Or install apparmor inside container ○ Requires --security-opt apparmor:unconfined --cap-add sys_admin

Slide 27

Slide 27 text

Runtime profiles are HARD

Slide 28

Slide 28 text

But... ● Can stop unexpected behaviour ● Microservice behaviour is easier to reason about Powerful with good tooling

Slide 29

Slide 29 text

Shellshock example

Slide 30

Slide 30 text

Runtime profile tools

Slide 31

Slide 31 text

Recap & more info ● How syscalls work ○ Tycho’s kernel talk ● Runtime profiles ○ Powerful in theory, hard in practice ● More on strace ○ Julia Evans strace-zine ○ github.com/lizrice/strace-from-scratch

Slide 32

Slide 32 text

Thank you Come say hi at Booth G10 @lizrice | @aquasecteam