From 05388f748d650208f25f135d56a296afed0bea28 Mon Sep 17 00:00:00 2001 From: Jaroslav Jindrak Date: Tue, 3 Aug 2021 20:03:46 +0200 Subject: [PATCH] passwd: handle NULL pw_passwd when printing password status When the -S and -a options are used for passwd to list the status of all passwords, there is a chance the pw_passwd field of struct passwd will be NULL. This can be due to 'files compat' being set for passwd in /etc/nsswitch.conf and the usage of some features not available in the 'files' mode (e.g. a plus sign at the start of a line). Example: germ161:~ # grep passwd /etc/nsswitch.conf passwd: files compat germ161:~ # rpm -qa shadow shadow-4.2.1-34.20.x86_64 germ161:~ # grep passwd /etc/nsswitch.conf passwd: files compat germ161:~ # grep + /etc/passwd +@nisgroup germ161:~ # passwd -S -a > /dev/null Segmentation fault (core dumped) With this commit: germ161:~ # passwd -S -a > /dev/null passwd: malformed password data obtained for user +@nisgroup --- src/passwd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/passwd.c b/src/passwd.c index 9d7df331..9d33d25e 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -490,9 +490,12 @@ static void print_status (const struct passwd *pw) ((long long)sp->sp_max * SCALE) / DAY, ((long long)sp->sp_warn * SCALE) / DAY, ((long long)sp->sp_inact * SCALE) / DAY); - } else { + } else if (NULL != pw->pw_passwd) { (void) printf ("%s %s\n", pw->pw_name, pw_status (pw->pw_passwd)); + } else { + (void) fprintf(stderr, _("%s: malformed password data obtained for user %s\n"), + Prog, pw->pw_name); } }