Consider the following Java code Race Conditions public class Shared { private int data; public Shared() { data = 0; } public void setData(int r) { data.

Slides:



Advertisements
Similar presentations
wwwcsif.cs.ucdavis.edu/~jacksoni
Advertisements

TOCTTOU Attacks Don Porter CS 380S
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Traps and Pitfalls: Practical Problems in System Call Interposition Based Security Tools Vinay Gangasani vcg
Multi-core Programming Thread Checker. 2 Topics What is Intel® Thread Checker? Detecting race conditions Thread Checker as threading assistant Some other.
Deadlock Problem Deadlock: A set of processes, each holding a resource, and each waiting to acquire a resource held by another process in the set. Example:
Object-Oriented Software Engineering Concurrent Programming.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Building Secure Software Chapter 9 Race Conditions.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Definitions Process – An executing program
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Lecture 4 : JAVA Thread Programming
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
 Traditionally, a process in an operating system consists of an execution environment and a single thread of execution (single activity).  However,
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Consider the following Java code Race Conditions public class Shared { private int data; public Shared() { data = 0; } public void setData(int r) { data.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Dynamic Architectures (Component Reconfiguration) with Fractal.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Linux Security. Authors:- Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel, of CodeSourcery LLC published by New Riders Publishing.
Synonyms: A _______ is a unit of software that is capable of executing concurrently with other similar units. _______ -- user-defined process in Ada _________.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Threading and Concurrency COM379T John Murray –
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Secure Coding in C and C++ Race conditions Lecture 6 Feb 12, 2013 Acknowledgement: These slides are based on author Seacord’s original presentation.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
09/13/12All Rights Reserved - ADVANTEST CORPORATION1 Java 7 Highlights Presented By: Andrey Loskutov.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
Chapter 7: Deadlocks Joe McCarthy CSS 430: Operating Systems - Deadlocks1.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Component-Based Software Engineering Understanding Thread Safety Paul Krause.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Multi-processor Scheduling
Concurrency, Processes and Threads
COEN346 Tutorial Monitor Objects.
Multithreaded Programming in Java
Secure Coding in C and C++ Race conditions
Multithreading.
Programming with Shared Memory Java Threads and Synchronization
Background and Motivation
Implementing Mutual Exclusion
Concurrency, Processes and Threads
Implementing Mutual Exclusion
Threads and Multithreading
CSS430 Deadlocks Textbook Ch8
Concurrency, Processes and Threads
Problems with Locks Andrew Whitaker CSE451.
CSE 542: Operating Systems
CSE 542: Operating Systems
CMSC 202 Threads.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Race Condition Vulnerability
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Consider the following Java code Race Conditions public class Shared { private int data; public Shared() { data = 0; } public void setData(int r) { data = r; } public int getData() { return data; } public class Shared { private int data; public Shared() { data = 0; } public void setData(int r) { data = r; } public int getData() { return data; } int localData = theShared.getData(); localData++; theShared.setData(localData); int localData = theShared.getData(); localData++; theShared.setData(localData); After executing this code what value is stored in Shared.data?

What is a thread / process / task? public class Driver { private Shared theShared; private MyThread threadA, threadB; public Driver() { theShared = new Shared(); threadA = new MyThread(theShared); threadB = new MyThread(theShared); threadA.start(); threadB.start(); try { threadA.join(); threadB.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(theShared.getData()); } public static void main(String[] args) { new Driver(); } public class Driver { private Shared theShared; private MyThread threadA, threadB; public Driver() { theShared = new Shared(); threadA = new MyThread(theShared); threadB = new MyThread(theShared); threadA.start(); threadB.start(); try { threadA.join(); threadB.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(theShared.getData()); } public static void main(String[] args) { new Driver(); } Threaded variation of the last program. public class MyThread extends Thread { private Shared theShared; public MyThread(Shared s) { theShared = s; } public void run() { int localData = theShared.getData(); localData++; theShared.setData(localData); } public class MyThread extends Thread { private Shared theShared; public MyThread(Shared s) { theShared = s; } public void run() { int localData = theShared.getData(); localData++; theShared.setData(localData); }

int localData = theShared.getData(); //1 localData++; //2 theShared.setData(localData); //3 int localData = theShared.getData(); //1 localData++; //2 theShared.setData(localData); //3 Code shared by threadA and threadB threadA -- execute //1 threadA -- execute //2 threadA -- execute //3 threadB -- execute //1 threadB -- execute //2 threadB -- execute //3 Execution Scenario 1: threadB -- execute //1 threadB -- execute //2 threadB -- execute //3 threadA -- execute //1 threadA -- execute //2 threadA -- execute //3 Execution Scenario 2: threadA -- execute //1 threadB -- execute //1 threadB -- execute //2 threadB -- execute //3 threadA -- execute //2 threadA -- execute //3 Execution Scenario 3: Whenever the potential order of execution can alter the outcome, this is called a _________ or ___________.

Three essential properties for a race condition _________ Property Two or more flows of control must execute concurrently/in parallel. _____________ Property Some resource must be shared by the concurrent flows. _____________ Property At least one of the concurrent flows must alter the state of the shared resource.

Solution to a race condition eliminate the concurrent access The “trick” is to use an atomic operation, such as a lock.

import java.util.concurrent.locks.ReentrantLock; public class Driver { private Shared theShared; private MyThread threadA, threadB; private ReentrantLock theLock; public Driver() { theShared = new Shared(); theLock = new ReetrantLock(); threadA = new MyThread(theShared, theLock); threadB = new MyThread(theShared, theLock); threadA.start(); threadB.start(); try { threadA.join(); threadB.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(theShared.getData()); } public static void main(String[] args) { new Driver(); } import java.util.concurrent.locks.ReentrantLock; public class Driver { private Shared theShared; private MyThread threadA, threadB; private ReentrantLock theLock; public Driver() { theShared = new Shared(); theLock = new ReetrantLock(); threadA = new MyThread(theShared, theLock); threadB = new MyThread(theShared, theLock); threadA.start(); threadB.start(); try { threadA.join(); threadB.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(theShared.getData()); } public static void main(String[] args) { new Driver(); } import java.util.concurrent.locks.ReentrantLock; public class MyThread extends Thread { private Shared theShared; private ReentrantLock theLock; public MyThread(Shared s, ReentrantLock l) { theShared = s; theLock = l; } public void run() { theLock.lock(); int localData = theShared.getData(); localData++; theShared.setData(localData); theLock.unlock(); } import java.util.concurrent.locks.ReentrantLock; public class MyThread extends Thread { private Shared theShared; private ReentrantLock theLock; public MyThread(Shared s, ReentrantLock l) { theShared = s; theLock = l; } public void run() { theLock.lock(); int localData = theShared.getData(); localData++; theShared.setData(localData); theLock.unlock(); }

Locks lead to another problem… _________ What if one thread terminates inside a critical section? lockSharedResource(); // the critical section unlockSharedResource(); A thread is deadlocked when it is impossible for it to resume execution even though the expected execution for the thread is incomplete. Potential Deadlock on two resources (A and B) lockSharedResourceA(); lockSharedResourceB(); // the critical section unlockSharedResourceB(); unlockSharedResourceA(); lockSharedResourceB(); lockSharedResourceA(); // the critical section unlockSharedResourceA(); unlockSharedResourceB(); Process 1Process 2

How can an attacker exploit race conditions? Deadlock leads to _____. Example: 2004 Apache HTTP Server Concurrency, and therefore, race conditions are sensitive to …  processor speeds  process/thread scheduling algorithms  memory constraints  asynchronous events  state of unrelated processes

What about loosely coupled (untrusted) processes? File targetFile = new File("/tmp/test"); if (targetFile.exists() && targetFile.canRead()) { try { FileInputStream = new FileInputStream(targetFile); inFile.read( someBuffer );... inFile.close(); } catch (IOException e) { e.printStackTrace(); } File targetFile = new File("/tmp/test"); if (targetFile.exists() && targetFile.canRead()) { try { FileInputStream = new FileInputStream(targetFile); inFile.read( someBuffer );... inFile.close(); } catch (IOException e) { e.printStackTrace(); } _________ (Time of Check, Time of Use) the window from TOC through TOU can lead to a race vulnerability

TOCTOU Mitigation   ________the file from other access. File targetFile = new File("/tmp/test"); if (targetFile.exists()) { try { FileChannel channel = null; FileLock lock = null; try { channel = new RandomAccessFile(targetFile,"rw").getChannel(); lock = channel.tryLock(); if (lock != null) { ByteBuffer bytes = ByteBuffer.allocate(100); channel.read(bytes);... lock.release(); } else // file is already locked } catch (OverlappingFileLockException e) { // file is already locked } finally { channel.close(); } } catch (IOException e) { e.printStackTrace(); } File targetFile = new File("/tmp/test"); if (targetFile.exists()) { try { FileChannel channel = null; FileLock lock = null; try { channel = new RandomAccessFile(targetFile,"rw").getChannel(); lock = channel.tryLock(); if (lock != null) { ByteBuffer bytes = ByteBuffer.allocate(100); channel.read(bytes);... lock.release(); } else // file is already locked } catch (OverlappingFileLockException e) { // file is already locked } finally { channel.close(); } } catch (IOException e) { e.printStackTrace(); }

A non-TOCTOU race condition: walking trees... chdir( “/tmp/a” ); chdir( “b” ); chdir( “c” ); // race window chdir( “..” ); unlink( “*” ); //delete all files... chdir( “/tmp/a” ); chdir( “b” ); chdir( “c” ); // race window chdir( “..” ); unlink( “*” ); //delete all files... Example (GNU utilities) file tree

A non-TOCTOU race condition: walking trees... chdir( “/tmp/a” ); chdir( “b” ); chdir( “c” ); // race window chdir( “..” ); unlink( “*” ); //delete all files... chdir( “/tmp/a” ); chdir( “b” ); chdir( “c” ); // race window chdir( “..” ); unlink( “*” ); //delete all files... Example (GNU utilities) the exploit mv /tmp/a/b/c /tmp/c file tree

Mitigation   avoid the use of relative path names use and verify ___________________   “..” and “.” in file names and URLs must be disallowed.   avoid using shared access containers

symlink vul This is a classic problem in Unix systems involving the use of symbolic links. The problem is that an attacker's symbolic link can be substituted for a file. (Symbolic links can even reference directories.) A classic example - passwd() 1) open some_dir/.rhosts to authenticate user; close.rhosts 2) create and open some_dir/ptmp 3) reopen some_dir/.rhosts and copy into opened ptmp 4) close files and rename some_dir/ptmp as some_dir/.rhosts

Suppose the user's directory is called victim_dir. Further suppose that the attacker uses s similar directory called attack_dir. A classic example - passwd() 1) open some_dir/.rhosts to authenticate user; close.rhosts 2) create and open some_dir/ptmp 3) reopen some_dir/.rhosts and copy into opened ptmp 4) close files and rename some_dir/ptmp as some_dir/.rhosts Attacker causes some_dir to be a link to attack_dir Attacker causes some_dir to revert to victim_dir Attacker causes some_dir to be a link to attack_dir Attacker causes some_dir to revert to victim_dir

Mitigation – All Race Conditions Closing the race window   identify all shared resources   use mutual exclusion via locks, semaphores, monitors, etc. Eliminating the race (shared) resource   be permission, authorization and privilege aware Controlling access to the race (shared) resource   use “thread safe” threads   check file properties securely   use canonical full path names   use trustworthy containers   static and dynamic detection tools can find some race conditions