Skip to content

NC | supplemental groups - getpwuid concurrency issue #9010

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/native/fs/fs_napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ typedef std::map<std::string, std::string> 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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2419,7 +2419,7 @@ fs_napi(Napi::Env env, Napi::Object exports)
if (passwd_bufsize == -1) {
passwd_bufsize = 16384;
}
PASSWD_BUF_SIZE = passwd_bufsize;
ThreadScope::set_passwd_buf_size(passwd_bufsize);
Copy link
Contributor

Choose a reason for hiding this comment

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

we should move lines 2418 - 2421 inside set_passwd_buf_size()
although I must say it's a bit weird to set this static size per thread



#ifdef O_DIRECT
Expand Down
12 changes: 12 additions & 0 deletions src/native/util/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class ThreadScope
change_user();
}

static void set_passwd_buf_size(int buf_size)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
static void set_passwd_buf_size(int buf_size)
static void set_passwd_buf_size(long buf_size)

{
passwd_buf_size = buf_size;
}

static long get_passwd_buf_size()
{
return passwd_buf_size;
}

int add_thread_capabilities();

const static uid_t orig_uid;
Expand All @@ -55,6 +65,8 @@ class ThreadScope
uid_t _uid;
gid_t _gid;
std::vector<gid_t> _groups;

static long passwd_buf_size;
};

} // namespace noobaa
12 changes: 8 additions & 4 deletions src/native/util/os_darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ get_current_uid()
const uid_t ThreadScope::orig_uid = getuid();
const gid_t ThreadScope::orig_gid = getgid();
const std::vector<gid_t> ThreadScope::orig_groups = get_process_groups();
long ThreadScope::passwd_buf_size = -1;

static int
get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& 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<char[]> 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));
Expand Down
12 changes: 8 additions & 4 deletions src/native/util/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gid_t> ThreadScope::orig_groups = get_process_groups();

static int
get_supplemental_groups_by_uid(uid_t uid, std::vector<gid_t>& 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<char[]> 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));
Expand Down