Skip to content

Commit 6c85770

Browse files
committed
src/sss_client/common.c: Use getpwnam_r to avoid clobbering struct passwd
If something else uses PAM (like openrc, see OpenRC/openrc#984) and getpwnam, and calls something like pam_open_session, sssd's call to getpwnam in init_sssd_ids clobbers the cached value by the other program. Signed-off-by: Christopher Byrne <salah.coronya@gmail.com>
1 parent 64ee91f commit 6c85770

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/sss_client/common.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,48 @@ static void init_sssd_ids(void)
148148
/* 'libnss_sss' doesn't resolve SSSD_USER,
149149
* so no need to set '_SSS_LOOPS'
150150
*/
151-
struct passwd *pwd = getpwnam(SSSD_USER);
152-
if (pwd != NULL) {
153-
sss_sssd_uid = pwd->pw_uid;
154-
sss_sssd_gid = pwd->pw_gid;
151+
struct passwd pwd, *result;
152+
ssize_t bufsize;
153+
char *buf, *newbuf;
154+
int error;
155+
156+
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
157+
158+
if (bufsize == -1) {
159+
bufsize = 16384;
160+
}
161+
162+
buf = malloc(bufsize);
163+
164+
if (buf == NULL) {
165+
return;
155166
}
167+
168+
do {
169+
error = getpwnam_r(SSSD_USER, &pwd, buf, bufsize, &result);
170+
if (result == NULL) {
171+
if (error == ERANGE) {
172+
bufsize += 4096;
173+
newbuf = realloc(buf, bufsize);
174+
175+
if (newbuf == NULL) {
176+
break;
177+
}
178+
179+
if (bufsize >= 65536) {
180+
break;
181+
}
182+
183+
buf = newbuf;
184+
continue;
185+
}
186+
} else {
187+
sss_sssd_uid = result->pw_uid;
188+
sss_sssd_gid = result->pw_gid;
189+
}
190+
} while (result == NULL && error == ERANGE);
191+
192+
free(buf);
156193
}
157194
#endif
158195
#endif /* SSSD_NON_ROOT_USER */

0 commit comments

Comments
 (0)