#include "strv.h"
#include "sysctl-util.h"
#include "tmpfile-util.h"
+#include "user-util.h"
#include "xattr-util.h"
#if ENABLE_IDN
assert(fd >= 0);
assert(ret);
- long ngroups_max = sysconf(_SC_NGROUPS_MAX);
+ int ngroups_max = sysconf_ngroups_max();
if (ngroups_max > 0)
n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
return -ESRCH;
}
+int sysconf_ngroups_max(void) {
+ /* Query sysconf _SC_NGROUPS_MAX. Returns an int because the expected value is 64k
+ * and later on this is used as an int with various glibc consumers. */
+
+ errno = 0;
+ long ngroups_max = sysconf(_SC_NGROUPS_MAX);
+ if (ngroups_max <= 0)
+ return errno_or_else(EOPNOTSUPP);
+ if (ngroups_max > INT_MAX)
+ return -ERANGE;
+ return ngroups_max;
+}
+
static size_t getgr_buffer_size(void) {
long bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
return bufsize <= 0 ? 4096U : (size_t) bufsize;
*/
#define PASSWORD_UNPROVISIONED "!unprovisioned"
+int sysconf_ngroups_max(void);
+
int lookup_pwent_in_files(
char * const *files,
const char *name,
return 0;
}
- /*
- * If SupplementaryGroups= was passed then NGROUPS_MAX has to
- * be positive, otherwise fail.
- */
- errno = 0;
- long ngroups_max = sysconf(_SC_NGROUPS_MAX);
- if (ngroups_max <= 0)
- return errno_or_else(EOPNOTSUPP);
+ int ngroups_max = sysconf_ngroups_max();
+ if (ngroups_max < 0)
+ return ngroups_max;
_cleanup_free_ gid_t *l_gids = new(gid_t, ngroups_max);
if (!l_gids)
char gid[DECIMAL_STR_MAX(uint32_t)];
gid_t *gids, max_gid;
int ngroups, r, i;
- long ngroups_max;
+ int ngroups_max;
xsprintf(gid, "%u", UINT32_C(0xFFFF));
ASSERT_NOT_NULL((condition = condition_new(CONDITION_GROUP, gid, false, false)));
ASSERT_OK_POSITIVE(r);
condition_free(condition);
- ngroups_max = ASSERT_OK_ERRNO(sysconf(_SC_NGROUPS_MAX));
+ ngroups_max = ASSERT_OK(sysconf_ngroups_max());
ASSERT_GT(ngroups_max, 0);
gids = newa(gid_t, ngroups_max);
assert_se(fully_set_uid_gid(test_uid, test_gid, test_gids, n_test_gids) >= 0);
} else {
- long ngroups_max;
-
test_uid = getuid();
test_gid = getgid();
- ngroups_max = sysconf(_SC_NGROUPS_MAX);
+ int ngroups_max = sysconf_ngroups_max();
assert_se(ngroups_max > 0);
test_gids = newa(gid_t, ngroups_max);