Skip to content

Commit 33e83df

Browse files
duanegcolesbury
authored andcommitted
[3.13] pythongh-127081: use getlogin_r if available (pythongh-132751)
The `getlogin` function is not thread-safe: replace with `getlogin_r` where available. (cherry picked from commit 1ffe913) Co-authored-by: Duane Griffin <[email protected]>
1 parent aa9eb5f commit 33e83df

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix libc thread safety issues with :mod:`os` by replacing ``getlogin`` with
2+
``getlogin_r`` re-entrant version.

Modules/posixmodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9464,6 +9464,24 @@ os_getlogin_impl(PyObject *module)
94649464
}
94659465
else
94669466
result = PyErr_SetFromWindowsErr(GetLastError());
9467+
#elif defined (HAVE_GETLOGIN_R)
9468+
# if defined (HAVE_MAXLOGNAME)
9469+
char name[MAXLOGNAME + 1];
9470+
# elif defined (HAVE_UT_NAMESIZE)
9471+
char name[UT_NAMESIZE + 1];
9472+
# else
9473+
char name[256];
9474+
# endif
9475+
int err = getlogin_r(name, sizeof(name));
9476+
if (err) {
9477+
int old_errno = errno;
9478+
errno = -err;
9479+
posix_error();
9480+
errno = old_errno;
9481+
}
9482+
else {
9483+
result = PyUnicode_DecodeFSDefault(name);
9484+
}
94679485
#else
94689486
char *name;
94699487
int old_errno = errno;

configure

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5116,7 +5116,7 @@ AC_CHECK_FUNCS([ \
51165116
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
51175117
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
51185118
gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \
5119-
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin \
5119+
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin getlogin_r \
51205120
getpeername getpgid getpid getppid getpriority _getpty \
51215121
getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \
51225122
getspnam getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \
@@ -5400,6 +5400,18 @@ PY_CHECK_FUNC([setgroups], [
54005400
#endif
54015401
])
54025402

5403+
AC_CHECK_DECL([MAXLOGNAME],
5404+
[AC_DEFINE([HAVE_MAXLOGNAME], [1],
5405+
[Define if you have the 'MAXLOGNAME' constant.])],
5406+
[],
5407+
[@%:@include <sys/params.h>])
5408+
5409+
AC_CHECK_DECLS([UT_NAMESIZE],
5410+
[AC_DEFINE([HAVE_UT_NAMESIZE], [1],
5411+
[Define if you have the 'HAVE_UT_NAMESIZE' constant.])],
5412+
[],
5413+
[@%:@include <utmp.h>])
5414+
54035415
# check for openpty, login_tty, and forkpty
54045416

54055417
AC_CHECK_FUNCS([openpty], [],

pyconfig.h.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@
258258
*/
259259
#undef HAVE_DECL_TZNAME
260260

261+
/* Define to 1 if you have the declaration of 'UT_NAMESIZE', and to 0 if you
262+
don't. */
263+
#undef HAVE_DECL_UT_NAMESIZE
264+
261265
/* Define to 1 if you have the device macros. */
262266
#undef HAVE_DEVICE_MACROS
263267

@@ -521,6 +525,9 @@
521525
/* Define to 1 if you have the `getlogin' function. */
522526
#undef HAVE_GETLOGIN
523527

528+
/* Define to 1 if you have the 'getlogin_r' function. */
529+
#undef HAVE_GETLOGIN_R
530+
524531
/* Define to 1 if you have the `getnameinfo' function. */
525532
#undef HAVE_GETNAMEINFO
526533

@@ -786,6 +793,9 @@
786793
/* Define this if you have the makedev macro. */
787794
#undef HAVE_MAKEDEV
788795

796+
/* Define if you have the 'MAXLOGNAME' constant. */
797+
#undef HAVE_MAXLOGNAME
798+
789799
/* Define to 1 if you have the `mbrtowc' function. */
790800
#undef HAVE_MBRTOWC
791801

@@ -1539,6 +1549,9 @@
15391549
/* Define to 1 if you have the <utmp.h> header file. */
15401550
#undef HAVE_UTMP_H
15411551

1552+
/* Define if you have the 'HAVE_UT_NAMESIZE' constant. */
1553+
#undef HAVE_UT_NAMESIZE
1554+
15421555
/* Define to 1 if you have the `uuid_create' function. */
15431556
#undef HAVE_UUID_CREATE
15441557

0 commit comments

Comments
 (0)