root@thehacksparrow:~/writeups$ SYSTEM ONLINE
root@sparrow:~/writeups$ cat baron-samedit.md
// Offensive

Baron Samedit

16 Nov 2026 · 4 min read
Overview — Baron Samedit is the third room in TryHackMe's SudoVulns series and covers CVE-2021-3156, a heap-based buffer overflow in sudo that lets any local user become root without a single sudoers entry. You log in over SSH to a deliberately vulnerable Ubuntu 18.04 box, confirm the bug with a one-line crash test, compile a public proof of concept and use it to obtain a root shell.
PlatformTryHackMe
CategoryOffensive
DifficultyEasy
RoomBaron Samedit
Target IPMACHINE_IP

Attack map

[SSH]  low-privileged account provided by the room
   |  sudoedit -s '\' AAAA...   -> heap corruption crash = vulnerable
   v
[BUILD]  make  (public PoC for CVE-2021-3156)
   |  ./exploit 0   (target 0 = Ubuntu 18.04.5, sudo 1.8.21)
   v
[ROOT]  uid=0  -> read /root/flag.txt  (FLAG)

Task 1 — Deploy

The room hands you a ready-made low-privileged account, so there is no initial foothold to find. This is a local privilege escalation lesson, and the only thing needed is an SSH session on the target. The credentials are published in the room itself (tryhackme / tryhackme):

ssh tryhackme@MACHINE_IP

The prompt of the target shows the hostname CVE-2021-3156, which is a handy reminder of what the machine is for.

Task 2 — Buffer Overflow

The vulnerability

Qualys disclosed this bug in January 2021 and nicknamed it Baron Samedit, a pun on the Haitian loa Baron Samedi and on sudoedit. It is a heap-based buffer overflow in sudo's argument handling. When sudo runs a command through a shell (-s or -i), it escapes meta-characters with backslashes before building the command line. The flaw is triggered when the arguments end in a lone backslash. The parser then reads past the terminating NUL byte and copies data it should not, overflowing the heap buffer. Running as sudoedit sets the mode flags in such a way that the unescaping step is skipped, which is why sudoedit -s reaches the vulnerable path.

What makes it dangerous is the combination of reach and prerequisites:

  • Affected versions: sudo 1.8.2 to 1.8.31p2 and 1.9.0 to 1.9.5p1, a code path that had existed for roughly ten years.
  • The exploit works with the default configuration. The user does not need to appear in sudoers and does not need to know any password.
  • Because sudo is installed setuid-root, corrupting its memory yields code execution as root.
The previous room in the series (CVE-2019-18634) was a stack overflow. The heap, by contrast, is the region used for dynamic allocations, so exploiting an overflow there depends on the allocator layout and libc version. That is why public exploits list specific distribution targets.

Checking whether a host is vulnerable

A harmless test is to invoke sudoedit in shell mode with an argument that ends in a backslash, followed by a long string:

sudoedit -s '\' $(python3 -c 'print("A"*1000)')

The single-quoted \ is the trailing backslash that triggers the faulty parsing, and the 1000 A characters give the overflow enough data to be visible. On a vulnerable build the allocator detects its own corrupted metadata and aborts:

malloc(): memory corruption
Aborted (core dumped)

A patched sudo instead prints a usage or "sudoedit: ... not allowed" style error and exits normally. A crash therefore means the host is exploitable in principle. You can also compare sudo -V against the affected version ranges above, keeping in mind that distributions backport fixes without changing the upstream version number.

Exploiting it

Qualys originally published only the advisory, not exploit code. Independent researchers reproduced it within days, and the first widely available working exploit came from a researcher using the handle blasty. The room has already placed that source in an Exploit folder in the home directory, containing two C files and a Makefile.

cd Exploit
ls
make

The Makefile compiles the main binary and also builds a small shared object into a libnss_X directory. The exploit makes sudo load that library through the NSS (name service switch) mechanism once its heap has been corrupted, and the library's constructor spawns the root shell. The compiled executable has a joke name, which is the answer to the first question.

Running the binary with no arguments prints its usage and the list of supported targets. Each target is a specific distribution, sudo and libc combination because the heap layout is version-dependent:

0) Ubuntu 18.04.5 (Bionic Beaver) - sudo 1.8.21, libc-2.27
1) Ubuntu 20.04.1 (Focal Fossa)   - sudo 1.8.31, libc-2.31
2) Debian 10.0 (Buster)           - sudo 1.8.27, libc-2.28

Check the operating system to pick the right one:

cat /etc/os-release
./sudo-hax-me-a-sandwich 0

The target is Ubuntu 18.04, so target 0 applies. Choosing the wrong target typically just crashes sudo, so it is worth getting this right. After a moment the exploit reports success and drops you into a shell:

id
cat /root/flag.txt
uid=0(root) gid=0(root) groups=0(root),1000(tryhackme)
FLAG

Q: After compiling the exploit, what is the name of the executable created?
A: sudo-hax-me-a-sandwich

Q: What is the flag in /root/flag.txt?
A: FLAG

Quick reference

ItemPurposeWhere it appears
CVE-2021-3156 (Baron Samedit)Heap-based buffer overflow in sudo; local privilege escalation to rootTask 2
Affected versionssudo 1.8.2 to 1.8.31p2 and 1.9.0 to 1.9.5p1Task 2
sudoedit -s '\' AAAA...Harmless crash test; "malloc(): memory corruption" means vulnerableTask 2, checking the host
make in the Exploit folderBuilds the exploit binary and the libnss_X shared objectTask 2, exploiting it
cat /etc/os-releaseIdentify the distribution to pick the right exploit targetTask 2
./sudo-hax-me-a-sandwich 0Target 0 (Ubuntu 18.04.5, sudo 1.8.21) gives a root shellTask 2
FixUpdate sudo to 1.9.5p2 or later, or the distribution's backported fixKey takeaways

Key takeaways

  • CVE-2021-3156 is a local privilege escalation in a setuid binary present on almost every Linux system; no sudoers entry or password is needed.
  • The detection test (sudoedit -s '\' ...) is quick and non-destructive on a lab box, but still crashes the process, so use it carefully on production systems.
  • Heap exploits are version-specific. Always match the exploit target to the exact distribution and libc before running it.
  • Defence: update sudo to 1.9.5p2 or later (or the distribution's backported fix), and treat any setuid binary as high-value attack surface. Monitoring for sudoedit invocations with -s/-i and a trailing backslash, plus sudo crashes in the logs, is a reasonable detection signal.