Skip to content

fix group/passwd api misuse if ZTS #13876

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
41 changes: 23 additions & 18 deletions ext/standard/filestat.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@
# include "win32/pwd.h"
# else
# include <pwd.h>
#ifndef NSS_BUFLEN_PASSWD
#define NSS_BUFLEN_PASSWD 1024
#endif
# endif
#endif

#if HAVE_GRP_H
# include <grp.h>
#ifndef NSS_BUFLEN_GROUP
#define NSS_BUFLEN_GROUP 1024
#endif
#endif

#ifdef HAVE_UTIME
Expand Down Expand Up @@ -277,20 +283,20 @@ PHPAPI zend_result php_get_gid_by_name(const char *name, gid_t *gid)
#if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
struct group gr;
struct group *retgrptr;
long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
char *grbuf;
long initialsize = sysconf(_SC_GETGR_R_SIZE_MAX);
zend_string *str = zend_string_alloc(MAX(initialsize, NSS_BUFLEN_GROUP), 0);
int retval;

if (grbuflen < 1) {
return FAILURE;
while ((retval = getgrnam_r(name, &gr, ZSTR_VAL(str), ZSTR_LEN(str), &retgrptr)) == ERANGE) {
str = zend_string_extend(str, (ZSTR_LEN(str) * 2) , 0);
}

grbuf = emalloc(grbuflen);
if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) != 0 || retgrptr == NULL) {
efree(grbuf);
if(retval != 0 || retgrptr == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: space after if

zend_string_release(str);
return FAILURE;
}
efree(grbuf);
*gid = gr.gr_gid;
zend_string_release(str);

#else
struct group *gr = getgrnam(name);

Expand Down Expand Up @@ -403,20 +409,19 @@ PHPAPI zend_result php_get_uid_by_name(const char *name, uid_t *uid)
#if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
struct passwd pw;
struct passwd *retpwptr = NULL;
long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
char *pwbuf;
long initialsize = sysconf(_SC_GETPW_R_SIZE_MAX);
zend_string *str = zend_string_alloc(MAX(initialsize, NSS_BUFLEN_PASSWD), 0);
int retval;

if (pwbuflen < 1) {
return FAILURE;
while ((retval = getpwnam_r(name, &pw, ZSTR_VAL(str), ZSTR_LEN(str), &retpwptr)) == ERANGE) {
str = zend_string_extend(str, (ZSTR_LEN(str) * 2) , 0);
Copy link
Member

@devnexen devnexen Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall makes sense, but I like the ZSTR_LEN(str) << 1 form better.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}

pwbuf = emalloc(pwbuflen);
if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) != 0 || retpwptr == NULL) {
efree(pwbuf);
if(retval != 0 || retpwptr == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: space after if

zend_string_release(str);
return FAILURE;
}
efree(pwbuf);
*uid = pw.pw_uid;
zend_string_release(str);
#else
struct passwd *pw = getpwnam(name);

Expand Down