Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What have syscalls done for you lately?

Liz Rice
October 17, 2017

What have syscalls done for you lately?

DockerCon EU - Black Belt track

What are syscalls, and how can you use knowledge of syscalls to help secure your containers?

Liz Rice

October 17, 2017
Tweet

More Decks by Liz Rice

Other Decks in Technology

Transcript

  1. When do you need syscalls? • Files • Devices •

    Processes • Communications • Time & date And creating containers
  2. How do you make a syscall? Language-specific library • C

    - libc • Golang - syscall package func Write(fd int, p []byte) (n int, err error)
  3. 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
  4. 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
  5. Transition to kernel • Execute in privileged mode • Look

    up kernel code to run ◦ syscall code from %rax
  6. 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.
  7. 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
  8. Seccomp ... { "names": [ "reboot" ], "action": "SCMP_ACT_ALLOW", "args":

    [], "comment": "", "includes": { "caps": [ "CAP_SYS_BOOT" ] }, "excludes": {} }, ... Can I reboot the host?
  9. So you’ve got your syscalls • Creating a seccomp profile

    • Portability? ◦ Kernel / architecture
  10. AppArmor profiles Define what a program can do • File

    access (read, write, execute…) • Capabilities • Network access • ...
  11. Generating AppArmor profiles • aa-autodep - blank profile • aa-complain

    - Generate logs • aa-logprof - Review logs • Manual edits? • Zzzzzzzzz
  12. #include <tunables/global> /usr/sbin/nginx { #include <abstractions/apache2-common> #include <abstractions/base> #include <abstractions/nis>

    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
  13. Container AppArmor profiles • Generate profile on host • Or

    install apparmor inside container ◦ Requires --security-opt apparmor:unconfined --cap-add sys_admin
  14. But... • Can stop unexpected behaviour • Microservice behaviour is

    easier to reason about Powerful with good tooling
  15. 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