Summary
A normal user can privesc to root just by renaming their own account.
AccountsService lets you change your own “real name” over D-Bus, and it does that by running /usr/sbin/usermod as root with your “real name” as the -c argument. It doesn’t ask for a password or a polkit prompt. The only check on the “real name” is for commas (,), which lets us set it to --prefix=/home/user/pfx.
That works because usermod scans every argument for --prefix= up front, before it does its real option parsing. A proper parse would pair --prefix=/home/user/pfx with the -c flag it was handed to - this early scan doesn’t pair anything. It just finds --prefix= sitting in the argument list and takes it as a prefix directory, then builds the passwd database path as <prefix>/etc/passwd. You point pfx at a directory you own, holding a fake passwd that lists your account with uid 0.
Time to race
usermod opens your fake file, reads the uid-0 line, then opens it again to rewrite it. Between those two opens, you flip the pfx symlink to point at / instead. The path string usermod holds never changes - but pfx sits in the middle of it, so that same string now resolves to the real /etc/passwd. When usermod renames its result into place, it overwrites that file as root.
The result is a root shell with the account’s existing password. /etc/shadow is never touched, so the old hash still matches - so you can su into your own account and come back as root. The new uid 0 entry survives a reboot, and because the temp file is chowned from the fake database, /etc/passwd is left writable by your group.
The chain also slips past the systemd unit’s SystemCallFilter=~@mount sandbox: prefix mode never calls chroot, so the filter never covers the write. The fix is to reject a leading dash in the real name and, in shadow-utils, stop scanning argv for options and use fd-relative operations for the prefix.
POC
There is an accompanying repository with the exploit.
Run the POC
Run the following command in a bash shell as any user that has a set password:
# run the exploit script
python3 ./exploit.py
# switch to the user you ran the exploit script to update UID/EID
# we can also update or add a user to /etc/passwd because we are now the owner
su userThe separate exploit-mint.py exists because Mint and Debian accounts normally carry a GECOS such as user,,,, which AccountsService appends to the -c value, so the daemon resolves /home/user/pfx,,, instead of the comma-free /home/user/pfx; the Mint variant creates and flips the suffixed path while still sending --prefix=/home/user/pfx over D-Bus.
Background knowledge
A root D-Bus service
AccountsService runs as root and owns the bus name org.freedesktop.Accounts. Each local account has an object such as /org/freedesktop/Accounts/User1000. The SetRealName method changes the real name, which is the first field of the GECOS comment in /etc/passwd. It is a normal D-Bus call. A local user can make it from a background process; no session or terminal is needed.
The account databases are colon-separated records
/etc/passwd is world-readable and holds the account name, password placeholder, uid, gid, GECOS, home, and shell. /etc/shadow is root-only and holds the password hash. The two files are linked by the account name. If /etc/passwd maps a name to uid 0 but /etc/shadow still has that name’s old hash, the user can log in with their old password and receive uid 0.
usermod -c writes the GECOS field
AccountsService does not edit /etc/passwd directly. It runs /usr/sbin/usermod as root with -c and the caller’s string. usermod is a shadow-utils program. It is normally a careful tool, but the caller’s string is placed in an argument position that the program later re-reads.
--prefix is a path prefix, not a chroot
shadow-utils has a --prefix option for operating on a mounted system image. It builds database paths as prefix + "/etc/passwd". It does not call chroot. The final file is opened with O_NOFOLLOW, but the prefix is an intermediate path component. An intermediate symlink is followed.
A symlink can be changed between calls
A path name is not a stable reference to a file. Each operation resolves the name again. If the prefix is a symlink owned by the attacker, the database can be opened under one target and written under another. The open and the close are separated by the parse of a large fake database, which gives the attacker a wide window.
Prior art
shadow-utils PR #731 (2023) described a related prefix race and added a privilege drop. That drop is a no-op when the caller is already root, as in the AccountsService child. This report covers the unauthenticated AccountsService path, the root-run helper, the /etc/passwd target, and the @mount filter bypass. It does not claim the prefix race itself as new.
Detailed explanation
Step 1: the daemon lets the caller change their own name
The attacker starts by finding the D-Bus object for their own account. The AccountsService method FindUserByName returns the object path, for example /org/freedesktop/Accounts/User1000. The attacker then calls SetRealName on that object.
In AccountsService’s upstream/src/user.c, user_set_real_name() first asks the D-Bus invocation for the caller’s uid:
if (!get_caller_uid (context, &uid)) {
throw_error (context, ERROR_FAILED, "identifying caller failed");
return TRUE;
}It then checks whether the target account’s uid is the caller’s uid. If it is, the action is the self-service action; otherwise it is the administrative action:
if (accounts_user_get_uid (ACCOUNTS_USER (user)) == (uid_t) uid)
action_id = "org.freedesktop.accounts.change-own-user-data";
else
action_id = "org.freedesktop.accounts.user-administration";The only validation on the new name is a comma check:
if (g_utf8_strchr (real_name, -1, ',') != NULL) {
throw_error (context, ERROR_FAILED,
"setting real name failed: real name '%s' must not contain commas",
real_name);
return TRUE;
}There is no check for a leading -, no check for --, and no check that the value could be confused with an option. The daemon then calls daemon_local_check_auth() with the chosen action. polkit answers “yes” because of allow_any=yes, and the callback runs.
At this point the attacker has done nothing privileged. They have only asked to change their own display name. The value they chose happens to be:
--prefix=/home/user/pfxStep 2: the name becomes a root helper’s argument
The authorized callback in upstream/src/user.c prepares the GECOS value and builds the command line:
if (user->gecos != NULL)
first_comma = g_utf8_strchr (user->gecos, -1, ',');
if (first_comma != NULL) {
/* Preserve the existing value of the GECOS
* except for the first element, full name.
*/
new_gecos = g_strconcat (name, first_comma, NULL);
} else {
new_gecos = g_strdup (name);
}
argv[0] = "/usr/sbin/usermod";
argv[1] = "-c";
argv[2] = new_gecos;
argv[3] = "--";
argv[4] = accounts_user_get_user_name (ACCOUNTS_USER (user));
argv[5] = NULL;The comma check in user_set_real_name() applies to the new name, not to the GECOS suffix that may already exist. If the account’s current GECOS is User,,,, the new GECOS becomes --prefix=/home/user/pfx,,,. The lab accounts are created with useradd and have an empty GECOS, so in the lab the value is exactly the attacker’s string. On a real account with commas already in the GECOS, the attacker has to account for the appended suffix when placing the symlink, but the injection itself still works.
The callback then calls spawn_sync() from AccountsService’s upstream/src/util.c:
if (!g_spawn_sync (NULL, (gchar **) argv, NULL, 0, NULL, NULL,
NULL, NULL, &status, error))g_spawn_sync() does not invoke a shell. The argv array goes directly to execve. So metacharacters such as ;, $(), or backticks have no special meaning. The only thing that matters is that argv[2] is a single string that begins with --prefix=.
The -- at argv[3] is a normal getopt end-of-options marker. It protects the username at argv[4] from being interpreted as an option. It does not protect argv[2], because argv[2] is the required argument of -c. A getopt-style parser will consume it as data even though it starts with -.
More importantly, usermod does not begin with a normal getopt pass.
Step 3: shadow-utils scans every argv element for a prefix
Near the start of main() in shadow-utils’ shadow/src/usermod.c, before the tool parses its normal options, it calls:
process_root_flag ("-R", argc, argv);
prefix = process_prefix_flag ("-P", argc, argv);The implementation of process_prefix_flag() is in shadow-utils’ shadow/lib/prefix_flag.c:
for (int i = 0; i < argc; i++) {
const char *val = strprefix(argv[i], "--prefix=");
if (streq(argv[i], "--prefix")
|| val != NULL
|| streq(argv[i], short_opt)) {
if (val)
prefix = val;
else
prefix = argv[++i];
}
}This loop has no idea which arguments belong to which option. It does not know that argv[1] is -c and argv[2] is the argument of -c. It just walks the whole array looking for a prefix spelling. In the exploit, argv[2] is --prefix=/home/user/pfx, so strprefix() returns a pointer to /home/user/pfx, and that becomes the prefix.
The same file then sets the database paths:
passwd_db_file = xaprintf("%s/%s", prefix, PASSWD_FILE);
pw_setdbname(passwd_db_file);
group_db_file = xaprintf("%s/%s", prefix, GROUP_FILE);
gr_setdbname(group_db_file);
...PASSWD_FILE is "etc/passwd", so the passwd database path becomes /home/user/pfx/etc/passwd. The group and other database paths are set the same way.
There is a privilege-drop block immediately after the prefix is selected:
/* Drop privileges */
if ((setregid (getgid (), getgid ()) != 0)
|| (setreuid (getuid (), getuid ()) != 0)) {
...
exit (EXIT_FAILURE);
}
if (streq(prefix, "") || streq(prefix, "/"))
return "";
/* should we prevent symbolic link from being used as a prefix? */The drop is intended for setuid account-management tools. If an ordinary user runs a setuid usermod, the real uid is the user and the effective uid is root; setreuid(getuid(), getuid()) drops the effective root uid. But here the caller is not an ordinary user invoking a setuid binary. The caller is the root AccountsService daemon, and it spawned usermod as root. The real uid and effective uid are both 0, so the “drop” keeps uid 0. It is a no-op.
The prefix is also checked for being absolute and for not being the empty string or /. /home/user/pfx passes both checks. The code comment about symbolic links is the missing defense: there is no lstat, no O_NOFOLLOW on the prefix, and no check that the prefix is a real directory. The prefix is just a string.
Step 4: the fake database and the watcher
Before sending the D-Bus call, the attacker prepares a directory and a symlink. The PoC in report/exploit.py does this in build_fake_db() and Watcher.
The fake root is /home/user/pfxA. It contains an etc directory with:
passwd: a copy of the real/etc/passwdwith the attacker’s own line removed, and a new line for the attacker at the end with uid 0 and gid 0.group: a copy of the real group file, so lookups that need groups still work.
The passwd copy is padded with a very large number of blank lines. The PoC uses one million. The padding is not needed for correctness; it is a scheduling aid. It keeps usermod busy parsing long enough for the attacker’s watcher thread to run before usermod reaches the close-and-rename stage.
The attacker then creates:
/home/user/pfx -> /home/user/pfxAand starts an inotify watcher on /home/user/pfxA/etc/passwd for IN_OPEN.
The watcher counts opens. The first open is the account lookup. The second open is the one that matters. When the count reaches two, the watcher creates a temporary symlink whose target is / and atomically replaces pfx with it:
os.symlink("/", tmp)
os.replace(tmp, self.link)After that, /home/user/pfx points to /, so the path /home/user/pfx/etc/passwd denotes /etc/passwd.
Step 5: usermod opens the fake database twice
usermod begins by finding the account it was asked to modify. In shadow-utils’ shadow/src/usermod.c:
pwd = prefix_getpwnam (user_name);
if (NULL == pwd) {
eprintf(_("%s: user '%s' does not exist\n"), Prog, user_name);
exit (E_NOTFOUND);
}prefix_getpwnam() is in shadow-utils’ shadow/lib/prefix_flag.c. When a prefix is set, it opens the prefixed passwd file with a plain fopen():
fg = fopen(passwd_db_file, "r");
...
while (NULL != (pwd = fgetpwent(fg))) {
if (streq(name, pwd->pw_name))
break;
}This is the first open. It reads the fake file and finds the attacker’s line with uid 0.
Later, usermod locks and opens the database for modification:
if (pw_open (O_CREAT | O_RDWR) == 0) {
eprintf(_("%s: cannot open %s\n"), Prog, pw_dbname());
fail_exit (E_PW_UPDATE, process_selinux);
}pw_open() goes through commonio_open() in shadow-utils’ shadow/lib/commonio.c:
fd = open (db->filename,
(db->readonly ? O_RDONLY : O_RDWR)
| O_NOCTTY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC);This is the second open. It succeeds because the final component is the regular file passwd inside the fake directory. O_NOFOLLOW applies to that final component. The intermediate component pfx is a symlink, and symlinks in the middle of a path are followed normally. The file descriptor now refers to the fake file, and commonio_open() parses its contents into memory.
At this point the watcher sees the second IN_OPEN and flips the prefix. The open file descriptor is unaffected: it still points to the fake inode that was opened. But the string db->filename is still /home/user/pfx/etc/passwd, and that string now resolves to /etc/passwd.
The order matters. If the watcher flipped the prefix before the second open, commonio_open() would open the real /etc/passwd and read the real account entries, so the fake uid 0 would never enter the in-memory database. The flip has to happen after the second open. It also has to happen before commonio_close() resolves the path again. The watcher’s inotify event guarantees the “after open” part; the million blank lines give it time before the close.
Step 6: the close path writes through the re-resolved string
usermod -c changes the attacker’s GECOS in the in-memory database. When usermod is done, it calls pw_close(), which reaches commonio_close() in shadow-utils’ shadow/lib/commonio.c.
commonio_close() sees that the database is open and changed. It calls:
create_backup(db->filename, db->fp);db->filename is the string /home/user/pfx/etc/passwd. After the flip, that is /etc/passwd. create_backup() creates a temporary file next to that path, copies the fake database from the already-open descriptor, and renames the temporary file to /etc/passwd-. The backup is therefore also the fake database, but the important target is the next one.
Then commonio_close() builds the final temporary filename:
if (stprintf_a(tmpf, "%s.cioXXXXXX", db->filename) == -1)
goto fail;Again the string resolves to /etc/passwd.cioXXXXXX. The process creates that temporary file, writes the modified database, flushes it, and calls:
if (rename(tmpf, db->filename) != 0)
goto fail;The rename target is /etc/passwd. rename() does not care that the process opened a different inode earlier; it replaces the directory entry named by the target string. The temporary file is created and renamed by root, so the new /etc/passwd is installed by root.
The installed file contains the attacker’s fake account line, with the GECOS that usermod just wrote:
user:x:0:0:--prefix=/home/user/pfx:/home/user:/bin/bashThe attacker’s account now has uid 0 and gid 0. The account still has the attacker’s home directory and shell.
Step 7: logging in as the rewritten account
su user asks for the user’s password and validates it the normal way. Once the password is accepted, su looks up the target account in /etc/passwd to decide which uid and gid to switch to. That lookup now returns 0 and 0. The process changes to uid 0 and gives the attacker a root shell.
The attacker did not need the root password, did not need to invoke a root-owned helper directly with crafted input, and did not need to exploit a kernel bug. They only needed to change their own display name and win a filesystem race against a root helper.
Execution order at a glance
Each numbered exchange is one stage of the sequence above. The attacker-controlled name becomes the prefix, and the same path string is resolved again after the database is open.
Disclosure timeline
- Upstream history and advisories reviewed
The accountsservice and shadow-utils trackers and advisories show no report of the complete chain. The
--prefixsymlink primitive is prior art (shadowPR#731, 2023) but was left unfixed. - Local root reproduced end-to-end
- ZDI - Case Opened
- This post
Decided to go public - read previous LPE for the reason
- Vendor fix and CVE assignment
TODO: update once CVE is assigned.