diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-10-07 06:36:00 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-10-07 06:36:00 +0200 |
commit | 4c1d645c86f4e7a380d96f9ba962f8b270f595dc (patch) | |
tree | 365585f77fc146545119c17250eff32a61f2718c | |
parent | 860b3d066f6aaa12dfa0cd2351559e05288cf9b5 (diff) | |
download | busybox-w32-4c1d645c86f4e7a380d96f9ba962f8b270f595dc.tar.gz busybox-w32-4c1d645c86f4e7a380d96f9ba962f8b270f595dc.tar.bz2 busybox-w32-4c1d645c86f4e7a380d96f9ba962f8b270f595dc.zip |
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 <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/test.c | 26 | ||||
-rw-r--r-- | include/libbb.h | 10 | ||||
-rw-r--r-- | libbb/bb_getgroups.c | 10 | ||||
-rw-r--r-- | 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 { | |||
426 | /* set only by check_operator(), either to bogus struct | 426 | /* set only by check_operator(), either to bogus struct |
427 | * or points to matching operator_t struct. Never NULL. */ | 427 | * or points to matching operator_t struct. Never NULL. */ |
428 | const struct operator_t *last_operator; | 428 | const struct operator_t *last_operator; |
429 | gid_t *group_array; | 429 | struct cached_groupinfo groupinfo; |
430 | int ngroups; | ||
431 | #if BASH_TEST2 | 430 | #if BASH_TEST2 |
432 | bool bash_test2; | 431 | bool bash_test2; |
433 | #endif | 432 | #endif |
@@ -440,8 +439,7 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics; | |||
440 | #define S (*test_ptr_to_statics) | 439 | #define S (*test_ptr_to_statics) |
441 | #define args (S.args ) | 440 | #define args (S.args ) |
442 | #define last_operator (S.last_operator) | 441 | #define last_operator (S.last_operator) |
443 | #define group_array (S.group_array ) | 442 | #define groupinfo (S.groupinfo ) |
444 | #define ngroups (S.ngroups ) | ||
445 | #define bash_test2 (S.bash_test2 ) | 443 | #define bash_test2 (S.bash_test2 ) |
446 | #define leaving (S.leaving ) | 444 | #define leaving (S.leaving ) |
447 | 445 | ||
@@ -449,7 +447,7 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics; | |||
449 | XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \ | 447 | XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \ |
450 | } while (0) | 448 | } while (0) |
451 | #define DEINIT_S() do { \ | 449 | #define DEINIT_S() do { \ |
452 | free(group_array); \ | 450 | free(groupinfo.supplementary_array); \ |
453 | free(test_ptr_to_statics); \ | 451 | free(test_ptr_to_statics); \ |
454 | } while (0) | 452 | } while (0) |
455 | 453 | ||
@@ -644,7 +642,7 @@ static int is_a_group_member(gid_t gid) | |||
644 | if (gid == getgid() || gid == getegid()) | 642 | if (gid == getgid() || gid == getegid()) |
645 | return 1; | 643 | return 1; |
646 | 644 | ||
647 | return is_in_supplementary_groups(&ngroups, &group_array, gid); | 645 | return is_in_supplementary_groups(&groupinfo, gid); |
648 | } | 646 | } |
649 | 647 | ||
650 | /* | 648 | /* |
@@ -654,8 +652,20 @@ static int is_a_group_member(gid_t gid) | |||
654 | */ | 652 | */ |
655 | static int test_st_mode(struct stat *st, int mode) | 653 | static int test_st_mode(struct stat *st, int mode) |
656 | { | 654 | { |
657 | unsigned int euid = geteuid(); | 655 | enum { ANY_IX = S_IXUSR | S_IXGRP | S_IXOTH }; |
658 | 656 | unsigned euid; | |
657 | |||
658 | //TODO if (mode == X_OK) { | ||
659 | // /* Do we already know with no extra syscalls? */ | ||
660 | // if (!S_ISREG(st->st_mode)) | ||
661 | // return 0; /* not a regular file */ | ||
662 | // if ((st->st_mode & ANY_IX) == 0) | ||
663 | // return 0; /* no one can execute */ | ||
664 | // if ((st->st_mode & ANY_IX) == ANY_IX) | ||
665 | // return 1; /* anyone can execute */ | ||
666 | // } | ||
667 | |||
668 | euid = geteuid(); | ||
659 | if (euid == 0) { | 669 | if (euid == 0) { |
660 | /* Root can read or write any file. */ | 670 | /* Root can read or write any file. */ |
661 | if (mode != X_OK) | 671 | 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; | |||
1203 | gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC; | 1203 | gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC; |
1204 | /* | 1204 | /* |
1205 | * True if GID is in our getgroups() result. | 1205 | * True if GID is in our getgroups() result. |
1206 | * getgroups() is cached in group_array[], to makse successive calls faster. | 1206 | * getgroups() is cached in supplementary_array[], to make successive calls faster. |
1207 | */ | 1207 | */ |
1208 | int FAST_FUNC is_in_supplementary_groups(int *pngroups, gid_t **pgroup_array, gid_t gid); | 1208 | struct cached_groupinfo { |
1209 | //TODO? gid_t egid; | ||
1210 | int ngroups; | ||
1211 | gid_t *supplementary_array; | ||
1212 | }; | ||
1213 | //TODO? int FAST_FUNC get_cached_egid(gid_t *egid); | ||
1214 | int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid); | ||
1209 | 1215 | ||
1210 | #if ENABLE_FEATURE_UTMP | 1216 | #if ENABLE_FEATURE_UTMP |
1211 | void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname); | 1217 | 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) | |||
47 | } | 47 | } |
48 | 48 | ||
49 | /* Return non-zero if GID is in our supplementary group list. */ | 49 | /* Return non-zero if GID is in our supplementary group list. */ |
50 | int FAST_FUNC is_in_supplementary_groups(int *pngroups, gid_t **pgroup_array, gid_t gid) | 50 | int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid) |
51 | { | 51 | { |
52 | int i; | 52 | int i; |
53 | int ngroups; | 53 | int ngroups; |
54 | gid_t *group_array; | 54 | gid_t *group_array; |
55 | 55 | ||
56 | if (*pngroups == 0) | 56 | if (groupinfo->ngroups == 0) |
57 | *pgroup_array = bb_getgroups(pngroups, NULL); | 57 | groupinfo->supplementary_array = bb_getgroups(&groupinfo->ngroups, NULL); |
58 | ngroups = *pngroups; | 58 | ngroups = groupinfo->ngroups; |
59 | group_array = *pgroup_array; | 59 | group_array = groupinfo->supplementary_array; |
60 | 60 | ||
61 | /* Search through the list looking for GID. */ | 61 | /* Search through the list looking for GID. */ |
62 | for (i = 0; i < ngroups; i++) | 62 | 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 { | |||
493 | /* Rarely referenced stuff */ | 493 | /* Rarely referenced stuff */ |
494 | 494 | ||
495 | /* Cached supplementary group array (for testing executable'ity of files) */ | 495 | /* Cached supplementary group array (for testing executable'ity of files) */ |
496 | int ngroups; | 496 | struct cached_groupinfo groupinfo; |
497 | gid_t *group_array; | ||
498 | 497 | ||
499 | #if ENABLE_ASH_RANDOM_SUPPORT | 498 | #if ENABLE_ASH_RANDOM_SUPPORT |
500 | random_t random_gen; | 499 | random_t random_gen; |
@@ -528,8 +527,7 @@ extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc; | |||
528 | #define may_have_traps (G_misc.may_have_traps ) | 527 | #define may_have_traps (G_misc.may_have_traps ) |
529 | #define trap (G_misc.trap ) | 528 | #define trap (G_misc.trap ) |
530 | #define trap_ptr (G_misc.trap_ptr ) | 529 | #define trap_ptr (G_misc.trap_ptr ) |
531 | #define ngroups (G_misc.ngroups ) | 530 | #define groupinfo (G_misc.groupinfo ) |
532 | #define group_array (G_misc.group_array) | ||
533 | #define random_gen (G_misc.random_gen ) | 531 | #define random_gen (G_misc.random_gen ) |
534 | #define backgndpid (G_misc.backgndpid ) | 532 | #define backgndpid (G_misc.backgndpid ) |
535 | #define INIT_G_misc() do { \ | 533 | #define INIT_G_misc() do { \ |
@@ -13821,7 +13819,7 @@ static int test_exec(/*const char *fullname,*/ struct stat *statb) | |||
13821 | stmode = S_IXUSR; | 13819 | stmode = S_IXUSR; |
13822 | else if (statb->st_gid == getegid()) | 13820 | else if (statb->st_gid == getegid()) |
13823 | stmode = S_IXGRP; | 13821 | stmode = S_IXGRP; |
13824 | else if (is_in_supplementary_groups(&ngroups, &group_array, statb->st_gid)) | 13822 | else if (is_in_supplementary_groups(&groupinfo, statb->st_gid)) |
13825 | stmode = S_IXGRP; | 13823 | stmode = S_IXGRP; |
13826 | 13824 | ||
13827 | return statb->st_mode & stmode; | 13825 | return statb->st_mode & stmode; |