Race Conditions and Threads
Introductions
- introductions of two students
Questions on the readings
The readings today are from Computer Security and the Internet, Chapter 6, sections 6.0 - 6.1.
Key Concepts
- understand why software security is important — your most likely security-related role if you will be a developer
- the importance of thinking like an attacker if you are a defender
- we study C because it is filled with security vulnerabilities, these are still prevalent, and we see them plague other languages as well
Race Conditions
-
time-of-check, time-of-use (TOCTOU) race
- see Figure 6.1, page 157
- see the subsequent example
- walk through example in Figure 6.2
-
the book walks through various approaches and discusses pros and cons — this is useful to understanding offensive and defensive thinking
Threads
- Creating a thread: see pthread_create() man page.
- Waiting for a child thread to finish: see pthread_join() man page
- Printing a thread identifier: see pthread_self() man page
- See the
threads-example
program in the class exercises
Mutexes
- Provides a way for a thread to lock access to shared memory
- A thread locks and then unlocks a mutex
- The area between the lock/unlock is called a critical section
- See the pthread_mutex_init() man page, this Stack Overflow description of mutex attributes, and the pthread_mutex_lock() man page.
Class Exercises
See the software security repo for code we will use:
- In
race
we will see an example of threads usingthread-example
- In
race
we will run aninventory
program with a vulnerability, then fix it with mutexes ininventory-fixed
. We will then show a monitor-like solution to provide better scalability and reliability ininventory-monitor
. - For both of these we will explain the Docker and Makefile setup that accompanies this code.