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 Rlog.i(TAG, "reportAnomaly: Received anomaly event report with eventId= " + eventId 109 + " and description= " + description); 110 if (sContext == null) { 111 Rlog.w(TAG, "AnomalyReporter not yet initialized, dropping event=" + eventId); 112 return; 113 } 114 115 //always write atoms to statsd 116 TelephonyStatsLog.write( 117 TELEPHONY_ANOMALY_DETECTED, 118 carrierId, 119 eventId.getLeastSignificantBits(), 120 eventId.getMostSignificantBits()); 121 122 // Don't report via Intent if the server-side flag isn't loaded, as it implies other anomaly 123 // report related config hasn't loaded. 124 try { 125 boolean isAnomalyReportEnabledFromServer = DeviceConfig.getBoolean( 126 DeviceConfig.NAMESPACE_TELEPHONY, KEY_IS_TELEPHONY_ANOMALY_REPORT_ENABLED, 127 false); 128 if (!isAnomalyReportEnabledFromServer) return; 129 } catch (Exception e) { 130 Rlog.w(TAG, "Unable to read device config, dropping event=" + eventId); 131 return; 132 } 133 134 // If this event has already occurred, skip sending intents for it; regardless log its 135 // invocation here. 136 Integer count = sEvents.containsKey(eventId) ? sEvents.get(eventId) + 1 : 1; 137 sEvents.put(eventId, count); 138 if (count > 1) return; 139 140 // Even if we are initialized, that doesn't mean that a package name has been found. 141 // This is normal in many cases, such as when no debug package is installed on the system, 142 // so drop these events silently. 143 if (sDebugPackageName == null) return; 144 145 Intent dbgIntent = new Intent(TelephonyManager.ACTION_ANOMALY_REPORTED); 146 dbgIntent.putExtra(TelephonyManager.EXTRA_ANOMALY_ID, new ParcelUuid(eventId)); 147 if (description != null) { 148 dbgIntent.putExtra(TelephonyManager.EXTRA_ANOMALY_DESCRIPTION, description); 149 } 150 dbgIntent.setPackage(sDebugPackageName); 151 sContext.sendBroadcast(dbgIntent, android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE); 152 } 153 154 /** 155 * Initialize the AnomalyReporter with the current context. 156 * 157 * This method must be invoked before any calls to reportAnomaly() will succeed. This method 158 * should only be invoked at most once. 159 * 160 * @param context a Context object used to initialize this singleton AnomalyReporter in 161 * the current process. 162 */ 163 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) initialize(@onNull Context context)164 public static void initialize(@NonNull Context context) { 165 if (context == null) { 166 throw new IllegalArgumentException("AnomalyReporter needs a non-null context."); 167 } 168 169 // Ensure that this context has sufficient permissions to send debug events. 170 context.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, 171 "This app does not have privileges to send debug events"); 172 173 sContext = context; 174 175 // Check to see if there is a valid debug package; if there are multiple, that's a config 176 // error, so just take the first one. 177 PackageManager pm = sContext.getPackageManager(); 178 if (pm == null) return; 179 List<ResolveInfo> packages = pm.queryBroadcastReceivers( 180 new Intent(TelephonyManager.ACTION_ANOMALY_REPORTED), 181 PackageManager.MATCH_SYSTEM_ONLY 182 | PackageManager.MATCH_DIRECT_BOOT_AWARE 183 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); 184 if (packages == null || packages.isEmpty()) return; 185 if (packages.size() > 1) { 186 Rlog.e(TAG, "Multiple Anomaly Receivers installed."); 187 } 188 189 for (ResolveInfo r : packages) { 190 if (r.activityInfo == null) { 191 Rlog.w(TAG, "Found package without activity"); 192 continue; 193 } else if (pm.checkPermission( 194 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, 195 r.activityInfo.packageName) 196 != PackageManager.PERMISSION_GRANTED) { 197 Rlog.w(TAG, "Found package without proper permissions" 198 + r.activityInfo.packageName); 199 continue; 200 } 201 Rlog.d(TAG, "Found a valid package " + r.activityInfo.packageName); 202 sDebugPackageName = r.activityInfo.packageName; 203 break; 204 } 205 // Initialization may only be performed once. 206 } 207 208 /** Dump the contents of the AnomalyReporter */ dump(FileDescriptor fd, PrintWriter printWriter, String[] args)209 public static void dump(FileDescriptor fd, PrintWriter printWriter, String[] args) { 210 if (sContext == null) return; 211 IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, " "); 212 sContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, "Requires DUMP"); 213 pw.println("Initialized=" + (sContext != null ? "Yes" : "No")); 214 pw.println("Debug Package=" + sDebugPackageName); 215 pw.println("Anomaly Counts:"); 216 pw.increaseIndent(); 217 for (UUID event : sEvents.keySet()) { 218 pw.println(event + ": " + sEvents.get(event)); 219 } 220 pw.decreaseIndent(); 221 pw.flush(); 222 } 223 } 224