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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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``.
35 changes: 30 additions & 5 deletions Modules/pwdmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "Python.h"
#include "posixmodule.h"
#include "listobject.h" // PyList_Size/PyList_GetItem

#include <errno.h> // ERANGE
#include <pwd.h> // getpwuid()
Expand Down Expand Up @@ -301,18 +302,42 @@ 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 *orphan = NULL;
setpwent();
while ((p = getpwent()) != NULL) {
/* NOTE: Ref counts are not decremented here, as we cannot allow
* re-entrancy while holding the mutex. */
PyObject *v = mkpwent(module, p);
if (v == NULL || PyList_Append(d, v) != 0) {
Py_XDECREF(v);
Py_DECREF(d);
endpwent();
return NULL;
orphan = v;
failure = 1;
goto done;
}
Py_DECREF(v);
}

done:
endpwent();
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&getpwall_mutex);
#endif
/* Deferred decref on entries created above and added to the list */
Py_ssize_t n = PyList_Size(d);
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *entry = PyList_GetItem(d, i);
Py_DECREF(entry);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Py_ssize_t n = PyList_Size(d);
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *entry = PyList_GetItem(d, i);
Py_DECREF(entry);
}
Py_ssize_t n = PyList_GET_SIZE(d);
while (--n >= 0) {
Py_DECREF(PyList_GET_ITEM(d, n));
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change broke GIL builds, as they use the limited API only, which does not include those functions. Unfortunately I was using a free-threading build, so didn't notice initially. I'll switch back to using the ones available in the limited API, but keep the better loop construct.

if (failure) {
/* If there was a failure we might have created an entry but not added
* it: dec-ref that, if it exists, before clearing the list. */
Py_XDECREF(orphan);
Py_CLEAR(d);
}
return d;
}
#endif
Expand Down
Loading