From 72ea6912f7408eee9172482fb2a21fe35a21a0e8 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Thu, 7 Dec 2006 01:41:18 +0000 Subject: [PATCH] Fixed bug #39754 (Some POSIX extension functions not thread safe). --- NEWS | 2 ++ ext/posix/config.m4 | 2 +- ext/posix/posix.c | 76 ++++++++++++++++++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index 2010cc359e..6afddf16bc 100644 --- a/NEWS +++ b/NEWS @@ -49,6 +49,8 @@ PHP NEWS - Fixed FastCGI impersonation for persistent connections on Windows. (Dmitry) - Fixed wrong signature initialization in imagepng (Takeshi Abe) - Added optimization for imageline with horizontal and vertial lines (Pierre) +- Fixed bug #39754 (Some POSIX extension functions not thread safe). + (Ilia, wharmby at uk dot ibm dot com) - Fixed bug #39724 (Broken build due to spl/filter usage of pcre extension). (Tony, Ilia) - Fixed bug #39718 (possible crash if assert.callback is set in ini). (Ilia) diff --git a/ext/posix/config.m4 b/ext/posix/config.m4 index 0075e32599..9038330bb1 100644 --- a/ext/posix/config.m4 +++ b/ext/posix/config.m4 @@ -11,5 +11,5 @@ if test "$PHP_POSIX" = "yes"; then AC_CHECK_HEADERS(sys/mkdev.h) - AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo mknod getrlimit getlogin getgroups makedev initgroups getpwuid_r getgrgid_r) + AC_CHECK_FUNCS(seteuid setegid setsid getsid setpgid getpgid ctermid mkfifo mknod getrlimit getlogin getgroups makedev initgroups getpwuid_r getgrgid_r ttyname_r) fi diff --git a/ext/posix/posix.c b/ext/posix/posix.c index 6d429ec89c..95aa9665f0 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -555,6 +555,9 @@ PHP_FUNCTION(posix_ttyname) zval **z_fd; char *p; int fd; +#if HAVE_TTYNAME_R + size_t buflen; +#endif if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &z_fd) == FAILURE) { RETURN_FALSE; @@ -570,12 +573,22 @@ PHP_FUNCTION(posix_ttyname) convert_to_long_ex(z_fd); fd = Z_LVAL_PP(z_fd); } +#if HAVE_TTYNAME_R + buflen = sysconf(_SC_TTY_NAME_MAX); + p = emalloc(buflen); + if (ttyname_r(fd, p, buflen)) { + POSIX_G(last_error) = errno; + efree(p); + RETURN_FALSE; + } + RETURN_STRING(p, 0); +#else if (NULL == (p = ttyname(fd))) { POSIX_G(last_error) = errno; RETURN_FALSE; } - +#endif RETURN_STRING(p, 1); } /* }}} */ @@ -809,22 +822,41 @@ PHP_FUNCTION(posix_getgrnam) char *name; struct group *g; int name_len; +#if HAVE_GETGRNAM_R + struct group gbuf; + int buflen; + char *buf; +#endif if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { RETURN_FALSE; } +#if HAVE_GETGRNAM_R + buflen = sysconf(_SC_GETGR_R_SIZE_MAX); + buf = emalloc(buflen); + g = &gbuf; + + if (getgrnam_r(name, g, buf, buflen, &g) || g == NULL) { + POSIX_G(last_error) = errno; + efree(buf); + RETURN_FALSE; + } +#else if (NULL == (g = getgrnam(name))) { POSIX_G(last_error) = errno; RETURN_FALSE; } - +#endif array_init(return_value); if (!php_posix_group_to_array(g, return_value)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to convert posix group to array"); - RETURN_FALSE; + RETVAL_FALSE; } +#if HAVE_GETGRNAM_R + efree(buf); +#endif } /* }}} */ @@ -852,7 +884,6 @@ PHP_FUNCTION(posix_getgrgid) efree(grbuf); RETURN_FALSE; } - efree(grbuf); g = &_g; #else if (NULL == (g = getgrgid(gid))) { @@ -864,8 +895,11 @@ PHP_FUNCTION(posix_getgrgid) if (!php_posix_group_to_array(g, return_value)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to convert posix group struct to array"); - RETURN_FALSE; + RETVAL_FALSE; } +#ifdef HAVE_GETGRGID_R + efree(grbuf); +#endif } /* }}} */ @@ -892,23 +926,41 @@ PHP_FUNCTION(posix_getpwnam) struct passwd *pw; char *name; int name_len; - +#ifdef HAVE_GETPWNAM_R + struct passwd pwbuf; + int buflen; + char *buf; +#endif + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { RETURN_FALSE; } +#ifdef HAVE_GETPWNAM_R + buflen = sysconf(_SC_GETPW_R_SIZE_MAX); + buf = emalloc(buflen); + pw = &pwbuf; + + if (getpwnam_r(name, pw, buf, buflen, &pw) || pw == NULL) { + efree(buf); + POSIX_G(last_error) = errno; + RETURN_FALSE; + } +#else if (NULL == (pw = getpwnam(name))) { POSIX_G(last_error) = errno; RETURN_FALSE; } - +#endif array_init(return_value); if (!php_posix_passwd_to_array(pw, return_value)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to convert posix passwd struct to array"); - RETURN_FALSE; + RETVAL_FALSE; } - +#ifdef HAVE_GETPWNAM_R + efree(buf); +#endif } /* }}} */ @@ -936,7 +988,6 @@ PHP_FUNCTION(posix_getpwuid) efree(pwbuf); RETURN_FALSE; } - efree(pwbuf); pw = &_pw; #else if (NULL == (pw = getpwuid(uid))) { @@ -948,8 +999,11 @@ PHP_FUNCTION(posix_getpwuid) if (!php_posix_passwd_to_array(pw, return_value)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to convert posix passwd struct to array"); - RETURN_FALSE; + RETVAL_FALSE; } +#ifdef HAVE_GETPWUID_R + efree(pwbuf); +#endif } /* }}} */ -- 2.40.0