Skip to content

Commit

Permalink
gh-126316: Make grp.getgrall() thread-safe: add a mutex (#127055)
Browse files Browse the repository at this point in the history
grpmodule.c is no longer built with the limited C API, since PyMutex
is excluded from the limited C API.
  • Loading branch information
vstinner authored Nov 21, 2024
1 parent 60ec854 commit 3c2bd66
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 22 deletions.
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(hi)
STRUCT_FOR_ID(hook)
STRUCT_FOR_ID(hour)
STRUCT_FOR_ID(id)
STRUCT_FOR_ID(ident)
STRUCT_FOR_ID(identity_hint)
STRUCT_FOR_ID(ignore)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`grp`: Make :func:`grp.getgrall` thread-safe by adding a mutex. Patch
by Victor Stinner.
88 changes: 77 additions & 11 deletions Modules/clinic/grpmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions Modules/grpmodule.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* UNIX group file access module */

// Need limited C API version 3.13 for PyMem_RawRealloc()
#include "pyconfig.h" // Py_GIL_DISABLED
#ifndef Py_GIL_DISABLED
#define Py_LIMITED_API 0x030d0000
// Argument Clinic uses the internal C API
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"
Expand Down Expand Up @@ -281,23 +280,33 @@ static PyObject *
grp_getgrall_impl(PyObject *module)
/*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/
{
PyObject *d;
struct group *p;

if ((d = PyList_New(0)) == NULL)
PyObject *d = PyList_New(0);
if (d == NULL) {
return NULL;
}

static PyMutex getgrall_mutex = {0};
PyMutex_Lock(&getgrall_mutex);
setgrent();

struct group *p;
while ((p = getgrent()) != NULL) {
// gh-126316: Don't release the mutex around mkgrent() since
// setgrent()/endgrent() are not reentrant / thread-safe. A deadlock
// is unlikely since mkgrent() should not be able to call arbitrary
// Python code.
PyObject *v = mkgrent(module, p);
if (v == NULL || PyList_Append(d, v) != 0) {
Py_XDECREF(v);
Py_DECREF(d);
endgrent();
return NULL;
Py_CLEAR(d);
goto done;
}
Py_DECREF(v);
}

done:
endgrent();
PyMutex_Unlock(&getgrall_mutex);
return d;
}

Expand Down
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ Modules/expat/xmlrole.c - declClose -
Modules/expat/xmlrole.c - error -

## other
Modules/grpmodule.c grp_getgrall_impl getgrall_mutex -
Modules/_io/_iomodule.c - _PyIO_Module -
Modules/_sqlite/module.c - _sqlite3module -
Modules/clinic/md5module.c.h _md5_md5 _keywords -
Expand Down

0 comments on commit 3c2bd66

Please sign in to comment.