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