blob: 21c5351e48e50f33cf4aa306ee7c41b5ecdb0b79 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
2 * Copyright (C) 2015 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
Yabin Cuia5697762020-01-13 14:48:01 -080017#if defined(__ANDROID__)
18#include <android-base/properties.h>
19#endif
20
Yabin Cui67d3abd2015-04-16 15:26:31 -070021#include "command.h"
Yabin Cuia5697762020-01-13 14:48:01 -080022#include "environment.h"
23
Yabin Cuiacbdb242020-07-07 15:56:34 -070024using namespace simpleperf;
25
Yabin Cuia5697762020-01-13 14:48:01 -080026#if defined(__ANDROID__)
27
28bool AndroidSecurityCheck() {
29 // Simpleperf can be executed by the shell, or by apps themselves. To avoid malicious apps
30 // exploiting perf_event_open interface via simpleperf, simpleperf needs proof that the user
31 // is expecting simpleperf to be ran:
Yabin Cui17cffbb2020-07-28 17:11:39 -070032 // 1) On Android < R, perf_event_open is secured by perf_event_allow_path, which is controlled
Yabin Cuia5697762020-01-13 14:48:01 -080033 // by security.perf_harden property. perf_event_open syscall can be used only after user setting
34 // security.perf_harden to 0 in shell. So we don't need to check security.perf_harden explicitly.
35 // 2) On Android R, perf_event_open may be controlled by selinux instead of
Yabin Cui17cffbb2020-07-28 17:11:39 -070036 // perf_event_allow_path. So we need to check security.perf_harden explicitly. If simpleperf is
Yabin Cuia5697762020-01-13 14:48:01 -080037 // running via shell, we already know the origin of the request is the user, so set the property
38 // ourselves for convenience. When started by the app, we won't have the permission to set the
39 // property, so the user will need to prove this intent by setting it manually via shell.
40 if (GetAndroidVersion() >= 11) {
41 std::string prop_name = "security.perf_harden";
42 if (android::base::GetProperty(prop_name, "") != "0") {
43 if (!android::base::SetProperty(prop_name, "0")) {
44 fprintf(stderr,
45 "failed to set system property security.perf_harden to 0.\n"
46 "Try using `adb shell setprop security.perf_harden 0` to allow profiling.\n");
47 return false;
48 }
49 }
50 }
51 return true;
52}
53
54#endif
Yabin Cui6d2db332015-06-30 18:03:34 -070055
Yabin Cui67d3abd2015-04-16 15:26:31 -070056int main(int argc, char** argv) {
Yabin Cuia5697762020-01-13 14:48:01 -080057#if defined(__ANDROID__)
58 if (!AndroidSecurityCheck()) {
59 return 1;
60 }
61#endif
Yabin Cui616b3a02017-07-14 15:59:56 -070062 return RunSimpleperfCmd(argc, argv) ? 0 : 1;
Yabin Cui67d3abd2015-04-16 15:26:31 -070063}