Skip to content

Commit e359bc2

Browse files
Alexey Izbyshevserhiy-storchaka
authored andcommitted
bpo-35161: Fix stack-use-after-scope in grp.getgr{nam,gid} and pwd.getpw{nam,uid}. (GH-10319)
Reported by ASAN.
1 parent 52465e1 commit e359bc2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Modules/grpmodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
124124
Py_DECREF(py_int_id);
125125
}
126126
#ifdef HAVE_GETGRGID_R
127-
Py_BEGIN_ALLOW_THREADS
128127
int status;
129128
Py_ssize_t bufsize;
129+
/* Note: 'grp' will be used via pointer 'p' on getgrgid_r success. */
130130
struct group grp;
131131

132+
Py_BEGIN_ALLOW_THREADS
132133
bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
133134
if (bufsize == -1) {
134135
bufsize = DEFAULT_BUFFER_SIZE;
@@ -204,11 +205,12 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
204205
if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
205206
goto out;
206207
#ifdef HAVE_GETGRNAM_R
207-
Py_BEGIN_ALLOW_THREADS
208208
int status;
209209
Py_ssize_t bufsize;
210+
/* Note: 'grp' will be used via pointer 'p' on getgrnam_r success. */
210211
struct group grp;
211212

213+
Py_BEGIN_ALLOW_THREADS
212214
bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
213215
if (bufsize == -1) {
214216
bufsize = DEFAULT_BUFFER_SIZE;

Modules/pwdmodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,12 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj)
131131
return NULL;
132132
}
133133
#ifdef HAVE_GETPWUID_R
134-
Py_BEGIN_ALLOW_THREADS
135134
int status;
136135
Py_ssize_t bufsize;
136+
/* Note: 'pwd' will be used via pointer 'p' on getpwuid_r success. */
137137
struct passwd pwd;
138138

139+
Py_BEGIN_ALLOW_THREADS
139140
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
140141
if (bufsize == -1) {
141142
bufsize = DEFAULT_BUFFER_SIZE;
@@ -212,11 +213,12 @@ pwd_getpwnam_impl(PyObject *module, PyObject *name)
212213
if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
213214
goto out;
214215
#ifdef HAVE_GETPWNAM_R
215-
Py_BEGIN_ALLOW_THREADS
216216
int status;
217217
Py_ssize_t bufsize;
218+
/* Note: 'pwd' will be used via pointer 'p' on getpwnam_r success. */
218219
struct passwd pwd;
219220

221+
Py_BEGIN_ALLOW_THREADS
220222
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
221223
if (bufsize == -1) {
222224
bufsize = DEFAULT_BUFFER_SIZE;

0 commit comments

Comments
 (0)