Omit privileged setgroups(2) call to test set[ug]id(2) to same user

Every user may change its user/group ID to the current one.
With an ugly hack, skip the superuser-only part of chuser()
to exercise this rest of the code path in regular tests.
This commit is contained in:
Klemens Nanni 2024-11-12 02:36:28 +03:00
parent 988cd0e207
commit 12acd710be
No known key found for this signature in database
2 changed files with 8 additions and 10 deletions

View file

@ -12,7 +12,7 @@ import (
"golang.org/x/sys/unix"
)
func chuser(input string) error {
func chuser(input string, skipSetgroupsForTests ...any) error {
givenUser, givenGroup, _ := strings.Cut(input, ":")
var (
@ -43,8 +43,10 @@ func chuser(input string) error {
gid, _ = strconv.Atoi(usr.Gid)
}
if err := unix.Setgroups([]int{gid}); err != nil {
return fmt.Errorf("setgroups: %d: %v", gid, err)
if len(skipSetgroupsForTests) == 0 {
if err := unix.Setgroups([]int{gid}); err != nil {
return fmt.Errorf("setgroups: %d: %v", gid, err)
}
}
if err := unix.Setgid(gid); err != nil {
return fmt.Errorf("setgid: %d: %v", gid, err)