blob: bcd22d442b97c5939e503b3b8780499dd8b24a1e [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;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020056import android.util.Slog;
57import android.util.proto.ProtoOutputStream;
58
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;
Kevin Chyn4cc49f72019-04-24 13:53:35 -070067import com.android.server.biometrics.Constants;
Kevin Chyn0ce70852019-05-10 10:29:18 -070068import com.android.server.biometrics.EnumerateClient;
Kevin Chyn6737c572019-02-08 16:10:54 -080069import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020070
71import org.json.JSONArray;
72import org.json.JSONException;
73import org.json.JSONObject;
74
75import java.io.File;
76import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070077import java.io.FileOutputStream;
78import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020079import java.io.PrintWriter;
80import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070081import java.util.Arrays;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020082import java.util.List;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020083
84/**
85 * A service to manage multiple clients that want to access the face HAL API.
86 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080087 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020088 *
89 * @hide
90 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070091public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070092
93 protected static final String TAG = "FaceService";
94 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020095 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020096 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070097 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -070098 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +020099
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700100 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700101 private int mLastAcquire;
102
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700103 public FaceAuthClient(Context context,
104 DaemonWrapper daemon, long halDeviceId, IBinder token,
105 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800106 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700107 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800108 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700109 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800110
111 @Override
112 protected int statsModality() {
113 return FaceService.this.statsModality();
114 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800115
116 @Override
117 public boolean shouldFrameworkHandleLockout() {
118 return false;
119 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800120
121 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700122 public boolean wasUserDetected() {
123 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED;
124 }
125
126 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800127 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
128 boolean authenticated, ArrayList<Byte> token) {
129 final boolean result = super.onAuthenticated(identifier, authenticated, token);
130
131 // For face, the authentication lifecycle ends either when
132 // 1) Authenticated == true
133 // 2) Error occurred
134 // 3) Authenticated == false
135 // Fingerprint currently does not end when the third condition is met which is a bug,
136 // but let's leave it as-is for now.
137 return result || !authenticated;
138 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700139
140 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700141 public int[] getAcquireIgnorelist() {
142 if (isBiometricPrompt()) {
143 return mBiometricPromptIgnoreList;
144 } else {
145 // Keyguard
146 return mKeyguardIgnoreList;
147 }
148 }
149
150 @Override
151 public int[] getAcquireVendorIgnorelist() {
152 if (isBiometricPrompt()) {
153 return mBiometricPromptIgnoreListVendor;
154 } else {
155 // Keyguard
156 return mKeyguardIgnoreListVendor;
157 }
158 }
159
160 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700161 public boolean onAcquired(int acquireInfo, int vendorCode) {
162
Kevin Chyn0ce70852019-05-10 10:29:18 -0700163 mLastAcquire = acquireInfo;
164
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700165 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
166 final String name =
167 getContext().getString(R.string.face_recalibrate_notification_name);
168 final String title =
169 getContext().getString(R.string.face_recalibrate_notification_title);
170 final String content =
171 getContext().getString(R.string.face_recalibrate_notification_content);
172
173 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
174 intent.setPackage("com.android.settings");
175
176 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
177 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
178 UserHandle.CURRENT);
179
180 final String id = "FaceService";
181
182 NotificationManager nm =
183 getContext().getSystemService(NotificationManager.class);
184 NotificationChannel channel = new NotificationChannel(id, name,
185 NotificationManager.IMPORTANCE_HIGH);
186 Notification notification = new Notification.Builder(getContext(), id)
187 .setSmallIcon(R.drawable.ic_lock)
188 .setContentTitle(title)
189 .setContentText(content)
190 .setSubText(name)
191 .setOnlyAlertOnce(true)
192 .setLocalOnly(true)
193 .setAutoCancel(true)
194 .setCategory(Notification.CATEGORY_SYSTEM)
195 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700196 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700197 .build();
198
199 nm.createNotificationChannel(channel);
200 nm.notifyAsUser(null /* tag */, 0 /* id */, notification, UserHandle.CURRENT);
201 }
202
203 return super.onAcquired(acquireInfo, vendorCode);
204 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700205 }
206
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200207 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700208 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200209 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200210 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700211
212 /**
213 * The following methods contain common code which is shared in biometrics/common.
214 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800215
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200216 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700217 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700218 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700219 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200220 }
221
222 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700223 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700224 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700225 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200226 }
227
228 @Override // Binder call
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800229 public void enroll(final IBinder token, final byte[] cryptoToken,
230 final IFaceServiceReceiver receiver, final String opPackageName,
231 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700232 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200233
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200234 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700235 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
236 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800237 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700238
239 @Override
240 public int[] getAcquireIgnorelist() {
241 return mEnrollIgnoreList;
242 }
243
244 @Override
245 public int[] getAcquireVendorIgnorelist() {
246 return mEnrollIgnoreListVendor;
247 }
248
Kevin Chyn1429a312019-01-28 16:08:09 -0800249 @Override
250 public boolean shouldVibrate() {
251 return false;
252 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800253
254 @Override
255 protected int statsModality() {
256 return FaceService.this.statsModality();
257 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800258 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200259
Kevin Chyn1a878c12019-04-04 15:50:11 -0700260 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200261 }
262
263 @Override // Binder call
264 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700265 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700266 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200267 }
268
269 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800270 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700271 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700272 final String opPackageName) {
273 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800274 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200275 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700276 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700277 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700278 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800279 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700280 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200281 }
282
283 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800284 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800285 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
286 String opPackageName, int cookie, int callingUid, int callingPid,
287 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700288 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800289 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700290 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700291 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700292 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800293 new BiometricPromptServiceListenerImpl(wrapperReceiver),
294 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800295 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700296 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
297 callingUserId);
298 }
299
300 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800301 public void startPreparedClient(int cookie) {
302 checkPermission(MANAGE_BIOMETRIC);
303 startCurrentClient(cookie);
304 }
305
306 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200307 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700308 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700309 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200310 }
311
312 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700313 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800314 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700315 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800316 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
317 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700318 }
319
320 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200321 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700322 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700323 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200324 }
325
326 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700327 public void remove(final IBinder token, final int faceId, final int userId,
328 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700329 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700330
331 if (token == null) {
332 Slog.w(TAG, "remove(): token is null");
333 return;
334 }
335
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200336 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700337 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800338 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
339 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800340 @Override
341 protected int statsModality() {
342 return FaceService.this.statsModality();
343 }
344 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700345 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200346 }
347
Kevin Chyna56dff72018-06-19 18:41:12 -0700348 @Override
349 public void enumerate(final IBinder token, final int userId,
350 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700351 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700352
353 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700354 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800355 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
356 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800357 @Override
358 protected int statsModality() {
359 return FaceService.this.statsModality();
360 }
361 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700362 enumerateInternal(client);
363 }
364
365 @Override
366 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
367 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700368 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700369 FaceService.super.addLockoutResetCallback(callback);
370 }
371
372 @Override // Binder call
373 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
374 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
375 return;
376 }
377
378 final long ident = Binder.clearCallingIdentity();
379 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700380 if (args.length > 1 && "--hal".equals(args[0])) {
381 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Joe Onoratobf955d22019-03-25 00:16:58 -0700382 } else if (args.length > 0 && "--proto".equals(args[0])) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700383 dumpProto(fd);
384 } else {
385 dumpInternal(pw);
386 }
387 } finally {
388 Binder.restoreCallingIdentity(ident);
389 }
390 }
391
392 /**
393 * The following methods don't use any common code from BiometricService
394 */
395
396 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200397 @Override // Binder call
398 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700399 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700400 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200401 Binder.getCallingUid(), Binder.getCallingPid(),
402 UserHandle.getCallingUserId())) {
403 return false;
404 }
405
406 final long token = Binder.clearCallingIdentity();
407 try {
408 IBiometricsFace daemon = getFaceDaemon();
409 return daemon != null && mHalDeviceId != 0;
410 } finally {
411 Binder.restoreCallingIdentity(token);
412 }
413 }
414
415 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700416 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700417 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700418 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
419 return;
420 }
421 mHandler.post(new Runnable() {
422 @Override
423 public void run() {
424 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
425 faceId, name);
426 }
427 });
428 }
429
430 @Override // Binder call
431 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700432 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700433 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200434 Binder.getCallingUid(), Binder.getCallingPid(),
435 UserHandle.getCallingUserId())) {
436 return null;
437 }
438
Kevin Chyn6737c572019-02-08 16:10:54 -0800439 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200440 }
441
442 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700443 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700444 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700445 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200446 Binder.getCallingUid(), Binder.getCallingPid(),
447 UserHandle.getCallingUserId())) {
448 return false;
449 }
450
Kevin Chyna56dff72018-06-19 18:41:12 -0700451 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200452 }
453
454 @Override // Binder call
455 public long getAuthenticatorId(String opPackageName) {
456 // In this method, we're not checking whether the caller is permitted to use face
457 // API because current authenticator ID is leaked (in a more contrived way) via Android
458 // Keystore (android.security.keystore package): the user of that API can create a key
459 // which requires face authentication for its use, and then query the key's
460 // characteristics (hidden API) which returns, among other things, face
461 // authenticator ID which was active at key creation time.
462 //
463 // Reason: The part of Android Keystore which runs inside an app's process invokes this
464 // method in certain cases. Those cases are not always where the developer demonstrates
465 // explicit intent to use face functionality. Thus, to avoiding throwing an
466 // unexpected SecurityException this method does not check whether its caller is
467 // permitted to use face API.
468 //
469 // The permission check should be restored once Android Keystore no longer invokes this
470 // method from inside app processes.
471
472 return FaceService.this.getAuthenticatorId(opPackageName);
473 }
474
475 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800476 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700477 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700478
479 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
480 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
481 return;
482 }
483
Kevin Chynbe67ce02019-06-10 16:14:22 -0700484 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
485
Kevin Chyna38653c2019-02-11 17:46:21 -0800486 try {
487 mDaemonWrapper.resetLockout(token);
488 } catch (RemoteException e) {
489 Slog.e(getTag(), "Unable to reset lockout", e);
490 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700491 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700492
493 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700494 public void setFeature(int feature, boolean enabled, final byte[] token,
495 IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700496 checkPermission(MANAGE_BIOMETRIC);
497
Kevin Chyne62749a2019-04-02 19:33:56 -0700498 mHandler.post(() -> {
499 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
500 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
501 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800502 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700503
504 final ArrayList<Byte> byteToken = new ArrayList<>();
505 for (int i = 0; i < token.length; i++) {
506 byteToken.add(token[i]);
507 }
508
509 // TODO: Support multiple faces
510 final int faceId = getFirstTemplateForUser(mCurrentUserId);
511
512 if (mDaemon != null) {
513 try {
514 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
515 receiver.onFeatureSet(result == Status.OK, feature);
516 } catch (RemoteException e) {
517 Slog.e(getTag(), "Unable to set feature: " + feature
518 + " to enabled:" + enabled, e);
519 }
520 }
521 });
522
Kevin Chynd79e24e2018-09-25 12:06:59 -0700523 }
524
525 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700526 public void getFeature(int feature, IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700527 checkPermission(MANAGE_BIOMETRIC);
528
Kevin Chyne62749a2019-04-02 19:33:56 -0700529 mHandler.post(() -> {
530 // This should ideally return tri-state, but the user isn't shown settings unless
531 // they are enrolled so it's fine for now.
532 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
533 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
534 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800535 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700536
537 // TODO: Support multiple faces
538 final int faceId = getFirstTemplateForUser(mCurrentUserId);
539
540 if (mDaemon != null) {
541 try {
542 OptionalBool result = mDaemon.getFeature(feature, faceId);
543 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
544 } catch (RemoteException e) {
545 Slog.e(getTag(), "Unable to getRequireAttention", e);
546 }
547 }
548 });
549
Kevin Chynd79e24e2018-09-25 12:06:59 -0700550 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700551
552 @Override
553 public void userActivity() {
554 checkPermission(MANAGE_BIOMETRIC);
555
556 if (mDaemon != null) {
557 try {
558 mDaemon.userActivity();
559 } catch (RemoteException e) {
560 Slog.e(getTag(), "Unable to send userActivity", e);
561 }
562 }
563 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800564
565 // TODO: Support multiple faces
566 private int getFirstTemplateForUser(int user) {
567 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
568 if (!faces.isEmpty()) {
569 return faces.get(0).getBiometricId();
570 }
571 return 0;
572 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700573 }
574
575 /**
576 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700577 * BiometricPrompt.
578 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800579 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800580 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800581 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700582 }
583
584 @Override
585 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
586 throws RemoteException {
587 /**
588 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
589 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800590 if (getWrapperReceiver() != null) {
591 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700592 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
593 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700594 }
595 }
596
597 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800598 public void onError(long deviceId, int error, int vendorCode, int cookie)
599 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800600 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800601 getWrapperReceiver().onError(cookie, error,
602 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700603 }
604 }
605 }
606
607 /**
608 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700609 * the FaceManager.
610 */
611 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700612 private IFaceServiceReceiver mFaceServiceReceiver;
613
614 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
615 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200616 }
617
618 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700619 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200620 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700621 if (mFaceServiceReceiver != null) {
622 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
623 identifier.getBiometricId(),
624 remaining);
625 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200626 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700627
628 @Override
629 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
630 throws RemoteException {
631 if (mFaceServiceReceiver != null) {
632 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
633 }
634 }
635
636 @Override
637 public void onAuthenticationSucceeded(long deviceId,
638 BiometricAuthenticator.Identifier biometric, int userId)
639 throws RemoteException {
640 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800641 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700642 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
643 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700644 } else {
645 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
646 }
647 }
648 }
649
650 @Override
651 public void onAuthenticationFailed(long deviceId) throws RemoteException {
652 if (mFaceServiceReceiver != null) {
653 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
654 }
655 }
656
657 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800658 public void onError(long deviceId, int error, int vendorCode, int cookie)
659 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700660 if (mFaceServiceReceiver != null) {
661 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
662 }
663 }
664
665 @Override
666 public void onRemoved(BiometricAuthenticator.Identifier identifier,
667 int remaining) throws RemoteException {
668 if (mFaceServiceReceiver != null) {
669 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
670 identifier.getBiometricId(), remaining);
671 }
672 }
673
674 @Override
675 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
676 throws RemoteException {
677 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800678 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
679 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700680 }
681 }
682 }
683
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700684 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700685
686 @GuardedBy("this")
687 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800688 // One of the AuthenticationClient constants
689 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700690
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700691 private int[] mBiometricPromptIgnoreList;
692 private int[] mBiometricPromptIgnoreListVendor;
693 private int[] mKeyguardIgnoreList;
694 private int[] mKeyguardIgnoreListVendor;
695 private int[] mEnrollIgnoreList;
696 private int[] mEnrollIgnoreListVendor;
697
Kevin Chyna56dff72018-06-19 18:41:12 -0700698 /**
699 * Receives callbacks from the HAL.
700 */
701 private IBiometricsFaceClientCallback mDaemonCallback =
702 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800703 @Override
704 public void onEnrollResult(final long deviceId, int faceId, int userId,
705 int remaining) {
706 mHandler.post(() -> {
707 final Face face = new Face(getBiometricUtils()
708 .getUniqueName(getContext(), userId), faceId, deviceId);
709 FaceService.super.handleEnrollResult(face, remaining);
710 });
711 }
712
713 @Override
714 public void onAcquired(final long deviceId, final int userId,
715 final int acquiredInfo,
716 final int vendorCode) {
717 mHandler.post(() -> {
718 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
719 });
720 }
721
722 @Override
723 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
724 ArrayList<Byte> token) {
725 mHandler.post(() -> {
726 Face face = new Face("", faceId, deviceId);
727 FaceService.super.handleAuthenticated(face, token);
728 });
729 }
730
731 @Override
732 public void onError(final long deviceId, final int userId, final int error,
733 final int vendorCode) {
734 mHandler.post(() -> {
735 FaceService.super.handleError(deviceId, error, vendorCode);
736
737 // TODO: this chunk of code should be common to all biometric services
738 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
739 // If we get HW_UNAVAILABLE, try to connect again later...
740 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
741 synchronized (this) {
742 mDaemon = null;
743 mHalDeviceId = 0;
744 mCurrentUserId = UserHandle.USER_NULL;
745 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700746 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800747 });
748 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700749
Kevin Chyn6737c572019-02-08 16:10:54 -0800750 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700751 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800752 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700753 if (!faceIds.isEmpty()) {
754 for (int i = 0; i < faceIds.size(); i++) {
755 final Face face = new Face("", faceIds.get(i), deviceId);
756 // Convert to old behavior
757 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
758 }
759 } else {
760 final Face face = new Face("", 0 /* identifier */, deviceId);
761 FaceService.super.handleRemoved(face, 0 /* remaining */);
762 }
763
Kevin Chyn6737c572019-02-08 16:10:54 -0800764 });
765 }
766
767 @Override
768 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
769 throws RemoteException {
770 mHandler.post(() -> {
771 if (!faceIds.isEmpty()) {
772 for (int i = 0; i < faceIds.size(); i++) {
773 final Face face = new Face("", faceIds.get(i), deviceId);
774 // Convert to old old behavior
775 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
776 }
777 } else {
778 // For face, the HIDL contract is to receive an empty list when there are no
779 // templates enrolled. Send a null identifier since we don't consume them
780 // anywhere, and send remaining == 0 to plumb this with existing common code.
781 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700782 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800783 });
784 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700785
Kevin Chyn6737c572019-02-08 16:10:54 -0800786 @Override
787 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800788 Slog.d(TAG, "onLockoutChanged: " + duration);
789 if (duration == 0) {
790 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
791 } else if (duration == Long.MAX_VALUE) {
792 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
793 } else {
794 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
795 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700796
Kevin Chyna38653c2019-02-11 17:46:21 -0800797 mHandler.post(() -> {
798 if (duration == 0) {
799 notifyLockoutResetMonitors();
800 }
801 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800802 }
803 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700804
805 /**
806 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
807 * can be shared between the multiple biometric services.
808 */
809 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
810 @Override
811 public int authenticate(long operationId, int groupId) throws RemoteException {
812 IBiometricsFace daemon = getFaceDaemon();
813 if (daemon == null) {
814 Slog.w(TAG, "authenticate(): no face HAL!");
815 return ERROR_ESRCH;
816 }
817 return daemon.authenticate(operationId);
818 }
819
820 @Override
821 public int cancel() throws RemoteException {
822 IBiometricsFace daemon = getFaceDaemon();
823 if (daemon == null) {
824 Slog.w(TAG, "cancel(): no face HAL!");
825 return ERROR_ESRCH;
826 }
827 return daemon.cancel();
828 }
829
830 @Override
831 public int remove(int groupId, int biometricId) throws RemoteException {
832 IBiometricsFace daemon = getFaceDaemon();
833 if (daemon == null) {
834 Slog.w(TAG, "remove(): no face HAL!");
835 return ERROR_ESRCH;
836 }
837 return daemon.remove(biometricId);
838 }
839
840 @Override
841 public int enumerate() throws RemoteException {
842 IBiometricsFace daemon = getFaceDaemon();
843 if (daemon == null) {
844 Slog.w(TAG, "enumerate(): no face HAL!");
845 return ERROR_ESRCH;
846 }
847 return daemon.enumerate();
848 }
849
850 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800851 public int enroll(byte[] cryptoToken, int groupId, int timeout,
852 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700853 IBiometricsFace daemon = getFaceDaemon();
854 if (daemon == null) {
855 Slog.w(TAG, "enroll(): no face HAL!");
856 return ERROR_ESRCH;
857 }
858 final ArrayList<Byte> token = new ArrayList<>();
859 for (int i = 0; i < cryptoToken.length; i++) {
860 token.add(cryptoToken[i]);
861 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800862 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700863 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800864
865 @Override
866 public void resetLockout(byte[] cryptoToken) throws RemoteException {
867 IBiometricsFace daemon = getFaceDaemon();
868 if (daemon == null) {
869 Slog.w(TAG, "resetLockout(): no face HAL!");
870 return;
871 }
872 final ArrayList<Byte> token = new ArrayList<>();
873 for (int i = 0; i < cryptoToken.length; i++) {
874 token.add(cryptoToken[i]);
875 }
876 daemon.resetLockout(token);
877 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700878 };
879
880
881 public FaceService(Context context) {
882 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700883
884 mBiometricPromptIgnoreList = getContext().getResources()
885 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
886 mBiometricPromptIgnoreListVendor = getContext().getResources()
887 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
888 mKeyguardIgnoreList = getContext().getResources()
889 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
890 mKeyguardIgnoreListVendor = getContext().getResources()
891 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
892 mEnrollIgnoreList = getContext().getResources()
893 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
894 mEnrollIgnoreListVendor = getContext().getResources()
895 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -0700896 }
897
898 @Override
899 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700900 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700901 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -0700902 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
903 // blocked
904 SystemServerInitThreadPool.get().submit(() -> mHandler.post(this::getFaceDaemon),
905 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -0700906 }
907
908 @Override
909 public String getTag() {
910 return TAG;
911 }
912
913 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800914 protected DaemonWrapper getDaemonWrapper() {
915 return mDaemonWrapper;
916 }
917
918 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700919 protected BiometricUtils getBiometricUtils() {
920 return FaceUtils.getInstance();
921 }
922
923 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700924 protected Constants getConstants() {
925 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -0700926 }
927
928 @Override
929 protected boolean hasReachedEnrollmentLimit(int userId) {
930 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700931 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -0800932 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -0700933 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -0700934 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700935 return true;
936 }
937 return false;
938 }
939
940 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -0800941 public void serviceDied(long cookie) {
942 super.serviceDied(cookie);
943 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -0800944
945 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -0800946 }
947
948 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700949 protected void updateActiveGroup(int userId, String clientPackage) {
950 IBiometricsFace daemon = getFaceDaemon();
951
952 if (daemon != null) {
953 try {
954 userId = getUserOrWorkProfileId(clientPackage, userId);
955 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +0000956 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700957 final File faceDir = new File(baseDir, FACE_DATA_DIR);
958 if (!faceDir.exists()) {
959 if (!faceDir.mkdir()) {
960 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
961 return;
962 }
963 // Calling mkdir() from this process will create a directory with our
964 // permissions (inherited from the containing dir). This command fixes
965 // the label.
966 if (!SELinux.restorecon(faceDir)) {
967 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
968 return;
969 }
970 }
971
972 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
973 mCurrentUserId = userId;
974 }
975 mAuthenticatorIds.put(userId,
976 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
977 } catch (RemoteException e) {
978 Slog.e(TAG, "Failed to setActiveUser():", e);
979 }
980 }
981 }
982
983 @Override
984 protected String getLockoutResetIntent() {
985 return ACTION_LOCKOUT_RESET;
986 }
987
988 @Override
989 protected String getLockoutBroadcastPermission() {
990 return RESET_FACE_LOCKOUT;
991 }
992
993 @Override
994 protected long getHalDeviceId() {
995 return mHalDeviceId;
996 }
997
998 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -0800999 protected void handleUserSwitching(int userId) {
1000 super.handleUserSwitching(userId);
1001 // Will be updated when we get the callback from HAL
1002 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1003 }
1004
1005 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001006 protected boolean hasEnrolledBiometrics(int userId) {
1007 if (userId != UserHandle.getCallingUserId()) {
1008 checkPermission(INTERACT_ACROSS_USERS);
1009 }
1010 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1011 }
1012
1013 @Override
1014 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001015 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001016 }
1017
1018 @Override
1019 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001020 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001021 }
1022
1023 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001024 protected boolean checkAppOps(int uid, String opPackageName) {
1025 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1026 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001027 }
1028
1029 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001030 protected List<Face> getEnrolledTemplates(int userId) {
1031 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1032 }
1033
1034 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001035 protected void notifyClientActiveCallbacks(boolean isActive) {
1036 // noop for Face.
1037 }
1038
Kevin Chyn7782d142019-01-18 12:51:33 -08001039 @Override
1040 protected int statsModality() {
1041 return BiometricsProtoEnums.MODALITY_FACE;
1042 }
1043
Kevin Chyna38653c2019-02-11 17:46:21 -08001044 @Override
1045 protected int getLockoutMode() {
1046 return mCurrentUserLockoutMode;
1047 }
1048
Kevin Chyna56dff72018-06-19 18:41:12 -07001049 /** Gets the face daemon */
1050 private synchronized IBiometricsFace getFaceDaemon() {
1051 if (mDaemon == null) {
1052 Slog.v(TAG, "mDaemon was null, reconnect to face");
1053 try {
1054 mDaemon = IBiometricsFace.getService();
1055 } catch (java.util.NoSuchElementException e) {
1056 // Service doesn't exist or cannot be opened. Logged below.
1057 } catch (RemoteException e) {
1058 Slog.e(TAG, "Failed to get biometric interface", e);
1059 }
1060 if (mDaemon == null) {
1061 Slog.w(TAG, "face HIDL not available");
1062 return null;
1063 }
1064
1065 mDaemon.asBinder().linkToDeath(this, 0);
1066
1067 try {
1068 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1069 } catch (RemoteException e) {
1070 Slog.e(TAG, "Failed to open face HAL", e);
1071 mDaemon = null; // try again later!
1072 }
1073
1074 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1075 if (mHalDeviceId != 0) {
1076 loadAuthenticatorIds();
1077 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001078 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001079 } else {
1080 Slog.w(TAG, "Failed to open Face HAL!");
1081 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1082 mDaemon = null;
1083 }
1084 }
1085 return mDaemon;
1086 }
1087
Kevin Chynd79e24e2018-09-25 12:06:59 -07001088 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001089 IBiometricsFace daemon = getFaceDaemon();
1090 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001091 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001092 return 0;
1093 }
1094 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001095 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001096 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001097 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001098 }
1099 return 0;
1100 }
1101
Kevin Chynd79e24e2018-09-25 12:06:59 -07001102 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001103 IBiometricsFace daemon = getFaceDaemon();
1104 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001105 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001106 return 0;
1107 }
1108 try {
Kevin Chyn96c92972018-08-31 16:09:31 -07001109 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -07001110 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001111 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001112 }
1113 return 0;
1114 }
1115
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001116 private void dumpInternal(PrintWriter pw) {
1117 JSONObject dump = new JSONObject();
1118 try {
1119 dump.put("service", "Face Manager");
1120
1121 JSONArray sets = new JSONArray();
1122 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1123 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001124 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001125 PerformanceStats stats = mPerformanceMap.get(userId);
1126 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1127 JSONObject set = new JSONObject();
1128 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001129 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001130 set.put("accept", (stats != null) ? stats.accept : 0);
1131 set.put("reject", (stats != null) ? stats.reject : 0);
1132 set.put("acquire", (stats != null) ? stats.acquire : 0);
1133 set.put("lockout", (stats != null) ? stats.lockout : 0);
1134 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1135 // cryptoStats measures statistics about secure face transactions
1136 // (e.g. to unlock password storage, make secure purchases, etc.)
1137 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1138 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1139 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1140 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001141 set.put("permanentLockoutCrypto",
1142 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001143 sets.put(set);
1144 }
1145
1146 dump.put("prints", sets);
1147 } catch (JSONException e) {
1148 Slog.e(TAG, "dump formatting failure", e);
1149 }
1150 pw.println(dump);
Kevin Chyn9ba99912019-01-16 16:24:36 -08001151 pw.println("HAL Deaths: " + mHALDeathCount);
1152 mHALDeathCount = 0;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001153 }
1154
1155 private void dumpProto(FileDescriptor fd) {
1156 final ProtoOutputStream proto = new ProtoOutputStream(fd);
1157 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1158 final int userId = user.getUserHandle().getIdentifier();
1159
1160 final long userToken = proto.start(FaceServiceDumpProto.USERS);
1161
1162 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001163 proto.write(FaceUserStatsProto.NUM_FACES,
1164 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001165
1166 // Normal face authentications (e.g. lockscreen)
1167 final PerformanceStats normal = mPerformanceMap.get(userId);
1168 if (normal != null) {
1169 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
1170 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
1171 proto.write(FaceActionStatsProto.REJECT, normal.reject);
1172 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
1173 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
1174 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
1175 proto.end(countsToken);
1176 }
1177
1178 // Statistics about secure face transactions (e.g. to unlock password
1179 // storage, make secure purchases, etc.)
1180 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
1181 if (crypto != null) {
1182 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
1183 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
1184 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
1185 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
1186 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
1187 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
1188 proto.end(countsToken);
1189 }
1190
1191 proto.end(userToken);
1192 }
1193 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -07001194 mPerformanceMap.clear();
1195 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001196 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001197
Joe Onoratodb396002019-04-05 19:49:27 -07001198 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001199 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1200 // production devices:
1201 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1202 // data or any data derived from it (such as embeddings) to the
1203 // Application Processor outside the context of the TEE.
1204 // As such, this API should only be enabled for testing purposes on
1205 // engineering and userdebug builds. All modules in the software stack
1206 // MUST enforce final build products do NOT have this functionality.
1207 // Additionally, the following check MUST NOT be removed.
1208 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1209 return;
1210 }
1211
Joe Onorato108413a2019-04-03 18:20:52 -07001212 // Additionally, this flag allows turning off face for a device
1213 // (either permanently through the build or on an individual device).
1214 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1215 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1216 return;
1217 }
1218
Joe Onoratodb396002019-04-05 19:49:27 -07001219 // The debug method takes two file descriptors. The first is for text
1220 // output, which we will drop. The second is for binary data, which
1221 // will be the protobuf data.
1222 final IBiometricsFace daemon = getFaceDaemon();
1223 if (daemon != null) {
1224 FileOutputStream devnull = null;
1225 try {
1226 devnull = new FileOutputStream("/dev/null");
1227 final NativeHandle handle = new NativeHandle(
1228 new FileDescriptor[] { devnull.getFD(), fd },
1229 new int[0], false);
1230 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1231 } catch (IOException | RemoteException ex) {
1232 Slog.d(TAG, "error while reading face debugging data", ex);
1233 } finally {
1234 if (devnull != null) {
1235 try {
1236 devnull.close();
1237 } catch (IOException ex) {
1238 }
1239 }
1240 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001241 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001242 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001243}