Skip to content

Commit dc6970c

Browse files
salahcoronyaalexey-tikhonov
authored andcommitted
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> Reviewed-by: Alexey Tikhonov <atikhono@redhat.com> Reviewed-by: Tomáš Halman <thalman@redhat.com>
1 parent 03b7441 commit dc6970c

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

src/sss_client/common.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,54 @@ 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;
152+
struct passwd *result = NULL;
153+
long sc_bufsize;
154+
size_t bufsize;
155+
char *buf;
156+
char *newbuf;
157+
int error;
158+
159+
sc_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
160+
161+
if (sc_bufsize > 0)
162+
bufsize = (size_t)sc_bufsize;
163+
else {
164+
bufsize = 16384;
165+
}
166+
167+
buf = malloc(bufsize);
168+
169+
if (buf == NULL) {
170+
return;
155171
}
172+
173+
do {
174+
error = getpwnam_r(SSSD_USER, &pwd, buf, bufsize, &result);
175+
if (result == NULL) {
176+
if (error == ERANGE) {
177+
bufsize += 4096;
178+
179+
if (bufsize >= 65536) {
180+
break;
181+
}
182+
183+
newbuf = realloc(buf, bufsize);
184+
185+
if (newbuf == NULL) {
186+
break;
187+
}
188+
189+
buf = newbuf;
190+
continue;
191+
}
192+
} else {
193+
sss_sssd_uid = result->pw_uid;
194+
sss_sssd_gid = result->pw_gid;
195+
}
196+
} while (result == NULL && error == ERANGE);
197+
198+
free(buf);
156199
}
157200
#endif
158201
#endif /* SSSD_NON_ROOT_USER */

0 commit comments

Comments
 (0)