Top Banner
Sandboxing Linux code to mitigate exploitation (Or: How to ship a secure operating system that includes third-party code)
41

Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Aug 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Sandboxing Linux codeto mitigate exploitation(Or: How to ship a secure operating system that includes third-party code)

Page 2: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Work by Will Drewry, Elly Jones, Kees Cook, Chris Evans, Julien Tinnes, Markus Gutschke, and me:

Jorge Lucángeli Obesjorgelo@{chromium.org,google.com}

Page 3: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Truth is...Code will always have bugs.

Page 4: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Reasoning aboutthe system● What can an attacker do if they

compromise X?● We need to understand how

programs interact with each other.

Page 5: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Reasoning aboutthe system● Minimize the number of ways

programs can interact with or influence each other.

● Sandboxing

Page 6: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

SandboxingRunning a program in a restricted, controlled environment.

Page 7: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Sandboxing● Capabilities● setuid sandbox● Seccomp filtering using BPF

Page 8: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

User ids● Easiest sandboxing primitive.● Present on Unix from the beginning.

Page 9: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

root

● On Linux, the root user is equivalent to kernel mode.

● Unless module loading has been disabled.

We don’t want to run things as root.

Page 10: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Things want to run as root● Problem: system programs expect

root privileges.● Realization: programs don’t need the

full extent of root privileges.● Solution: capabilities.

Page 11: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Linux capabilities● Divides the privileges traditionally

associated with superuser● into distinct units, known as

capabilities,● which can be independently enabled

and disabled.

Page 12: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Linux capabilities● CAP_NET_ADMIN● CAP_NET_BIND_SERVICE● CAP_NET_RAW● CAP_SYS_ADMIN● CAP_SYS_BOOT● CAP_SYS_CHROOT● CAP_SYS_MODULE● CAP_SYS_NICE

Page 13: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Setting capabilitiescap_value_t flag[1];

flag[0] = CAP_NET_ADMIN;

cap_set_flag(caps, CAP_EFFECTIVE, 1,

flag, CAP_SET)

cap_set_flag(caps, CAP_PERMITTED, 1,

flag, CAP_SET)

cap_set_flag(caps, CAP_INHERITABLE, 1,

flag, CAP_SET)

Page 14: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Use capabilitiesHow do we get all Linux programs to use capabilities?

Answer: we don’t.

Page 15: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Helper/launcher program to set up jails.

Enter Minijail

Page 16: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Minijail● User/group changes● Linux capabilities● PID/VFS namespacing● chroot()'ing, bind-mounting● no_new_privs● Seccomp filtering

Page 17: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Every program launched by Minijail will be launched with:

LD_PRELOAD=libminijailpreload.so

LD_PRELOAD

Page 18: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

libc_handle = dlopen("libc.so.6", RTLD_NOW);

sym = dlsym(libc_handle, "__libc_start_main");

real_libc_start_main = sym;

real_main = main;

return real_libc_start_main(

fake_main, argc, ubp_av, init, fini,

rtld_fini, stack_end);

__libc_start_main

Page 19: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

minijail_enter(j);

dlclose(libc_handle);

return real_main(argc, argv, envp);

fake_main

Page 20: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Multi-process browser.

● Renderers take HTML, request

resources and return a bitmap.

● Renderers run as the same user.

Chrome

Page 21: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● First layer, semantic sandbox.

● Remove FS/net access.

● Uses a seutid-root binary.

setuid sandbox

Page 22: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

chrome --fork-> chrome-sandbox

--clone(CLONE_NEWPID | CLONE_NEWNET)->

-> chroot helper --clone(CLONE_FS)->

-> zygote

setuid sandbox

Page 23: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

1. zygote: chroot me!

2. helper:

○ chroot(/proc/self/fdinfo)

○ _exit(0)

setuid sandbox

Page 24: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● The kernel exposes a huge attack

surface

● CVE-2012-0056 (Mempodipper)

● CVE-2013-2094 (PERF_EVENTS)

What about the kernel?

Page 25: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Don’t allow every syscall.

● Many attempts through the years.

Filtering system calls

Page 26: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● SECure COMPuting

● read(), write(), sigreturn(),

exit()

seccomp

Page 27: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● How do we tell the kernel what to

filter?

● Enter BPF.

We need more granularity

Page 28: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Assembler-like language to describe

network packet filters.

● No loops, just a decision tree.

● Allows to examine parts of the

packet.

Berkeley Packet Filter

Page 29: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Treat the system call number and the

register set as a network packet.

● Express the system call policy as a

filter.

Will’s brilliant idea

Page 30: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

struct seccomp_data {

int nr;

__u32 arch;

__u64 instruction_pointer;

__u64 args[6];

};

Example filter

Page 31: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

struct sock_filter filter[] = {

BPF_STMT(BPF_LD+BPF_W+BPF_ABS,

offsetof(struct seccomp_data, nr)),

BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_getpid, 0, 1),

BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),

BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),

};

Example filter

Page 32: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

...

struct sock_fprog prog = {

.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),

.filter = filter,

};

int ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);

ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);

Example filter

Page 33: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● SECCOMP_RET_KILL

● SECCOMP_RET_TRAP

● SECCOMP_RET_ERRNO

● SECCOMP_RET_TRACE

● SECCOMP_RET_ALLOW

Return codes

Page 34: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Minijail

● Chrome

● libseccomp

Using Seccomp filtering

Page 35: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

We designed a policy language based

on ftrace.

Seccomp filtering with Minijail

Page 36: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

# open: return ENOENT

open: return 1

read: 1

# socket: arg0 == PF_FILE

socket: arg0 == 1

Seccomp filtering with Minijail

Page 37: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Separate implementation.

● Policies are compiled.

● Involved system calls (e.g. open())

use TRAP handler.

Seccomp filtering in Chrome

Page 38: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

Sandboxing in Chrome OS● Only two services running as root● Seccomp filtering for all services

accessing devices (USB, et c.)● setuid sandbox + seccomp filtering

for Chrome

Page 39: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

● Capabilities

● setuid sandbox

● Seccomp filtering with BPF

Circling back

Page 41: Sandboxing Linux code to mitigate exploitationrepository.root-me.org/Exploitation - Système/EN - Ekoparty 2013... · Sandboxing Linux code to mitigate exploitation (Or: How to ship

jorgelo@{chromium.org,google.com}

Questions?