Skip to content

Commit bd1f037

Browse files
committed
Create privileged executable for refreshing groups
`acloud` relies on [`sg`](https://man7.org/linux/man-pages/man1/sg.1.html) to [refresh group membership](https://cs.android.com/android/platform/superproject/+/android-latest-release:tools/acloud/internal/lib/utils.py;l=1322;drc=02cef780efe034b789d0e3708d4929d2f236327b) if the user has joined the `cvdnetwork` / `kvm` / `render` groups but has not yet restarted or re-logged in. Debian 13 changed providers of `sg` / `newgrp` from `shadow` to `util-linux`. In the `util-linux` implementation, `sg` only updates the primary group but does not update secondary groups. - https://www.github.com/util-linux/util-linux/issues/4098 - https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1130245 Since `sg` no longer works to refresh the apparent secondary group memberships, we'll need an alternative implementation. Mechanism for raising privilege copied from https://www.github.com/google/android-cuttlefish/pull/1706 Sample invocation: ``` cvd_refresh_groups /usr/bin/groups groups ``` Bug: b/510899675
1 parent 3257ff8 commit bd1f037

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("//cuttlefish/bazel:rules.bzl", "cf_cc_binary")
2+
3+
package(
4+
default_visibility = ["//:android_cuttlefish"],
5+
)
6+
7+
cf_cc_binary(
8+
name = "cvd_refresh_groups",
9+
srcs = ["cvd_refresh_groups.cc"],
10+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright (C) 2026 The Android Open Source Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include <grp.h>
17+
#include <limits.h>
18+
#include <stdio.h>
19+
#include <unistd.h>
20+
21+
/**
22+
* Privileged executable that calls initgroups to refresh the secondary groups
23+
* list. Comparable to `sg`, except that it doesn't update the primary group,
24+
* and refreshes the entire list of secondary groups.
25+
*
26+
* Necessary because Debian 13's `sg` no longer updates the secondary groups
27+
* list.
28+
*
29+
* - https://www.github.com/util-linux/util-linux/issues/4098
30+
* - https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1130245
31+
*
32+
* Sample invocation:
33+
* ```
34+
* cvd_refresh_groups /usr/bin/groups groups
35+
* ```
36+
*/
37+
int main(int argc, char** argv) {
38+
if (argc < 3) {
39+
fprintf(stderr,
40+
"Usage: cvd_refresh_groups <file> <argv[0]> [argv<N> ...]\n");
41+
return 1;
42+
}
43+
char user[LOGIN_NAME_MAX];
44+
if (getlogin_r(user, sizeof(user))) {
45+
perror("getlogin_r failed");
46+
return 1;
47+
}
48+
if (initgroups(user, getgid())) {
49+
perror("initgroups failed");
50+
return 1;
51+
}
52+
execv(argv[1], argv + 2);
53+
perror("execvp failed");
54+
}

base/cvd/cuttlefish/package/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ package_files(
3333
"cuttlefish-common/bin/cvd_internal_start": "//cuttlefish/host/commands/start:cvd_internal_start",
3434
"cuttlefish-common/bin/cvd_internal_status": "//cuttlefish/host/commands/status:cvd_internal_status",
3535
"cuttlefish-common/bin/cvd_internal_stop": "//cuttlefish/host/commands/stop:cvd_internal_stop",
36+
"cuttlefish-common/bin/cvd_refresh_groups": "//cuttlefish/host/commands/refresh_groups:cvd_refresh_groups",
3637
"cuttlefish-common/bin/cvd_send_id_disclosure": "//cuttlefish/host/commands/cvd_send_id_disclosure",
3738
"cuttlefish-common/bin/cvd_send_sms": "//cuttlefish/host/commands/cvd_send_sms",
3839
"cuttlefish-common/bin/cvd_update_location": "//cuttlefish/host/commands/cvd_update_location",

base/debian/cuttlefish-base.postinst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ case "$1" in
2727

2828
setcap cap_net_admin,cap_net_bind_service,cap_net_raw=+ep \
2929
/usr/lib/cuttlefish-common/bin/cvdalloc
30+
setcap cap_setgid=+ep \
31+
/usr/lib/cuttlefish-common/bin/cvd_refresh_groups
3032
;;
3133
esac
3234

0 commit comments

Comments
 (0)