blob: edb36d32c39dd87e8fcdd3dbd73ecf64d490c3aa [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)
196 .build();
197
198 nm.createNotificationChannel(channel);
199 nm.notifyAsUser(null /* tag */, 0 /* id */, notification, UserHandle.CURRENT);
200 }
201
202 return super.onAcquired(acquireInfo, vendorCode);
203 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700204 }
205
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200206 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700207 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200208 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200209 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700210
211 /**
212 * The following methods contain common code which is shared in biometrics/common.
213 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800214
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200215 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700216 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700217 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700218 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200219 }
220
221 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700222 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700223 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700224 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200225 }
226
227 @Override // Binder call
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800228 public void enroll(final IBinder token, final byte[] cryptoToken,
229 final IFaceServiceReceiver receiver, final String opPackageName,
230 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700231 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200232
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200233 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700234 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
235 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800236 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700237
238 @Override
239 public int[] getAcquireIgnorelist() {
240 return mEnrollIgnoreList;
241 }
242
243 @Override
244 public int[] getAcquireVendorIgnorelist() {
245 return mEnrollIgnoreListVendor;
246 }
247
Kevin Chyn1429a312019-01-28 16:08:09 -0800248 @Override
249 public boolean shouldVibrate() {
250 return false;
251 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800252
253 @Override
254 protected int statsModality() {
255 return FaceService.this.statsModality();
256 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800257 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200258
Kevin Chyn1a878c12019-04-04 15:50:11 -0700259 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200260 }
261
262 @Override // Binder call
263 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700264 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700265 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200266 }
267
268 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800269 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700270 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700271 final String opPackageName) {
272 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800273 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200274 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700275 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700276 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700277 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800278 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700279 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200280 }
281
282 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800283 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800284 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
285 String opPackageName, int cookie, int callingUid, int callingPid,
286 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700287 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800288 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700289 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700290 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700291 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800292 new BiometricPromptServiceListenerImpl(wrapperReceiver),
293 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800294 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700295 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
296 callingUserId);
297 }
298
299 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800300 public void startPreparedClient(int cookie) {
301 checkPermission(MANAGE_BIOMETRIC);
302 startCurrentClient(cookie);
303 }
304
305 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200306 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700307 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700308 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200309 }
310
311 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700312 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800313 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700314 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800315 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
316 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700317 }
318
319 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200320 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700321 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700322 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200323 }
324
325 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700326 public void remove(final IBinder token, final int faceId, final int userId,
327 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700328 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700329
330 if (token == null) {
331 Slog.w(TAG, "remove(): token is null");
332 return;
333 }
334
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200335 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700336 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800337 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
338 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800339 @Override
340 protected int statsModality() {
341 return FaceService.this.statsModality();
342 }
343 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700344 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200345 }
346
Kevin Chyna56dff72018-06-19 18:41:12 -0700347 @Override
348 public void enumerate(final IBinder token, final int userId,
349 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700350 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700351
352 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700353 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800354 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
355 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800356 @Override
357 protected int statsModality() {
358 return FaceService.this.statsModality();
359 }
360 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700361 enumerateInternal(client);
362 }
363
364 @Override
365 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
366 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700367 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700368 FaceService.super.addLockoutResetCallback(callback);
369 }
370
371 @Override // Binder call
372 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
373 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
374 return;
375 }
376
377 final long ident = Binder.clearCallingIdentity();
378 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700379 if (args.length > 1 && "--hal".equals(args[0])) {
380 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Joe Onoratobf955d22019-03-25 00:16:58 -0700381 } else if (args.length > 0 && "--proto".equals(args[0])) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700382 dumpProto(fd);
383 } else {
384 dumpInternal(pw);
385 }
386 } finally {
387 Binder.restoreCallingIdentity(ident);
388 }
389 }
390
391 /**
392 * The following methods don't use any common code from BiometricService
393 */
394
395 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200396 @Override // Binder call
397 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700398 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700399 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200400 Binder.getCallingUid(), Binder.getCallingPid(),
401 UserHandle.getCallingUserId())) {
402 return false;
403 }
404
405 final long token = Binder.clearCallingIdentity();
406 try {
407 IBiometricsFace daemon = getFaceDaemon();
408 return daemon != null && mHalDeviceId != 0;
409 } finally {
410 Binder.restoreCallingIdentity(token);
411 }
412 }
413
414 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700415 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700416 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700417 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
418 return;
419 }
420 mHandler.post(new Runnable() {
421 @Override
422 public void run() {
423 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
424 faceId, name);
425 }
426 });
427 }
428
429 @Override // Binder call
430 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700431 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700432 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200433 Binder.getCallingUid(), Binder.getCallingPid(),
434 UserHandle.getCallingUserId())) {
435 return null;
436 }
437
Kevin Chyn6737c572019-02-08 16:10:54 -0800438 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200439 }
440
441 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700442 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700443 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700444 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200445 Binder.getCallingUid(), Binder.getCallingPid(),
446 UserHandle.getCallingUserId())) {
447 return false;
448 }
449
Kevin Chyna56dff72018-06-19 18:41:12 -0700450 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200451 }
452
453 @Override // Binder call
454 public long getAuthenticatorId(String opPackageName) {
455 // In this method, we're not checking whether the caller is permitted to use face
456 // API because current authenticator ID is leaked (in a more contrived way) via Android
457 // Keystore (android.security.keystore package): the user of that API can create a key
458 // which requires face authentication for its use, and then query the key's
459 // characteristics (hidden API) which returns, among other things, face
460 // authenticator ID which was active at key creation time.
461 //
462 // Reason: The part of Android Keystore which runs inside an app's process invokes this
463 // method in certain cases. Those cases are not always where the developer demonstrates
464 // explicit intent to use face functionality. Thus, to avoiding throwing an
465 // unexpected SecurityException this method does not check whether its caller is
466 // permitted to use face API.
467 //
468 // The permission check should be restored once Android Keystore no longer invokes this
469 // method from inside app processes.
470
471 return FaceService.this.getAuthenticatorId(opPackageName);
472 }
473
474 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800475 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700476 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700477
478 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
479 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
480 return;
481 }
482
Kevin Chynbe67ce02019-06-10 16:14:22 -0700483 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
484
Kevin Chyna38653c2019-02-11 17:46:21 -0800485 try {
486 mDaemonWrapper.resetLockout(token);
487 } catch (RemoteException e) {
488 Slog.e(getTag(), "Unable to reset lockout", e);
489 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700490 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700491
492 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700493 public void setFeature(int feature, boolean enabled, final byte[] token,
494 IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700495 checkPermission(MANAGE_BIOMETRIC);
496
Kevin Chyne62749a2019-04-02 19:33:56 -0700497 mHandler.post(() -> {
498 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
499 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
500 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800501 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700502
503 final ArrayList<Byte> byteToken = new ArrayList<>();
504 for (int i = 0; i < token.length; i++) {
505 byteToken.add(token[i]);
506 }
507
508 // TODO: Support multiple faces
509 final int faceId = getFirstTemplateForUser(mCurrentUserId);
510
511 if (mDaemon != null) {
512 try {
513 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
514 receiver.onFeatureSet(result == Status.OK, feature);
515 } catch (RemoteException e) {
516 Slog.e(getTag(), "Unable to set feature: " + feature
517 + " to enabled:" + enabled, e);
518 }
519 }
520 });
521
Kevin Chynd79e24e2018-09-25 12:06:59 -0700522 }
523
524 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700525 public void getFeature(int feature, IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700526 checkPermission(MANAGE_BIOMETRIC);
527
Kevin Chyne62749a2019-04-02 19:33:56 -0700528 mHandler.post(() -> {
529 // This should ideally return tri-state, but the user isn't shown settings unless
530 // they are enrolled so it's fine for now.
531 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
532 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
533 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800534 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700535
536 // TODO: Support multiple faces
537 final int faceId = getFirstTemplateForUser(mCurrentUserId);
538
539 if (mDaemon != null) {
540 try {
541 OptionalBool result = mDaemon.getFeature(feature, faceId);
542 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
543 } catch (RemoteException e) {
544 Slog.e(getTag(), "Unable to getRequireAttention", e);
545 }
546 }
547 });
548
Kevin Chynd79e24e2018-09-25 12:06:59 -0700549 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700550
551 @Override
552 public void userActivity() {
553 checkPermission(MANAGE_BIOMETRIC);
554
555 if (mDaemon != null) {
556 try {
557 mDaemon.userActivity();
558 } catch (RemoteException e) {
559 Slog.e(getTag(), "Unable to send userActivity", e);
560 }
561 }
562 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800563
564 // TODO: Support multiple faces
565 private int getFirstTemplateForUser(int user) {
566 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
567 if (!faces.isEmpty()) {
568 return faces.get(0).getBiometricId();
569 }
570 return 0;
571 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700572 }
573
574 /**
575 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700576 * BiometricPrompt.
577 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800578 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800579 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800580 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700581 }
582
583 @Override
584 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
585 throws RemoteException {
586 /**
587 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
588 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800589 if (getWrapperReceiver() != null) {
590 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700591 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
592 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700593 }
594 }
595
596 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800597 public void onError(long deviceId, int error, int vendorCode, int cookie)
598 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800599 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800600 getWrapperReceiver().onError(cookie, error,
601 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700602 }
603 }
604 }
605
606 /**
607 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700608 * the FaceManager.
609 */
610 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700611 private IFaceServiceReceiver mFaceServiceReceiver;
612
613 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
614 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200615 }
616
617 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700618 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200619 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700620 if (mFaceServiceReceiver != null) {
621 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
622 identifier.getBiometricId(),
623 remaining);
624 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200625 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700626
627 @Override
628 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
629 throws RemoteException {
630 if (mFaceServiceReceiver != null) {
631 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
632 }
633 }
634
635 @Override
636 public void onAuthenticationSucceeded(long deviceId,
637 BiometricAuthenticator.Identifier biometric, int userId)
638 throws RemoteException {
639 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800640 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700641 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
642 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700643 } else {
644 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
645 }
646 }
647 }
648
649 @Override
650 public void onAuthenticationFailed(long deviceId) throws RemoteException {
651 if (mFaceServiceReceiver != null) {
652 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
653 }
654 }
655
656 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800657 public void onError(long deviceId, int error, int vendorCode, int cookie)
658 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700659 if (mFaceServiceReceiver != null) {
660 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
661 }
662 }
663
664 @Override
665 public void onRemoved(BiometricAuthenticator.Identifier identifier,
666 int remaining) throws RemoteException {
667 if (mFaceServiceReceiver != null) {
668 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
669 identifier.getBiometricId(), remaining);
670 }
671 }
672
673 @Override
674 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
675 throws RemoteException {
676 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800677 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
678 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700679 }
680 }
681 }
682
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700683 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700684
685 @GuardedBy("this")
686 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800687 // One of the AuthenticationClient constants
688 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700689
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700690 private int[] mBiometricPromptIgnoreList;
691 private int[] mBiometricPromptIgnoreListVendor;
692 private int[] mKeyguardIgnoreList;
693 private int[] mKeyguardIgnoreListVendor;
694 private int[] mEnrollIgnoreList;
695 private int[] mEnrollIgnoreListVendor;
696
Kevin Chyna56dff72018-06-19 18:41:12 -0700697 /**
698 * Receives callbacks from the HAL.
699 */
700 private IBiometricsFaceClientCallback mDaemonCallback =
701 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800702 @Override
703 public void onEnrollResult(final long deviceId, int faceId, int userId,
704 int remaining) {
705 mHandler.post(() -> {
706 final Face face = new Face(getBiometricUtils()
707 .getUniqueName(getContext(), userId), faceId, deviceId);
708 FaceService.super.handleEnrollResult(face, remaining);
709 });
710 }
711
712 @Override
713 public void onAcquired(final long deviceId, final int userId,
714 final int acquiredInfo,
715 final int vendorCode) {
716 mHandler.post(() -> {
717 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
718 });
719 }
720
721 @Override
722 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
723 ArrayList<Byte> token) {
724 mHandler.post(() -> {
725 Face face = new Face("", faceId, deviceId);
726 FaceService.super.handleAuthenticated(face, token);
727 });
728 }
729
730 @Override
731 public void onError(final long deviceId, final int userId, final int error,
732 final int vendorCode) {
733 mHandler.post(() -> {
734 FaceService.super.handleError(deviceId, error, vendorCode);
735
736 // TODO: this chunk of code should be common to all biometric services
737 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
738 // If we get HW_UNAVAILABLE, try to connect again later...
739 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
740 synchronized (this) {
741 mDaemon = null;
742 mHalDeviceId = 0;
743 mCurrentUserId = UserHandle.USER_NULL;
744 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700745 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800746 });
747 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700748
Kevin Chyn6737c572019-02-08 16:10:54 -0800749 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700750 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800751 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700752 if (!faceIds.isEmpty()) {
753 for (int i = 0; i < faceIds.size(); i++) {
754 final Face face = new Face("", faceIds.get(i), deviceId);
755 // Convert to old behavior
756 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
757 }
758 } else {
759 final Face face = new Face("", 0 /* identifier */, deviceId);
760 FaceService.super.handleRemoved(face, 0 /* remaining */);
761 }
762
Kevin Chyn6737c572019-02-08 16:10:54 -0800763 });
764 }
765
766 @Override
767 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
768 throws RemoteException {
769 mHandler.post(() -> {
770 if (!faceIds.isEmpty()) {
771 for (int i = 0; i < faceIds.size(); i++) {
772 final Face face = new Face("", faceIds.get(i), deviceId);
773 // Convert to old old behavior
774 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
775 }
776 } else {
777 // For face, the HIDL contract is to receive an empty list when there are no
778 // templates enrolled. Send a null identifier since we don't consume them
779 // anywhere, and send remaining == 0 to plumb this with existing common code.
780 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700781 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800782 });
783 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700784
Kevin Chyn6737c572019-02-08 16:10:54 -0800785 @Override
786 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800787 Slog.d(TAG, "onLockoutChanged: " + duration);
788 if (duration == 0) {
789 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
790 } else if (duration == Long.MAX_VALUE) {
791 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
792 } else {
793 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
794 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700795
Kevin Chyna38653c2019-02-11 17:46:21 -0800796 mHandler.post(() -> {
797 if (duration == 0) {
798 notifyLockoutResetMonitors();
799 }
800 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800801 }
802 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700803
804 /**
805 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
806 * can be shared between the multiple biometric services.
807 */
808 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
809 @Override
810 public int authenticate(long operationId, int groupId) throws RemoteException {
811 IBiometricsFace daemon = getFaceDaemon();
812 if (daemon == null) {
813 Slog.w(TAG, "authenticate(): no face HAL!");
814 return ERROR_ESRCH;
815 }
816 return daemon.authenticate(operationId);
817 }
818
819 @Override
820 public int cancel() throws RemoteException {
821 IBiometricsFace daemon = getFaceDaemon();
822 if (daemon == null) {
823 Slog.w(TAG, "cancel(): no face HAL!");
824 return ERROR_ESRCH;
825 }
826 return daemon.cancel();
827 }
828
829 @Override
830 public int remove(int groupId, int biometricId) throws RemoteException {
831 IBiometricsFace daemon = getFaceDaemon();
832 if (daemon == null) {
833 Slog.w(TAG, "remove(): no face HAL!");
834 return ERROR_ESRCH;
835 }
836 return daemon.remove(biometricId);
837 }
838
839 @Override
840 public int enumerate() throws RemoteException {
841 IBiometricsFace daemon = getFaceDaemon();
842 if (daemon == null) {
843 Slog.w(TAG, "enumerate(): no face HAL!");
844 return ERROR_ESRCH;
845 }
846 return daemon.enumerate();
847 }
848
849 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800850 public int enroll(byte[] cryptoToken, int groupId, int timeout,
851 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700852 IBiometricsFace daemon = getFaceDaemon();
853 if (daemon == null) {
854 Slog.w(TAG, "enroll(): no face HAL!");
855 return ERROR_ESRCH;
856 }
857 final ArrayList<Byte> token = new ArrayList<>();
858 for (int i = 0; i < cryptoToken.length; i++) {
859 token.add(cryptoToken[i]);
860 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800861 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700862 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800863
864 @Override
865 public void resetLockout(byte[] cryptoToken) throws RemoteException {
866 IBiometricsFace daemon = getFaceDaemon();
867 if (daemon == null) {
868 Slog.w(TAG, "resetLockout(): no face HAL!");
869 return;
870 }
871 final ArrayList<Byte> token = new ArrayList<>();
872 for (int i = 0; i < cryptoToken.length; i++) {
873 token.add(cryptoToken[i]);
874 }
875 daemon.resetLockout(token);
876 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700877 };
878
879
880 public FaceService(Context context) {
881 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700882
883 mBiometricPromptIgnoreList = getContext().getResources()
884 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
885 mBiometricPromptIgnoreListVendor = getContext().getResources()
886 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
887 mKeyguardIgnoreList = getContext().getResources()
888 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
889 mKeyguardIgnoreListVendor = getContext().getResources()
890 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
891 mEnrollIgnoreList = getContext().getResources()
892 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
893 mEnrollIgnoreListVendor = getContext().getResources()
894 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -0700895 }
896
897 @Override
898 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700899 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700900 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
901 SystemServerInitThreadPool.get().submit(this::getFaceDaemon, TAG + ".onStart");
902 }
903
904 @Override
905 public String getTag() {
906 return TAG;
907 }
908
909 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800910 protected DaemonWrapper getDaemonWrapper() {
911 return mDaemonWrapper;
912 }
913
914 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700915 protected BiometricUtils getBiometricUtils() {
916 return FaceUtils.getInstance();
917 }
918
919 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700920 protected Constants getConstants() {
921 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -0700922 }
923
924 @Override
925 protected boolean hasReachedEnrollmentLimit(int userId) {
926 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700927 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -0800928 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -0700929 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -0700930 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700931 return true;
932 }
933 return false;
934 }
935
936 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -0800937 public void serviceDied(long cookie) {
938 super.serviceDied(cookie);
939 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -0800940
941 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -0800942 }
943
944 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700945 protected void updateActiveGroup(int userId, String clientPackage) {
946 IBiometricsFace daemon = getFaceDaemon();
947
948 if (daemon != null) {
949 try {
950 userId = getUserOrWorkProfileId(clientPackage, userId);
951 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +0000952 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700953 final File faceDir = new File(baseDir, FACE_DATA_DIR);
954 if (!faceDir.exists()) {
955 if (!faceDir.mkdir()) {
956 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
957 return;
958 }
959 // Calling mkdir() from this process will create a directory with our
960 // permissions (inherited from the containing dir). This command fixes
961 // the label.
962 if (!SELinux.restorecon(faceDir)) {
963 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
964 return;
965 }
966 }
967
968 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
969 mCurrentUserId = userId;
970 }
971 mAuthenticatorIds.put(userId,
972 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
973 } catch (RemoteException e) {
974 Slog.e(TAG, "Failed to setActiveUser():", e);
975 }
976 }
977 }
978
979 @Override
980 protected String getLockoutResetIntent() {
981 return ACTION_LOCKOUT_RESET;
982 }
983
984 @Override
985 protected String getLockoutBroadcastPermission() {
986 return RESET_FACE_LOCKOUT;
987 }
988
989 @Override
990 protected long getHalDeviceId() {
991 return mHalDeviceId;
992 }
993
994 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -0800995 protected void handleUserSwitching(int userId) {
996 super.handleUserSwitching(userId);
997 // Will be updated when we get the callback from HAL
998 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
999 }
1000
1001 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001002 protected boolean hasEnrolledBiometrics(int userId) {
1003 if (userId != UserHandle.getCallingUserId()) {
1004 checkPermission(INTERACT_ACROSS_USERS);
1005 }
1006 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1007 }
1008
1009 @Override
1010 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001011 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001012 }
1013
1014 @Override
1015 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001016 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001017 }
1018
1019 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001020 protected boolean checkAppOps(int uid, String opPackageName) {
1021 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1022 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001023 }
1024
1025 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001026 protected List<Face> getEnrolledTemplates(int userId) {
1027 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1028 }
1029
1030 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001031 protected void notifyClientActiveCallbacks(boolean isActive) {
1032 // noop for Face.
1033 }
1034
Kevin Chyn7782d142019-01-18 12:51:33 -08001035 @Override
1036 protected int statsModality() {
1037 return BiometricsProtoEnums.MODALITY_FACE;
1038 }
1039
Kevin Chyna38653c2019-02-11 17:46:21 -08001040 @Override
1041 protected int getLockoutMode() {
1042 return mCurrentUserLockoutMode;
1043 }
1044
Kevin Chyna56dff72018-06-19 18:41:12 -07001045 /** Gets the face daemon */
1046 private synchronized IBiometricsFace getFaceDaemon() {
1047 if (mDaemon == null) {
1048 Slog.v(TAG, "mDaemon was null, reconnect to face");
1049 try {
1050 mDaemon = IBiometricsFace.getService();
1051 } catch (java.util.NoSuchElementException e) {
1052 // Service doesn't exist or cannot be opened. Logged below.
1053 } catch (RemoteException e) {
1054 Slog.e(TAG, "Failed to get biometric interface", e);
1055 }
1056 if (mDaemon == null) {
1057 Slog.w(TAG, "face HIDL not available");
1058 return null;
1059 }
1060
1061 mDaemon.asBinder().linkToDeath(this, 0);
1062
1063 try {
1064 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1065 } catch (RemoteException e) {
1066 Slog.e(TAG, "Failed to open face HAL", e);
1067 mDaemon = null; // try again later!
1068 }
1069
1070 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1071 if (mHalDeviceId != 0) {
1072 loadAuthenticatorIds();
1073 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001074 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001075 } else {
1076 Slog.w(TAG, "Failed to open Face HAL!");
1077 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1078 mDaemon = null;
1079 }
1080 }
1081 return mDaemon;
1082 }
1083
Kevin Chynd79e24e2018-09-25 12:06:59 -07001084 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001085 IBiometricsFace daemon = getFaceDaemon();
1086 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001087 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001088 return 0;
1089 }
1090 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001091 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001092 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001093 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001094 }
1095 return 0;
1096 }
1097
Kevin Chynd79e24e2018-09-25 12:06:59 -07001098 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001099 IBiometricsFace daemon = getFaceDaemon();
1100 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001101 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001102 return 0;
1103 }
1104 try {
Kevin Chyn96c92972018-08-31 16:09:31 -07001105 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -07001106 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001107 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001108 }
1109 return 0;
1110 }
1111
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001112 private void dumpInternal(PrintWriter pw) {
1113 JSONObject dump = new JSONObject();
1114 try {
1115 dump.put("service", "Face Manager");
1116
1117 JSONArray sets = new JSONArray();
1118 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1119 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001120 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001121 PerformanceStats stats = mPerformanceMap.get(userId);
1122 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1123 JSONObject set = new JSONObject();
1124 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001125 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001126 set.put("accept", (stats != null) ? stats.accept : 0);
1127 set.put("reject", (stats != null) ? stats.reject : 0);
1128 set.put("acquire", (stats != null) ? stats.acquire : 0);
1129 set.put("lockout", (stats != null) ? stats.lockout : 0);
1130 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1131 // cryptoStats measures statistics about secure face transactions
1132 // (e.g. to unlock password storage, make secure purchases, etc.)
1133 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1134 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1135 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1136 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001137 set.put("permanentLockoutCrypto",
1138 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001139 sets.put(set);
1140 }
1141
1142 dump.put("prints", sets);
1143 } catch (JSONException e) {
1144 Slog.e(TAG, "dump formatting failure", e);
1145 }
1146 pw.println(dump);
Kevin Chyn9ba99912019-01-16 16:24:36 -08001147 pw.println("HAL Deaths: " + mHALDeathCount);
1148 mHALDeathCount = 0;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001149 }
1150
1151 private void dumpProto(FileDescriptor fd) {
1152 final ProtoOutputStream proto = new ProtoOutputStream(fd);
1153 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1154 final int userId = user.getUserHandle().getIdentifier();
1155
1156 final long userToken = proto.start(FaceServiceDumpProto.USERS);
1157
1158 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001159 proto.write(FaceUserStatsProto.NUM_FACES,
1160 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001161
1162 // Normal face authentications (e.g. lockscreen)
1163 final PerformanceStats normal = mPerformanceMap.get(userId);
1164 if (normal != null) {
1165 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
1166 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
1167 proto.write(FaceActionStatsProto.REJECT, normal.reject);
1168 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
1169 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
1170 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
1171 proto.end(countsToken);
1172 }
1173
1174 // Statistics about secure face transactions (e.g. to unlock password
1175 // storage, make secure purchases, etc.)
1176 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
1177 if (crypto != null) {
1178 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
1179 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
1180 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
1181 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
1182 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
1183 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
1184 proto.end(countsToken);
1185 }
1186
1187 proto.end(userToken);
1188 }
1189 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -07001190 mPerformanceMap.clear();
1191 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001192 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001193
Joe Onoratodb396002019-04-05 19:49:27 -07001194 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001195 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1196 // production devices:
1197 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1198 // data or any data derived from it (such as embeddings) to the
1199 // Application Processor outside the context of the TEE.
1200 // As such, this API should only be enabled for testing purposes on
1201 // engineering and userdebug builds. All modules in the software stack
1202 // MUST enforce final build products do NOT have this functionality.
1203 // Additionally, the following check MUST NOT be removed.
1204 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1205 return;
1206 }
1207
Joe Onorato108413a2019-04-03 18:20:52 -07001208 // Additionally, this flag allows turning off face for a device
1209 // (either permanently through the build or on an individual device).
1210 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1211 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1212 return;
1213 }
1214
Joe Onoratodb396002019-04-05 19:49:27 -07001215 // The debug method takes two file descriptors. The first is for text
1216 // output, which we will drop. The second is for binary data, which
1217 // will be the protobuf data.
1218 final IBiometricsFace daemon = getFaceDaemon();
1219 if (daemon != null) {
1220 FileOutputStream devnull = null;
1221 try {
1222 devnull = new FileOutputStream("/dev/null");
1223 final NativeHandle handle = new NativeHandle(
1224 new FileDescriptor[] { devnull.getFD(), fd },
1225 new int[0], false);
1226 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1227 } catch (IOException | RemoteException ex) {
1228 Slog.d(TAG, "error while reading face debugging data", ex);
1229 } finally {
1230 if (devnull != null) {
1231 try {
1232 devnull.close();
1233 } catch (IOException ex) {
1234 }
1235 }
1236 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001237 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001238 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001239}