Skip to content

Commit 387512c

Browse files
authored
bpo-28503: Use crypt_r() when available instead of crypt() (GH-11373)
Use crypt_r() when available instead of crypt() in the crypt module. As a nice side effect: This also avoids a memory sanitizer flake as clang msan doesn't know about crypt's internal libc allocated buffer.
1 parent 1b29c03 commit 387512c

File tree

6 files changed

+185
-1
lines changed

6 files changed

+185
-1
lines changed

Include/Python.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@
3636
#include <unistd.h>
3737
#endif
3838
#ifdef HAVE_CRYPT_H
39+
#if defined(HAVE_CRYPT_R) && !defined(_GNU_SOURCE)
40+
/* Required for glibc to expose the crypt_r() function prototype. */
41+
# define _GNU_SOURCE
42+
# define _Py_GNU_SOURCE_FOR_CRYPT
43+
#endif
3944
#include <crypt.h>
45+
#ifdef _Py_GNU_SOURCE_FOR_CRYPT
46+
/* Don't leak the _GNU_SOURCE define to other headers. */
47+
# undef _GNU_SOURCE
48+
# undef _Py_GNU_SOURCE_FOR_CRYPT
49+
#endif
4050
#endif
4151

4252
/* For size_t? */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The `crypt` module now internally uses the `crypt_r()` library function
2+
instead of `crypt()` when available.

Modules/_cryptmodule.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ static PyObject *
3434
crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
3535
/*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
3636
{
37-
return Py_BuildValue("s", crypt(word, salt));
37+
char *crypt_result;
38+
#ifdef HAVE_CRYPT_R
39+
struct crypt_data data;
40+
memset(&data, 0, sizeof(data));
41+
crypt_result = crypt_r(word, salt, &data);
42+
#else
43+
crypt_result = crypt(word, salt);
44+
#endif
45+
return Py_BuildValue("s", crypt_result);
3846
}
3947

4048

configure

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12677,6 +12677,150 @@ fi
1267712677
done
1267812678

1267912679

12680+
# We search for both crypt and crypt_r as one or the other may be defined
12681+
# This gets us our -lcrypt in LIBS when required on the target platform.
12682+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing crypt" >&5
12683+
$as_echo_n "checking for library containing crypt... " >&6; }
12684+
if ${ac_cv_search_crypt+:} false; then :
12685+
$as_echo_n "(cached) " >&6
12686+
else
12687+
ac_func_search_save_LIBS=$LIBS
12688+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
12689+
/* end confdefs.h. */
12690+
12691+
/* Override any GCC internal prototype to avoid an error.
12692+
Use char because int might match the return type of a GCC
12693+
builtin and then its argument prototype would still apply. */
12694+
#ifdef __cplusplus
12695+
extern "C"
12696+
#endif
12697+
char crypt ();
12698+
int
12699+
main ()
12700+
{
12701+
return crypt ();
12702+
;
12703+
return 0;
12704+
}
12705+
_ACEOF
12706+
for ac_lib in '' crypt; do
12707+
if test -z "$ac_lib"; then
12708+
ac_res="none required"
12709+
else
12710+
ac_res=-l$ac_lib
12711+
LIBS="-l$ac_lib $ac_func_search_save_LIBS"
12712+
fi
12713+
if ac_fn_c_try_link "$LINENO"; then :
12714+
ac_cv_search_crypt=$ac_res
12715+
fi
12716+
rm -f core conftest.err conftest.$ac_objext \
12717+
conftest$ac_exeext
12718+
if ${ac_cv_search_crypt+:} false; then :
12719+
break
12720+
fi
12721+
done
12722+
if ${ac_cv_search_crypt+:} false; then :
12723+
12724+
else
12725+
ac_cv_search_crypt=no
12726+
fi
12727+
rm conftest.$ac_ext
12728+
LIBS=$ac_func_search_save_LIBS
12729+
fi
12730+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_crypt" >&5
12731+
$as_echo "$ac_cv_search_crypt" >&6; }
12732+
ac_res=$ac_cv_search_crypt
12733+
if test "$ac_res" != no; then :
12734+
test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
12735+
12736+
fi
12737+
12738+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing crypt_r" >&5
12739+
$as_echo_n "checking for library containing crypt_r... " >&6; }
12740+
if ${ac_cv_search_crypt_r+:} false; then :
12741+
$as_echo_n "(cached) " >&6
12742+
else
12743+
ac_func_search_save_LIBS=$LIBS
12744+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
12745+
/* end confdefs.h. */
12746+
12747+
/* Override any GCC internal prototype to avoid an error.
12748+
Use char because int might match the return type of a GCC
12749+
builtin and then its argument prototype would still apply. */
12750+
#ifdef __cplusplus
12751+
extern "C"
12752+
#endif
12753+
char crypt_r ();
12754+
int
12755+
main ()
12756+
{
12757+
return crypt_r ();
12758+
;
12759+
return 0;
12760+
}
12761+
_ACEOF
12762+
for ac_lib in '' crypt; do
12763+
if test -z "$ac_lib"; then
12764+
ac_res="none required"
12765+
else
12766+
ac_res=-l$ac_lib
12767+
LIBS="-l$ac_lib $ac_func_search_save_LIBS"
12768+
fi
12769+
if ac_fn_c_try_link "$LINENO"; then :
12770+
ac_cv_search_crypt_r=$ac_res
12771+
fi
12772+
rm -f core conftest.err conftest.$ac_objext \
12773+
conftest$ac_exeext
12774+
if ${ac_cv_search_crypt_r+:} false; then :
12775+
break
12776+
fi
12777+
done
12778+
if ${ac_cv_search_crypt_r+:} false; then :
12779+
12780+
else
12781+
ac_cv_search_crypt_r=no
12782+
fi
12783+
rm conftest.$ac_ext
12784+
LIBS=$ac_func_search_save_LIBS
12785+
fi
12786+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_crypt_r" >&5
12787+
$as_echo "$ac_cv_search_crypt_r" >&6; }
12788+
ac_res=$ac_cv_search_crypt_r
12789+
if test "$ac_res" != no; then :
12790+
test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
12791+
12792+
fi
12793+
12794+
12795+
ac_fn_c_check_func "$LINENO" "crypt_r" "ac_cv_func_crypt_r"
12796+
if test "x$ac_cv_func_crypt_r" = xyes; then :
12797+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
12798+
/* end confdefs.h. */
12799+
12800+
#define _GNU_SOURCE /* Required for crypt_r()'s prototype in glibc. */
12801+
#include <crypt.h>
12802+
12803+
int
12804+
main ()
12805+
{
12806+
12807+
struct crypt_data d;
12808+
char *r = crypt_r("", "", &d);
12809+
12810+
;
12811+
return 0;
12812+
}
12813+
_ACEOF
12814+
if ac_fn_c_try_compile "$LINENO"; then :
12815+
12816+
$as_echo "#define HAVE_CRYPT_R 1" >>confdefs.h
12817+
12818+
fi
12819+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
12820+
12821+
fi
12822+
12823+
1268012824
for ac_func in clock_gettime
1268112825
do :
1268212826
ac_fn_c_check_func "$LINENO" "clock_gettime" "ac_cv_func_clock_gettime"

configure.ac

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,6 +3818,23 @@ AC_CHECK_FUNCS(gettimeofday,
38183818
])
38193819
)
38203820

3821+
# We search for both crypt and crypt_r as one or the other may be defined
3822+
# This gets us our -lcrypt in LIBS when required on the target platform.
3823+
AC_SEARCH_LIBS(crypt, crypt)
3824+
AC_SEARCH_LIBS(crypt_r, crypt)
3825+
3826+
AC_CHECK_FUNC(crypt_r,
3827+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
3828+
#define _GNU_SOURCE /* Required for crypt_r()'s prototype in glibc. */
3829+
#include <crypt.h>
3830+
]], [[
3831+
struct crypt_data d;
3832+
char *r = crypt_r("", "", &d);
3833+
]])],
3834+
[AC_DEFINE(HAVE_CRYPT_R, 1, [Define if you have the crypt_r() function.])],
3835+
[])
3836+
)
3837+
38213838
AC_CHECK_FUNCS(clock_gettime, [], [
38223839
AC_CHECK_LIB(rt, clock_gettime, [
38233840
LIBS="$LIBS -lrt"

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
/* Define to 1 if you have the <crypt.h> header file. */
148148
#undef HAVE_CRYPT_H
149149

150+
/* Define if you have the crypt_r() function. */
151+
#undef HAVE_CRYPT_R
152+
150153
/* Define to 1 if you have the `ctermid' function. */
151154
#undef HAVE_CTERMID
152155

0 commit comments

Comments
 (0)