Skip to content

gh-127081: use getlogin_r if available #132751

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 3 commits into from
Jun 3, 2025
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 @@
Fix libc thread safety issues with :mod:`os` by replacing ``getlogin`` with
``getlogin_r`` re-entrant version.
18 changes: 18 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9538,6 +9538,24 @@ os_getlogin_impl(PyObject *module)
}
else
result = PyErr_SetFromWindowsErr(GetLastError());
#elif defined (HAVE_GETLOGIN_R)
# if defined (HAVE_MAXLOGNAME)
char name[MAXLOGNAME + 1];
# elif defined (HAVE_UT_NAMESIZE)
char name[UT_NAMESIZE + 1];
# else
char name[256];
# endif
int err = getlogin_r(name, sizeof(name));
if (err) {
int old_errno = errno;
errno = -err;
posix_error();
errno = old_errno;
}
else {
result = PyUnicode_DecodeFSDefault(name);
}
#else
char *name;
int old_errno = errno;
Expand Down
33 changes: 33 additions & 0 deletions configure

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

14 changes: 13 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5137,7 +5137,7 @@ AC_CHECK_FUNCS([ \
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin \
getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin getlogin_r \
getpeername getpgid getpid getppid getpriority _getpty \
getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \
getspnam getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \
Expand Down Expand Up @@ -5427,6 +5427,18 @@ PY_CHECK_FUNC([setgroups], [
#endif
])

AC_CHECK_DECL([MAXLOGNAME],
[AC_DEFINE([HAVE_MAXLOGNAME], [1],
[Define if you have the 'MAXLOGNAME' constant.])],
[],
[@%:@include <sys/params.h>])

AC_CHECK_DECLS([UT_NAMESIZE],
[AC_DEFINE([HAVE_UT_NAMESIZE], [1],
[Define if you have the 'HAVE_UT_NAMESIZE' constant.])],
[],
[@%:@include <utmp.h>])

# check for openpty, login_tty, and forkpty

AC_CHECK_FUNCS([openpty], [],
Expand Down
13 changes: 13 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@
*/
#undef HAVE_DECL_TZNAME

/* Define to 1 if you have the declaration of 'UT_NAMESIZE', and to 0 if you
don't. */
#undef HAVE_DECL_UT_NAMESIZE

/* Define to 1 if you have the device macros. */
#undef HAVE_DEVICE_MACROS

Expand Down Expand Up @@ -530,6 +534,9 @@
/* Define to 1 if you have the 'getlogin' function. */
#undef HAVE_GETLOGIN

/* Define to 1 if you have the 'getlogin_r' function. */
#undef HAVE_GETLOGIN_R

/* Define to 1 if you have the 'getnameinfo' function. */
#undef HAVE_GETNAMEINFO

Expand Down Expand Up @@ -795,6 +802,9 @@
/* Define this if you have the makedev macro. */
#undef HAVE_MAKEDEV

/* Define if you have the 'MAXLOGNAME' constant. */
#undef HAVE_MAXLOGNAME

/* Define to 1 if you have the 'mbrtowc' function. */
#undef HAVE_MBRTOWC

Expand Down Expand Up @@ -1563,6 +1573,9 @@
/* Define to 1 if you have the <utmp.h> header file. */
#undef HAVE_UTMP_H

/* Define if you have the 'HAVE_UT_NAMESIZE' constant. */
#undef HAVE_UT_NAMESIZE

/* Define to 1 if you have the 'uuid_create' function. */
#undef HAVE_UUID_CREATE

Expand Down
Loading