Skip to content
Memory corruption Write-up Advanced

Exploit of SUDO vulnerability Heap-Based Overflow [CVE-2021-3156 ]

The text walks through a heap-based buffer overflow in sudo's argument parsing, where a trailing backslash causes the parser to read past the end of the string. An attacker could use crafted arguments to corrupt memory and, with a controlled library name, load an arbitrary shared library.

Mohamed Sayed Follow

Published on flex0geek.blogspot.com

on May 25, 2023

Mohamed Sayed (Flex), on flex0geek.blogspot.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 argument parser does not check that a character following a backslash exists, so a trailing backslash makes it read the string terminator and continue past the buffer.

Impact

An attacker with local access could corrupt heap memory and cause sudo to load a shared library under their control, which could lead to privilege escalation.

What the attacker needed

Local access to run sudo; crafted arguments and environment; a shared library to load

How it was found

  1. Compile sudo with AddressSanitizer and run the proof-of-concept to see the crash.
  2. Inspect the crash in gdb at the parser line in sudoers.c and follow the backslash handling.
  3. Fuzz sudo with random environment variables and arguments to find crashing inputs.
  4. Pass the crashing arguments through a small C wrapper so each backslash is preserved.
  5. Review glibc's nss_lookup_function and nss_load_library to find the dlopen path.
  6. Craft arguments that set the library pointer to null and supply a controlled library name.

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. A buffer overflow happens when a program writes more data into a memory area than it was sized for, which can let an attacker change nearby data. Here, a backslash at the end of an argument made sudo read one byte past the string, and the author shows how that could be steered into loading a library of the attacker's choosing.

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.

More by Mohamed Sayed

View all

9 more disclosed reports, best first.

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.