blob: 387d7a85f4a8f5b3c22d449a293151634658b755 [file] [log] [blame]
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001/*
2 * Copyright (C) 2018 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
Kevin Chyn2ffadb32018-06-19 11:29:38 -070017package com.android.server.biometrics.face;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020018
19import static android.Manifest.permission.INTERACT_ACROSS_USERS;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070020import static android.Manifest.permission.MANAGE_BIOMETRIC;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020021import static android.Manifest.permission.RESET_FACE_LOCKOUT;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070022import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020023
24import android.app.ActivityManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020025import android.app.AppOpsManager;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070026import android.app.Notification;
27import android.app.NotificationChannel;
28import android.app.NotificationManager;
29import android.app.PendingIntent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020030import android.content.Context;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070031import android.content.Intent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020032import android.content.pm.UserInfo;
Kevin Chyna56dff72018-06-19 18:41:12 -070033import android.hardware.biometrics.BiometricAuthenticator;
34import android.hardware.biometrics.BiometricConstants;
Kevin Chyn7782d142019-01-18 12:51:33 -080035import android.hardware.biometrics.BiometricsProtoEnums;
Kevin Chyna56dff72018-06-19 18:41:12 -070036import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Kevin Chyn23289ef2018-11-28 16:32:36 -080037import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020038import android.hardware.biometrics.face.V1_0.IBiometricsFace;
39import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
Kevin Chynb95f1522019-03-04 16:45:15 -080040import android.hardware.biometrics.face.V1_0.OptionalBool;
Kevin Chynd79e24e2018-09-25 12:06:59 -070041import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020042import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070043import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020044import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020045import android.hardware.face.IFaceServiceReceiver;
46import android.os.Binder;
Joe Onoratobf955d22019-03-25 00:16:58 -070047import android.os.Build;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020048import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020049import android.os.IBinder;
Joe Onoratodb396002019-04-05 19:49:27 -070050import android.os.NativeHandle;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020051import android.os.RemoteException;
52import android.os.SELinux;
Joe Onorato108413a2019-04-03 18:20:52 -070053import android.os.SystemProperties;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020054import android.os.UserHandle;
55import android.os.UserManager;
Curtis Belmonte8cfe3712019-09-26 16:02:41 -070056import android.provider.Settings;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020057import android.util.Slog;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020058
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070059import com.android.internal.R;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020060import com.android.internal.annotations.GuardedBy;
61import com.android.internal.logging.MetricsLogger;
62import com.android.internal.util.DumpUtils;
63import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080064import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070065import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070066import com.android.server.biometrics.BiometricUtils;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -070067import com.android.server.biometrics.ClientMonitor;
Kevin Chyn4cc49f72019-04-24 13:53:35 -070068import com.android.server.biometrics.Constants;
Kevin Chyn0ce70852019-05-10 10:29:18 -070069import com.android.server.biometrics.EnumerateClient;
Kevin Chyn6737c572019-02-08 16:10:54 -080070import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020071
72import org.json.JSONArray;
73import org.json.JSONException;
74import org.json.JSONObject;
75
76import java.io.File;
77import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070078import java.io.FileOutputStream;
79import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020080import java.io.PrintWriter;
81import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070082import java.util.Arrays;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070083import java.util.HashMap;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020084import java.util.List;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070085import java.util.Map;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020086
87/**
88 * A service to manage multiple clients that want to access the face HAL API.
89 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080090 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020091 *
92 * @hide
93 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070094public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070095
96 protected static final String TAG = "FaceService";
97 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020098 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020099 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -0700100 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -0700101 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200102
Kevin Chyndbfbed42019-06-13 17:01:30 -0700103 private static final String NOTIFICATION_TAG = "FaceService";
104 private static final int NOTIFICATION_ID = 1;
105
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700106 /**
107 * Events for bugreports.
108 */
109 public static final class AuthenticationEvent {
110 private long mStartTime;
111 private long mLatency;
112 // Only valid if mError is 0
113 private boolean mAuthenticated;
114 private int mError;
115 // Only valid if mError is ERROR_VENDOR
116 private int mVendorError;
117
118 AuthenticationEvent(long startTime, long latency, boolean authenticated, int error,
119 int vendorError) {
120 mStartTime = startTime;
121 mLatency = latency;
122 mAuthenticated = authenticated;
123 mError = error;
124 mVendorError = vendorError;
125 }
126
127 public String toString(Context context) {
128 return "Start: " + mStartTime
129 + "\tLatency: " + mLatency
130 + "\tAuthenticated: " + mAuthenticated
131 + "\tError: " + mError
132 + "\tVendorCode: " + mVendorError
133 + "\t" + FaceManager.getErrorString(context, mError, mVendorError);
134 }
135 }
136
137 /**
138 * Keep a short historical buffer of stats, with an aggregated usage time.
139 */
140 private class UsageStats {
141 static final int EVENT_LOG_SIZE = 100;
142
143 Context mContext;
144 List<AuthenticationEvent> mAuthenticationEvents;
145
146 int acceptCount;
147 int rejectCount;
148 Map<Integer, Integer> mErrorCount;
149
150 long acceptLatency;
151 long rejectLatency;
152 Map<Integer, Long> mErrorLatency;
153
154 UsageStats(Context context) {
155 mAuthenticationEvents = new ArrayList<>();
156 mErrorCount = new HashMap<>();
157 mErrorLatency = new HashMap<>();
158 mContext = context;
159 }
160
161 void addEvent(AuthenticationEvent event) {
162 if (mAuthenticationEvents.size() >= EVENT_LOG_SIZE) {
163 mAuthenticationEvents.remove(0);
164 }
165 mAuthenticationEvents.add(event);
166
167 if (event.mAuthenticated) {
168 acceptCount++;
169 acceptLatency += event.mLatency;
170 } else if (event.mError == 0) {
171 rejectCount++;
172 rejectLatency += event.mLatency;
173 } else {
174 mErrorCount.put(event.mError, mErrorCount.getOrDefault(event.mError, 0) + 1);
175 mErrorLatency.put(event.mError,
176 mErrorLatency.getOrDefault(event.mError, 0l) + event.mLatency);
177 }
178 }
179
180 void print(PrintWriter pw) {
181 pw.println("Events since last reboot: " + mAuthenticationEvents.size());
182 for (int i = 0; i < mAuthenticationEvents.size(); i++) {
183 pw.println(mAuthenticationEvents.get(i).toString(mContext));
184 }
185
186 // Dump aggregated usage stats
187 // TODO: Remove or combine with json dump in a future release
188 pw.println("Accept\tCount: " + acceptCount + "\tLatency: " + acceptLatency
189 + "\tAverage: " + (acceptCount > 0 ? acceptLatency / acceptCount : 0));
190 pw.println("Reject\tCount: " + rejectCount + "\tLatency: " + rejectLatency
191 + "\tAverage: " + (rejectCount > 0 ? rejectLatency / rejectCount : 0));
192
193 for (Integer key : mErrorCount.keySet()) {
194 final int count = mErrorCount.get(key);
195 pw.println("Error" + key + "\tCount: " + count
196 + "\tLatency: " + mErrorLatency.getOrDefault(key, 0l)
197 + "\tAverage: " + (count > 0 ? mErrorLatency.getOrDefault(key, 0l) / count
198 : 0)
199 + "\t" + FaceManager.getErrorString(mContext, key, 0 /* vendorCode */));
200 }
201 }
202 }
203
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700204 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700205 private int mLastAcquire;
206
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700207 public FaceAuthClient(Context context,
208 DaemonWrapper daemon, long halDeviceId, IBinder token,
209 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800210 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700211 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800212 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700213 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800214
215 @Override
216 protected int statsModality() {
217 return FaceService.this.statsModality();
218 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800219
220 @Override
221 public boolean shouldFrameworkHandleLockout() {
222 return false;
223 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800224
225 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700226 public boolean wasUserDetected() {
Ilya Matyukhinc90507e2019-08-20 15:38:57 -0700227 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED
228 && mLastAcquire != FaceManager.FACE_ACQUIRED_SENSOR_DIRTY;
Kevin Chyn0ce70852019-05-10 10:29:18 -0700229 }
230
231 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800232 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
233 boolean authenticated, ArrayList<Byte> token) {
234 final boolean result = super.onAuthenticated(identifier, authenticated, token);
235
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700236 mUsageStats.addEvent(new AuthenticationEvent(
237 getStartTimeMs(),
238 System.currentTimeMillis() - getStartTimeMs() /* latency */,
239 authenticated,
240 0 /* error */,
241 0 /* vendorError */));
242
Kevin Chyn56d6b072019-02-13 18:39:01 -0800243 // For face, the authentication lifecycle ends either when
244 // 1) Authenticated == true
245 // 2) Error occurred
246 // 3) Authenticated == false
247 // Fingerprint currently does not end when the third condition is met which is a bug,
248 // but let's leave it as-is for now.
249 return result || !authenticated;
250 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700251
252 @Override
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700253 public boolean onError(long deviceId, int error, int vendorCode) {
254 mUsageStats.addEvent(new AuthenticationEvent(
255 getStartTimeMs(),
256 System.currentTimeMillis() - getStartTimeMs() /* latency */,
257 false /* authenticated */,
258 error,
259 vendorCode));
260
261 return super.onError(deviceId, error, vendorCode);
262 }
263
264 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700265 public int[] getAcquireIgnorelist() {
266 if (isBiometricPrompt()) {
267 return mBiometricPromptIgnoreList;
268 } else {
269 // Keyguard
270 return mKeyguardIgnoreList;
271 }
272 }
273
274 @Override
275 public int[] getAcquireVendorIgnorelist() {
276 if (isBiometricPrompt()) {
277 return mBiometricPromptIgnoreListVendor;
278 } else {
279 // Keyguard
280 return mKeyguardIgnoreListVendor;
281 }
282 }
283
284 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700285 public boolean onAcquired(int acquireInfo, int vendorCode) {
286
Kevin Chyn0ce70852019-05-10 10:29:18 -0700287 mLastAcquire = acquireInfo;
288
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700289 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
290 final String name =
291 getContext().getString(R.string.face_recalibrate_notification_name);
292 final String title =
293 getContext().getString(R.string.face_recalibrate_notification_title);
294 final String content =
295 getContext().getString(R.string.face_recalibrate_notification_content);
296
297 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
298 intent.setPackage("com.android.settings");
299
300 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
301 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
302 UserHandle.CURRENT);
303
Kevin Chyndbfbed42019-06-13 17:01:30 -0700304 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700305
Kevin Chyndbfbed42019-06-13 17:01:30 -0700306 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700307 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700308 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700309 .setSmallIcon(R.drawable.ic_lock)
310 .setContentTitle(title)
311 .setContentText(content)
312 .setSubText(name)
313 .setOnlyAlertOnce(true)
314 .setLocalOnly(true)
315 .setAutoCancel(true)
316 .setCategory(Notification.CATEGORY_SYSTEM)
317 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700318 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700319 .build();
320
Kevin Chyndbfbed42019-06-13 17:01:30 -0700321 mNotificationManager.createNotificationChannel(channel);
322 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
323 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700324 }
325
326 return super.onAcquired(acquireInfo, vendorCode);
327 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700328 }
329
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200330 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700331 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200332 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200333 private final class FaceServiceWrapper extends IFaceService.Stub {
Curtis Belmonte45788542019-07-23 11:07:27 -0700334 private static final int ENROLL_TIMEOUT_SEC = 75;
Kevin Chyna56dff72018-06-19 18:41:12 -0700335
336 /**
337 * The following methods contain common code which is shared in biometrics/common.
338 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800339
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200340 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700341 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700342 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700343 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200344 }
345
346 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700347 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700348 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn7a207e42019-07-27 17:50:33 -0700349 mHandler.post(() -> {
350 // TODO(b/137106905): Schedule binder calls in FaceService to avoid deadlocks.
351 if (getCurrentClient() == null) {
352 // if we aren't handling any other HIDL calls (mCurrentClient == null), revoke
353 // the challenge right away.
354 startRevokeChallenge(token);
355 } else {
356 // postpone revoking the challenge until we finish processing the current HIDL
357 // call.
358 mRevokeChallengePending = true;
359 }
360 });
361 return Status.OK;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200362 }
363
364 @Override // Binder call
Kevin Chyn593e6262019-06-28 13:24:44 -0700365 public void enroll(int userId, final IBinder token, final byte[] cryptoToken,
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800366 final IFaceServiceReceiver receiver, final String opPackageName,
367 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700368 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700369 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200370
Kevin Chyndbfbed42019-06-13 17:01:30 -0700371 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
372 UserHandle.CURRENT);
373
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200374 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700375 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
376 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Curtis Belmonte45788542019-07-23 11:07:27 -0700377 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures,
378 ENROLL_TIMEOUT_SEC) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700379
380 @Override
381 public int[] getAcquireIgnorelist() {
382 return mEnrollIgnoreList;
383 }
384
385 @Override
386 public int[] getAcquireVendorIgnorelist() {
387 return mEnrollIgnoreListVendor;
388 }
389
Kevin Chyn1429a312019-01-28 16:08:09 -0800390 @Override
391 public boolean shouldVibrate() {
392 return false;
393 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800394
395 @Override
396 protected int statsModality() {
397 return FaceService.this.statsModality();
398 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800399 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200400
Kevin Chyn1a878c12019-04-04 15:50:11 -0700401 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200402 }
403
404 @Override // Binder call
405 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700406 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700407 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200408 }
409
410 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800411 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700412 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700413 final String opPackageName) {
414 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800415 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200416 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700417 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700418 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700419 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800420 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700421 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200422 }
423
424 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800425 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800426 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
427 String opPackageName, int cookie, int callingUid, int callingPid,
428 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700429 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800430 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700431 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700432 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700433 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800434 new BiometricPromptServiceListenerImpl(wrapperReceiver),
435 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800436 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700437 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
438 callingUserId);
439 }
440
441 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800442 public void startPreparedClient(int cookie) {
443 checkPermission(MANAGE_BIOMETRIC);
444 startCurrentClient(cookie);
445 }
446
447 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200448 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700449 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700450 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200451 }
452
453 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700454 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800455 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700456 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800457 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
458 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700459 }
460
461 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200462 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700463 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700464 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200465 }
466
467 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700468 public void remove(final IBinder token, final int faceId, final int userId,
Kevin Chyn593e6262019-06-28 13:24:44 -0700469 final IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700470 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700471 updateActiveGroup(userId, opPackageName);
Kevin Chyna56dff72018-06-19 18:41:12 -0700472
473 if (token == null) {
474 Slog.w(TAG, "remove(): token is null");
475 return;
476 }
477
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200478 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700479 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800480 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
481 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800482 @Override
483 protected int statsModality() {
484 return FaceService.this.statsModality();
485 }
486 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700487 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200488 }
489
Kevin Chyna56dff72018-06-19 18:41:12 -0700490 @Override
491 public void enumerate(final IBinder token, final int userId,
492 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700493 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700494
495 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700496 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800497 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
498 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800499 @Override
500 protected int statsModality() {
501 return FaceService.this.statsModality();
502 }
503 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700504 enumerateInternal(client);
505 }
506
507 @Override
508 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
509 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700510 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700511 FaceService.super.addLockoutResetCallback(callback);
512 }
513
514 @Override // Binder call
515 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
516 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
517 return;
518 }
519
520 final long ident = Binder.clearCallingIdentity();
521 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700522 if (args.length > 1 && "--hal".equals(args[0])) {
523 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Kevin Chyna56dff72018-06-19 18:41:12 -0700524 } else {
525 dumpInternal(pw);
526 }
527 } finally {
528 Binder.restoreCallingIdentity(ident);
529 }
530 }
531
532 /**
533 * The following methods don't use any common code from BiometricService
534 */
535
536 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200537 @Override // Binder call
538 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700539 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700540 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200541 Binder.getCallingUid(), Binder.getCallingPid(),
542 UserHandle.getCallingUserId())) {
543 return false;
544 }
545
546 final long token = Binder.clearCallingIdentity();
547 try {
548 IBiometricsFace daemon = getFaceDaemon();
549 return daemon != null && mHalDeviceId != 0;
550 } finally {
551 Binder.restoreCallingIdentity(token);
552 }
553 }
554
555 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700556 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700557 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700558 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
559 return;
560 }
561 mHandler.post(new Runnable() {
562 @Override
563 public void run() {
564 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
565 faceId, name);
566 }
567 });
568 }
569
570 @Override // Binder call
571 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700572 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700573 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200574 Binder.getCallingUid(), Binder.getCallingPid(),
575 UserHandle.getCallingUserId())) {
576 return null;
577 }
578
Kevin Chyn6737c572019-02-08 16:10:54 -0800579 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200580 }
581
582 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700583 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700584 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700585 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200586 Binder.getCallingUid(), Binder.getCallingPid(),
587 UserHandle.getCallingUserId())) {
588 return false;
589 }
590
Kevin Chyna56dff72018-06-19 18:41:12 -0700591 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200592 }
593
594 @Override // Binder call
595 public long getAuthenticatorId(String opPackageName) {
596 // In this method, we're not checking whether the caller is permitted to use face
597 // API because current authenticator ID is leaked (in a more contrived way) via Android
598 // Keystore (android.security.keystore package): the user of that API can create a key
599 // which requires face authentication for its use, and then query the key's
600 // characteristics (hidden API) which returns, among other things, face
601 // authenticator ID which was active at key creation time.
602 //
603 // Reason: The part of Android Keystore which runs inside an app's process invokes this
604 // method in certain cases. Those cases are not always where the developer demonstrates
605 // explicit intent to use face functionality. Thus, to avoiding throwing an
606 // unexpected SecurityException this method does not check whether its caller is
607 // permitted to use face API.
608 //
609 // The permission check should be restored once Android Keystore no longer invokes this
610 // method from inside app processes.
611
612 return FaceService.this.getAuthenticatorId(opPackageName);
613 }
614
615 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800616 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700617 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700618
joshmccloskeya4772062019-07-24 17:13:16 -0700619 mHandler.post(() -> {
620 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
621 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
622 return;
623 }
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700624
joshmccloskeya4772062019-07-24 17:13:16 -0700625 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
Kevin Chynbe67ce02019-06-10 16:14:22 -0700626
joshmccloskeya4772062019-07-24 17:13:16 -0700627 try {
628 mDaemonWrapper.resetLockout(token);
629 } catch (RemoteException e) {
630 Slog.e(getTag(), "Unable to reset lockout", e);
631 }
632 });
Kevin Chyna56dff72018-06-19 18:41:12 -0700633 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700634
635 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700636 public void setFeature(int userId, int feature, boolean enabled, final byte[] token,
637 IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700638 checkPermission(MANAGE_BIOMETRIC);
639
Kevin Chyne62749a2019-04-02 19:33:56 -0700640 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700641 if (DEBUG) {
642 Slog.d(TAG, "setFeature for user(" + userId + ")");
643 }
644 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700645 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
646 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
647 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800648 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700649
650 final ArrayList<Byte> byteToken = new ArrayList<>();
651 for (int i = 0; i < token.length; i++) {
652 byteToken.add(token[i]);
653 }
654
655 // TODO: Support multiple faces
656 final int faceId = getFirstTemplateForUser(mCurrentUserId);
657
658 if (mDaemon != null) {
659 try {
660 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
661 receiver.onFeatureSet(result == Status.OK, feature);
662 } catch (RemoteException e) {
663 Slog.e(getTag(), "Unable to set feature: " + feature
664 + " to enabled:" + enabled, e);
665 }
666 }
667 });
668
Kevin Chynd79e24e2018-09-25 12:06:59 -0700669 }
670
671 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700672 public void getFeature(int userId, int feature, IFaceServiceReceiver receiver,
673 final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700674 checkPermission(MANAGE_BIOMETRIC);
675
Kevin Chyne62749a2019-04-02 19:33:56 -0700676 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700677 if (DEBUG) {
678 Slog.d(TAG, "getFeature for user(" + userId + ")");
679 }
680 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700681 // This should ideally return tri-state, but the user isn't shown settings unless
682 // they are enrolled so it's fine for now.
683 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
684 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
685 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800686 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700687
688 // TODO: Support multiple faces
689 final int faceId = getFirstTemplateForUser(mCurrentUserId);
690
691 if (mDaemon != null) {
692 try {
693 OptionalBool result = mDaemon.getFeature(feature, faceId);
694 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
695 } catch (RemoteException e) {
696 Slog.e(getTag(), "Unable to getRequireAttention", e);
697 }
698 }
699 });
700
Kevin Chynd79e24e2018-09-25 12:06:59 -0700701 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700702
703 @Override
704 public void userActivity() {
705 checkPermission(MANAGE_BIOMETRIC);
706
707 if (mDaemon != null) {
708 try {
709 mDaemon.userActivity();
710 } catch (RemoteException e) {
711 Slog.e(getTag(), "Unable to send userActivity", e);
712 }
713 }
714 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800715
716 // TODO: Support multiple faces
717 private int getFirstTemplateForUser(int user) {
718 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
719 if (!faces.isEmpty()) {
720 return faces.get(0).getBiometricId();
721 }
722 return 0;
723 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700724 }
725
726 /**
727 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700728 * BiometricPrompt.
729 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800730 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800731 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800732 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700733 }
734
735 @Override
736 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
737 throws RemoteException {
738 /**
739 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
740 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800741 if (getWrapperReceiver() != null) {
742 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700743 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
744 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700745 }
746 }
747
748 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800749 public void onError(long deviceId, int error, int vendorCode, int cookie)
750 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800751 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800752 getWrapperReceiver().onError(cookie, error,
753 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700754 }
755 }
756 }
757
758 /**
759 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700760 * the FaceManager.
761 */
762 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700763 private IFaceServiceReceiver mFaceServiceReceiver;
764
765 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
766 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200767 }
768
769 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700770 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200771 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700772 if (mFaceServiceReceiver != null) {
773 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
774 identifier.getBiometricId(),
775 remaining);
776 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200777 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700778
779 @Override
780 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
781 throws RemoteException {
782 if (mFaceServiceReceiver != null) {
783 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
784 }
785 }
786
787 @Override
788 public void onAuthenticationSucceeded(long deviceId,
789 BiometricAuthenticator.Identifier biometric, int userId)
790 throws RemoteException {
791 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800792 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700793 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
794 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700795 } else {
796 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
797 }
798 }
799 }
800
801 @Override
802 public void onAuthenticationFailed(long deviceId) throws RemoteException {
803 if (mFaceServiceReceiver != null) {
804 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
805 }
806 }
807
808 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800809 public void onError(long deviceId, int error, int vendorCode, int cookie)
810 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700811 if (mFaceServiceReceiver != null) {
812 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
813 }
814 }
815
816 @Override
817 public void onRemoved(BiometricAuthenticator.Identifier identifier,
818 int remaining) throws RemoteException {
819 if (mFaceServiceReceiver != null) {
820 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
821 identifier.getBiometricId(), remaining);
822 }
823 }
824
825 @Override
826 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
827 throws RemoteException {
828 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800829 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
830 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700831 }
832 }
833 }
834
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700835 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700836
837 @GuardedBy("this")
838 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700839 private UsageStats mUsageStats;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -0700840 private boolean mRevokeChallengePending = false;
Kevin Chyna38653c2019-02-11 17:46:21 -0800841 // One of the AuthenticationClient constants
842 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700843
Kevin Chyndbfbed42019-06-13 17:01:30 -0700844 private NotificationManager mNotificationManager;
845
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700846 private int[] mBiometricPromptIgnoreList;
847 private int[] mBiometricPromptIgnoreListVendor;
848 private int[] mKeyguardIgnoreList;
849 private int[] mKeyguardIgnoreListVendor;
850 private int[] mEnrollIgnoreList;
851 private int[] mEnrollIgnoreListVendor;
852
Kevin Chyna56dff72018-06-19 18:41:12 -0700853 /**
854 * Receives callbacks from the HAL.
855 */
856 private IBiometricsFaceClientCallback mDaemonCallback =
857 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800858 @Override
859 public void onEnrollResult(final long deviceId, int faceId, int userId,
860 int remaining) {
861 mHandler.post(() -> {
862 final Face face = new Face(getBiometricUtils()
863 .getUniqueName(getContext(), userId), faceId, deviceId);
864 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700865
866 // Enrollment changes the authenticatorId, so update it here.
867 IBiometricsFace daemon = getFaceDaemon();
868 if (remaining == 0 && daemon != null) {
869 try {
870 mAuthenticatorIds.put(userId,
871 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
872 : 0L);
873 } catch (RemoteException e) {
874 Slog.e(TAG, "Unable to get authenticatorId", e);
875 }
876 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800877 });
878 }
879
880 @Override
881 public void onAcquired(final long deviceId, final int userId,
882 final int acquiredInfo,
883 final int vendorCode) {
884 mHandler.post(() -> {
885 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
886 });
887 }
888
889 @Override
890 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
891 ArrayList<Byte> token) {
892 mHandler.post(() -> {
893 Face face = new Face("", faceId, deviceId);
894 FaceService.super.handleAuthenticated(face, token);
895 });
896 }
897
898 @Override
899 public void onError(final long deviceId, final int userId, final int error,
900 final int vendorCode) {
901 mHandler.post(() -> {
902 FaceService.super.handleError(deviceId, error, vendorCode);
903
904 // TODO: this chunk of code should be common to all biometric services
905 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
906 // If we get HW_UNAVAILABLE, try to connect again later...
907 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
908 synchronized (this) {
909 mDaemon = null;
910 mHalDeviceId = 0;
911 mCurrentUserId = UserHandle.USER_NULL;
912 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700913 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800914 });
915 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700916
Kevin Chyn6737c572019-02-08 16:10:54 -0800917 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700918 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800919 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700920 if (!faceIds.isEmpty()) {
921 for (int i = 0; i < faceIds.size(); i++) {
922 final Face face = new Face("", faceIds.get(i), deviceId);
923 // Convert to old behavior
924 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
925 }
926 } else {
927 final Face face = new Face("", 0 /* identifier */, deviceId);
928 FaceService.super.handleRemoved(face, 0 /* remaining */);
929 }
Curtis Belmonte8cfe3712019-09-26 16:02:41 -0700930 Settings.Secure.putIntForUser(getContext().getContentResolver(),
931 Settings.Secure.FACE_UNLOCK_RE_ENROLL, 0, UserHandle.USER_CURRENT);
Kevin Chyn6737c572019-02-08 16:10:54 -0800932 });
933 }
934
935 @Override
936 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
937 throws RemoteException {
938 mHandler.post(() -> {
939 if (!faceIds.isEmpty()) {
940 for (int i = 0; i < faceIds.size(); i++) {
941 final Face face = new Face("", faceIds.get(i), deviceId);
942 // Convert to old old behavior
943 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
944 }
945 } else {
946 // For face, the HIDL contract is to receive an empty list when there are no
947 // templates enrolled. Send a null identifier since we don't consume them
948 // anywhere, and send remaining == 0 to plumb this with existing common code.
949 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700950 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800951 });
952 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700953
Kevin Chyn6737c572019-02-08 16:10:54 -0800954 @Override
955 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800956 Slog.d(TAG, "onLockoutChanged: " + duration);
957 if (duration == 0) {
958 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
959 } else if (duration == Long.MAX_VALUE) {
960 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
961 } else {
962 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
963 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700964
Kevin Chyna38653c2019-02-11 17:46:21 -0800965 mHandler.post(() -> {
966 if (duration == 0) {
967 notifyLockoutResetMonitors();
968 }
969 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800970 }
971 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700972
973 /**
974 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
975 * can be shared between the multiple biometric services.
976 */
977 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
978 @Override
979 public int authenticate(long operationId, int groupId) throws RemoteException {
980 IBiometricsFace daemon = getFaceDaemon();
981 if (daemon == null) {
982 Slog.w(TAG, "authenticate(): no face HAL!");
983 return ERROR_ESRCH;
984 }
985 return daemon.authenticate(operationId);
986 }
987
988 @Override
989 public int cancel() throws RemoteException {
990 IBiometricsFace daemon = getFaceDaemon();
991 if (daemon == null) {
992 Slog.w(TAG, "cancel(): no face HAL!");
993 return ERROR_ESRCH;
994 }
995 return daemon.cancel();
996 }
997
998 @Override
999 public int remove(int groupId, int biometricId) throws RemoteException {
1000 IBiometricsFace daemon = getFaceDaemon();
1001 if (daemon == null) {
1002 Slog.w(TAG, "remove(): no face HAL!");
1003 return ERROR_ESRCH;
1004 }
1005 return daemon.remove(biometricId);
1006 }
1007
1008 @Override
1009 public int enumerate() throws RemoteException {
1010 IBiometricsFace daemon = getFaceDaemon();
1011 if (daemon == null) {
1012 Slog.w(TAG, "enumerate(): no face HAL!");
1013 return ERROR_ESRCH;
1014 }
1015 return daemon.enumerate();
1016 }
1017
1018 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001019 public int enroll(byte[] cryptoToken, int groupId, int timeout,
1020 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001021 IBiometricsFace daemon = getFaceDaemon();
1022 if (daemon == null) {
1023 Slog.w(TAG, "enroll(): no face HAL!");
1024 return ERROR_ESRCH;
1025 }
1026 final ArrayList<Byte> token = new ArrayList<>();
1027 for (int i = 0; i < cryptoToken.length; i++) {
1028 token.add(cryptoToken[i]);
1029 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001030 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -07001031 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001032
1033 @Override
1034 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1035 IBiometricsFace daemon = getFaceDaemon();
1036 if (daemon == null) {
1037 Slog.w(TAG, "resetLockout(): no face HAL!");
1038 return;
1039 }
1040 final ArrayList<Byte> token = new ArrayList<>();
1041 for (int i = 0; i < cryptoToken.length; i++) {
1042 token.add(cryptoToken[i]);
1043 }
1044 daemon.resetLockout(token);
1045 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001046 };
1047
1048
1049 public FaceService(Context context) {
1050 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001051
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001052 mUsageStats = new UsageStats(context);
1053
Kevin Chyndbfbed42019-06-13 17:01:30 -07001054 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1055
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001056 mBiometricPromptIgnoreList = getContext().getResources()
1057 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1058 mBiometricPromptIgnoreListVendor = getContext().getResources()
1059 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
1060 mKeyguardIgnoreList = getContext().getResources()
1061 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
1062 mKeyguardIgnoreListVendor = getContext().getResources()
1063 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
1064 mEnrollIgnoreList = getContext().getResources()
1065 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1066 mEnrollIgnoreListVendor = getContext().getResources()
1067 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001068 }
1069
1070 @Override
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001071 protected void removeClient(ClientMonitor client) {
1072 super.removeClient(client);
1073 if (mRevokeChallengePending) {
1074 startRevokeChallenge(null);
1075 mRevokeChallengePending = false;
1076 }
1077 }
1078
1079 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001080 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001081 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001082 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001083 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1084 // blocked
1085 SystemServerInitThreadPool.get().submit(() -> mHandler.post(this::getFaceDaemon),
1086 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001087 }
1088
1089 @Override
1090 public String getTag() {
1091 return TAG;
1092 }
1093
1094 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001095 protected DaemonWrapper getDaemonWrapper() {
1096 return mDaemonWrapper;
1097 }
1098
1099 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001100 protected BiometricUtils getBiometricUtils() {
1101 return FaceUtils.getInstance();
1102 }
1103
1104 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001105 protected Constants getConstants() {
1106 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001107 }
1108
1109 @Override
1110 protected boolean hasReachedEnrollmentLimit(int userId) {
1111 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001112 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001113 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001114 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001115 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001116 return true;
1117 }
1118 return false;
1119 }
1120
1121 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001122 public void serviceDied(long cookie) {
1123 super.serviceDied(cookie);
1124 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001125
1126 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001127 }
1128
1129 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001130 protected void updateActiveGroup(int userId, String clientPackage) {
1131 IBiometricsFace daemon = getFaceDaemon();
1132
1133 if (daemon != null) {
1134 try {
1135 userId = getUserOrWorkProfileId(clientPackage, userId);
1136 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001137 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001138 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1139 if (!faceDir.exists()) {
1140 if (!faceDir.mkdir()) {
1141 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1142 return;
1143 }
1144 // Calling mkdir() from this process will create a directory with our
1145 // permissions (inherited from the containing dir). This command fixes
1146 // the label.
1147 if (!SELinux.restorecon(faceDir)) {
1148 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1149 return;
1150 }
1151 }
1152
1153 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1154 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001155 mAuthenticatorIds.put(userId,
1156 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001157 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001158 } catch (RemoteException e) {
1159 Slog.e(TAG, "Failed to setActiveUser():", e);
1160 }
1161 }
1162 }
1163
1164 @Override
1165 protected String getLockoutResetIntent() {
1166 return ACTION_LOCKOUT_RESET;
1167 }
1168
1169 @Override
1170 protected String getLockoutBroadcastPermission() {
1171 return RESET_FACE_LOCKOUT;
1172 }
1173
1174 @Override
1175 protected long getHalDeviceId() {
1176 return mHalDeviceId;
1177 }
1178
1179 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001180 protected void handleUserSwitching(int userId) {
1181 super.handleUserSwitching(userId);
1182 // Will be updated when we get the callback from HAL
1183 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1184 }
1185
1186 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001187 protected boolean hasEnrolledBiometrics(int userId) {
1188 if (userId != UserHandle.getCallingUserId()) {
1189 checkPermission(INTERACT_ACROSS_USERS);
1190 }
1191 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1192 }
1193
1194 @Override
1195 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001196 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001197 }
1198
1199 @Override
1200 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001201 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001202 }
1203
1204 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001205 protected boolean checkAppOps(int uid, String opPackageName) {
1206 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1207 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001208 }
1209
1210 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001211 protected List<Face> getEnrolledTemplates(int userId) {
1212 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1213 }
1214
1215 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001216 protected void notifyClientActiveCallbacks(boolean isActive) {
1217 // noop for Face.
1218 }
1219
Kevin Chyn7782d142019-01-18 12:51:33 -08001220 @Override
1221 protected int statsModality() {
1222 return BiometricsProtoEnums.MODALITY_FACE;
1223 }
1224
Kevin Chyna38653c2019-02-11 17:46:21 -08001225 @Override
1226 protected int getLockoutMode() {
1227 return mCurrentUserLockoutMode;
1228 }
1229
Kevin Chyna56dff72018-06-19 18:41:12 -07001230 /** Gets the face daemon */
1231 private synchronized IBiometricsFace getFaceDaemon() {
1232 if (mDaemon == null) {
1233 Slog.v(TAG, "mDaemon was null, reconnect to face");
1234 try {
1235 mDaemon = IBiometricsFace.getService();
1236 } catch (java.util.NoSuchElementException e) {
1237 // Service doesn't exist or cannot be opened. Logged below.
1238 } catch (RemoteException e) {
1239 Slog.e(TAG, "Failed to get biometric interface", e);
1240 }
1241 if (mDaemon == null) {
1242 Slog.w(TAG, "face HIDL not available");
1243 return null;
1244 }
1245
1246 mDaemon.asBinder().linkToDeath(this, 0);
1247
1248 try {
1249 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1250 } catch (RemoteException e) {
1251 Slog.e(TAG, "Failed to open face HAL", e);
1252 mDaemon = null; // try again later!
1253 }
1254
1255 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1256 if (mHalDeviceId != 0) {
1257 loadAuthenticatorIds();
1258 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001259 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001260 } else {
1261 Slog.w(TAG, "Failed to open Face HAL!");
1262 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1263 mDaemon = null;
1264 }
1265 }
1266 return mDaemon;
1267 }
1268
Kevin Chynd79e24e2018-09-25 12:06:59 -07001269 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001270 IBiometricsFace daemon = getFaceDaemon();
1271 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001272 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001273 return 0;
1274 }
1275 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001276 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001277 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001278 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001279 }
1280 return 0;
1281 }
1282
Kevin Chynd79e24e2018-09-25 12:06:59 -07001283 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001284 IBiometricsFace daemon = getFaceDaemon();
1285 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001286 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001287 return 0;
1288 }
1289 try {
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001290 final int res = daemon.revokeChallenge();
1291 if (res != Status.OK) {
1292 Slog.e(TAG, "revokeChallenge returned " + res);
1293 }
1294 return res;
Kevin Chyna56dff72018-06-19 18:41:12 -07001295 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001296 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001297 }
1298 return 0;
1299 }
1300
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001301 private void dumpInternal(PrintWriter pw) {
1302 JSONObject dump = new JSONObject();
1303 try {
1304 dump.put("service", "Face Manager");
1305
1306 JSONArray sets = new JSONArray();
1307 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1308 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001309 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001310 PerformanceStats stats = mPerformanceMap.get(userId);
1311 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1312 JSONObject set = new JSONObject();
1313 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001314 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001315 set.put("accept", (stats != null) ? stats.accept : 0);
1316 set.put("reject", (stats != null) ? stats.reject : 0);
1317 set.put("acquire", (stats != null) ? stats.acquire : 0);
1318 set.put("lockout", (stats != null) ? stats.lockout : 0);
1319 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1320 // cryptoStats measures statistics about secure face transactions
1321 // (e.g. to unlock password storage, make secure purchases, etc.)
1322 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1323 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1324 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1325 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001326 set.put("permanentLockoutCrypto",
1327 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001328 sets.put(set);
1329 }
1330
1331 dump.put("prints", sets);
1332 } catch (JSONException e) {
1333 Slog.e(TAG, "dump formatting failure", e);
1334 }
1335 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001336 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1337
1338 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001339 }
1340
Joe Onoratodb396002019-04-05 19:49:27 -07001341 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001342 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1343 // production devices:
1344 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1345 // data or any data derived from it (such as embeddings) to the
1346 // Application Processor outside the context of the TEE.
1347 // As such, this API should only be enabled for testing purposes on
1348 // engineering and userdebug builds. All modules in the software stack
1349 // MUST enforce final build products do NOT have this functionality.
1350 // Additionally, the following check MUST NOT be removed.
1351 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1352 return;
1353 }
1354
Joe Onorato108413a2019-04-03 18:20:52 -07001355 // Additionally, this flag allows turning off face for a device
1356 // (either permanently through the build or on an individual device).
1357 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1358 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1359 return;
1360 }
1361
Joe Onoratodb396002019-04-05 19:49:27 -07001362 // The debug method takes two file descriptors. The first is for text
1363 // output, which we will drop. The second is for binary data, which
1364 // will be the protobuf data.
1365 final IBiometricsFace daemon = getFaceDaemon();
1366 if (daemon != null) {
1367 FileOutputStream devnull = null;
1368 try {
1369 devnull = new FileOutputStream("/dev/null");
1370 final NativeHandle handle = new NativeHandle(
1371 new FileDescriptor[] { devnull.getFD(), fd },
1372 new int[0], false);
1373 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1374 } catch (IOException | RemoteException ex) {
1375 Slog.d(TAG, "error while reading face debugging data", ex);
1376 } finally {
1377 if (devnull != null) {
1378 try {
1379 devnull.close();
1380 } catch (IOException ex) {
1381 }
1382 }
1383 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001384 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001385 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001386}