xref: /aosp_15_r20/system/extras/simpleperf/main.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
18*288bf522SAndroid Build Coastguard Worker #include <android-base/properties.h>
19*288bf522SAndroid Build Coastguard Worker #endif
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include "command.h"
22*288bf522SAndroid Build Coastguard Worker #include "environment.h"
23*288bf522SAndroid Build Coastguard Worker #include "utils.h"
24*288bf522SAndroid Build Coastguard Worker 
25*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
28*288bf522SAndroid Build Coastguard Worker 
AndroidSecurityCheck()29*288bf522SAndroid Build Coastguard Worker bool AndroidSecurityCheck() {
30*288bf522SAndroid Build Coastguard Worker   if (IsRoot()) {
31*288bf522SAndroid Build Coastguard Worker     return true;
32*288bf522SAndroid Build Coastguard Worker   }
33*288bf522SAndroid Build Coastguard Worker   // Simpleperf can be executed by the shell, or by apps themselves. To avoid malicious apps
34*288bf522SAndroid Build Coastguard Worker   // exploiting perf_event_open interface via simpleperf, simpleperf needs proof that the user
35*288bf522SAndroid Build Coastguard Worker   // is expecting simpleperf to be ran:
36*288bf522SAndroid Build Coastguard Worker   //   1) On Android < 11, perf_event_open is secured by perf_event_allow_path, which is controlled
37*288bf522SAndroid Build Coastguard Worker   // by security.perf_harden property. perf_event_open syscall can be used only after user setting
38*288bf522SAndroid Build Coastguard Worker   // security.perf_harden to 0 in shell. So we don't need to check security.perf_harden explicitly.
39*288bf522SAndroid Build Coastguard Worker   //   2) On Android >= 11, perf_event_open may be controlled by selinux instead of
40*288bf522SAndroid Build Coastguard Worker   // perf_event_allow_path. So we need to check security.perf_harden explicitly. If simpleperf is
41*288bf522SAndroid Build Coastguard Worker   // running via shell, we already know the origin of the request is the user, so set the property
42*288bf522SAndroid Build Coastguard Worker   // ourselves for convenience. When started by the app, we won't have the permission to set the
43*288bf522SAndroid Build Coastguard Worker   // property, so the user will need to prove this intent by setting it manually via shell.
44*288bf522SAndroid Build Coastguard Worker   //   3) On Android >= 13, besides perf_harden property, we use persist properties to allow an app
45*288bf522SAndroid Build Coastguard Worker   // profiling itself even after device reboot. User needs to set the uid of the app which wants to
46*288bf522SAndroid Build Coastguard Worker   // profile itself. And the permission has an expiration time.
47*288bf522SAndroid Build Coastguard Worker   int android_version = GetAndroidVersion();
48*288bf522SAndroid Build Coastguard Worker   if (android_version >= 13) {
49*288bf522SAndroid Build Coastguard Worker     if (IsInAppUid() && android::base::GetUintProperty("persist.simpleperf.profile_app_uid", 0u,
50*288bf522SAndroid Build Coastguard Worker                                                        UINT_MAX) == getuid()) {
51*288bf522SAndroid Build Coastguard Worker       if (android::base::GetUintProperty<uint64_t>("persist.simpleperf.profile_app_expiration_time",
52*288bf522SAndroid Build Coastguard Worker                                                    0, UINT64_MAX) > time(nullptr)) {
53*288bf522SAndroid Build Coastguard Worker         return true;
54*288bf522SAndroid Build Coastguard Worker       }
55*288bf522SAndroid Build Coastguard Worker     }
56*288bf522SAndroid Build Coastguard Worker   }
57*288bf522SAndroid Build Coastguard Worker   if (android_version >= 11) {
58*288bf522SAndroid Build Coastguard Worker     std::string prop_name = "security.perf_harden";
59*288bf522SAndroid Build Coastguard Worker     if (android::base::GetProperty(prop_name, "") != "0") {
60*288bf522SAndroid Build Coastguard Worker       if (!android::base::SetProperty(prop_name, "0")) {
61*288bf522SAndroid Build Coastguard Worker         fprintf(stderr,
62*288bf522SAndroid Build Coastguard Worker                 "failed to set system property security.perf_harden to 0.\n"
63*288bf522SAndroid Build Coastguard Worker                 "Try using `adb shell setprop security.perf_harden 0` to allow profiling.\n");
64*288bf522SAndroid Build Coastguard Worker         return false;
65*288bf522SAndroid Build Coastguard Worker       }
66*288bf522SAndroid Build Coastguard Worker     }
67*288bf522SAndroid Build Coastguard Worker   }
68*288bf522SAndroid Build Coastguard Worker   return true;
69*288bf522SAndroid Build Coastguard Worker }
70*288bf522SAndroid Build Coastguard Worker 
71*288bf522SAndroid Build Coastguard Worker #endif
72*288bf522SAndroid Build Coastguard Worker 
main(int argc,char ** argv)73*288bf522SAndroid Build Coastguard Worker int main(int argc, char** argv) {
74*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
75*288bf522SAndroid Build Coastguard Worker   if (!AndroidSecurityCheck()) {
76*288bf522SAndroid Build Coastguard Worker     return 1;
77*288bf522SAndroid Build Coastguard Worker   }
78*288bf522SAndroid Build Coastguard Worker #endif
79*288bf522SAndroid Build Coastguard Worker   RegisterAllCommands();
80*288bf522SAndroid Build Coastguard Worker   return RunSimpleperfCmd(argc, argv) ? 0 : 1;
81*288bf522SAndroid Build Coastguard Worker }
82