← Commit list

4c1d645c8

Denys Vlasenko
Denys Vlasenko
2024-10-07
SUCCESS Duration: 156s

Subject

libbb: simplify parameter passing in is_in_supplementary_groups()

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

coreutils/test.c+18 -8
@@ -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)
include/libbb.h+8 -2
@@ -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);
libbb/bb_getgroups.c+5 -5
@@ -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++)
shell/ash.c+3 -5
@@ -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;