Skip to content

Commit

Permalink
pythongh-126316: Use critical sections in grp module
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Nov 6, 2024
1 parent 6431f37 commit ede8bc4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`grp`: Fix segfault on concurrent calls by using critical sections.
Patch by Victor Stinner.
16 changes: 14 additions & 2 deletions Modules/clinic/grpmodule.c.h

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

21 changes: 16 additions & 5 deletions Modules/grpmodule.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/* UNIX group file access module */
/* UNIX group file access module
*
* Use @critical_section since getgrgid(), getgrnam() and getgrall() are not
* thread safe (gh-126316).
*/

// 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
# define Py_LIMITED_API 0x030d0000
#else
# ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
# endif
#endif

#include "Python.h"
Expand Down Expand Up @@ -111,6 +119,7 @@ mkgrent(PyObject *module, struct group *p)
}

/*[clinic input]
@critical_section
grp.getgrgid
id: object
Expand All @@ -122,7 +131,7 @@ If id is not valid, raise KeyError.

static PyObject *
grp_getgrgid_impl(PyObject *module, PyObject *id)
/*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
/*[clinic end generated code: output=30797c289504a1ba input=f5bf2ce5d1b3d6bc]*/
{
PyObject *retval = NULL;
int nomem = 0;
Expand Down Expand Up @@ -191,6 +200,7 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
}

/*[clinic input]
@critical_section
grp.getgrnam
name: unicode
Expand All @@ -202,7 +212,7 @@ If name is not valid, raise KeyError.

static PyObject *
grp_getgrnam_impl(PyObject *module, PyObject *name)
/*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
/*[clinic end generated code: output=67905086f403c21c input=da37ffb830c57159]*/
{
char *buf = NULL, *buf2 = NULL, *name_chars;
int nomem = 0;
Expand Down Expand Up @@ -269,6 +279,7 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
}

/*[clinic input]
@critical_section
grp.getgrall
Return a list of all available group entries, in arbitrary order.
Expand All @@ -279,7 +290,7 @@ to use YP/NIS and may not be accessible via getgrnam or getgrgid.

static PyObject *
grp_getgrall_impl(PyObject *module)
/*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/
/*[clinic end generated code: output=585dad35e2e763d7 input=b257403558f07a1d]*/
{
PyObject *d;
struct group *p;
Expand Down

0 comments on commit ede8bc4

Please sign in to comment.