Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 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 com.android.server.os; |
| 18 | |
| 19 | import android.annotation.RequiresPermission; |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 21 | import android.app.AppOpsManager; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 22 | import android.content.Context; |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 23 | import android.content.pm.UserInfo; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 24 | import android.os.Binder; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 25 | import android.os.BugreportParams; |
| 26 | import android.os.IDumpstate; |
| 27 | import android.os.IDumpstateListener; |
| 28 | import android.os.IDumpstateToken; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.os.SystemClock; |
| 32 | import android.os.SystemProperties; |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 33 | import android.os.UserManager; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 34 | import android.util.Slog; |
| 35 | |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 36 | import com.android.internal.annotations.GuardedBy; |
| 37 | import com.android.internal.util.Preconditions; |
| 38 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 39 | import java.io.FileDescriptor; |
| 40 | |
| 41 | // TODO(b/111441001): |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 42 | // Intercept onFinished() & implement death recipient here and shutdown |
| 43 | // bugreportd service. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Implementation of the service that provides a privileged API to capture and consume bugreports. |
| 47 | * |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 48 | * <p>Delegates the actualy generation to a native implementation of {@code IDumpstate}. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 49 | */ |
| 50 | class BugreportManagerServiceImpl extends IDumpstate.Stub { |
| 51 | private static final String TAG = "BugreportManagerService"; |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 52 | private static final String BUGREPORT_SERVICE = "bugreportd"; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 53 | private static final long DEFAULT_BUGREPORT_SERVICE_TIMEOUT_MILLIS = 30 * 1000; |
| 54 | |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 55 | private final Object mLock = new Object(); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 56 | private final Context mContext; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 57 | private final AppOpsManager mAppOps; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 58 | |
| 59 | BugreportManagerServiceImpl(Context context) { |
| 60 | mContext = context; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 61 | mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | @RequiresPermission(android.Manifest.permission.DUMP) |
| 66 | public IDumpstateToken setListener(String name, IDumpstateListener listener, |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 67 | boolean getSectionDetails) { |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 68 | throw new UnsupportedOperationException("setListener is not allowed on this service"); |
| 69 | } |
| 70 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 71 | @Override |
| 72 | @RequiresPermission(android.Manifest.permission.DUMP) |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 73 | public void startBugreport(int callingUidUnused, String callingPackage, |
| 74 | FileDescriptor bugreportFd, FileDescriptor screenshotFd, |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 75 | int bugreportMode, IDumpstateListener listener) { |
| 76 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, "startBugreport"); |
| 77 | Preconditions.checkNotNull(callingPackage); |
| 78 | Preconditions.checkNotNull(bugreportFd); |
| 79 | Preconditions.checkNotNull(listener); |
| 80 | validateBugreportMode(bugreportMode); |
| 81 | ensureIsPrimaryUser(); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 82 | |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 83 | int callingUid = Binder.getCallingUid(); |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 84 | mAppOps.checkPackage(callingUid, callingPackage); |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 85 | |
| 86 | synchronized (mLock) { |
| 87 | startBugreportLocked(callingUid, callingPackage, bugreportFd, screenshotFd, |
| 88 | bugreportMode, listener); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 89 | } |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 92 | @Override |
| 93 | @RequiresPermission(android.Manifest.permission.DUMP) |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 94 | public void cancelBugreport() { |
| 95 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, "startBugreport"); |
| 96 | // This tells init to cancel bugreportd service. Note that this is achieved through setting |
| 97 | // a system property which is not thread-safe. So the lock here offers thread-safety only |
| 98 | // among callers of the API. |
| 99 | synchronized (mLock) { |
| 100 | SystemProperties.set("ctl.stop", BUGREPORT_SERVICE); |
| 101 | } |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 104 | private void validateBugreportMode(@BugreportParams.BugreportMode int mode) { |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 105 | if (mode != BugreportParams.BUGREPORT_MODE_FULL |
| 106 | && mode != BugreportParams.BUGREPORT_MODE_INTERACTIVE |
| 107 | && mode != BugreportParams.BUGREPORT_MODE_REMOTE |
| 108 | && mode != BugreportParams.BUGREPORT_MODE_WEAR |
| 109 | && mode != BugreportParams.BUGREPORT_MODE_TELEPHONY |
| 110 | && mode != BugreportParams.BUGREPORT_MODE_WIFI) { |
| 111 | Slog.w(TAG, "Unknown bugreport mode: " + mode); |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 112 | throw new IllegalArgumentException("Unknown bugreport mode: " + mode); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 113 | } |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Validates that the current user is the primary user. |
| 118 | * |
| 119 | * @throws IllegalArgumentException if the current user is not the primary user |
| 120 | */ |
| 121 | private void ensureIsPrimaryUser() { |
| 122 | UserInfo currentUser = null; |
| 123 | try { |
| 124 | currentUser = ActivityManager.getService().getCurrentUser(); |
| 125 | } catch (RemoteException e) { |
| 126 | // Impossible to get RemoteException for an in-process call. |
| 127 | } |
| 128 | |
| 129 | UserInfo primaryUser = UserManager.get(mContext).getPrimaryUser(); |
| 130 | if (currentUser == null) { |
| 131 | logAndThrow("No current user. Only primary user is allowed to take bugreports."); |
| 132 | } |
| 133 | if (primaryUser == null) { |
| 134 | logAndThrow("No primary user. Only primary user is allowed to take bugreports."); |
| 135 | } |
| 136 | if (primaryUser.id != currentUser.id) { |
| 137 | logAndThrow("Current user not primary user. Only primary user" |
| 138 | + " is allowed to take bugreports."); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @GuardedBy("mLock") |
| 143 | private void startBugreportLocked(int callingUid, String callingPackage, |
| 144 | FileDescriptor bugreportFd, FileDescriptor screenshotFd, |
| 145 | int bugreportMode, IDumpstateListener listener) { |
| 146 | if (isDumpstateBinderServiceRunningLocked()) { |
| 147 | Slog.w(TAG, "'dumpstate' is already running. Cannot start a new bugreport" |
| 148 | + " while another one is currently in progress."); |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 149 | reportError(listener, |
| 150 | IDumpstateListener.BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS); |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
| 154 | IDumpstate ds = startAndGetDumpstateBinderServiceLocked(); |
| 155 | if (ds == null) { |
| 156 | Slog.w(TAG, "Unable to get bugreport service"); |
| 157 | reportError(listener, IDumpstateListener.BUGREPORT_ERROR_RUNTIME_ERROR); |
| 158 | return; |
| 159 | } |
| 160 | try { |
| 161 | ds.startBugreport(callingUid, callingPackage, |
| 162 | bugreportFd, screenshotFd, bugreportMode, listener); |
| 163 | } catch (RemoteException e) { |
| 164 | reportError(listener, IDumpstateListener.BUGREPORT_ERROR_RUNTIME_ERROR); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @GuardedBy("mLock") |
| 169 | private boolean isDumpstateBinderServiceRunningLocked() { |
| 170 | IDumpstate ds = IDumpstate.Stub.asInterface(ServiceManager.getService("dumpstate")); |
| 171 | return ds != null; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Start and get a handle to the native implementation of {@code IDumpstate} which does the |
| 176 | * actual bugreport generation. |
| 177 | * |
| 178 | * <p>Generating bugreports requires root privileges. To limit the footprint |
| 179 | * of the root access, the actual generation in Dumpstate binary is accessed as a |
| 180 | * oneshot service 'bugreport'. |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 181 | * |
| 182 | * <p>Note that starting the service is achieved through setting a system property, which is |
| 183 | * not thread-safe. So the lock here offers thread-safety only among callers of the API. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 184 | */ |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 185 | @GuardedBy("mLock") |
| 186 | private IDumpstate startAndGetDumpstateBinderServiceLocked() { |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 187 | // Start bugreport service. |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 188 | SystemProperties.set("ctl.start", BUGREPORT_SERVICE); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 189 | |
| 190 | IDumpstate ds = null; |
| 191 | boolean timedOut = false; |
| 192 | int totalTimeWaitedMillis = 0; |
| 193 | int seedWaitTimeMillis = 500; |
| 194 | while (!timedOut) { |
| 195 | // Note that the binder service on the native side is "dumpstate". |
| 196 | ds = IDumpstate.Stub.asInterface(ServiceManager.getService("dumpstate")); |
| 197 | if (ds != null) { |
| 198 | Slog.i(TAG, "Got bugreport service handle."); |
| 199 | break; |
| 200 | } |
| 201 | SystemClock.sleep(seedWaitTimeMillis); |
| 202 | Slog.i(TAG, |
| 203 | "Waiting to get dumpstate service handle (" + totalTimeWaitedMillis + "ms)"); |
| 204 | totalTimeWaitedMillis += seedWaitTimeMillis; |
| 205 | seedWaitTimeMillis *= 2; |
| 206 | timedOut = totalTimeWaitedMillis > DEFAULT_BUGREPORT_SERVICE_TIMEOUT_MILLIS; |
| 207 | } |
| 208 | if (timedOut) { |
| 209 | Slog.w(TAG, |
| 210 | "Timed out waiting to get dumpstate service handle (" |
| 211 | + totalTimeWaitedMillis + "ms)"); |
| 212 | } |
| 213 | return ds; |
| 214 | } |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 215 | |
| 216 | private void reportError(IDumpstateListener listener, int errorCode) { |
| 217 | try { |
| 218 | listener.onError(errorCode); |
| 219 | } catch (RemoteException e) { |
| 220 | // Something went wrong in binder or app process. There's nothing to do here. |
| 221 | Slog.w(TAG, "onError() transaction threw RemoteException: " + e.getMessage()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private void logAndThrow(String message) { |
| 226 | Slog.w(TAG, message); |
| 227 | throw new IllegalArgumentException(message); |
| 228 | } |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 229 | } |