xref: /aosp_15_r20/external/minijail/examples/drop_privs.cpp (revision 4b9c6d91573e8b3a96609339b46361b5476dd0f9)
1*4b9c6d91SCole Faust // Copyright (C) 2015 The Android Open Source Project
2*4b9c6d91SCole Faust //
3*4b9c6d91SCole Faust // Licensed under the Apache License, Version 2.0 (the "License");
4*4b9c6d91SCole Faust // you may not use this file except in compliance with the License.
5*4b9c6d91SCole Faust // You may obtain a copy of the License at
6*4b9c6d91SCole Faust //
7*4b9c6d91SCole Faust //      http://www.apache.org/licenses/LICENSE-2.0
8*4b9c6d91SCole Faust //
9*4b9c6d91SCole Faust // Unless required by applicable law or agreed to in writing, software
10*4b9c6d91SCole Faust // distributed under the License is distributed on an "AS IS" BASIS,
11*4b9c6d91SCole Faust // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*4b9c6d91SCole Faust // See the License for the specific language governing permissions and
13*4b9c6d91SCole Faust // limitations under the License.
14*4b9c6d91SCole Faust 
15*4b9c6d91SCole Faust #include <sys/types.h>
16*4b9c6d91SCole Faust #include <sys/capability.h>
17*4b9c6d91SCole Faust #include <unistd.h>
18*4b9c6d91SCole Faust 
19*4b9c6d91SCole Faust #include <libminijail.h>
20*4b9c6d91SCole Faust 
21*4b9c6d91SCole Faust #include <android-base/logging.h>
22*4b9c6d91SCole Faust #include <android-base/macros.h>
23*4b9c6d91SCole Faust 
24*4b9c6d91SCole Faust gid_t groups[] = { 1001, 1002 };
25*4b9c6d91SCole Faust 
log_resugid()26*4b9c6d91SCole Faust void log_resugid() {
27*4b9c6d91SCole Faust     uid_t ruid, euid, suid;
28*4b9c6d91SCole Faust     gid_t rgid, egid, sgid;
29*4b9c6d91SCole Faust     getresuid(&ruid, &euid, &suid);
30*4b9c6d91SCole Faust     getresgid(&rgid, &egid, &sgid);
31*4b9c6d91SCole Faust 
32*4b9c6d91SCole Faust     LOG(INFO) << "ruid " << ruid << " euid " << euid << " suid " << suid;
33*4b9c6d91SCole Faust     LOG(INFO) << "rgid " << rgid << " egid " << egid << " sgid " << sgid;
34*4b9c6d91SCole Faust 
35*4b9c6d91SCole Faust     int nsupp_groups = getgroups(0, NULL);
36*4b9c6d91SCole Faust     if (nsupp_groups < 0) {
37*4b9c6d91SCole Faust         PLOG(FATAL) << "getgroups(0)";
38*4b9c6d91SCole Faust     }
39*4b9c6d91SCole Faust     if (nsupp_groups == 0) {
40*4b9c6d91SCole Faust         LOG(INFO) << "no supplemental groups";
41*4b9c6d91SCole Faust         return;
42*4b9c6d91SCole Faust     }
43*4b9c6d91SCole Faust 
44*4b9c6d91SCole Faust     gid_t *list = (gid_t*)calloc((size_t)nsupp_groups, sizeof(gid_t));
45*4b9c6d91SCole Faust     nsupp_groups = getgroups(nsupp_groups, list);
46*4b9c6d91SCole Faust     if (nsupp_groups < 0) {
47*4b9c6d91SCole Faust         PLOG(FATAL) << "getgroups(nsupp_groups)";
48*4b9c6d91SCole Faust     }
49*4b9c6d91SCole Faust     for (size_t i = 0; i < (size_t)nsupp_groups; i++) {
50*4b9c6d91SCole Faust         LOG(INFO) << "supp gid " << i + 1 << " " << list[i];
51*4b9c6d91SCole Faust     }
52*4b9c6d91SCole Faust     free(list);
53*4b9c6d91SCole Faust }
54*4b9c6d91SCole Faust 
main(void)55*4b9c6d91SCole Faust int main(void) {
56*4b9c6d91SCole Faust     log_resugid();
57*4b9c6d91SCole Faust     minijail *j = minijail_new();
58*4b9c6d91SCole Faust     minijail_change_user(j, "system");
59*4b9c6d91SCole Faust     minijail_change_group(j, "system");
60*4b9c6d91SCole Faust     minijail_set_supplementary_gids(j, arraysize(groups), groups);
61*4b9c6d91SCole Faust     // minijail_use_caps(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
62*4b9c6d91SCole Faust     // minijail_use_seccomp_filter(j);
63*4b9c6d91SCole Faust     // minijail_log_seccomp_filter_failures(j);
64*4b9c6d91SCole Faust     // minijail_parse_seccomp_filters(j, "/data/filter.policy");
65*4b9c6d91SCole Faust     minijail_enter(j);
66*4b9c6d91SCole Faust     log_resugid();
67*4b9c6d91SCole Faust     minijail_destroy(j);
68*4b9c6d91SCole Faust     // minijail *j2 = minijail_new();
69*4b9c6d91SCole Faust     // minijail_change_uid(j2, 5000);
70*4b9c6d91SCole Faust     // minijail_change_gid(j2, 5000);
71*4b9c6d91SCole Faust     // minijail_enter(j2);
72*4b9c6d91SCole Faust     // log_resugid();
73*4b9c6d91SCole Faust     return 0;
74*4b9c6d91SCole Faust }
75