Skip to content
Memory corruption Write-up Advanced

Sudo Exploit Writeup (CVE-2021-3156)

A heap buffer overflow in sudo's argument processing allows an attacker to overflow into a service_user struct and control the name of a library loaded by nss_load_library, leading to privilege escalation to root. The authors developed a working exploit for libc 2.32 and sudo 1.9.4p2.

kalmarunionen.dk

Published on kalmarunionen.dk

On the 2021-01-26 qualy released this article describing a “new” (actually 10 year old) bug in sudo that allows an attacker to do privilege escalation though a heap buffer overflow. Unfortunately they did not release exploit/POC so I decided to build one myself and failed.

The author, on kalmarunionen.dk

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The code calculates the buffer size using strlen but then copies characters while skipping backslashes, so a trailing backslash causes it to read past the null terminator and write out of bounds.

Impact

An attacker could escalate privileges to root by loading a malicious library, and the exploit is 100% reliable with ASLR enabled on the tested environment.

What the attacker needed

Local access to a system with vulnerable sudo; ability to execute sudoedit with crafted arguments; ability to set environment variables; write access to a directory for the malicious library

How it was found

  1. Use the sudoedit symlink to sudo with the -s flag to trigger the vulnerable code path.
  2. Craft arguments and environment variables so that a trailing backslash causes the size calculation to be too small and the copy loop to overflow.
  3. Use environment variables ending with backslashes to insert null bytes and control the overflow contents.
  4. Groom the heap by using setlocale to free a chunk of a chosen size early in the heap.
  5. Overflow into a service_user struct, setting its library pointer to null and its name to a controlled value.
  6. Place a malicious shared library at the path libnss_X/X.so.2 so that nss_load_library loads it and executes the constructor.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Sudo is a program that lets users run commands as root. A bug in how it copied command-line arguments into memory allowed an attacker to write past the end of a buffer. By carefully crafting arguments and environment variables, the attacker could make sudo load a malicious library, which then ran with root privileges.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.