Make use of getgrnam() thread-safe

getgrnam is not thread-safe on Linux (but is on macOS). To ensure
we don't call it from two threads at the same time we move its use
inside a lambda that initializes a local static. This is fine, as
we don't expect the 'nogroup' gid to change.
This commit is contained in:
Tor Arne Vestbø
2018-09-11 23:13:37 +02:00
parent 9b3c59f63c
commit 9a4d8f3567

View File

@@ -114,16 +114,20 @@ static int sparsebundle_getattr(const char *path, struct stat *stbuf)
} else
return -ENOENT;
// Reflect user ID of the user who mounted the file system,
// but prefer 'nogroup' for the group, since the group has
// no effect on who can access the mount.
// Reflect user ID of the user who mounted the file system
stbuf->st_uid = getuid();
if (group *nogroup = getgrnam("nogroup"))
stbuf->st_gid = nogroup->gr_gid;
else if (group *nobody = getgrnam("nobody"))
stbuf->st_gid = nobody->gr_gid;
else
stbuf->st_gid = getgid();
static gid_t gid = []() {
// But prefer 'nogroup' for the group, since the group
// has no actual effect on who can access the mount.
if (group *nogroup = getgrnam("nogroup"))
return nogroup->gr_gid;
if (group *nobody = getgrnam("nobody"))
return nobody->gr_gid;
// Fall back to the mounting user
return getgid();
}();
stbuf->st_gid = gid;
// Once allow_other or allow_root is added into the mix we
// want the permissions to also reflect the the situation.