Skip to content

Commit 7d93074

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

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

src/native/fs/fs_napi.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,7 @@ fs_napi(Napi::Env env, Napi::Object exports)
24202420
passwd_bufsize = 16384;
24212421
}
24222422
PASSWD_BUF_SIZE = passwd_bufsize;
2423+
ThreadScope::set_passwd_buf_size(PASSWD_BUF_SIZE);
24232424

24242425

24252426
#ifdef O_DIRECT

src/native/util/os.h

+6
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ 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+
4449
int add_thread_capabilities();
4550

4651
const static uid_t orig_uid;
4752
const static gid_t orig_gid;
4853
const static std::vector<gid_t> orig_groups;
54+
static int passwd_buf_size;
4955

5056
static std::vector<gid_t> get_process_groups();
5157

src/native/util/os_darwin.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@ 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+
int 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);
53-
if (pw == NULL) {
54-
if (errno == 0) {
51+
std::unique_ptr<char[]> buf(new char[ThreadScope::passwd_buf_size]);
52+
struct passwd pwd;
53+
struct passwd *getpwnam_res;
54+
55+
const int res = getpwuid_r(uid, &pwd, buf.get(), ThreadScope::passwd_buf_size, &getpwnam_res);
56+
if (getpwnam_res == NULL) {
57+
if (res == 0) {
5558
// LOG("get_supplemental_groups_by_uid: no record for uid " << uid);
5659
} else {
5760
LOG("WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror(errno));
@@ -61,7 +64,7 @@ get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& groups)
6164
int ngroups = NGROUPS_MAX;
6265
//for some reason on mac getgrouplist accepts an array of int instead of gid_t. so need to create a vector of int and then insert it into groups
6366
std::vector<int> tmp_groups(ngroups);
64-
if (getgrouplist(pw->pw_name, pw->pw_gid, &tmp_groups[0], &ngroups) < 0) {
67+
if (getgrouplist(pwd.pw_name, pwd.pw_gid, &tmp_groups[0], &ngroups) < 0) {
6568
LOG("get_supplemental_groups_by_uid: getgrouplist failed: ngroups too small " << ngroups);
6669
return -1;
6770
}

src/native/util/os_linux.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ get_current_uid()
2727

2828
const uid_t ThreadScope::orig_uid = getuid();
2929
const gid_t ThreadScope::orig_gid = getgid();
30+
int 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);
39-
if (pw == NULL) {
40-
if (errno == 0) {
37+
std::unique_ptr<char[]> buf(new char[ThreadScope::passwd_buf_size]);
38+
struct passwd pwd;
39+
struct passwd *getpwnam_res;
40+
41+
const int res = getpwuid_r(uid, &pwd, buf.get(), ThreadScope::passwd_buf_size, &getpwnam_res);
42+
if (getpwnam_res == NULL) {
43+
if (res == 0) {
4144
// LOG("get_supplemental_groups_by_uid: no record for uid " << uid);
4245
} else {
4346
LOG("WARNING: get_supplemental_groups_by_uid: getpwuid failed: " << strerror(errno));
@@ -46,7 +49,7 @@ get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& groups)
4649
}
4750
int ngroups = NGROUPS_MAX;
4851
groups.resize(ngroups);
49-
if (getgrouplist(pw->pw_name, pw->pw_gid, &groups[0], &ngroups) < 0) {
52+
if (getgrouplist(pwd.pw_name, pwd.pw_gid, &groups[0], &ngroups) < 0) {
5053
LOG("get_supplemental_groups_by_uid: getgrouplist failed: ngroups too small " << ngroups);
5154
return -1;
5255
}

0 commit comments

Comments
 (0)