From 4c1d645c86f4e7a380d96f9ba962f8b270f595dc Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 7 Oct 2024 06:36:00 +0200 Subject: libbb: simplify parameter passing in is_in_supplementary_groups() function old new delta is_in_supplementary_groups 54 52 -2 nexpr 721 718 -3 test_exec 125 119 -6 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-11) Total: -11 bytes Signed-off-by: Denys Vlasenko --- coreutils/test.c | 26 ++++++++++++++++++-------- include/libbb.h | 10 ++++++++-- libbb/bb_getgroups.c | 10 +++++----- shell/ash.c | 8 +++----- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/coreutils/test.c b/coreutils/test.c index 874285704..96462d3a2 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -426,8 +426,7 @@ struct test_statics { /* set only by check_operator(), either to bogus struct * or points to matching operator_t struct. Never NULL. */ const struct operator_t *last_operator; - gid_t *group_array; - int ngroups; + struct cached_groupinfo groupinfo; #if BASH_TEST2 bool bash_test2; #endif @@ -440,8 +439,7 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics; #define S (*test_ptr_to_statics) #define args (S.args ) #define last_operator (S.last_operator) -#define group_array (S.group_array ) -#define ngroups (S.ngroups ) +#define groupinfo (S.groupinfo ) #define bash_test2 (S.bash_test2 ) #define leaving (S.leaving ) @@ -449,7 +447,7 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics; XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \ } while (0) #define DEINIT_S() do { \ - free(group_array); \ + free(groupinfo.supplementary_array); \ free(test_ptr_to_statics); \ } while (0) @@ -644,7 +642,7 @@ static int is_a_group_member(gid_t gid) if (gid == getgid() || gid == getegid()) return 1; - return is_in_supplementary_groups(&ngroups, &group_array, gid); + return is_in_supplementary_groups(&groupinfo, gid); } /* @@ -654,8 +652,20 @@ static int is_a_group_member(gid_t gid) */ static int test_st_mode(struct stat *st, int mode) { - unsigned int euid = geteuid(); - + enum { ANY_IX = S_IXUSR | S_IXGRP | S_IXOTH }; + unsigned euid; + +//TODO if (mode == X_OK) { +// /* Do we already know with no extra syscalls? */ +// if (!S_ISREG(st->st_mode)) +// return 0; /* not a regular file */ +// if ((st->st_mode & ANY_IX) == 0) +// return 0; /* no one can execute */ +// if ((st->st_mode & ANY_IX) == ANY_IX) +// return 1; /* anyone can execute */ +// } + + euid = geteuid(); if (euid == 0) { /* Root can read or write any file. */ if (mode != X_OK) diff --git a/include/libbb.h b/include/libbb.h index e06aef08e..99bbf623b 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1203,9 +1203,15 @@ void die_if_bad_username(const char* name) FAST_FUNC; gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC; /* * True if GID is in our getgroups() result. - * getgroups() is cached in group_array[], to makse successive calls faster. + * getgroups() is cached in supplementary_array[], to make successive calls faster. */ -int FAST_FUNC is_in_supplementary_groups(int *pngroups, gid_t **pgroup_array, gid_t gid); +struct cached_groupinfo { + //TODO? gid_t egid; + int ngroups; + gid_t *supplementary_array; +}; +//TODO? int FAST_FUNC get_cached_egid(gid_t *egid); +int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid); #if ENABLE_FEATURE_UTMP void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname); diff --git a/libbb/bb_getgroups.c b/libbb/bb_getgroups.c index f030d5eac..d9bbe95c3 100644 --- a/libbb/bb_getgroups.c +++ b/libbb/bb_getgroups.c @@ -47,16 +47,16 @@ gid_t* FAST_FUNC bb_getgroups(int *ngroups, gid_t *group_array) } /* Return non-zero if GID is in our supplementary group list. */ -int FAST_FUNC is_in_supplementary_groups(int *pngroups, gid_t **pgroup_array, gid_t gid) +int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid) { int i; int ngroups; gid_t *group_array; - if (*pngroups == 0) - *pgroup_array = bb_getgroups(pngroups, NULL); - ngroups = *pngroups; - group_array = *pgroup_array; + if (groupinfo->ngroups == 0) + groupinfo->supplementary_array = bb_getgroups(&groupinfo->ngroups, NULL); + ngroups = groupinfo->ngroups; + group_array = groupinfo->supplementary_array; /* Search through the list looking for GID. */ for (i = 0; i < ngroups; i++) diff --git a/shell/ash.c b/shell/ash.c index 984a71f07..fa57511a7 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -493,8 +493,7 @@ struct globals_misc { /* Rarely referenced stuff */ /* Cached supplementary group array (for testing executable'ity of files) */ - int ngroups; - gid_t *group_array; + struct cached_groupinfo groupinfo; #if ENABLE_ASH_RANDOM_SUPPORT random_t random_gen; @@ -528,8 +527,7 @@ extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc; #define may_have_traps (G_misc.may_have_traps ) #define trap (G_misc.trap ) #define trap_ptr (G_misc.trap_ptr ) -#define ngroups (G_misc.ngroups ) -#define group_array (G_misc.group_array) +#define groupinfo (G_misc.groupinfo ) #define random_gen (G_misc.random_gen ) #define backgndpid (G_misc.backgndpid ) #define INIT_G_misc() do { \ @@ -13821,7 +13819,7 @@ static int test_exec(/*const char *fullname,*/ struct stat *statb) stmode = S_IXUSR; else if (statb->st_gid == getegid()) stmode = S_IXGRP; - else if (is_in_supplementary_groups(&ngroups, &group_array, statb->st_gid)) + else if (is_in_supplementary_groups(&groupinfo, statb->st_gid)) stmode = S_IXGRP; return statb->st_mode & stmode; -- cgit v1.2.3-55-g6feb