Skip to content

Commit c84b027

Browse files
committed
login-utils: fix setpwnam() buffer use [CVE-2025-14104]
This issue has been originally fixed in the master branch, but unfortunately was not backported to stable/v2.41 yet. References: aaa9e718c88d6916b003da7ebcfe38a3c88df8e6 References: 9a36d77012c4c771f8d51eba46b6e62c29bf572a Signed-off-by: Karel Zak <kzak@redhat.com> (cherry picked from commit 9753e6ad9705104c3b05713f79ad6732cc4c7b30)
1 parent ec0dfcc commit c84b027

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

login-utils/setpwnam.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ int setpwnam(struct passwd *pwd, const char *prefix)
9999
goto fail;
100100

101101
namelen = strlen(pwd->pw_name);
102-
102+
if (namelen > buflen)
103+
buflen += namelen;
103104
linebuf = malloc(buflen);
104105
if (!linebuf)
105106
goto fail;
@@ -126,10 +127,12 @@ int setpwnam(struct passwd *pwd, const char *prefix)
126127
}
127128

128129
/* Is this the username we were sent to change? */
129-
if (!found && linebuf[namelen] == ':' &&
130-
!strncmp(linebuf, pwd->pw_name, namelen)) {
131-
/* Yes! So go forth in the name of the Lord and
132-
* change it! */
130+
if (!found &&
131+
strncmp(linebuf, pwd->pw_name, namelen) == 0 &&
132+
strlen(linebuf) > namelen &&
133+
linebuf[namelen] == ':') {
134+
/* Yes! But this time let’s not walk past the end of the buffer
135+
* in the name of the Lord, SUID, or anything else. */
133136
if (putpwent(pwd, fp) < 0)
134137
goto fail;
135138
found = 1;

0 commit comments

Comments
 (0)