software security ii other types of software
play

Software Security (II): Other types of software vulnerabilities - PowerPoint PPT Presentation

Computer Security Course. Dawn Computer Security Course. Dawn Song Song Software Security (II): Other types of software vulnerabilities Dawn Song 1 Dawn Song 3 #293 HRE-THR 850 1930 ALICE SMITH COACH SPECIAL INSTRUX: NONE Dawn Song


  1. Computer Security Course. Dawn Computer Security Course. Dawn Song Song Software Security (II): Other types of software vulnerabilities Dawn Song 1

  2. Dawn Song 3

  3. #293 HRE-THR 850 1930 ALICE SMITH COACH SPECIAL INSTRUX: NONE

  4. Dawn Song 5

  5. #293 HRE-THR 850 1930 ALICE SMITHHHHHHHHHHH HHACH SPECIAL INSTRUX: NONE

  6. Dawn Song 7

  7. #293 HRE-THR 850 1930 ALICE SMITH FIRST SPECIAL INSTRUX: NONE

  8. Example #1 void vulnerable() { void vulnerable() { char name[20]; char name[20]; … … gets(name); gets(name); … … } }

  9. Example #2 void vulnerable() { void vulnerable() { char instrux[80] = “none”; char instrux[80] = “none”; char name[20]; char name[20]; … … gets(name); gets(name); … … } }

  10. Example #3 void vulnerable() { void vulnerable() { char cmd[80]; char cmd[80]; char line[512]; char line[512]; … … strncpy(cmd,”/usr/bin/fjnger”, 80); strncpy(cmd,”/usr/bin/fjnger”, 80); gets(line); gets(line); … … execv(cmd, …); execv(cmd, …); } }

  11. Example #4 void vulnerable() { void vulnerable() { int (*fnptr)(); int (*fnptr)(); char buf[80]; char buf[80]; … … gets(buf); gets(buf); … … } }

  12. Example #5 void vulnerable() { void vulnerable() { int seatinfjrstclass = 0; int seatinfjrstclass = 0; char name[20]; char name[20]; … … gets(name); gets(name); … … } }

  13. Example #6 void vulnerable() { void vulnerable() { int authenticated = 0; int authenticated = 0; char name[20]; char name[20]; … … gets(name); gets(name); … … } }

  14. Common Coding Errors • Input validation vulnerabilities • Memory management vulnerabilities • TOCTTOU vulnerability (later) Dawn Song 15

  15. Input validation vulnerabilities • Program requires certain assumptions on inputs to run properly • Without correct checking for inputs – Program gets exploited • Example: – Bufger overfmow – Format string Dawn Song 16

  16. Example I Example I 1: unsigned int size; 2: Data **datalist; 3: 4: size = GetUntrustedSizeValue(); 5: datalist = (data **)malloc(size * sizeof(Data *)); 6: for(int i=0; i<size; i++) { 7: datalist[i] = InitData(); 8: } 9: datalist[size] = NULL; 10: ... Dawn Song 17

  17. Example II Example II 1: char buf[80]; 2: void vulnerable() { 3: int len = read_int_from_network(); 4: char *p = read_string_from_network(); 5: if (len > sizeof buf) { 6: error(“length too large, nice try!”); 7: return; 8: } 9: memcpy(buf, p, len); 10: } • What's wrong with this code? • Hint – memcpy() prototype: – void *memcpy(void *dest, const void *src, size_t n); • Defjnition of size_t : typedef unsigned int size_t; • Do you see it now? Dawn Song 18

  18. Implicit Casting Bug • Attacker provides a negative value for len – if won ’ t notice anything wrong – Execute memcpy() with negative third arg – Third arg is implicitly cast to an unsigned int , and becomes a very large positive int – memcpy() copies huge amount of memory into buf , yielding a bufger overrun! • A signed/unsigned or an implicit casting bug – Very nasty – hard to spot • C compiler doesn ’ t warn about type mismatch between signed int and unsigned int – Silently inserts an implicit cast Dawn Song 19

  19. Example III (Integer Overfmow) Example III 1: size_t len = read_int_from_network(); 2: char *buf; 3: buf = malloc(len+5); 4: read(fd, buf, len); 5: ... • What ’ s wrong with this code? – No bufger overrun problems (5 spare bytes) – No sign problems (all ints are unsigned) • But, len+5 can overfmow if len is too large – If len = 0xFFFFFFFF , then len+5 is 4 – Allocate 4-byte bufger then read a lot more than 4 bytes into it: classic bufger overrun! • Know programming language ’ s semantics well to avoid pitfalls Dawn Song 20

  20. Example IV Example IV 1: char* ptr = (char*) malloc(SIZE); 2: if (err) { 3: abrt = 1; 4: free(ptr); 5: } 6: ... 7: if (abrt) { 8: logError(“operation aborted before commit”, ptr); 9: } • Use-after-free • Corrupt memory http://cwe.mitre.org Dawn Song 21

  21. Example V Example V 1: char* ptr = (char*) malloc(SIZE); 2: if (err) { 3: abrt = 1; 4: free(ptr); 5: } 6: ... 7: free(ptr); • Double-free error • Corrupts memory-management data structure http://owasp.org Dawn Song 22

  22. Example VI: Format string problem Example VI int func(char *user) { fprintf( stderr, user); }

  23. Format Functions • Used to convert simple C data types to a string representation • Variable number of arguments • Including format string • Example – printf(“%s number %d”, “block”, 2) – Output: “block number 2” Dawn Song 24

  24. Format String Parameters Paramet Output Passed as er %d Decimal (int) Value %u Unsigned decimal (unsigned Value int) %x Hexadecimal (unsigned int) Value %s String ((const) (unsigned) Reference char *) %n # bytes written so far, (* int) Reference

  25. Example VI: Format string problem Example VI int func(char *user) { fprintf( stderr, user); } • Problem: what if * user = “%s%s%s%s%s%s %s” ?? – %s displays memory – Likely to read from an illegal address – If not, program will print memory contents. Correct form: fprintf( stdout, “%s”, user);

  26. Stack and Format Strings • Function behavior is controlled by the format string • Retrieves parameters from stack as requested: “%” • Example: A Address of the format printf(“Number %d has no address, number %d has: string stack top … %08x\n”, I, a, &a) i Value of variable I <&a> <a> a Value of variable a <i> A &a Address of variable a … stack bottom

  27. View Stack • printf(“%08x. %08x. %08x. %08x\n”) – 40012983.0806ba43.bfgfgf4a.0802738b • display 4 values from stack

  28. Read Arbitrary Memory • char input[] = “\x10\x01\x48\x08_%08x. %08x. %08x. %08x|%s|”; printf(input) – Will display memory from 0x08480110 • Uses reads to move stack pointer into format string • %s will read at 0x08480110 till it reaches null byte

  29. Writing to arbitrary memory • printf( “hello %n”, &temp) – writes ‘6’ into temp. • printf( “%08x.%08x.%08x.%08x.%n”)

  30. Vulnerable functions Any function using a format string. Printing: printf, fprintf, sprintf, … vprintf, vfprintf, vsprintf, … Logging: syslog, err, warn

  31. An Exploit Example syslog( “ Reading username: ” ); read_socket(username); syslog(username); ⇓ Welcome to InsecureCorp. Please login. Login: EvilUser%s%s…%400n…%n root@server> _

  32. Why The Bug Exists • C language has poor support for variable-argument functions – Callee doesn ’ t know the number of actual args • No run-time checking for consistency between format string and other args • Programmer error

  33. Real-world Vulnerability Samples • First exploit discovered in June 2000. • Examples: – wu-ftpd 2.* : remote root – Linux rpc.statd: remote root – IRIX telnetd: remote root – BSD chpass: local root

  34. What are software vulnerabilities? • Flaws in software • Break certain assumptions important for security – E.g., what assumptions are broken in bufger overfmow? Dawn Song 35

  35. Why does software have vulnerabilities? • Programmers are humans! – Humans make mistakes! • Programmers are not security-aware • Programming languages are not designed well for security Dawn Song 36

  36. What can you do? • Programmers are humans! – Humans make mistakes! – Use tools! (next lecture) • Programmers were not security aware – Learn about difgerent common classes of coding errors • Programming languages are not designed well for security – Pick better languages Dawn Song 37

  37. Computer Security Course. Dawn Computer Security Course. Dawn Song Song Software Security (III): Defenses against Memory-Safety Exploits Dawn Song 38

  38. Preventing hijacking attacks Fix bugs: • Audit software • Automated tools: Coverity, Prefast/Prefjx, Fortify • Rewrite software in a type-safe language (Java, ML) • Diffjcult for existing (legacy) code … Allow overfmow, but prevent code execution Add runtime code to detect overfmows exploits: • Halt process when overfmow exploit detected • StackGuard, Libsafe Dawn Song 39

  39. Control-hijacking Attack Space Defenses/Mitigations Code Injection Arc Injection Stack Heap Exceptio n Handler s Dawn Song 40

  40. Defense I: non-execute (W^X) Prevent attack code execution by marking stack and heap as non-executable • NX -bit on AMD Athlon 64, XD -bit on Intel P4 Prescott – NX bit in every Page T able Entry (PTE) • Deployment: – Linux (via PaX project); OpenBSD – Windows: since XP SP2 (DEP) • Boot.ini : /noexecute=OptIn or AlwaysOn • Visual Studio: /NXCompat[:NO] Dawn Song 41

  41. Efgectiveness and Limitations • Limitations: – Some apps need executable heap (e.g. JIT s). – Does not defend against exploits using return-oriented programming Defenses/Mitigations Code Injection Arc Injection Code Injection Arc Injection Stack Stack Non-Execute (NX)* Heap Non-Execute (NX)* Heap Exceptio Non-Execute (NX)* n Handler Exceptio s n Handler s * When Applicable Dawn Song 42

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend