Skip to content

Commit 48e9f77

Browse files
committed
NC | supplemental groups - replace getpwuid with getpwuid_r
Signed-off-by: nadav mizrahi <[email protected]>
1 parent 043d40c commit 48e9f77

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

src/native/fs/fs_napi.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ typedef std::map<std::string, std::string> XattrMap;
168168
const char* gpfs_dl_path = std::getenv("GPFS_DL_PATH");
169169

170170
int gpfs_lib_file_exists = -1;
171-
static long PASSWD_BUF_SIZE = -1;
172171

173172
static int (*dlsym_gpfs_fcntl)(gpfs_file_t file, void* arg) = 0;
174173

@@ -1410,12 +1409,13 @@ struct GetPwName : public FSWorker
14101409
}
14111410
virtual void Work()
14121411
{
1413-
_buf.reset(new char[PASSWD_BUF_SIZE]);
1412+
const long passwd_buf_size = ThreadScope::get_passwd_buf_size();
1413+
_buf.reset(new char[passwd_buf_size]);
14141414
if (!_buf) {
14151415
SetSyscallError();
14161416
return;
14171417
}
1418-
int rc = getpwnam_r(_user.c_str(), &_pwd, _buf.get(), PASSWD_BUF_SIZE, &_getpwnam_res);
1418+
int rc = getpwnam_r(_user.c_str(), &_pwd, _buf.get(), passwd_buf_size, &_getpwnam_res);
14191419
if (rc != 0) {
14201420
SetSyscallError();
14211421
return;
@@ -2419,7 +2419,7 @@ fs_napi(Napi::Env env, Napi::Object exports)
24192419
if (passwd_bufsize == -1) {
24202420
passwd_bufsize = 16384;
24212421
}
2422-
PASSWD_BUF_SIZE = passwd_bufsize;
2422+
ThreadScope::set_passwd_buf_size(passwd_bufsize);
24232423

24242424

24252425
#ifdef O_DIRECT

src/native/util/os.h

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ class ThreadScope
4141
change_user();
4242
}
4343

44+
static void set_passwd_buf_size(int buf_size)
45+
{
46+
passwd_buf_size = buf_size;
47+
}
48+
49+
static long get_passwd_buf_size()
50+
{
51+
return passwd_buf_size;
52+
}
53+
4454
int add_thread_capabilities();
4555

4656
const static uid_t orig_uid;
@@ -55,6 +65,8 @@ class ThreadScope
5565
uid_t _uid;
5666
gid_t _gid;
5767
std::vector<gid_t> _groups;
68+
69+
static long passwd_buf_size;
5870
};
5971

6072
} // namespace noobaa

src/native/util/os_darwin.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ get_current_uid()
4343
const uid_t ThreadScope::orig_uid = getuid();
4444
const gid_t ThreadScope::orig_gid = getgid();
4545
const std::vector<gid_t> ThreadScope::orig_groups = get_process_groups();
46+
long ThreadScope::passwd_buf_size = -1;
4647

4748
static int
4849
get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& groups)
4950
{
50-
// getpwuid will only indicate if an error happened by setting errno. set it to 0, so will know if there is a change
51-
errno = 0;
52-
struct passwd* pw = getpwuid(uid);
51+
const long passwd_buf_size = ThreadScope::get_passwd_buf_size();
52+
std::unique_ptr<char[]> buf(new char[passwd_buf_size]);
53+
struct passwd pwd;
54+
struct passwd *pw = NULL;
55+
56+
const int res = getpwuid_r(uid, &pwd, buf.get(), passwd_buf_size, &pw);
5357
if (pw == NULL) {
54-
if (errno == 0) {
58+
if (res == 0) {
5559
// LOG("get_supplemental_groups_by_uid: no record for uid " << uid);
5660
} else {
5761
LOG("WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror(errno));

src/native/util/os_linux.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ get_current_uid()
2727

2828
const uid_t ThreadScope::orig_uid = getuid();
2929
const gid_t ThreadScope::orig_gid = getgid();
30+
long ThreadScope::passwd_buf_size = -1;
3031

3132
const std::vector<gid_t> ThreadScope::orig_groups = get_process_groups();
3233

3334
static int
3435
get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& groups)
3536
{
36-
// getpwuid will only indicate if an error happened by setting errno. set it to 0, so will know if there is a change
37-
errno = 0;
38-
struct passwd* pw = getpwuid(uid);
37+
const long passwd_buf_size = ThreadScope::get_passwd_buf_size();
38+
std::unique_ptr<char[]> buf(new char[passwd_buf_size]);
39+
struct passwd pwd;
40+
struct passwd *pw = NULL;
41+
42+
const int res = getpwuid_r(uid, &pwd, buf.get(), passwd_buf_size, &pw);
3943
if (pw == NULL) {
40-
if (errno == 0) {
44+
if (res == 0) {
4145
// LOG("get_supplemental_groups_by_uid: no record for uid " << uid);
4246
} else {
4347
LOG("WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror(errno));

0 commit comments

Comments
 (0)