A collection of random things: Look what I found under the carpet Ilja van Sprundel

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Review: Name/Address conversion: –What does the domain name system do? –Which system calls use the domain name system? –How does it work? –How does getservbyname.
Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Yoshi
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 18 Sampling Distribution Models.
Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 18 Sampling Distribution Models.
Out-of-Band Data© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
CS 300 – Lecture 22 Intro to Computer Architecture / Assembly Language Virtual Memory.
More on protocol implementation Packet parsing Memory management Data structures for lookup.
CS 333 Introduction to Operating Systems Class 18 - File System Performance Jonathan Walpole Computer Science Portland State University.
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
A Moderately Directed Rant.  It’s very likely that you’ve been writing totally incorrect code without realizing it  Once you do realize it, it’s usually.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Lecture 18 Page 1 CS 111 Online Design Principles for Secure Systems Economy Complete mediation Open design Separation of privileges Least privilege Least.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
An anti-hacking guide.  Hackers are kindred of expert programmers who believe in freedom and spirit of mutual help. They are not malicious. They may.
Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
LWIP TCP/IP Stack 김백규.
Computer Security and Penetration Testing
Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information 
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
CS332, Ch. 26: TCP Victor Norman Calvin College 1.
CInt Function Stub Removal ROOT Team Meeting CERN Leandro Franco (Joint work with Diego Marcos)
Copyright © 2009 Pearson Education, Inc. Chapter 18 Sampling Distribution Models.
Games Development 2 Concurrent Programming CO3301 Week 9.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Agenda Attack Lab C Exercises C Conventions C Debugging
Lecture 14 Page 1 CS 236 Online Race Conditions A common cause of security bugs Usually involve multiprogramming or multithreaded programs Caused by different.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CS333 Intro to Operating Systems Jonathan Walpole.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Instructor: Craig Duckett Lecture 07: Tuesday, October 20 th, 2015 Conflicts and Isolation, MySQL Workbench 1 BIT275: Database Design (Fall 2015)
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
UDP : User Datagram Protocol 백 일 우
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 18 Sampling Distribution Models.
Lecture 4 Page 1 CS 111 Online Modularity and Memory Clearly, programs must have access to memory We need abstractions that give them the required access.
Lecture 1 Page 1 CS 111 Summer 2013 Important OS Properties For real operating systems built and used by real people Differs depending on who you are talking.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
Memory-Related Perils and Pitfalls in C
Protecting Memory What is there to protect in memory?
Jonathan Walpole Computer Science Portland State University
Presented by: Daniel Taylor
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
Modularity and Memory Clearly, programs must have access to memory
Lab: ssh, scp, gdb, valgrind
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Instructor: Craig Duckett
Tutorial: The Programming Interface
Week 7 - Friday CS222.
Dirty COW Race Condition Attack
Presentation transcript:

A collection of random things: Look what I found under the carpet Ilja van Sprundel

Who am I Loves to see things crash IOActive Netric

Agenda /dev/[k]mem race conditions Snprintf corner cases Using Out-Of-Band data to avoid ids Tcp stack fuzzer

Introduction Style somewhat like my unusual bugs talk No big secrets Mostly old, obscure and undocumented things

/dev/[k]mem race conditions

/dev/[k]mem is a virtual file Represents all of kernel memory as a file Is used to grep data out of the kernel – Statistics – Information about processes – Information about the network – Loaded kernel modules – Load average – …. Only root and group kmem can read it (for obvious reasons) Usually done with sgid kmem programs

/dev/[k]mem race conditions Some potential problems arise Most of the time multiple read calls are needed Can’t lock kernel data structs from userland Can lead to potential synchronisation issues

/dev/[k]mem race conditions Simple example: print the name of a process – Step 1: find the process table – Step 2: find the right process structure – Step 3: find the pointer in there that points to the name of the process – Step 4: get the string it points to. – Step 5: print it. Race between step 2 and 3. What if that process vanished in between ?

/dev/kmem race conditions Attacking this: Between step 2 and 3 Process structure gets free’d Same memory gets reallocated in the kernel Attacker has control over that content Lay it out so that if it were a process struct you’d have the process name pointer point to some piece of data you want to leak (tty buffer, buffer cache, ….)

/dev/kmem race conditions The race might seem small But it’s fairly easy to make the race window arbitrarily long Send sigstop to the sgid process at the right time. (this pauses the sgid process) Lay everything out correctly Send sigcont to the sgid process.

/dev/kmem race conditions This issue’s been around for a _long_ time. Noone ever raised a big stink over it. Clearly some people must have known Can’t find any public references about this People are moving away from this And use sysctl or proc file system Makes much more sense

/dev/kmem race conditions Mostly seen in the BSD’s. Taken a look at NetBSD It has a bunch of apps that read data out of /dev/kmem Some of them use /dev/kmem, some systcl The majority use both Since sysctl can’t yet provide all info that’s needed

/dev/kmem race conditions Uses a library for most of this Libkvm Uses /dev/kmem historically Modified to use sysctl whenever possible Mostly when libkvm api’s are used sysctl is used kvm_getprocs(), kvm_getproc2(), kvm_getargv(), …

/dev/kmem race conditions Things start getting bad when the direct read api’s are used Kvm_read() in particular When used on the running kernel, you end up with race conditions It can also operate on core files and swap files.

/dev/kmem race conditions Apps that’s that use both /dev/kmem and sysctl – Vmstat – Trsp (not used on the running kernel) – Trpt (not used on the running kernel) – Pstat – Fstat – Slstats – Ccdconfig – Systat – Netstat – pmap

/dev/kmem race conditions Most of those apps do privdrops as soon as possible Drops privs immediately if not grepping data out of the running kernel Except for – Trsp – Trpt

/dev/kmem race conditions Libkvm and these sgid apps are build under the assumption that the kernel can be trusted If you get to feed it a (malformed) core file, that assumption goes away Same with the race conditions Libkvm really doesn’t stand up well to random crap being parsed

/dev/kmem race conditions Example: char * kvm_getfiles(kd, op, arg, cnt) kvm_t *kd; int op, arg; int *cnt; { KREAD(kd, nl[0].n_value, &numfiles)... size = sizeof(fhead) + (numfiles + 10) * sizeof(struct file); <-- int overflow KVM_ALLOC(kd, argspc, size); <-- alloc too small numfiles = kvm_deadfiles(kd, op, arg, (long)nl[1].n_value,numfiles); <-- overflow... }

/dev/kmem race conditions When looking into this … I found some other issues. Was reading systat code. It has a signal handler Which is not safe at all Doesn’t seem to affect FreeBSD or OpenBSD.

/dev/kmem race conditions One liner poc: while [ 1 ] ; do kill -TSTP ; kill -CONT ; kill -TSTP ; done src/usr.bin/systat/main.c static void stop(int signo) { sigset_t set; signal(SIGTSTP, sv_stop_handler); /* unblock SIGTSTP */ sigemptyset(&set); sigaddset(&set, SIGTSTP); sigprocmask(SIG_UNBLOCK, &set, NULL); /* stop ourselves */ kill(0, SIGTSTP); /* must have been restarted */ signal(SIGTSTP, stop); redraw(signo); }

/dev/kmem race conditions

Reading some of the kvm manpages online Xss in the webform Html code gets escaped But incorrectly No > and the like But escaped with a \

Snprintf corner cases

Snprintf is the more secure version of sprintf Its behavior is described in the c99 standard It has a couple of corner cases Some of which are implementation dependant

Snprintf corner cases Snprintf’s return value, when successful is simular to sprintf’s It returns how many bytes got written …. … had there been no bounds check ! Is guarenteed to be 0-terminated … … unless the input length is 0 (duh)

Snprintf corner cases Snprintf can fail In which case it returns -1 What if you don’t catch that error ? The string is not guaranteed to be 0- terminated.

Snprintf corner cases Return value often gets abused. Increase the pointer with it: – Incase of the boundscheck being hit, you now have a pointer pointing outside of the buffer it points to – Incase of an error, the pointer actually gets decreased, something you probably didn’t want

Snprintf corner cases Making snprintf fail In glibc, under certain locals, snprintf will try to do some kind of unicode detection. If the input then turns out to be broken, snprintf will fail and return -1 You could also use this unicode conversion to to bypass certain filters

Snprintf corner cases The whole idea of encoding 3 different things in a return value is a bit silly imo Failed or not failed would be better

Snprintf corner cases It seems the redhat guys completely broke their snprintf implementation in fc7!! The return value will _always_ be equal to the size given as input How can that even work ? Apps break on this !

Using Out-Of-Band data to avoid ids

Out-of band data in tcp is usually used to signal for something Set the urg flag, and the pointer offset. 1 out-of-band byte per packet. Pretty obscure feature Noone uses it anymore these days And noone remembers how to use it

Using Out-Of-Band data to avoid ids Most descriptions out there on how to handle Out-of-band data really sucks They’re bad copies of what stevens said

Using Out-Of-Band data to avoid ids Sending oob data is simple Send(fd, data, len, MSG_OOB); Receiving it depends on what you want to do What happens by default ?

Using Out-Of-Band data to avoid ids By default Nothing should happen It gets discarded You need to let your kernel know you want to receive it. You do this by telling it you (your pid) owns the fd you’re going to receive oob data from Fcntl(fd, F_SETOWN, getpid())

Using Out-Of-Band data to avoid ids Once that is done, whenever oob data arrives you’ll get a sigurg send. If you don’t set the OOB_INLINE socket option, the oob data is kept out of band To retrieve it out of band you can do Recv(fd, buf, len, MSG_OOB) You can also set the OOB_INLINE socket options, and receive the oob data inline

Using Out-Of-Band data to avoid ids Older linux kernels just put the Out-Of-Band data inline by default Newer 2.6 kernels don’t do this anymore Allows you to sneak in data that the ids wouldn’t notice

Using Out-Of-Band data to avoid ids Besides this oob handling in older linux kernels, handling oob data is very much application specific And ids can really never know how exactly it’s going to be used.

Using Out-Of-Band data to avoid ids Some other interesting issues that could and did happen with oob handling When doing fcntl(fd, F_SETOWN, ) Set it to a pid of a root owned process. Get recv oob data Now get to send signals to a process you’re not supposed to send signals too Fixed in most unices a while ago Who knows, some might still be vuln

Using Out-Of-Band data to avoid ids Oob data can be used to remotely deliver a signal Might cause signal race conditions Depends if the application’s sigurg hander is well written or not Somewhat related: – In linux, in certain situations, you can get the oom killer to send signals for you remotely (if you can exaust enough memory).

Using Out-Of-Band data to avoid ids security/1997/09/15/0000.html security/1997/09/15/0000.html t t

Tcp stack fuzzer

Raincheck Yes, I’m a slacker There is code, its not functional …. Maybe next year ?

Q&A ?