1 /* 2 * Copyright (C) 2019 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 package android.telephony; 18 19 import static android.telephony.TelephonyManager.UNKNOWN_CARRIER_ID; 20 21 import static com.android.internal.telephony.TelephonyStatsLog.TELEPHONY_ANOMALY_DETECTED; 22 23 import android.annotation.NonNull; 24 import android.annotation.RequiresPermission; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.pm.PackageManager; 28 import android.content.pm.ResolveInfo; 29 import android.os.ParcelUuid; 30 import android.provider.DeviceConfig; 31 32 import com.android.internal.telephony.TelephonyStatsLog; 33 import com.android.internal.util.IndentingPrintWriter; 34 import com.android.telephony.Rlog; 35 36 import java.io.FileDescriptor; 37 import java.io.PrintWriter; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.UUID; 41 import java.util.concurrent.ConcurrentHashMap; 42 43 /** 44 * A Simple Surface for Telephony to notify a loosely-coupled debugger of particular issues. 45 * 46 * AnomalyReporter allows an optional external logging component to receive events detected by 47 * the framework and take action. This log surface is designed to provide maximium flexibility 48 * to the receiver of these events. Envisioned use cases of this include notifying a vendor 49 * component of: an event that necessitates (timely) log collection on non-AOSP components; 50 * notifying a vendor component of a rare event that should prompt further action such as a 51 * bug report or user intervention for debug purposes. 52 * 53 * <p>This surface is not intended to enable a diagnostic monitor, nor is it intended to support 54 * streaming logs. 55 * 56 * @hide 57 */ 58 public final class AnomalyReporter { 59 private static final String TAG = "AnomalyReporter"; 60 61 private static final String KEY_IS_TELEPHONY_ANOMALY_REPORT_ENABLED = 62 "is_telephony_anomaly_report_enabled"; 63 64 private static Context sContext = null; 65 66 private static Map<UUID, Integer> sEvents = new ConcurrentHashMap<>(); 67 68 /* 69 * Because this is only supporting system packages, once we find a package, it will be the 70 * same package until the next system upgrade. Thus, to save time in processing debug events 71 * we can cache this info and skip the resolution process after it's done the first time. 72 */ 73 private static String sDebugPackageName = null; 74 AnomalyReporter()75 private AnomalyReporter() {}; 76 77 /** 78 * If enabled, build and send an intent to a Debug Service for logging. 79 * 80 * This method sends the {@link TelephonyManager#ACTION_ANOMALY_REPORTED} broadcast, which is 81 * system protected. Invoking this method unless you are the system will result in an error. 82 * Carrier Id will be set as UNKNOWN_CARRIER_ID. 83 * 84 * @param eventId a fixed event ID that will be sent for each instance of the same event. This 85 * ID should be generated randomly. 86 * @param description an optional description, that if included will be used as the subject for 87 * identification and discussion of this event. This description should ideally be 88 * static and must not contain any sensitive information (especially PII). 89 */ reportAnomaly(@onNull UUID eventId, String description)90 public static void reportAnomaly(@NonNull UUID eventId, String description) { 91 reportAnomaly(eventId, description, UNKNOWN_CARRIER_ID); 92 } 93 94 /** 95 * If enabled, build and send an intent to a Debug Service for logging. 96 * 97 * This method sends the {@link TelephonyManager#ACTION_ANOMALY_REPORTED} broadcast, which is 98 * system protected. Invoking this method unless you are the system will result in an error. 99 * 100 * @param eventId a fixed event ID that will be sent for each instance of the same event. This 101 * ID should be generated randomly. 102 * @param description an optional description, that if included will be used as the subject for 103 * identification and discussion of this event. This description should ideally be 104 * static and must not contain any sensitive information (especially PII). 105 * @param carrierId the carrier of the id associated with this event. 106 */ reportAnomaly(@onNull UUID eventId, String description, int carrierId)107 public static void reportAnomaly(@NonNull UUID eventId, String description, int carrierId) { 108 if (sContext == null) { 109 Rlog.w(TAG, "AnomalyReporter not yet initialized, dropping event=" + eventId); 110 return; 111 } 112 113 // Don't report if the server-side flag isn't loaded, as it implies other anomaly report 114 // related config hasn't loaded. 115 try { 116 boolean isAnomalyReportEnabledFromServer = DeviceConfig.getBoolean( 117 DeviceConfig.NAMESPACE_TELEPHONY, KEY_IS_TELEPHONY_ANOMALY_REPORT_ENABLED, 118 false); 119 if (!isAnomalyReportEnabledFromServer) return; 120 } catch (Exception e) { 121 Rlog.w(TAG, "Unable to read device config, dropping event=" + eventId); 122 return; 123 } 124 125 TelephonyStatsLog.write( 126 TELEPHONY_ANOMALY_DETECTED, 127 carrierId, 128 eventId.getLeastSignificantBits(), 129 eventId.getMostSignificantBits()); 130 131 // If this event has already occurred, skip sending intents for it; regardless log its 132 // invocation here. 133 Integer count = sEvents.containsKey(eventId) ? sEvents.get(eventId) + 1 : 1; 134 sEvents.put(eventId, count); 135 if (count > 1) return; 136 137 // Even if we are initialized, that doesn't mean that a package name has been found. 138 // This is normal in many cases, such as when no debug package is installed on the system, 139 // so drop these events silently. 140 if (sDebugPackageName == null) return; 141 142 Intent dbgIntent = new Intent(TelephonyManager.ACTION_ANOMALY_REPORTED); 143 dbgIntent.putExtra(TelephonyManager.EXTRA_ANOMALY_ID, new ParcelUuid(eventId)); 144 if (description != null) { 145 dbgIntent.putExtra(TelephonyManager.EXTRA_ANOMALY_DESCRIPTION, description); 146 } 147 dbgIntent.setPackage(sDebugPackageName); 148 sContext.sendBroadcast(dbgIntent, android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE); 149 } 150 151 /** 152 * Initialize the AnomalyReporter with the current context. 153 * 154 * This method must be invoked before any calls to reportAnomaly() will succeed. This method 155 * should only be invoked at most once. 156 * 157 * @param context a Context object used to initialize this singleton AnomalyReporter in 158 * the current process. 159 */ 160 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) initialize(@onNull Context context)161 public static void initialize(@NonNull Context context) { 162 if (context == null) { 163 throw new IllegalArgumentException("AnomalyReporter needs a non-null context."); 164 } 165 166 // Ensure that this context has sufficient permissions to send debug events. 167 context.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, 168 "This app does not have privileges to send debug events"); 169 170 sContext = context; 171 172 // Check to see if there is a valid debug package; if there are multiple, that's a config 173 // error, so just take the first one. 174 PackageManager pm = sContext.getPackageManager(); 175 if (pm == null) return; 176 List<ResolveInfo> packages = pm.queryBroadcastReceivers( 177 new Intent(TelephonyManager.ACTION_ANOMALY_REPORTED), 178 PackageManager.MATCH_SYSTEM_ONLY 179 | PackageManager.MATCH_DIRECT_BOOT_AWARE 180 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); 181 if (packages == null || packages.isEmpty()) return; 182 if (packages.size() > 1) { 183 Rlog.e(TAG, "Multiple Anomaly Receivers installed."); 184 } 185 186 for (ResolveInfo r : packages) { 187 if (r.activityInfo == null 188 || pm.checkPermission( 189 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, 190 r.activityInfo.packageName) 191 != PackageManager.PERMISSION_GRANTED) { 192 Rlog.w(TAG, 193 "Found package without proper permissions or no activity" 194 + r.activityInfo.packageName); 195 continue; 196 } 197 Rlog.d(TAG, "Found a valid package " + r.activityInfo.packageName); 198 sDebugPackageName = r.activityInfo.packageName; 199 break; 200 } 201 // Initialization may only be performed once. 202 } 203 204 /** Dump the contents of the AnomalyReporter */ dump(FileDescriptor fd, PrintWriter printWriter, String[] args)205 public static void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) { 206 if (sContext == null) return; 207 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " "); 208 sContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, "Requires DUMP"); 209 pw.println("Initialized=" + (sContext != null ? "Yes" : "No")); 210 pw.println("Debug Package=" + sDebugPackageName); 211 pw.println("Anomaly Counts:"); 212 pw.increaseIndent(); 213 for (UUID event : sEvents.keySet()) { 214 pw.println(event + ": " + sEvents.get(event)); 215 } 216 pw.decreaseIndent(); 217 pw.flush(); 218 } 219 } 220