Skip to content

Commit

Permalink
restore: Skip setgroups() when already correct.
Browse files Browse the repository at this point in the history
Skip calling setgroups() when the list of auxiliary groups already has
the values we want.  This allows restoring into an unprivileged user
namespace where setgroups() is disabled.

From: Ambrose Feinstein <[email protected]>
Signed-off-by: Michał Mirosław <[email protected]>
  • Loading branch information
osctobe authored and avagin committed Jul 22, 2023
1 parent b9f360b commit 53dd6ba
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions criu/pie/restorer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
#include "shmem.h"
#include "restorer.h"

/*
* sys_getgroups() buffer size. Not too much, to avoid stack overflow.
*/
#define MAX_GETGROUPS_CHECKED (512 / sizeof(unsigned int))

#ifndef PR_SET_PDEATHSIG
#define PR_SET_PDEATHSIG 1
#endif
Expand Down Expand Up @@ -198,10 +203,19 @@ static int restore_creds(struct thread_creds_args *args, int procfd, int lsm_typ
* Setup supplementary group IDs early.
*/
if (args->groups) {
ret = sys_setgroups(ce->n_groups, args->groups);
if (ret) {
pr_err("Can't setup supplementary group IDs: %d\n", ret);
return -1;
/*
* We may be in an unprivileged user namespace where setgroups
* is disabled. If the current list of groups is already what
* we want, skip the call to setgroups.
*/
unsigned int gids[MAX_GETGROUPS_CHECKED];
int n = sys_getgroups(MAX_GETGROUPS_CHECKED, gids);
if (n != ce->n_groups || memcmp(gids, args->groups, n * sizeof(*gids))) {
ret = sys_setgroups(ce->n_groups, args->groups);
if (ret) {
pr_err("Can't setgroups([%zu gids]): %d\n", ce->n_groups, ret);
return -1;
}
}
}

Expand Down

0 comments on commit 53dd6ba

Please sign in to comment.