Skip to content

[3.13] gh-126316: Make grp.getgrall() thread-safe: add a mutex (#127055) #127104

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 2 commits into from
Nov 26, 2024
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 @@
:mod:`grp`: Make :func:`grp.getgrall` thread-safe by adding a mutex. Patch
by Victor Stinner.
77 changes: 42 additions & 35 deletions Modules/clinic/grpmodule.c.h

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

61 changes: 37 additions & 24 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 @@ -110,20 +109,15 @@ mkgrent(PyObject *module, struct group *p)
return v;
}

/*[clinic input]
grp.getgrgid

id: object

Return the group database entry for the given numeric group ID.

If id is not valid, raise KeyError.
[clinic start generated code]*/

static PyObject *
grp_getgrgid_impl(PyObject *module, PyObject *id)
/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
grp_getgrgid(PyObject *module, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = {"id", NULL};
PyObject *id;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &id)) {
return NULL;
}

PyObject *retval = NULL;
int nomem = 0;
char *buf = NULL, *buf2 = NULL;
Expand Down Expand Up @@ -190,6 +184,15 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
return retval;
}

PyDoc_STRVAR(grp_getgrgid__doc__,
"getgrgid($module, /, id)\n"
"--\n"
"\n"
"Return the group database entry for the given numeric group ID.\n"
"\n"
"If id is not valid, raise KeyError.");


/*[clinic input]
grp.getgrnam

Expand Down Expand Up @@ -281,28 +284,38 @@ 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;
}

static PyMethodDef grp_methods[] = {
GRP_GETGRGID_METHODDEF
{"getgrgid", _PyCFunction_CAST(grp_getgrgid), METH_VARARGS|METH_KEYWORDS, grp_getgrgid__doc__},
GRP_GETGRNAM_METHODDEF
GRP_GETGRALL_METHODDEF
{NULL, NULL}
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 @@ -742,6 +742,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
Loading