From dd756ae82d271b3d7cbc06f793a5132aed5e5e7d Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 21 Feb 2024 20:08:41 +0000 Subject: [PATCH] Stop trying to be clever with sysconf(_SC_NGROUPS_MAX). The test failed the first time someone ran it as a non-root user. Definitely not worth changing sepolicy for this, and even if we did, that wouldn't work for static binaries (to the extent that we care). Bug: http://b/326189243 Test: treehugger Change-Id: I02441ce7f69ac477b0223565ac490046cee12579 --- libc/bionic/sysconf.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libc/bionic/sysconf.cpp b/libc/bionic/sysconf.cpp index ff72b939d..9ffb58e86 100644 --- a/libc/bionic/sysconf.cpp +++ b/libc/bionic/sysconf.cpp @@ -41,16 +41,6 @@ #include "platform/bionic/page.h" #include "private/bionic_tls.h" -static long __sysconf_fread_long(const char* path) { - long result = 0; - FILE* fp = fopen(path, "re"); - if (fp != nullptr) { - fscanf(fp, "%ld", &result); - fclose(fp); - } - return result; -} - struct sysconf_cache { long size, assoc, linesize; @@ -100,6 +90,16 @@ static sysconf_caches* __sysconf_caches() { #else +static long __sysconf_fread_long(const char* path) { + long result = 0; + FILE* fp = fopen(path, "re"); + if (fp != nullptr) { + fscanf(fp, "%ld", &result); + fclose(fp); + } + return result; +} + static sysconf_caches* __sysconf_caches() { static sysconf_caches cached = []{ sysconf_caches info = {}; @@ -182,8 +182,8 @@ long sysconf(int name) { case _SC_AVPHYS_PAGES: return get_avphys_pages(); case _SC_CHILD_MAX: return __sysconf_rlimit(RLIMIT_NPROC); - case _SC_CLK_TCK: return static_cast(getauxval(AT_CLKTCK)); - case _SC_NGROUPS_MAX: return __sysconf_fread_long("/proc/sys/kernel/ngroups_max"); + case _SC_CLK_TCK: + return static_cast(getauxval(AT_CLKTCK)); case _SC_NPROCESSORS_CONF: return get_nprocs_conf(); case _SC_NPROCESSORS_ONLN: return get_nprocs(); case _SC_OPEN_MAX: return __sysconf_rlimit(RLIMIT_NOFILE); @@ -205,6 +205,11 @@ long sysconf(int name) { case _SC_COLL_WEIGHTS_MAX: return _POSIX2_COLL_WEIGHTS_MAX; // Minimum requirement. case _SC_EXPR_NEST_MAX: return _POSIX2_EXPR_NEST_MAX; // Minimum requirement. case _SC_LINE_MAX: return _POSIX2_LINE_MAX; // Minimum requirement. + case _SC_NGROUPS_MAX: + // Only root can read /proc/sys/kernel/ngroups_max on Android, and groups + // are vestigial anyway, so the "maximum maximum" of NGROUPS_MAX is a good + // enough answer for _SC_NGROUPS_MAX... + return NGROUPS_MAX; case _SC_PASS_MAX: return PASS_MAX; case _SC_2_C_BIND: return _POSIX2_C_BIND; case _SC_2_C_DEV: return _POSIX2_C_DEV;