• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 #include <errno.h>
18 #include <error.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include <android-base/file.h>
29 #include <android-base/parseint.h>
30 #include <android-base/strings.h>
31 #include <packagelistparser/packagelistparser.h>
32 #include <private/android_filesystem_config.h>
33 #include <scoped_minijail.h>
34 #include <selinux/android.h>
35 
36 // simpleperf_app_runner is used to run simpleperf to profile apps with <profileable shell="true">
37 // on user devices. It works as below:
38 //   simpleperf cmds in shell -> simpleperf_app_runner -> /system/bin/simpleperf in app's context
39 //
40 // 1. User types simpleperf cmds in adb shell. If that is to profile an app, simpleperf calls
41 //    /system/bin/simpleperf_app_runner with profiling arguments.
42 // 2. simpleperf_app_runner checks if the app is profileable_from_shell. Then it switches the
43 //    process to the app's user id / group id, switches secontext to the app's domain, and
44 //    executes /system/bin/simpleperf with profiling arguments.
45 // 3. /system/bin/simpleperf records profiling data and writes profiling data to a file descriptor
46 //    opened by simpleperf cmds in shell.
47 
48 struct PackageListCallbackArg {
49   const char* name;
50   pkg_info* info;
51 };
52 
PackageListParseCallback(pkg_info * info,void * userdata)53 static bool PackageListParseCallback(pkg_info* info, void* userdata) {
54   PackageListCallbackArg* arg = static_cast<PackageListCallbackArg*>(userdata);
55   if (strcmp(arg->name, info->name) == 0) {
56     arg->info = info;
57     return false;
58   }
59   packagelist_free(info);
60   return true;
61 }
62 
ReadPackageInfo(const char * pkgname)63 pkg_info* ReadPackageInfo(const char* pkgname) {
64   // Switch to package_info gid to read package info.
65   gid_t old_egid = getegid();
66   if (setegid(AID_PACKAGE_INFO) == -1) {
67     error(1, errno, "setegid failed");
68   }
69   PackageListCallbackArg arg;
70   arg.name = pkgname;
71   arg.info = nullptr;
72   if (!packagelist_parse(PackageListParseCallback, &arg)) {
73     error(1, errno, "packagelist_parse failed");
74   }
75   if (setegid(old_egid) == -1) {
76     error(1, errno, "setegid failed");
77   }
78   return arg.info;
79 }
80 
GetSupplementaryGids(uid_t userAppId)81 std::vector<gid_t> GetSupplementaryGids(uid_t userAppId) {
82   std::vector<gid_t> gids;
83   int size = getgroups(0, &gids[0]);
84   if (size < 0) {
85     error(1, errno, "getgroups failed");
86   }
87   gids.resize(size);
88   size = getgroups(size, &gids[0]);
89   if (size != static_cast<int>(gids.size())) {
90     error(1, errno, "getgroups failed");
91   }
92   // Profile guide compiled oat files (like /data/app/xxx/oat/arm64/base.odex) are not readable
93   // worldwide (DEXOPT_PUBLIC flag isn't set). To support reading them (needed by simpleperf for
94   // profiling), add shared app gid to supplementary groups.
95   gid_t shared_app_gid = userAppId % AID_USER_OFFSET - AID_APP_START + AID_SHARED_GID_START;
96   gids.push_back(shared_app_gid);
97   return gids;
98 }
99 
CheckSimpleperfArguments(const char * cmdname,char ** args)100 static void CheckSimpleperfArguments(const char* cmdname, char** args) {
101   if (strcmp(cmdname, "stat") != 0 && strcmp(cmdname, "record") != 0 &&
102       strcmp(cmdname, "api-collect") != 0) {
103     error(1, 0, "cmd isn't allowed: %s", cmdname);
104   }
105   std::set<std::string> zero_arg_options = {
106       "-b", "--csv", "--exit-with-parent", "-g", "--in-app", "--interval-only-values",
107       "--no-callchain-joiner", "--no-dump-kernel-symbols", "--no-dump-symbols", "--no-inherit",
108       "--post-unwind=no", "--post-unwind=yes", "--trace-offcpu", "--verbose",
109   };
110   std::set<std::string> one_arg_options = {
111       "-c", "--call-graph", "--callchain-joiner-min-matching-nodes", "--clockid", "--cpu",
112       "--cpu-percent", "--duration", "-e", "-f", "--group", "--interval", "-j", "--log", "-m",
113       "-p", "--size-limit", "-t",
114   };
115   // options with a file descriptor
116   std::set<std::string> fd_options = {
117       "--start_profiling_fd", "--stop-signal-fd", "--out-fd",
118   };
119   // options with path from /data/local/tmp/
120   std::set<std::string> path_options = {
121       "--symfs", "--tracepoint-events",
122   };
123   one_arg_options.insert(fd_options.begin(), fd_options.end());
124   one_arg_options.insert(path_options.begin(), path_options.end());
125   for (int i = 0; args[i] != nullptr; ++i) {
126     if (zero_arg_options.count(args[i])) {
127       continue;
128     } else if (one_arg_options.count(args[i])) {
129       if (args[i + 1] == nullptr) {
130         error(1, 0, "invalid arg: %s", args[i]);
131       }
132       if (fd_options.count(args[i])) {
133         // Check if the file descriptor is valid.
134         int fd;
135         if (!android::base::ParseInt(args[i + 1], &fd) || fd < 3 || fcntl(fd, F_GETFD) == -1) {
136           error(1, 0, "invalid fd for arg: %s", args[i]);
137         }
138       } else if (path_options.count(args[i])) {
139         std::string path;
140         if (!android::base::Realpath(args[i + 1], &path) ||
141             !android::base::StartsWith(path, "/data/local/tmp/")) {
142           error(1, 0, "invalid path for arg: %s", args[i]);
143         }
144       }
145       ++i;
146     } else {
147       error(1, 0, "arg isn't allowed: %s", args[i]);
148     }
149   }
150 }
151 
main(int argc,char * argv[])152 int main(int argc, char* argv[]) {
153   if (argc < 2) {
154     error(1, 0, "usage: simpleperf_app_runner package_name simpleperf_cmd simpleperf_cmd_args...");
155   }
156   if (argc < 3) {
157     error(1, 0, "no simpleperf command name");
158   }
159   char* pkgname = argv[1];
160   char* simpleperf_cmdname = argv[2];
161   int simpleperf_arg_start = 3;
162   CheckSimpleperfArguments(simpleperf_cmdname, argv + simpleperf_arg_start);
163 
164   if (getuid() != AID_SHELL && getuid() != AID_ROOT) {
165     error(1, 0, "program can only run from shell or root");
166   }
167 
168   pkg_info* info = ReadPackageInfo(pkgname);
169   if (info == nullptr) {
170     error(1, 0, "failed to find package %s", pkgname);
171   }
172   if (info->uid < AID_APP_START || info->uid > AID_APP_END) {
173     error(1, 0, "package isn't an application: %s", pkgname);
174   }
175   if (!info->profileable_from_shell) {
176     error(1, 0, "package isn't profileable from shell: %s", pkgname);
177   }
178 
179   // Switch to the app's user id and group id.
180   uid_t uid = info->uid;
181   gid_t gid = info->uid;
182   std::vector<gid_t> supplementary_gids = GetSupplementaryGids(info->uid);
183   ScopedMinijail j(minijail_new());
184   minijail_change_uid(j.get(), uid);
185   minijail_change_gid(j.get(), gid);
186   minijail_set_supplementary_gids(j.get(), supplementary_gids.size(), &supplementary_gids[0]);
187   minijail_enter(j.get());
188 
189   // Switch to the app's selinux context.
190   if (selinux_android_setcontext(uid, 0, info->seinfo, pkgname) < 0) {
191     error(1, errno, "couldn't set SELinux security context");
192   }
193 
194   // Switch to the app's data directory.
195   if (TEMP_FAILURE_RETRY(chdir(info->data_dir)) == -1) {
196     error(1, errno, "couldn't chdir to package's data directory");
197   }
198 
199   // Run /system/bin/simpleperf.
200   std::string simpleperf_in_system_img = "/system/bin/simpleperf";
201   int new_argc = 4 + argc - simpleperf_arg_start;
202   char* new_argv[new_argc + 1];
203 
204   new_argv[0] = &simpleperf_in_system_img[0];
205   new_argv[1] = simpleperf_cmdname;
206   std::string app_option = "--app";
207   new_argv[2] = &app_option[0];
208   new_argv[3] = pkgname;
209   for (int i = 4, j = simpleperf_arg_start; j < argc;) {
210     new_argv[i++] = argv[j++];
211   }
212   new_argv[new_argc] = nullptr;
213   execvp(new_argv[0], new_argv);
214   error(1, errno, "exec failed");
215 }
216