Skip to content

gh-127081: lock non-re-entrant *pwent calls #132748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix libc thread safety issues with :mod:`pwd` by locking access to
``getpwall``.
25 changes: 20 additions & 5 deletions Modules/pwdmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,33 @@ pwd_getpwall_impl(PyObject *module)
struct passwd *p;
if ((d = PyList_New(0)) == NULL)
return NULL;

#ifdef Py_GIL_DISABLED
static PyMutex getpwall_mutex = {0};
PyMutex_Lock(&getpwall_mutex);
#endif
int failure = 0;
PyObject *v = NULL;
setpwent();
while ((p = getpwent()) != NULL) {
PyObject *v = mkpwent(module, p);
v = mkpwent(module, p);
if (v == NULL || PyList_Append(d, v) != 0) {
Py_XDECREF(v);
Py_DECREF(d);
endpwent();
return NULL;
/* NOTE: cannot dec-ref here, while holding the mutex. */
failure = 1;
goto done;
}
Py_DECREF(v);
}

done:
endpwent();
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&getpwall_mutex);
#endif
if (failure) {
Py_XDECREF(v);
Py_CLEAR(d);
}
return d;
}
#endif
Expand Down
Loading