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