41

How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

How STACKLEAK improves Linux kernel security

Alexander Popov

Positive Technologies

2017

Page 2: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

About Me

Alexander Popov

Linux kernel developer

Security researcher at

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 2 / 41

Page 3: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Agenda

Mission of the Kernel Self Protection Project

STACKLEAK overview, credit to grsecurity/PaX

My goal, tactics and the current state

STACKLEAK as a security feature:

I A�ected kernel vulnerabilitiesI Protection mechanismsI Performance penalty

STACKLEAK inner workings:

I The asm code erasing the kernel stackI The GCC plugin for compile-time instrumentation

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 3 / 41

Page 4: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Kernel Self Protection Project

Security is beyond �xing bugs

Kernel has to fail safely, in addition to running safely

Goal: eliminate bug classes and methods of exploitation

Links:

I KSPP wiki:

http://kernsec.org/wiki/index.php/Kernel_Self_Protection_Project

I KSPP overview by Kees Cook:

https://out�ux.net/slides/2017/lss/kspp.pdf

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 4 / 41

Page 5: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Overview

Awesome Linux kernel security feature

Developed by PaX Team (kudos!)

PAX_MEMORY_STACKLEAK in grsecurity/PaX patch

grsecurity/PaX patch is now private

The last public version is for 4.9 kernel (April 2017)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 5 / 41

Page 6: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

My Goal

Bring STACKLEAK into the Linux kernel mainline

Thanks to Positive Technologies for allowing me

to spend part of my working time on it!

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 6 / 41

Page 7: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

My Tactics

Extract STACKLEAK from grsecurity/PaX patch��

� wc -l ../grsecurity-3.1-4.9.24-201704252333.patch

225976 ../grsecurity-3.1-4.9.24-201704252333.patch

Carefully learn it bit by bit

Send to LKML, get feedback, improve, repeat

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 7 / 41

Page 8: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

My Tactics

Generally resemble this:

Credit: @EatSleepPwnRpt

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 8 / 41

Page 9: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Current State

Patch series v5 (22 Oct 2017) for x86_64 and x86_32

http://www.openwall.com/lists/kernel-hardening/2017/10/22/1

21 �les changed, 978 insertions(+), 12 deletions(-)

Patches:1 x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls

2 gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

3 lkdtm: Add test for STACKLEAK (developed together with Tycho Andersen)

4 fs/proc: Show STACKLEAK metrics in the /proc �le system5 doc: self-protection: Add information about STACKLEAK feature

You are welcome to join the review!

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 9 / 41

Page 10: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Security Features

Now about STACKLEAK security features

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 10 / 41

Page 11: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Security Features (1)

Erases the kernel stack at the end of syscalls

Reduces the information that can be revealed throughsome* kernel stack leak bugs

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 11 / 41

Page 12: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Kernel Stack Leak Bug Example

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 12 / 41

Page 13: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Mitigation of Such Bugs

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 13 / 41

Page 14: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Security Features (2)

Blocks some* uninitialized kernel stack variable attacks

Nice example: CVE-2010-2963 exploitation

See cool write-up by Kees Cook:https://out�ux.net/blog/archives/2010/10/19/cve-2010-2963-v4l-compat-exploit/

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 14 / 41

Page 15: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Uninitialized Stack Variable Attack

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 15 / 41

Page 16: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Mitigation of Uninitialized Stack Variable Attacks

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 16 / 41

Page 17: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Important Limitation

* STACKLEAK doesn't help against such attacks during a

single syscall

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 17 / 41

Page 18: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Security Features (3)

Adds runtime detection of kernel stack depth over�ow

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 18 / 41

Page 19: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Interrelation of Security Mechanisms

In mainline kernel STACKLEAK would be e�ective againstkernel stack depth over�ow only in combination with:

CONFIG_THREAD_INFO_IN_TASK

CONFIG_VMAP_STACK (kudos to Andy Lutomirski)

Viktor Vasnetsov, Bogatyrs (1898)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 19 / 41

Page 20: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Kernel Stack Depth Over�ow (1)

See �The Stack is Back� by Jon Oberheide (2012)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 20 / 41

Page 21: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Kernel Stack Depth Over�ow Strikes Back

http://www.thegeekedgods.com/wp-content/uploads/2016/03/Empire-Strikes-Back.jpg

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 21 / 41

Page 22: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Kernel Stack Depth Over�ow (2)

See "The Stack is Back" by Jon Oberheide (2012) and"Exploiting Recursion in the Linux Kernel" by Jann Horn (2016)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 22 / 41

Page 23: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

CONFIG_VMAP_STACK or STACKLEAK Protection

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 23 / 41

Page 24: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Demo #1

DEMO

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 24 / 41

Page 25: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Stack Clash Attack for the Kernel Stack

http://hacktext.com/seo201/lib/imgs/darth-vader-force-choke.jpg

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 25 / 41

Page 26: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Stack Clash Attack for the Kernel Stack

Idea by Gael Delalleau: "Large memory management vulnerabilities" (2005)Revisited in "The Stack Clash" by Qualys Research Team (2017)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 26 / 41

Page 27: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Protection

Read about STACKLEAK vs Stack Clash on grsecurity blog:https://grsecurity.net/an_ancient_kernel_hole_is_not_closed.php

This code runs before each alloca call:

void __used check_alloca(unsigned long size)

{

unsigned long sp = (unsigned long)&sp;

struct stack_info stack_info = {0};

unsigned long visit_mask = 0;

unsigned long stack_left;

BUG_ON(get_stack_info(&sp, current,

&stack_info, &visit_mask));

stack_left = sp - (unsigned long)stack_info.begin;

BUG_ON(stack_left < 256 || size >= stack_left - 256);

}

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 27 / 41

Page 28: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Demo #2

DEMO

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 28 / 41

Page 29: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Cool, But What's the Price? (1)

Brief performance testing on x86_64Hardware: Intel Core i7-4770, 16 GB RAMTest 1, attractive: building the Linux kernel with Ubuntu con�g

time make -j9

Result on 4.11-rc8:

real 32m14.893s

user 237m30.886s

sys 11m12.273s

Result on 4.11-rc8+stackleak:

real 32m26.881s (+0.62%)

user 238m38.926s (+0.48%)

sys 11m36.426s (+3.59%)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 29 / 41

Page 30: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Cool, But What's the Price? (2)

Brief performance testing on x86_64

Hardware: Intel Core i7-4770, 16 GB RAM

Test 2, UNattractive:

hackbench -s 4096 -l 2000 -g 15 -f 25 -P

Average on 4.11-rc8: 8.71s

Average on 4.11-rc8+stackleak: 9.08s (+4.29%)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 30 / 41

Page 31: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Cool, But What's the Price? (3)

Conclusions

1. The performance penalty varies for di�erent workloads

2. Test STACKLEAK on your expected workload before

deploying in production

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 31 / 41

Page 32: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Inner Workings

The STACKLEAK feature consists of:

The asm code erasing the kernel stack

The GCC plugin for compile-time instrumentation

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 32 / 41

Page 33: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Erasing the Kernel Stack (1)

The architecture-speci�c erase_kstack() function

Works before returning from a syscall to userspace

Writes STACKLEAK_POISON to the used part of the

thread stack

Uses lowest_stack updated by track_stack() as a

starting point

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 33 / 41

Page 34: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Erasing the Kernel Stack (2)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 34 / 41

Page 35: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Erasing the Kernel Stack (3)

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 35 / 41

Page 36: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

Kernel Compile-Time Instrumentation

Is done by STACKLEAK GCC plugin

Inserts track_stack() call for the functions with a

big stack frame

Inserts the check_alloca() call before alloca and

track_stack() call after it

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 36 / 41

Page 37: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

GCC Plugins

Are compiler loadable modules

Are project-speci�c

Register new passes via the GCC Pass Manager

Provide the callbacks for these passes

See wonderful slides by Diego Novillo:

https://www.airs.com/dnovillo/200711-GCC-Internals/

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 37 / 41

Page 38: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK GCC Plugin

Inserts function calls (complex operation)

But needs to know the stack frame size (available too late)

Nice solution by PaX Team!

Registers 2 passes working with the IR of the code:

1 stackleak_tree_instrument inserts function calls to GIMPLE

2 stackleak_�nal removes them from RTL depending on the

stack frame size

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 38 / 41

Page 39: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

STACKLEAK Instrumentation Statistics

For x86_64_defconfig

The readelf utility shows 45602 functions in vmlinux

STACKLEAK instrumented 2.853% of them

The plugin inserted:

I 36 check_alloca() calls,I 1265 track_stack() calls:

F 42274 calls are inserted during GIMPLE passF 41009 calls are deleted during RTL pass

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 39 / 41

Page 40: How STACKLEAK improves Linux kernel security Alexander ... · I The GCC plugin for compile-time instrumentation Alexander Popov(Positive echnologies)T How ACKLESTAK improves Linux

My Final Propaganda

WE are the Linux Kernel Community

WE are responsible for servers, laptops, phones, PLCs,

laser cutters and other crazy things running GNU/Linux

Let's put some e�ort into Linux Kernel Security!

Alexander Popov(Positive Technologies) How STACKLEAK improves Linux kernel security 40 / 41