Skip to content

Commit 92b2646

Browse files
committed
nss-systemd: pack pw_passwd result into supplied buffer
getpwnam_r() guarantees that the strings in the struct passwd that it returns are pointers into the buffer allocated by the application and passed to getpwnam_r(). This means applications may choose to modify the strings in place, as long as the length of the strings is not increased. So it's wrong for us to return a static string here, we really do have to copy it into the application-provided buffer like we do for all the other strings. This is only a theoretical problem since it would be very weird for an application to modify the pw_passwd field, but I spotted this when investigating a similar crash caused by glib editing a different field. See also: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2244
1 parent 9f6ef46 commit 92b2646

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/nss-systemd/userdb-glue.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ int nss_pack_user_record(
3535
assert(hr->user_name);
3636
required = strlen(hr->user_name) + 1;
3737

38+
required += 2; /* strlen(PASSWORD_SEE_SHADOW) + 1 */
39+
3840
assert_se(rn = user_record_real_name(hr));
3941
required += strlen(rn) + 1;
4042

@@ -51,12 +53,12 @@ int nss_pack_user_record(
5153
.pw_name = buffer,
5254
.pw_uid = hr->uid,
5355
.pw_gid = user_record_gid(hr),
54-
.pw_passwd = (char*) PASSWORD_SEE_SHADOW,
5556
};
5657

5758
assert(buffer);
5859

59-
pwd->pw_gecos = stpcpy(pwd->pw_name, hr->user_name) + 1;
60+
pwd->pw_passwd = stpcpy(pwd->pw_name, hr->user_name) + 1;
61+
pwd->pw_gecos = stpcpy(pwd->pw_passwd, PASSWORD_SEE_SHADOW) + 1;
6062
pwd->pw_dir = stpcpy(pwd->pw_gecos, rn) + 1;
6163
pwd->pw_shell = stpcpy(pwd->pw_dir, hd) + 1;
6264
strcpy(pwd->pw_shell, shell);

0 commit comments

Comments
 (0)