A security architect is briefing a development team on what each runtime defence does against memory and timing flaws.
Match each defence to what it blocks.
Choose the match for each entry. Every match has to be right.
A running program keeps its working data in reserved spaces in memory, and it often does two things at once. Two families of flaw follow. A program that writes past the end of a space, or uses a space after handing it back, can be taken over. A program that checks something and then acts on it a moment later can be fooled in between. The environment a program runs in can block much of this. No programming background is assumed.
An illustrative example
A small cinema sells tickets online. On a Saturday night one seat is left: F12. Two customers in different towns press Book within the same second.
The booking program checks for each of them that F12 is free. It is, both times. It takes both payments, then marks the seat as sold, twice. At seven o'clock two people arrive holding tickets for F12. The manager looks at the program's records and finds nothing wrong: every sale checked the seat before taking the money.
Each sale checked that the seat was free before selling it. How did both checks pass?
A program at work needs somewhere to keep the things it is handling: a name typed into a form, a running total, a list of seats. It asks for space in memory, the computer's short-term working store, and is given a block of fixed size. A block reserved to hold data is called a buffer.
Next to a buffer the program keeps its own notes. One of them says where to carry on when the current task finishes, much as a bookmark marks the page to return to. If the program writes past the end of a buffer, it writes over whatever comes next, including those notes.
The program's runtime is everything around it while it runs. That means the operating system's services and, for many languages, a virtual machine that carries out the code and checks it as it goes. Some flaws are the programmer's to prevent. Others can be caught or blunted by the runtime, whoever wrote the code.
A buffer overflow happens when a program copies more data into a buffer than it holds. It never compared the size of the input with the size of the space. The surplus spills over the neighbouring memory.
A buffer is a block of memory of fixed size reserved to hold data. Next to it the program keeps its own notes, and one of them, like a bookmark, says where to carry on when the current task finishes.
An attacker sends input of exactly the right length and content. The overflow replaces the bookmark with an address of the attacker's choosing, often pointing back into the attacker's own input. When the task ends, the program carries on from there and runs the attacker's instructions with the program's own rights.
An integer overflow is a number growing too big for the space that holds it. A counter that can hold up to 255 goes from 255 to 0, like a car's mileage display turning over. If the program then uses that wrapped number to decide how much space to reserve, it reserves far too little, and the next copy overflows the buffer.
The lasting fixes are in the code. Check the length of every input against the space before copying, and check each calculated size for overflow before using it. A memory-safe language makes both checks for the programmer.
In C and C++ a program reserves memory, uses it, and must release it when finished. Two opposite mistakes follow.
A leak harms . A use after free harms all three security properties.
Garbage collection, in which the runtime releases memory once nothing refers to it, removes use after free. To do the work, the collector stops the program briefly; each stop is called a pause. Garbage collection does not prevent leaks: a program that keeps references it no longer needs, such as a list that only grows, still leaks. In C and C++, careful rules and testing tools reduce both flaws.
Computers handle many requests at once. A race condition is a flaw whose result depends on which of two simultaneous activities gets to a shared resource first. The cinema's seat was the shared resource, and both sales got there before either finished.
The commonest form has its own name. Time of check to time of use (TOCTOU) is a race in which a program checks something and acts on the answer a moment later. The thing changes in the gap. The seat was free when each check ran. By the time each sale used that answer, it was no longer true.
A switched file
A program with administrator rights checks that a user may write to a file, then opens it. Between the check and the opening, the user swaps the file for a pointer to the password file. The program writes to a file the user could never have touched.
The fix is to make the check and the use one step that nothing can interrupt. The program locks the resource before checking it and releases the lock after using it, or uses an operation that checks and acts together. A database transaction that books the seat only if it is still free does exactly this. Shrinking the gap makes an attack harder but does not remove it.
Code should be correct, but code written in C or C++ will contain some memory flaws. The runtime adds layers that make them hard to exploit.
| Defence | How it works | What it blocks |
|---|---|---|
| Bounds checking | The language or runtime checks every read and write against the size of the buffer | Buffer overflow at its cause |
| Stack canary | A known value is placed before the bookmark and checked before it is used. A changed value stops the program | Overwriting the bookmark unnoticed |
| Data execution prevention (DEP) | Memory that holds data is marked as not runnable | Running instructions hidden in input |
| Address space layout randomisation (ASLR) | Code is loaded at unpredictable addresses | Jumping to a known useful address |
| Garbage collection | The runtime releases memory once nothing refers to it | Use after free, but not memory leaks |
| Locks and single-step operations | Only one activity at a time may check and change the resource | and TOCTOU |
A is a controlled place to run code, with access only to what it is allowed to touch: usually no files and no network. A browser runs each web page's scripts in one. Managed code, run by a virtual machine such as Java's or that of .NET, gets bounds checking and garbage collection on every operation.
A container packages a program with everything it needs and runs it apart from others on the same machine. Containers share one operating system core, so their separation is weaker than separate virtual machines.
Common misconception
“With data execution prevention and address randomisation switched on, buffer overflows no longer matter.”
Both make an overflow harder to exploit. Neither removes it, and attackers have ways round each. The overflow can still crash the program. The cure is bounds checking in the code or a memory-safe language.
A medical device maker's firmware is written in C. A penetration test finds a buffer overflow in the code that reads patient records from a memory card. The firmware ships next month, and rewriting it in another language would take two years. Address randomisation and data execution prevention are already enabled on the device.
What should the security manager require FIRST?
Answer: A. Correct the copy so that it checks input length against the buffer
5 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.
These questions are drafts that no named reviewer has approved yet.
Question 1 of 5
A security architect is briefing a development team on what each runtime defence does against memory and timing flaws.
Match each defence to what it blocks.
Choose the match for each entry. Every match has to be right.
Bounds checking stops buffer overflows at the cause. Stack canaries detect an overwritten return note. DEP stops data being run as code. ASLR hides where code sits. Garbage collection prevents use after free, not leaks. Locks prevent race conditions and TOCTOU. A sandbox limits what code can reach. Containers share one operating system core, so they separate less than virtual machines.
Input longer than the buffer spills over neighbouring memory and replaces the note saying where to carry on with an address the attacker chose. An integer overflow is a number too big for its space, which wraps round, such as 255 to 0. If that number sets how much space to reserve, too little is reserved and the next copy overflows.
A memory leak is never releasing memory no longer needed, until the program slows or stops; it harms availability. A use after free is old code still reading or writing a released block now given to another part; it harms all three security properties. Garbage collection removes use after free but does not prevent leaks.
A race condition in which a program checks something and acts on the answer a moment later, and the thing changes in the gap. The fix is to make the check and the use one step that nothing can interrupt, by locking the resource or using an operation that checks and acts together. Shrinking the gap does not remove it.