Pwnkit: CVE-2021-4034
Overview — Pwnkit (CVE-2021-4034) is a memory-corruption bug inpkexec, the SUID-root front end of Polkit, that lets any local user become root on practically every mainstream Linux distribution. This free TryHackMe room explains what Polkit is, walks through the root cause found by Qualys (an emptyargvthat makespkexecread and writeenvp), lets you compile and run a public proof of concept on a prepared VM, and closes with patching and mitigation. The path is short: SSH as an unprivileged user → compile PoC → root shell.
| Platform | TryHackMe |
| Category | Offensive |
| Difficulty | Easy (listed as "Info" on the platform) |
| Room | Pwnkit: CVE-2021-4034 |
Attack map
[SSH] low-privileged user on MACHINE_IP
| gcc compiles the PoC (needs a compiler on the target)
v
[PoC] execve("/usr/bin/pkexec", argv = { NULL }, crafted envp)
| argc == 0 -> argv[1] is really envp[0]
v
[pkexec] PATH lookup result is written over envp[0]
| re-creates GCONV_PATH=... after the SUID env scrub
v
[glibc iconv] loads attacker's pwnkit.so as root
| gconv_init() -> setuid(0) + /bin/sh
v
[ROOT] uid=0 -> /root/flag.txt
Task 1 — Introduction and Deploy
The first task only sets the scene and starts the target VM. What matters is the scale of the problem: Polkit ships by default on most Linux distributions, and the vulnerable code in pkexec had been there since the tool was first written in 2009. Anyone with a shell account, however restricted, was a single command away from root. There is no remote angle, so the bug is a pure local privilege escalation (LPE), but that is still devastating on shared servers, CI runners, containers with a shell, or any host where an attacker has already landed as www-data.
No question needs answering here; start the machine and wait for it to boot.
Task 2 — Background
What Polkit and pkexec are
Polkit (formerly PolicyKit) is the authorisation framework used by systemd-era desktops and services to decide whether an unprivileged process may perform a privileged action. Instead of the all-or-nothing model of sudo, administrators describe fine-grained actions and rules, and a daemon answers "allowed", "denied" or "authenticate first". pkexec is the command-line client of that system: roughly the Polkit counterpart of sudo, used as pkexec COMMAND. To change identity it needs root rights, so the binary is installed SUID root:
ls -l /usr/bin/pkexec
# -rwsr-xr-x 1 root root ... /usr/bin/pkexec (the "s" is the SUID bit)The SUID bit is the key to the whole story. Whenever a user runs the file, the process starts with an effective UID of 0, so every parsing mistake inside pkexec happens with root privileges while the attacker still controls the arguments and the environment.
The root cause in detail
Two ordinary facts about Unix program start-up collide:
- argv and envp sit next to each other. When the kernel executes a program it builds on the new stack an array of argument pointers (
argv), a terminating NULL, and straight after it the array of environment pointers (envp). So, in memory,argv[argc]is the NULL terminator andargv[argc + 1]isenvp[0]. - argc can be zero. Convention says
argv[0]holds the program name, but nothing forces the caller ofexecve()to supply it. A custom launcher can passargv = { NULL }, givingargc == 0. (OpenBSD refuses such a call in its kernel, which is why it was not affected; Linux accepted it at the time.)
The main() of vulnerable pkexec assumes at least one argument exists. Its parsing loop starts at index n = 1, skipping the program name. With argc == 0 the loop condition n < argc is false immediately, so n stays at 1, and the code afterwards blindly uses argv[n] as the path of the program to run. But with no arguments, argv[1] is not an argument at all: it is envp[0], the first environment string. Two out-of-bounds accesses follow:
- Read:
pkexectakes the value ofenvp[0]as "the command to execute". - Write: if that string does not start with
/,pkexecresolves it by searching the directories ofPATH(find_program_in_path) and stores the resulting full path back intoargv[1], i.e. it overwritesenvp[0].
Why does overwriting an environment entry matter? For SUID programs the dynamic loader and libc deliberately delete "unsecure" variables such as LD_PRELOAD, LD_LIBRARY_PATH and GCONV_PATH before main() runs, so a user cannot inject code into a privileged process. The write above happens after that scrub. By choosing the value of PATH and the name of the file inside it, the attacker controls the string that lands in envp[0] and can therefore smuggle back a variable such as GCONV_PATH=... that was supposed to be gone.
The final step is to make a privileged code path actually honour that variable. Later, pkexec hits error paths (for example a SHELL value that is not listed in /etc/shells) and reports them through GLib's g_printerr(). That function converts the message to the locale charset using glibc's iconv. If the CHARSET variable names a non-UTF-8 charset, iconv consults a gconv-modules configuration file, looked up through GCONV_PATH, and dlopen()s the shared library it names. Result: an attacker-supplied .so is loaded into a root process and its initialisation function runs as root. Qualys also noted the bug can be exploited with other variables and in ways that leave no log trace, so you should not rely on one specific log line for detection.
Summary of the chain: emptyargv→argv[1]aliasesenvp[0]→ PATH-resolved string overwrites it → forbiddenGCONV_PATHis back → error message triggersiconv→ attacker library loaded as root.
The flaw was introduced in pkexec's first commit in May 2009, was reported to Polkit and distributions by Qualys, and was disclosed in a coordinated release on 25 January 2022. It is rated CVSS 7.8 (High).
Q: Is Pwnkit exploitable remotely (Aye/Nay)?
A: Nay
Q: In which Polkit utility does the Pwnkit vulnerability reside?
A: pkexec
Task 3 — Exploitation
Many public exploits exist; the room uses a compact C proof of concept (by arthepsy) that stays close to the Qualys technique and is already placed in the home directory of the target. Log in with the credentials the room provides (or use the in-browser terminal):
ssh tryhackme@MACHINE_IP
# password supplied by the roomThe PoC lives in a pwnkit/ directory. It is a single C file that is compiled on the box, so the machine needs gcc (this is also a useful reminder to check for a compiler during real engagements; if there is none, compile statically elsewhere, or use a Python/shell variant):
cd pwnkit
gcc cve-2021-4034-poc.c -o exploit
./exploit# id
uid=0(root) gid=0(root) groups=0(root),1000(tryhackme)The prompt changes to # and id confirms UID 0: instant root, no password, no race condition and no crash.
Reading the PoC
It is worth understanding why this works rather than only running it. The program does the following, in order:
- Prepares the trap files. It creates a directory literally named
GCONV_PATH=.containing an executable file calledpwnkit. This gives the laterPATHlookup something to find: searchingPATH=GCONV_PATH=.for a program namedpwnkitsucceeds and yields the pathGCONV_PATH=./pwnkit. - Writes a fake gconv configuration. A
pwnkit/gconv-modulesfile with a line of the formmodule UTF-8// PWNKIT// pwnkit 2declares a charset calledPWNKITthat is converted to UTF-8 by a module namedpwnkit(the shared objectpwnkit.soin the same directory). - Builds the payload library. It writes and compiles
pwnkit.sowith-shared -fPIC. Itsgconv_init()function, which glibc calls when it loads the module, runssetuid(0)/setgid(0)and thensystem()to spawn/bin/sh. Because the host process is the SUIDpkexec, this shell is a root shell. - Launches pkexec with the poisoned vector. The decisive call is:
char *env[] = { "pwnkit", "PATH=GCONV_PATH=.", "CHARSET=PWNKIT", "SHELL=pwnkit", NULL };
execve("/usr/bin/pkexec", (char*[]){NULL}, env);Mapping each element to the theory above:
| Element | Purpose |
|---|---|
argv = { NULL } | Makes argc == 0, so argv[1] aliases envp[0] |
"pwnkit" (envp[0]) | The "program name" that pkexec reads and resolves through PATH |
PATH=GCONV_PATH=. | A search directory whose name looks like an assignment, so the resolved path GCONV_PATH=./pwnkit is a valid variable definition once written into envp[0] |
CHARSET=PWNKIT | Forces g_printerr() to convert through our fake charset instead of plain UTF-8 |
SHELL=pwnkit | An invalid shell (not in /etc/shells), which makes pkexec take the error path that prints a message and triggers the conversion |
The shell command inside the payload also deletes the helper directory and file it created, so the working directory is left tidy.
Q: Exploit the vulnerability! What is the flag located at /root/flag.txt?
A: FLAG (flag redacted)
# cat /root/flag.txt
Task 4 — Remediations
Defence is straightforward, which is part of why leaving this unpatched is hard to excuse.
- Patch. Install the updated
policykit-1/polkitpackage from your distribution. The fix makespkexecrefuse to run when it is started without any argument. On Debian/Ubuntu:sudo apt update && sudo apt upgrade. Use your distribution's advisory to check the exact fixed version, since version numbers differ per vendor. - Temporary mitigation. If no package is available yet, strip the SUID bit so the binary no longer runs as root. The cost is that legitimate
pkexecuse stops working until you patch:
sudo chmod 0755 "$(which pkexec)"- Verify. Run the PoC on a fixed system. A patched
pkexecjust prints its usage text instead of giving a shell:
pkexec --version |
--help |
--disable-internal-agent |
[--user username] PROGRAM [ARGUMENTS...]
See the pkexec manual page for more details.- Detect. Several public variants leave messages in the authentication log, for example complaints that the
SHELLvalue is not in/etc/shellsor that an environment variable contains suspicious content. Hunting for those strings is useful but incomplete, because variants exist that avoid them. Better signals are unexpected processes that spawn a root shell withpkexecas the parent, andpkexecexecutions with no arguments. - Harden in depth. Remove compilers from production hosts, restrict shell access, and inventory SUID binaries regularly (
find / -perm -4000 -type f 2>/dev/null). Kernels released after the disclosure also changedexecve()so that an emptyargvis replaced with an empty-string argument, closing the root cause for every program, not justpkexec.
Task 5 — Conclusion
The closing task recaps the room and points to a related Polkit room (CVE-2021-3560, a timing-based authentication bypass in the Polkit daemon). No questions are asked.
Key takeaways
- Pwnkit is a local-only privilege escalation in the SUID-root
pkexec; it is not remotely exploitable on its own but is ideal as a second stage after any foothold. - The root cause is a classic assumption failure: code that assumed
argc >= 1, combined withargvandenvpbeing adjacent in memory, turned an out-of-bounds read/write into control of the environment. - SUID programs rely on the loader to remove dangerous variables such as
GCONV_PATHandLD_PRELOAD; any bug that re-injects them after that point defeats the protection. - The exploit is reliable and architecture-independent because it needs no memory-layout guessing: it abuses logic, not heap or stack addresses.
- Fix by patching Polkit (or by removing the SUID bit as a stop-gap) and verify by re-running the PoC; also keep compilers and unnecessary SUID binaries off servers.
- When enumerating a Linux host for privilege escalation, check the installed
polkitversion and the SUID list early: this single bug affected nearly everything built since 2009.