Skip to content

Commit 1ffe913

Browse files
authored
gh-127081: use getlogin_r if available (gh-132751)
The `getlogin` function is not thread-safe: replace with `getlogin_r` where available.
1 parent 54ca559 commit 1ffe913

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
@@ -9562,6 +9562,24 @@ os_getlogin_impl(PyObject *module)
95629562
}
95639563
else
95649564
result = PyErr_SetFromWindowsErr(GetLastError());
9565+
#elif defined (HAVE_GETLOGIN_R)
9566+
# if defined (HAVE_MAXLOGNAME)
9567+
char name[MAXLOGNAME + 1];
9568+
# elif defined (HAVE_UT_NAMESIZE)
9569+
char name[UT_NAMESIZE + 1];
9570+
# else
9571+
char name[256];
9572+
# endif
9573+
int err = getlogin_r(name, sizeof(name));
9574+
if (err) {
9575+
int old_errno = errno;
9576+
errno = -err;
9577+
posix_error();
9578+
errno = old_errno;
9579+
}
9580+
else {
9581+
result = PyUnicode_DecodeFSDefault(name);
9582+
}
95659583
#else
95669584
char *name;
95679585
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
@@ -5219,7 +5219,7 @@ AC_CHECK_FUNCS([ \
52195219
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
52205220
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
52215221
gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \
5222-
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin \
5222+
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin getlogin_r \
52235223
getpeername getpgid getpid getppid getpriority _getpty \
52245224
getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \
52255225
getspnam getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \
@@ -5538,6 +5538,18 @@ PY_CHECK_FUNC([setgroups], [
55385538
#endif
55395539
])
55405540

5541+
AC_CHECK_DECL([MAXLOGNAME],
5542+
[AC_DEFINE([HAVE_MAXLOGNAME], [1],
5543+
[Define if you have the 'MAXLOGNAME' constant.])],
5544+
[],
5545+
[@%:@include <sys/params.h>])
5546+
5547+
AC_CHECK_DECLS([UT_NAMESIZE],
5548+
[AC_DEFINE([HAVE_UT_NAMESIZE], [1],
5549+
[Define if you have the 'HAVE_UT_NAMESIZE' constant.])],
5550+
[],
5551+
[@%:@include <utmp.h>])
5552+
55415553
# check for openpty, login_tty, and forkpty
55425554

55435555
AC_CHECK_FUNCS([openpty], [],

pyconfig.h.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@
267267
*/
268268
#undef HAVE_DECL_TZNAME
269269

270+
/* Define to 1 if you have the declaration of 'UT_NAMESIZE', and to 0 if you
271+
don't. */
272+
#undef HAVE_DECL_UT_NAMESIZE
273+
270274
/* Define to 1 if you have the device macros. */
271275
#undef HAVE_DEVICE_MACROS
272276

@@ -539,6 +543,9 @@
539543
/* Define to 1 if you have the 'getlogin' function. */
540544
#undef HAVE_GETLOGIN
541545

546+
/* Define to 1 if you have the 'getlogin_r' function. */
547+
#undef HAVE_GETLOGIN_R
548+
542549
/* Define to 1 if you have the 'getnameinfo' function. */
543550
#undef HAVE_GETNAMEINFO
544551

@@ -807,6 +814,9 @@
807814
/* Define this if you have the makedev macro. */
808815
#undef HAVE_MAKEDEV
809816

817+
/* Define if you have the 'MAXLOGNAME' constant. */
818+
#undef HAVE_MAXLOGNAME
819+
810820
/* Define to 1 if you have the 'mbrtowc' function. */
811821
#undef HAVE_MBRTOWC
812822

@@ -1575,6 +1585,9 @@
15751585
/* Define to 1 if you have the <utmp.h> header file. */
15761586
#undef HAVE_UTMP_H
15771587

1588+
/* Define if you have the 'HAVE_UT_NAMESIZE' constant. */
1589+
#undef HAVE_UT_NAMESIZE
1590+
15781591
/* Define to 1 if you have the 'uuid_create' function. */
15791592
#undef HAVE_UUID_CREATE
15801593

0 commit comments

Comments
 (0)