Skip to content
Memory corruption Write-up Advanced

CVE-2021-3156: Heap-Based Buffer Overflow in Sudo (Baron Samedit)

A heap-based buffer overflow in sudo's command-line argument processing allows any unprivileged user to gain root privileges. The flaw is reachable via sudoedit with the -s option, and the researchers developed working exploits for several Linux distributions.

blog.qualys.com

Published on blog.qualys.com

Sudo is a powerful utility that’s included in most if not all Unix- and Linux-based OSes. It allows users to run programs with the security privileges of another user.

The author, on blog.qualys.com

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

Likely the size calculation for the user_args buffer did not account for out-of-bounds characters copied when an argument ends with a single backslash, because the escaping in parse_args() was bypassed by invoking sudoedit with -s.

What the attacker needed

Local unprivileged user account; sudo version 1.8.2 to 1.8.31p2 or 1.9.0 to 1.9.5p1; default sudo configuration

The fix

The vulnerability was fixed by patching sudo; users should apply patches immediately.

How it was found

  1. Run sudoedit with the -s option and a command-line argument that ends with a single backslash, followed by a long string of characters.
  2. The argument bypasses the escaping in parse_args() because sudoedit sets MODE_EDIT and -s sets MODE_SHELL, avoiding MODE_RUN.
  3. The vulnerable set_cmnd() function copies out-of-bounds characters into the user_args heap buffer, causing a heap overflow.
  4. The overflow can be shaped by controlling the size of the buffer and the contents of the overflow, including null bytes, to overwrite adjacent heap metadata.

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 another user, often the administrator (root). A buffer overflow is a bug where a program writes more data into a memory area than it was sized for, which can corrupt nearby memory and let an attacker take control. Here, a specially crafted command-line argument causes sudo to write past the end of a heap buffer, and an attacker can use that to run code as root.

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.