Skip to content

Lesson 2 of 2

Use after free

Listen to an AI summary of Use after free

0:00 / 0:32

What is a pointer

Every place in memory has a number, and the number is the location of the place. A block of memory is a group of places next to each other.

A pointer is a value that holds the location of some data in memory. A value is one piece of data, such as a number. Programmers say that a pointer points at the data.

A sports shop website prints a name on the back of a hoodie, and Sam types the name SAM K. The code keeps SAM K in a block of memory that starts at location 5000. A pointer called name holds the number 5000.

Freeing memory

C code asks for a block of memory, uses the block, and then frees the block. Freeing memory is handing a block of memory back so that the block can be used for something else.

In C, the function free frees a block, and C++ also has delete. The line free(name) frees the block that name points at. After free(name), the pointer name still holds 5000.

A hotel key after checkout

A block of memory is like a hotel room, and a pointer is like the key to the room. Checking out of the hotel is freeing the memory. The hotel then gives the room to a new guest.

A key that still opens the room after checkout is a pointer to freed memory. A real hotel cancels the old key. C leaves the pointer unchanged.

  1. 1.In use

    The block at 5000 holds SAM K.

  2. `free(name)`

    2.Freed

    The block is handed back.

  3. another part of the program asks for memory

    3.Used for something else

    The block holds another customer's address.

The pointer name holds 5000 at all three moments.

What is use after free

Use after free (CWE-416) is using a pointer to read or write memory after the memory has been freed.

A use after free in five lines

The five lines of C read the name for a hoodie from a request. A name that is too long gets a warning. Look at lines 3 and 4.

  1. char *name = read_name(request);// makes the pointer name
  2. if (too_long(name)) {// checks the length of the name
  3. free(name);// frees the block
  4. print_warning(name);// reads the block after the free
  5. }// ends the if
Line 3 frees the memory and line 4 uses the memory afterwards.

On line 1, char * makes name a pointer to text. The function read_name puts the name from the request in a new block of memory and returns the location of the block.

Line 3 frees the block. Line 4 passes name to the function print_warning, which reads the text at location 5000. Line 4 is a use after free.

The safer version prints the warning first and frees the block last.

char *name = read_name(request);
if (too_long(name)) {
    print_warning(name);
    free(name);
}

What can go wrong

A freed block can be given to another part of the program a moment later. The block at location 5000 may now hold another customer's address. Line 4 then prints the other customer's address in the warning. A write through the pointer changes the other customer's address.

An attacker can send requests whose data the program stores in the freed block. Some data in a block is the location of code for the program to run next. An attacker who fills the freed block with a chosen location chooses which code runs.

Double free

A double free (CWE-415) is freeing the same block of memory twice. The C functions that hand out memory keep records of which blocks are free. The second free damages the records. The functions can then give the same block to two parts of the program at the same moment.

Two pointers to one block

Two pointers can hold the same location. One part of the code frees the block, and another part later uses a copy of the pointer kept in a list. The free and the use can be in different files. A diff can show the free and not the use.

Examples of fixes

A fix gives each block one owner, the one part of the code that frees the block. Every other pointer to the block is cleared before the free, and the owner clears its own pointer straight after. Clearing a pointer is setting the pointer to NULL, a value that means the pointer points at nothing. The two lines free the name and then clear the pointer.

free(name);
name = NULL;

Why Python has no use after free

Python code never frees memory itself. A garbage collector is the part of a programming language that frees memory automatically once nothing uses the memory. The languages Python, Java, JavaScript and Go each have a garbage collector.

The garbage collector frees a block only after nothing in the program refers to the block. Ordinary Python code therefore has no pointer to freed memory.

Use after free is found in C and C++ code, and in C code that other languages use.

Examples of what to look for

  • A free or a delete with a later use of the same pointer, such as on the next turn of a loop.
  • A copy of a pointer kept in a list, and a free that does not clear the copy.
  • A free at the end of a request, where a pointer kept for later requests still points at the freed block.
  • Two uses of free on the same pointer.
  • A free of a pointer that is used or kept afterwards, with no line that sets the pointer to NULL.

Check your understanding

  1. State what a pointer holds.

    Show the answer

    A pointer holds the location of some data in memory.

  2. Which line of the five line example uses memory after the memory is freed?

    Show the answer

    Line 4, print_warning(name), reads the block that line 3 freed.

  3. Give one reason why ordinary Python code has no use after free.

    Show the answer

    The garbage collector frees a block only after nothing in the program refers to the block.

Practise it

Challenges with a weakness this lesson teaches.