
Exploitation and mitigation analysis of CVE-2021-3156 heap-based buffer overflow in sudo
CVE-2021-3156, known as Baron Samedit, is a heap-based buffer overflow vulnerability in the sudo program. It allows any unprivileged local user to gain root privileges on vulnerable systems without authentication.
This project documents the full exploitation and mitigation of this vulnerability as part of the Software Vulnerabilities: Exploitation and Mitigation course at the University of Luxembourg.
OS: Ubuntu 20.04.1
sudo version: 1.8.31-1ubuntu1 (vulnerable)
libc version: 2.31
User: Non-root account
To reproduce the vulnerable state:
sudo apt install sudo=1.8.31-1ubuntu1
The vulnerability is triggered when sudo is executed in sudoedit mode with carefully crafted command-line arguments that overflow internal heap buffers.
The exploit uses four critical parameters fine-tuned through memory analysis:
exploit_launcher.c — Main ExploitTriggers the vulnerability through 5 steps:
LC_ALL to control heap memory layoutsudoedit with the malicious arguments and environmentexploit_payload.c — Malicious PayloadExecuted automatically when the exploit succeeds via _init function:
0 (root)$ make
$ ./exploit_it
[!] CVE-2021-3156 Exploit
[+] Target: Ubuntu 20.04.1 (sudo 1.8.31, libc-2.31)
[+] Parameters: A=56, B=54, Nulls=63, LC_ALL=212
[+] Launching exploit...
[+] Exploit successful. Root privileges obtained.
# whoami
root
✅ Successfully escalated from unprivileged user to root.
Upgrade sudo to the patched version:
sudo apt install sudo=1.8.31-1ubuntu1.5
After upgrading, the same exploit failed to execute and root privileges were not granted — confirming the patch is effective.
Key takeaway: Keeping system utilities like
sudoup to date is critical for preventing privilege escalation attacks.
├── exploit_launcher.c # Main exploit triggering the heap overflow
├── exploit_payload.c # Malicious payload spawning root shell
├── libnss_X/ # Malicious library loaded during exploitation
└── Makefile # Build configuration
# Build
make
# Run exploit
./exploit_it
| Property | Details |
|---|
| CVE ID | CVE-2021-3156 |
| Nickname | Baron Samedit |
| Type | Heap-Based Buffer Overflow |
| Component | sudo (sudoedit mode) |
| Impact | Local Privilege Escalation to Root |
| Affected Versions | sudo ≤ 1.8.31 (Ubuntu), ≤ 1.9.5p1 (others) |
| Patched Version | sudo 1.8.31-1ubuntu1.5 |
| CVSS Score | 7.8 (High) |
| Parameter | Size | Purpose |
|---|
A | 56 bytes | Overflow the initial buffer |
B | 54 bytes | Overflow a second heap chunk |
Nulls | 63 bytes | Structure memory alignment |
LC_ALL | 212 bytes | Control heap layout via environment variable |