HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/nss-systemd/nss-systemd.c
Git blob: 2af9b95e4cc1ed0e41e5acfc289c57b60b4dd49b
Lines 1001–1141 of 1,141
r = membershipdb_by_user(user_name, nss_glue_userdb_flags(), &iterator);
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
for (;;) {
_cleanup_(group_record_unrefp) GroupRecord *g = NULL;
_cleanup_free_ char *group_name = NULL;
r = membershipdb_iterator_get(iterator, NULL, &group_name);
if (r == -ESRCH)
break;
if (r < 0) {
UNPROTECT_ERRNO;
*errnop = -r;
return NSS_STATUS_UNAVAIL;
}
/* The group might be defined via traditional NSS only, hence let's do a full look-up without
* disabling NSS. This means we are operating recursively here. */
r = groupdb_by_name(group_name, /* match= */ NULL, (nss_glue_userdb_flags() & ~USERDB_EXCLUDE_NSS) | USERDB_SUPPRESS_SHADOW, &g);
if (r == -ESRCH)
continue;
if (r < 0) {
log_debug_errno(r, "Failed to resolve group '%s', ignoring: %m", group_name);
continue;
}
if (g->gid == gid)
continue;
if (*start >= *size) {
gid_t *new_groups;
long new_size;
if (limit > 0 && *size >= limit) /* Reached the limit.? */
break;
if (*size > LONG_MAX/2) { /* Check for overflow */
UNPROTECT_ERRNO;
*errnop = ENOMEM;
return NSS_STATUS_TRYAGAIN;
}
new_size = *start * 2;
if (limit > 0 && new_size > limit)
new_size = limit;
/* Enlarge buffer */
new_groups = reallocarray(*groupsp, new_size, sizeof(**groupsp));
if (!new_groups) {
UNPROTECT_ERRNO;
*errnop = ENOMEM;
return NSS_STATUS_TRYAGAIN;
}
*groupsp = new_groups;
*size = new_size;
}
(*groupsp)[(*start)++] = g->gid;
any = true;
}
return any ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND;
}
/* Note that we intentionally use POSIX thread-specific data instead of a plain thread_local variable.
* A thread_local in this lazily-loaded DSO uses a dynamic TLS model by default and may require
* a dynamic TLS allocation. If that allocation fails, glibc calls _exit() from the dynamic linker,
* making the failure unrecoverable. Using pthread_key_t avoids ELF TLS entirely and lets any such
* failure propagate as a normal error instead of terminating the process. */
static pthread_once_t nss_blocked_key_once = PTHREAD_ONCE_INIT;
static pthread_key_t nss_blocked_key;
static int nss_blocked_key_error;
static void nss_blocked_key_init(void) {
/* NULL destructor: the per-thread value is a plain integer counter encoded as void*,
* not a heap allocation, so nothing needs to be freed at thread exit.
* No pthread_key_delete: this library is linked with -z nodelete and always opened with
* RTLD_NODELETE, so it is never unloaded and the key exists for the process lifetime. */
nss_blocked_key_error = pthread_key_create(&nss_blocked_key, NULL);
}
static int nss_blocked_key_ensure(void) {
int r;
r = pthread_once(&nss_blocked_key_once, nss_blocked_key_init);
if (r != 0)
return -r;
if (nss_blocked_key_error != 0)
return -nss_blocked_key_error;
return 0;
}
_public_ int _nss_systemd_block(bool b) {
int r;
uintptr_t blocked;
r = nss_blocked_key_ensure();
if (r < 0)
return r;
blocked = (uintptr_t) pthread_getspecific(nss_blocked_key);
/* This blocks recursively: it's blocked for as many times this function is called with `true` until
* it is called an equal time with `false`. */
if (b) {
if (blocked >= UINTPTR_MAX)
return -EOVERFLOW;
blocked++;
} else {
if (blocked == 0)
return -EOVERFLOW;
blocked--;
}
r = pthread_setspecific(nss_blocked_key, (void*) blocked);
/* Ignore failure on the unblock path: callers may assert on it. */
if (r != 0 && b)
return -r;
return b; /* Return what is passed in, i.e. the new state from the PoV of the caller */
}
_public_ bool _nss_systemd_is_blocked(void) {
if (nss_blocked_key_ensure() < 0)
return false;
return (uintptr_t) pthread_getspecific(nss_blocked_key) > 0;
}