CS 216 Program and Data Representation University of

  • Slides: 28
Download presentation
CS 216: Program and Data Representation University of Virginia Computer Science Spring 2006 David

CS 216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 20: Hair-Dryer Attacks and Introducing x 86 Image from www. clean-funny. com, Golden. Blue LLC. http: //www. cs. virginia. edu/cs 216

Java Security Trusted Computing Base malcode. java UVa CS 216 Spring 2006 - javac

Java Security Trusted Computing Base malcode. java UVa CS 216 Spring 2006 - javac Compiler malcode. class JVML Java Bytecode Verifier Invalid “Okay” STOP Java. VM 2

Bytecode Verifier • Checks JVML code satisfies safety properties – Simulates program execution to

Bytecode Verifier • Checks JVML code satisfies safety properties – Simulates program execution to know types are correct, but doesn’t need to examine any instruction more than once – After code is verified, it is trusted: is not checked for type safety at run time (except for casts, array stores) Key assumption: when a value is written to a memory location, the value in that memory location is the same value when it is read. UVa CS 216 Spring 2006 - 3

Violating the Assumption … // The object on top of the stack is a

Violating the Assumption … // The object on top of the stack is a Sim. Object astore_0 // There is a Sim. Object in location 0 aload_0 // The value on top of the stack is a Sim. Object If a cosmic ray hits the right bit of memory, between the store and load, the assumption might be wrong. UVa CS 216 Spring 2006 - 4

Improving the Odds • Set up memory so that a single bit error is

Improving the Odds • Set up memory so that a single bit error is likely to be exploitable • Mistreat the hardware memory to increase the odds that bits will flip Following slides adapted (with permission) from Sudhakar Govindavajhala and Andrew W. Appel, Using Memory Errors to Attack a Virtual Machine, July 2003. UVa CS 216 Spring 2006 - 5

Making Bit Flips Useful Fill up memory with Filler objects, and one Pointee object:

Making Bit Flips Useful Fill up memory with Filler objects, and one Pointee object: class Filler { Pointee a 1; Pointee a 2; Pointee a 3; Pointee a 4; Pointee a 5; Pointee a 6; Pointee a 7; } UVa CS 216 Spring 2006 - class Pointee { Pointee a 1; Pointee a 2; Filler f; int b; Pointee a 5; Pointee a 6; Pointee a 7; } 6

a 2 a 3 a 4 a 5 Pointee p = new Pointee ();

a 2 a 3 a 4 a 5 Pointee p = new Pointee (); Vector fillers = new Vector (); try { while (true) { Filler f = new Filler (); f. a 1 = p; f. a 2 = p; f. a 3 = p; …; f. a 7 =p; fillers. add (f); } } catch (Out. Of. Memory. Exception e) { ; } a 6 a 7 a 1 a 2 f b a 5 a 6 Pointee Object Filling Up Memory Filler Object a 1 a 7 a 1 Filler Object a 2 a 3 UVa CS 216 Spring 2006 - a 4 7

Wait for a bit flip… a 3 a 4 a 5 a 6 a

Wait for a bit flip… a 3 a 4 a 5 a 6 a 7 a 1 a 2 f b a 5 a 6 Pointee Object • Remember: there are lots of Filler objects (fill up all of memory) • If a bit flips, good chance (~70%) it will be in a field of a Filler object and it will now point to a Filler object instead of a Pointee object a 2 Filler Object a 1 a 7 a 1 Filler Object a 2 a 3 UVa CS 216 Spring 2006 - a 4 8

a 2 Type Violation a 3 a 4 a 5 After the bit flip,

a 2 Type Violation a 3 a 4 a 5 After the bit flip, the value of f. a 2 is a Filler object, but f. a 2 was declared as a Pointee object! a 6 Filler Object a 1 a 7 a 2 f b a 5 Can an attacker exploit this? a 6 Pointee Object a 1 a 7 a 1 Filler Object a 2 a 3 UVa CS 216 Spring 2006 - a 4 9

Pointee p = new Pointee (); Vector fillers = new Vector (); try {

Pointee p = new Pointee (); Vector fillers = new Vector (); try { while (true) { Filler f = new Filler (); f. a 1 = p; f. a 2 = p; f. a 3 = p; …; f. a 7 =p; fillers. add (f); } } catch (Out. Of. Memory. Exception e) { ; } Finding the Bit Flip while (true) { for (Enumeration e = fillers. elements (); e. has. More. Elements () ; ) { Filler f = (Filler) e. next. Element (); if (f. a 1 != p) { // bit flipped! … } else if (f. a 2 != p) { … } } UVa CS 216 Spring 2006 - 10

Violating Type Safety class Filler { class Pointee { Pointee a 1; Pointee a

Violating Type Safety class Filler { class Pointee { Pointee a 1; Pointee a 2; Pointee a 3; Filler f; Pointee a 4; int b; Pointee a 5; Pointee a 6; Pointee a 7; } } Filler f = (Filler) e. next. Element (); if (f. a 1 != p) { // bit flipped! Object r = f. a 1; // Filler fr = (Filler) r; // Cast is checked at run-time f. a 1. b fr == f. a 1 fr. a 4 == f. a 1. b UVa CS 216 Spring 2006 - Declared Type Pointee int Filler Pointee 11

Violating Type Safety class Filler { class Pointee { Pointee a 1; Pointee a

Violating Type Safety class Filler { class Pointee { Pointee a 1; Pointee a 2; Pointee a 3; Filler f; Pointee a 4; int b; Pointee a 5; Pointee a 6; Pointee a 7; } } Filler f = (Filler) e. next. Element (); if (f. a 1 != p) { // bit flipped! Object r = f. a 1; // Filler fr = (Filler) r; // Cast is checked at run-time f. a 1. b = 1524383; // Address of the Security. Manager fr. a 4. a 1 = null; // Set it to a null // Do whatever you want! No security policy now… new File (“C: thesis. doc”). delete (); UVa CS 216 Spring 2006 - 12

Getting a Bit Flip • Wait for a Cosmic Ray – You have to

Getting a Bit Flip • Wait for a Cosmic Ray – You have to be really, really patient… (or move machine out of Earth’s atmosphere) • X-Rays – Expensive, not enough power to generate bit-flip • High energy protons and neutrons – Work great - but, you need a particle accelerator • Hmm…. UVa CS 216 Spring 2006 - 13

 • 50 -watt spotlight bulb • Between 80° 100°C, memory starts to have

• 50 -watt spotlight bulb • Between 80° 100°C, memory starts to have a few failures • Attack applet is successful (at least half the time)! • Hairdryer works too, but it fries too many bits at once UVa CS 216 Spring 2006 - Using Heat Picture from Sudhakar Govindavajhala 14

Should Anyone be Worried? Java virtual machine UVa CS 216 Spring 2006 - 15

Should Anyone be Worried? Java virtual machine UVa CS 216 Spring 2006 - 15

Recap • Verifier assumes the value you write is the same value when you

Recap • Verifier assumes the value you write is the same value when you read it • By flipping bits, we can violate this assumption • By violating this assumption, we can violate type safety: get two references to the same storage that have inconsistent types • By violating type safety, we can get around all other security measures • For details, see paper linked from notes UVa CS 216 Spring 2006 - 16

CS 216 Roadmap Data Representation Program Representation Real World Problems “Hello” [‘H’, ’i’, �]

CS 216 Roadmap Data Representation Program Representation Real World Problems “Hello” [‘H’, ’i’, ] 0 x 42381 a, 3. 14, ‘x’ 01001010 Objects Python code Arrays Addresses, C code Numbers, Characters JVML Bits x 86 High-level language Low-level language Virtual Machine language Assembly Real World Physics UVa CS 216 Spring 2006 - 17

From JVML to x 86 • More complex instructions: – JVML: 1 -byte opcodes,

From JVML to x 86 • More complex instructions: – JVML: 1 -byte opcodes, all instructions are 1 byte plus possible params on stack – x 86: 1 -, 2 -, and 3 -byte opcodes • Lower-level memory: – JVML: stack and locations, managed by VM – x 86: registers and memory, managed (mostly) by programmer Why is x 86 instruction set more complex? UVa CS 216 Spring 2006 - 18

x 86 History • 1960 s: Project Apollo • 1971: Intel 4004 Processor –

x 86 History • 1960 s: Project Apollo • 1971: Intel 4004 Processor – First commercial microprocessor – Target market: calculators UVa CS 216 Spring 2006 - 19

Intel 4004 16 4 -bit registers 3 -deep stack UVa CS 216 Spring 2006

Intel 4004 16 4 -bit registers 3 -deep stack UVa CS 216 Spring 2006 - 20

x 86 History • 1971: 4004 – 46 instructions (41 8 -bit wide, 5

x 86 History • 1971: 4004 – 46 instructions (41 8 -bit wide, 5 16 -bits) – Separate program and data store • 1974: 8080 – 8 -bit processor – Used in MITS Altair • 1978: 8086, 8088 – 16 -bit architecture – Assembly backwards compatible with 8080 UVa CS 216 Spring 2006 - 21

x 86 History • 1982: 80186 – Backwards compatible with 8086 – Added some

x 86 History • 1982: 80186 – Backwards compatible with 8086 – Added some new instructions • 1982: 80286 • 1986: 386 – First 32 -bit version (but still backwards compatible with 16 -bit 8086) • 1989: 486 (Added a few instructions) • 1993: Pentium (can’t trademark numbers) • Now: Athlon 64, x 86 -64 – 64 -bit versions, but still backwards compatible UVa CS 216 Spring 2006 - 22

x 86 Registers General-purpose Registers 8 bits EAX AX AH AL EBX BX BH

x 86 Registers General-purpose Registers 8 bits EAX AX AH AL EBX BX BH BL ECX CX CH CL EDX DX DH DL ESI EDI ESP (stack pointer) EBP (base pointer) IP (instruction pointer) UVa CS 216 Spring 2006 - 16 bits 32 bits 23

x 86 Instructions • Variable length: 1 -17 bytes long (average is ~3 bytes)

x 86 Instructions • Variable length: 1 -17 bytes long (average is ~3 bytes) • Opcodes: 1 -4 bytes long – e. g. , 660 F 3 A 0 FC 108 H = PALIGNR • Parameters: registers, memory locations, constants – Need different opcodes to distinguish them UVa CS 216 Spring 2006 - 24

Move Instruction mov [destination], [source] • Copies the value in source into the location

Move Instruction mov [destination], [source] • Copies the value in source into the location destination • Many different versions depending on types of destination and source: – destination: register, memory – source: register, memory, constant • Not all combinations are possible: cannot have both destination and source be memory locations UVa CS 216 Spring 2006 - 25

Move Examples • mov eax, [ebx] – [<reg>]: the value of the memory location

Move Examples • mov eax, [ebx] – [<reg>]: the value of the memory location referenced by <reg> – Copies the 4 -byte value in location [ebx] into register eax • mov [ebp+4], eax – Copies the 4 -byte value in register eax into the location [ebp+4] (typically this is the first local variable) UVa CS 216 Spring 2006 - 26

More Moves • mov [ebx], 2 – Ambiguous: is it moving 0 b 0000010

More Moves • mov [ebx], 2 – Ambiguous: is it moving 0 b 0000010 mov BYTE PTR [ebx], 2 or 0 b 000000010 mov WORD PTR [ebx], 2 or 0 b[30 zeros]10 mov DWORD PTR [ebx], 2 UVa CS 216 Spring 2006 - 27

Charge • Section this week: understanding x 86 assembly • Problem Set 7: out

Charge • Section this week: understanding x 86 assembly • Problem Set 7: out today, due in 1 week – Reading and writing x 86 assembly code – Figuring out what code is generated for different program constructs • Exam 2: out next Wednesday UVa CS 216 Spring 2006 - 28