From a17cc9ca8dce6d42602f65a8be3a6804d59dcb6e Mon Sep 17 00:00:00 2001 From: nadav mizrahi Date: Mon, 5 May 2025 14:06:37 +0300 Subject: [PATCH] NC | supplemental groups - replace getpwuid with getpwuid_r Signed-off-by: nadav mizrahi --- src/native/fs/fs_napi.cpp | 12 ++++-------- src/native/util/os.h | 16 ++++++++++++++++ src/native/util/os_darwin.cpp | 12 ++++++++---- src/native/util/os_linux.cpp | 12 ++++++++---- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/native/fs/fs_napi.cpp b/src/native/fs/fs_napi.cpp index 57528c7fc9..f3cdc1c6a6 100644 --- a/src/native/fs/fs_napi.cpp +++ b/src/native/fs/fs_napi.cpp @@ -168,7 +168,6 @@ typedef std::map XattrMap; const char* gpfs_dl_path = std::getenv("GPFS_DL_PATH"); int gpfs_lib_file_exists = -1; -static long PASSWD_BUF_SIZE = -1; static int (*dlsym_gpfs_fcntl)(gpfs_file_t file, void* arg) = 0; @@ -1410,12 +1409,13 @@ struct GetPwName : public FSWorker } virtual void Work() { - _buf.reset(new char[PASSWD_BUF_SIZE]); + const long passwd_buf_size = ThreadScope::get_passwd_buf_size(); + _buf.reset(new char[passwd_buf_size]); if (!_buf) { SetSyscallError(); return; } - int rc = getpwnam_r(_user.c_str(), &_pwd, _buf.get(), PASSWD_BUF_SIZE, &_getpwnam_res); + int rc = getpwnam_r(_user.c_str(), &_pwd, _buf.get(), passwd_buf_size, &_getpwnam_res); if (rc != 0) { SetSyscallError(); return; @@ -2415,11 +2415,7 @@ fs_napi(Napi::Env env, Napi::Object exports) exports_fs["DT_DIR"] = Napi::Number::New(env, DT_DIR); exports_fs["DT_LNK"] = Napi::Number::New(env, DT_LNK); exports_fs["PLATFORM_IOV_MAX"] = Napi::Number::New(env, IOV_MAX); - long passwd_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); - if (passwd_bufsize == -1) { - passwd_bufsize = 16384; - } - PASSWD_BUF_SIZE = passwd_bufsize; + ThreadScope::init_passwd_buf_size(); #ifdef O_DIRECT diff --git a/src/native/util/os.h b/src/native/util/os.h index 3178ecad8a..1ce9bd11db 100644 --- a/src/native/util/os.h +++ b/src/native/util/os.h @@ -41,6 +41,20 @@ class ThreadScope change_user(); } + static void init_passwd_buf_size() + { + long passwd_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + if (passwd_bufsize == -1) { + passwd_bufsize = 16384; + } + passwd_buf_size = passwd_bufsize; + } + + static long get_passwd_buf_size() + { + return passwd_buf_size; + } + int add_thread_capabilities(); const static uid_t orig_uid; @@ -55,6 +69,8 @@ class ThreadScope uid_t _uid; gid_t _gid; std::vector _groups; + + static long passwd_buf_size; }; } // namespace noobaa diff --git a/src/native/util/os_darwin.cpp b/src/native/util/os_darwin.cpp index a076a03b0f..85067f5cc3 100644 --- a/src/native/util/os_darwin.cpp +++ b/src/native/util/os_darwin.cpp @@ -43,15 +43,19 @@ get_current_uid() const uid_t ThreadScope::orig_uid = getuid(); const gid_t ThreadScope::orig_gid = getgid(); const std::vector ThreadScope::orig_groups = get_process_groups(); +long ThreadScope::passwd_buf_size = -1; static int get_supplemental_groups_by_uid(uid_t uid, std::vector& groups) { - // getpwuid will only indicate if an error happened by setting errno. set it to 0, so will know if there is a change - errno = 0; - struct passwd* pw = getpwuid(uid); + const long passwd_buf_size = ThreadScope::get_passwd_buf_size(); + std::unique_ptr buf(new char[passwd_buf_size]); + struct passwd pwd; + struct passwd *pw = NULL; + + const int res = getpwuid_r(uid, &pwd, buf.get(), passwd_buf_size, &pw); if (pw == NULL) { - if (errno == 0) { + if (res == 0) { // LOG("get_supplemental_groups_by_uid: no record for uid " << uid); } else { LOG("WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror(errno)); diff --git a/src/native/util/os_linux.cpp b/src/native/util/os_linux.cpp index cba316105b..f47722bcef 100644 --- a/src/native/util/os_linux.cpp +++ b/src/native/util/os_linux.cpp @@ -27,17 +27,21 @@ get_current_uid() const uid_t ThreadScope::orig_uid = getuid(); const gid_t ThreadScope::orig_gid = getgid(); +long ThreadScope::passwd_buf_size = -1; const std::vector ThreadScope::orig_groups = get_process_groups(); static int get_supplemental_groups_by_uid(uid_t uid, std::vector& groups) { - // getpwuid will only indicate if an error happened by setting errno. set it to 0, so will know if there is a change - errno = 0; - struct passwd* pw = getpwuid(uid); + const long passwd_buf_size = ThreadScope::get_passwd_buf_size(); + std::unique_ptr buf(new char[passwd_buf_size]); + struct passwd pwd; + struct passwd *pw = NULL; + + const int res = getpwuid_r(uid, &pwd, buf.get(), passwd_buf_size, &pw); if (pw == NULL) { - if (errno == 0) { + if (res == 0) { // LOG("get_supplemental_groups_by_uid: no record for uid " << uid); } else { LOG("WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror(errno));