blob: fe0fc91a4b270cc46d7c3b3f43f1ec9c4f3d42a8 [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;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020026import android.content.Context;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020027import android.content.pm.UserInfo;
Kevin Chyna56dff72018-06-19 18:41:12 -070028import android.hardware.biometrics.BiometricAuthenticator;
29import android.hardware.biometrics.BiometricConstants;
Kevin Chyn7782d142019-01-18 12:51:33 -080030import android.hardware.biometrics.BiometricsProtoEnums;
Kevin Chyna56dff72018-06-19 18:41:12 -070031import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Kevin Chyn23289ef2018-11-28 16:32:36 -080032import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020033import android.hardware.biometrics.face.V1_0.IBiometricsFace;
34import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
Kevin Chynb95f1522019-03-04 16:45:15 -080035import android.hardware.biometrics.face.V1_0.OptionalBool;
Kevin Chynd79e24e2018-09-25 12:06:59 -070036import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020037import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070038import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020039import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020040import android.hardware.face.IFaceServiceReceiver;
41import android.os.Binder;
Joe Onoratobf955d22019-03-25 00:16:58 -070042import android.os.Build;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020043import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020044import android.os.IBinder;
Joe Onoratodb396002019-04-05 19:49:27 -070045import android.os.NativeHandle;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020046import android.os.RemoteException;
47import android.os.SELinux;
Joe Onorato108413a2019-04-03 18:20:52 -070048import android.os.SystemProperties;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020049import android.os.UserHandle;
50import android.os.UserManager;
Joe Onoratodb396002019-04-05 19:49:27 -070051import android.provider.Settings;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020052import android.util.Slog;
53import android.util.proto.ProtoOutputStream;
54
55import com.android.internal.annotations.GuardedBy;
56import com.android.internal.logging.MetricsLogger;
57import com.android.internal.util.DumpUtils;
58import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080059import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070060import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070061import com.android.server.biometrics.BiometricUtils;
Kevin Chyn6737c572019-02-08 16:10:54 -080062import com.android.server.biometrics.EnumerateClient;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070063import com.android.server.biometrics.Metrics;
Kevin Chyn6737c572019-02-08 16:10:54 -080064import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020065
66import org.json.JSONArray;
67import org.json.JSONException;
68import org.json.JSONObject;
69
70import java.io.File;
71import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070072import java.io.FileOutputStream;
73import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020074import java.io.PrintWriter;
75import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070076import java.util.Arrays;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020077import java.util.List;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020078
79/**
80 * A service to manage multiple clients that want to access the face HAL API.
81 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080082 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020083 *
84 * @hide
85 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070086public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070087
88 protected static final String TAG = "FaceService";
89 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020090 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020091 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070092 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -070093 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +020094
Kevin Chyn8b7a0372018-09-17 15:06:05 -070095 private final class FaceAuthClient extends AuthenticationClientImpl {
96 public FaceAuthClient(Context context,
97 DaemonWrapper daemon, long halDeviceId, IBinder token,
98 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -080099 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700100 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800101 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700102 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800103
104 @Override
105 protected int statsModality() {
106 return FaceService.this.statsModality();
107 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800108
109 @Override
110 public boolean shouldFrameworkHandleLockout() {
111 return false;
112 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800113
114 @Override
115 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
116 boolean authenticated, ArrayList<Byte> token) {
117 final boolean result = super.onAuthenticated(identifier, authenticated, token);
118
119 // For face, the authentication lifecycle ends either when
120 // 1) Authenticated == true
121 // 2) Error occurred
122 // 3) Authenticated == false
123 // Fingerprint currently does not end when the third condition is met which is a bug,
124 // but let's leave it as-is for now.
125 return result || !authenticated;
126 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700127 }
128
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200129 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700130 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200131 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200132 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700133
134 /**
135 * The following methods contain common code which is shared in biometrics/common.
136 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800137
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200138 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700139 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700140 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700141 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200142 }
143
144 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700145 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700146 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700147 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200148 }
149
150 @Override // Binder call
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800151 public void enroll(final IBinder token, final byte[] cryptoToken,
152 final IFaceServiceReceiver receiver, final String opPackageName,
153 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700154 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200155
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200156 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700157 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
158 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800159 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
160 @Override
161 public boolean shouldVibrate() {
162 return false;
163 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800164
165 @Override
166 protected int statsModality() {
167 return FaceService.this.statsModality();
168 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800169 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200170
Kevin Chyn1a878c12019-04-04 15:50:11 -0700171 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200172 }
173
174 @Override // Binder call
175 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700176 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700177 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200178 }
179
180 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800181 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700182 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700183 final String opPackageName) {
184 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800185 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200186 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700187 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700188 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700189 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800190 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700191 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200192 }
193
194 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800195 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800196 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
197 String opPackageName, int cookie, int callingUid, int callingPid,
198 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700199 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800200 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700201 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700202 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700203 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800204 new BiometricPromptServiceListenerImpl(wrapperReceiver),
205 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800206 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700207 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
208 callingUserId);
209 }
210
211 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800212 public void startPreparedClient(int cookie) {
213 checkPermission(MANAGE_BIOMETRIC);
214 startCurrentClient(cookie);
215 }
216
217 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200218 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700219 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700220 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200221 }
222
223 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700224 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800225 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700226 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800227 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
228 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700229 }
230
231 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200232 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700233 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700234 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200235 }
236
237 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700238 public void remove(final IBinder token, final int faceId, final int userId,
239 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700240 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700241
242 if (token == null) {
243 Slog.w(TAG, "remove(): token is null");
244 return;
245 }
246
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200247 final boolean restricted = isRestricted();
Kevin Chyn6737c572019-02-08 16:10:54 -0800248 final RemovalClient client = new RemovalClient(getContext(), getMetrics(),
249 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
250 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800251 @Override
252 protected int statsModality() {
253 return FaceService.this.statsModality();
254 }
255 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700256 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200257 }
258
Kevin Chyna56dff72018-06-19 18:41:12 -0700259 @Override
260 public void enumerate(final IBinder token, final int userId,
261 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700262 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700263
264 final boolean restricted = isRestricted();
Kevin Chyn6737c572019-02-08 16:10:54 -0800265 final EnumerateClient client = new EnumerateClient(getContext(), getMetrics(),
266 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
267 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800268 @Override
269 protected int statsModality() {
270 return FaceService.this.statsModality();
271 }
272 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700273 enumerateInternal(client);
274 }
275
276 @Override
277 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
278 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700279 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700280 FaceService.super.addLockoutResetCallback(callback);
281 }
282
283 @Override // Binder call
284 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
285 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
286 return;
287 }
288
289 final long ident = Binder.clearCallingIdentity();
290 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700291 if (args.length > 1 && "--hal".equals(args[0])) {
292 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Joe Onoratobf955d22019-03-25 00:16:58 -0700293 } else if (args.length > 0 && "--proto".equals(args[0])) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700294 dumpProto(fd);
295 } else {
296 dumpInternal(pw);
297 }
298 } finally {
299 Binder.restoreCallingIdentity(ident);
300 }
301 }
302
303 /**
304 * The following methods don't use any common code from BiometricService
305 */
306
307 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200308 @Override // Binder call
309 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700310 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700311 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200312 Binder.getCallingUid(), Binder.getCallingPid(),
313 UserHandle.getCallingUserId())) {
314 return false;
315 }
316
317 final long token = Binder.clearCallingIdentity();
318 try {
319 IBiometricsFace daemon = getFaceDaemon();
320 return daemon != null && mHalDeviceId != 0;
321 } finally {
322 Binder.restoreCallingIdentity(token);
323 }
324 }
325
326 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700327 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700328 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700329 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
330 return;
331 }
332 mHandler.post(new Runnable() {
333 @Override
334 public void run() {
335 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
336 faceId, name);
337 }
338 });
339 }
340
341 @Override // Binder call
342 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700343 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700344 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200345 Binder.getCallingUid(), Binder.getCallingPid(),
346 UserHandle.getCallingUserId())) {
347 return null;
348 }
349
Kevin Chyn6737c572019-02-08 16:10:54 -0800350 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200351 }
352
353 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700354 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700355 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700356 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200357 Binder.getCallingUid(), Binder.getCallingPid(),
358 UserHandle.getCallingUserId())) {
359 return false;
360 }
361
Kevin Chyna56dff72018-06-19 18:41:12 -0700362 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200363 }
364
365 @Override // Binder call
366 public long getAuthenticatorId(String opPackageName) {
367 // In this method, we're not checking whether the caller is permitted to use face
368 // API because current authenticator ID is leaked (in a more contrived way) via Android
369 // Keystore (android.security.keystore package): the user of that API can create a key
370 // which requires face authentication for its use, and then query the key's
371 // characteristics (hidden API) which returns, among other things, face
372 // authenticator ID which was active at key creation time.
373 //
374 // Reason: The part of Android Keystore which runs inside an app's process invokes this
375 // method in certain cases. Those cases are not always where the developer demonstrates
376 // explicit intent to use face functionality. Thus, to avoiding throwing an
377 // unexpected SecurityException this method does not check whether its caller is
378 // permitted to use face API.
379 //
380 // The permission check should be restored once Android Keystore no longer invokes this
381 // method from inside app processes.
382
383 return FaceService.this.getAuthenticatorId(opPackageName);
384 }
385
386 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800387 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700388 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700389
390 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
391 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
392 return;
393 }
394
Kevin Chyna38653c2019-02-11 17:46:21 -0800395 try {
396 mDaemonWrapper.resetLockout(token);
397 } catch (RemoteException e) {
398 Slog.e(getTag(), "Unable to reset lockout", e);
399 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700400 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700401
402 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700403 public void setFeature(int feature, boolean enabled, final byte[] token,
404 IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700405 checkPermission(MANAGE_BIOMETRIC);
406
Kevin Chyne62749a2019-04-02 19:33:56 -0700407 mHandler.post(() -> {
408 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
409 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
410 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800411 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700412
413 final ArrayList<Byte> byteToken = new ArrayList<>();
414 for (int i = 0; i < token.length; i++) {
415 byteToken.add(token[i]);
416 }
417
418 // TODO: Support multiple faces
419 final int faceId = getFirstTemplateForUser(mCurrentUserId);
420
421 if (mDaemon != null) {
422 try {
423 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
424 receiver.onFeatureSet(result == Status.OK, feature);
425 } catch (RemoteException e) {
426 Slog.e(getTag(), "Unable to set feature: " + feature
427 + " to enabled:" + enabled, e);
428 }
429 }
430 });
431
Kevin Chynd79e24e2018-09-25 12:06:59 -0700432 }
433
434 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700435 public void getFeature(int feature, IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700436 checkPermission(MANAGE_BIOMETRIC);
437
Kevin Chyne62749a2019-04-02 19:33:56 -0700438 mHandler.post(() -> {
439 // This should ideally return tri-state, but the user isn't shown settings unless
440 // they are enrolled so it's fine for now.
441 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
442 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
443 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800444 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700445
446 // TODO: Support multiple faces
447 final int faceId = getFirstTemplateForUser(mCurrentUserId);
448
449 if (mDaemon != null) {
450 try {
451 OptionalBool result = mDaemon.getFeature(feature, faceId);
452 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
453 } catch (RemoteException e) {
454 Slog.e(getTag(), "Unable to getRequireAttention", e);
455 }
456 }
457 });
458
Kevin Chynd79e24e2018-09-25 12:06:59 -0700459 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700460
461 @Override
462 public void userActivity() {
463 checkPermission(MANAGE_BIOMETRIC);
464
465 if (mDaemon != null) {
466 try {
467 mDaemon.userActivity();
468 } catch (RemoteException e) {
469 Slog.e(getTag(), "Unable to send userActivity", e);
470 }
471 }
472 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800473
474 // TODO: Support multiple faces
475 private int getFirstTemplateForUser(int user) {
476 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
477 if (!faces.isEmpty()) {
478 return faces.get(0).getBiometricId();
479 }
480 return 0;
481 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700482 }
483
484 /**
485 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700486 * BiometricPrompt.
487 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800488 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800489 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800490 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700491 }
492
493 @Override
494 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
495 throws RemoteException {
496 /**
497 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
498 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800499 if (getWrapperReceiver() != null) {
500 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700501 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
502 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700503 }
504 }
505
506 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800507 public void onError(long deviceId, int error, int vendorCode, int cookie)
508 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800509 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800510 getWrapperReceiver().onError(cookie, error,
511 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700512 }
513 }
514 }
515
516 /**
517 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700518 * the FaceManager.
519 */
520 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700521 private IFaceServiceReceiver mFaceServiceReceiver;
522
523 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
524 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200525 }
526
527 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700528 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200529 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700530 if (mFaceServiceReceiver != null) {
531 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
532 identifier.getBiometricId(),
533 remaining);
534 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200535 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700536
537 @Override
538 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
539 throws RemoteException {
540 if (mFaceServiceReceiver != null) {
541 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
542 }
543 }
544
545 @Override
546 public void onAuthenticationSucceeded(long deviceId,
547 BiometricAuthenticator.Identifier biometric, int userId)
548 throws RemoteException {
549 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800550 if (biometric == null || biometric instanceof Face) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700551 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face)biometric);
552 } else {
553 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
554 }
555 }
556 }
557
558 @Override
559 public void onAuthenticationFailed(long deviceId) throws RemoteException {
560 if (mFaceServiceReceiver != null) {
561 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
562 }
563 }
564
565 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800566 public void onError(long deviceId, int error, int vendorCode, int cookie)
567 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700568 if (mFaceServiceReceiver != null) {
569 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
570 }
571 }
572
573 @Override
574 public void onRemoved(BiometricAuthenticator.Identifier identifier,
575 int remaining) throws RemoteException {
576 if (mFaceServiceReceiver != null) {
577 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
578 identifier.getBiometricId(), remaining);
579 }
580 }
581
582 @Override
583 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
584 throws RemoteException {
585 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800586 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
587 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700588 }
589 }
590 }
591
592 private final FaceMetrics mFaceMetrics = new FaceMetrics();
593
594 @GuardedBy("this")
595 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800596 // One of the AuthenticationClient constants
597 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700598
599 /**
600 * Receives callbacks from the HAL.
601 */
602 private IBiometricsFaceClientCallback mDaemonCallback =
603 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800604 @Override
605 public void onEnrollResult(final long deviceId, int faceId, int userId,
606 int remaining) {
607 mHandler.post(() -> {
608 final Face face = new Face(getBiometricUtils()
609 .getUniqueName(getContext(), userId), faceId, deviceId);
610 FaceService.super.handleEnrollResult(face, remaining);
611 });
612 }
613
614 @Override
615 public void onAcquired(final long deviceId, final int userId,
616 final int acquiredInfo,
617 final int vendorCode) {
618 mHandler.post(() -> {
619 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
620 });
621 }
622
623 @Override
624 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
625 ArrayList<Byte> token) {
626 mHandler.post(() -> {
627 Face face = new Face("", faceId, deviceId);
628 FaceService.super.handleAuthenticated(face, token);
629 });
630 }
631
632 @Override
633 public void onError(final long deviceId, final int userId, final int error,
634 final int vendorCode) {
635 mHandler.post(() -> {
636 FaceService.super.handleError(deviceId, error, vendorCode);
637
638 // TODO: this chunk of code should be common to all biometric services
639 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
640 // If we get HW_UNAVAILABLE, try to connect again later...
641 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
642 synchronized (this) {
643 mDaemon = null;
644 mHalDeviceId = 0;
645 mCurrentUserId = UserHandle.USER_NULL;
646 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700647 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800648 });
649 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700650
Kevin Chyn6737c572019-02-08 16:10:54 -0800651 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700652 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800653 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700654 if (!faceIds.isEmpty()) {
655 for (int i = 0; i < faceIds.size(); i++) {
656 final Face face = new Face("", faceIds.get(i), deviceId);
657 // Convert to old behavior
658 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
659 }
660 } else {
661 final Face face = new Face("", 0 /* identifier */, deviceId);
662 FaceService.super.handleRemoved(face, 0 /* remaining */);
663 }
664
Kevin Chyn6737c572019-02-08 16:10:54 -0800665 });
666 }
667
668 @Override
669 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
670 throws RemoteException {
671 mHandler.post(() -> {
672 if (!faceIds.isEmpty()) {
673 for (int i = 0; i < faceIds.size(); i++) {
674 final Face face = new Face("", faceIds.get(i), deviceId);
675 // Convert to old old behavior
676 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
677 }
678 } else {
679 // For face, the HIDL contract is to receive an empty list when there are no
680 // templates enrolled. Send a null identifier since we don't consume them
681 // anywhere, and send remaining == 0 to plumb this with existing common code.
682 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700683 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800684 });
685 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700686
Kevin Chyn6737c572019-02-08 16:10:54 -0800687 @Override
688 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800689 Slog.d(TAG, "onLockoutChanged: " + duration);
690 if (duration == 0) {
691 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
692 } else if (duration == Long.MAX_VALUE) {
693 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
694 } else {
695 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
696 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700697
Kevin Chyna38653c2019-02-11 17:46:21 -0800698 mHandler.post(() -> {
699 if (duration == 0) {
700 notifyLockoutResetMonitors();
701 }
702 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800703 }
704 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700705
706 /**
707 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
708 * can be shared between the multiple biometric services.
709 */
710 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
711 @Override
712 public int authenticate(long operationId, int groupId) throws RemoteException {
713 IBiometricsFace daemon = getFaceDaemon();
714 if (daemon == null) {
715 Slog.w(TAG, "authenticate(): no face HAL!");
716 return ERROR_ESRCH;
717 }
718 return daemon.authenticate(operationId);
719 }
720
721 @Override
722 public int cancel() throws RemoteException {
723 IBiometricsFace daemon = getFaceDaemon();
724 if (daemon == null) {
725 Slog.w(TAG, "cancel(): no face HAL!");
726 return ERROR_ESRCH;
727 }
728 return daemon.cancel();
729 }
730
731 @Override
732 public int remove(int groupId, int biometricId) throws RemoteException {
733 IBiometricsFace daemon = getFaceDaemon();
734 if (daemon == null) {
735 Slog.w(TAG, "remove(): no face HAL!");
736 return ERROR_ESRCH;
737 }
738 return daemon.remove(biometricId);
739 }
740
741 @Override
742 public int enumerate() throws RemoteException {
743 IBiometricsFace daemon = getFaceDaemon();
744 if (daemon == null) {
745 Slog.w(TAG, "enumerate(): no face HAL!");
746 return ERROR_ESRCH;
747 }
748 return daemon.enumerate();
749 }
750
751 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800752 public int enroll(byte[] cryptoToken, int groupId, int timeout,
753 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700754 IBiometricsFace daemon = getFaceDaemon();
755 if (daemon == null) {
756 Slog.w(TAG, "enroll(): no face HAL!");
757 return ERROR_ESRCH;
758 }
759 final ArrayList<Byte> token = new ArrayList<>();
760 for (int i = 0; i < cryptoToken.length; i++) {
761 token.add(cryptoToken[i]);
762 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800763 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700764 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800765
766 @Override
767 public void resetLockout(byte[] cryptoToken) throws RemoteException {
768 IBiometricsFace daemon = getFaceDaemon();
769 if (daemon == null) {
770 Slog.w(TAG, "resetLockout(): no face HAL!");
771 return;
772 }
773 final ArrayList<Byte> token = new ArrayList<>();
774 for (int i = 0; i < cryptoToken.length; i++) {
775 token.add(cryptoToken[i]);
776 }
777 daemon.resetLockout(token);
778 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700779 };
780
781
782 public FaceService(Context context) {
783 super(context);
Kevin Chyna56dff72018-06-19 18:41:12 -0700784 }
785
786 @Override
787 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700788 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700789 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
790 SystemServerInitThreadPool.get().submit(this::getFaceDaemon, TAG + ".onStart");
791 }
792
793 @Override
794 public String getTag() {
795 return TAG;
796 }
797
798 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800799 protected DaemonWrapper getDaemonWrapper() {
800 return mDaemonWrapper;
801 }
802
803 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700804 protected BiometricUtils getBiometricUtils() {
805 return FaceUtils.getInstance();
806 }
807
808 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700809 protected Metrics getMetrics() {
810 return mFaceMetrics;
811 }
812
813 @Override
814 protected boolean hasReachedEnrollmentLimit(int userId) {
815 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700816 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -0800817 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -0700818 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -0700819 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700820 return true;
821 }
822 return false;
823 }
824
825 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -0800826 public void serviceDied(long cookie) {
827 super.serviceDied(cookie);
828 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -0800829
830 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -0800831 }
832
833 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700834 protected void updateActiveGroup(int userId, String clientPackage) {
835 IBiometricsFace daemon = getFaceDaemon();
836
837 if (daemon != null) {
838 try {
839 userId = getUserOrWorkProfileId(clientPackage, userId);
840 if (userId != mCurrentUserId) {
841 final File baseDir = Environment.getDataVendorDeDirectory(userId);
842 final File faceDir = new File(baseDir, FACE_DATA_DIR);
843 if (!faceDir.exists()) {
844 if (!faceDir.mkdir()) {
845 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
846 return;
847 }
848 // Calling mkdir() from this process will create a directory with our
849 // permissions (inherited from the containing dir). This command fixes
850 // the label.
851 if (!SELinux.restorecon(faceDir)) {
852 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
853 return;
854 }
855 }
856
857 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
858 mCurrentUserId = userId;
859 }
860 mAuthenticatorIds.put(userId,
861 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
862 } catch (RemoteException e) {
863 Slog.e(TAG, "Failed to setActiveUser():", e);
864 }
865 }
866 }
867
868 @Override
869 protected String getLockoutResetIntent() {
870 return ACTION_LOCKOUT_RESET;
871 }
872
873 @Override
874 protected String getLockoutBroadcastPermission() {
875 return RESET_FACE_LOCKOUT;
876 }
877
878 @Override
879 protected long getHalDeviceId() {
880 return mHalDeviceId;
881 }
882
883 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -0800884 protected void handleUserSwitching(int userId) {
885 super.handleUserSwitching(userId);
886 // Will be updated when we get the callback from HAL
887 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
888 }
889
890 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700891 protected boolean hasEnrolledBiometrics(int userId) {
892 if (userId != UserHandle.getCallingUserId()) {
893 checkPermission(INTERACT_ACROSS_USERS);
894 }
895 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
896 }
897
898 @Override
899 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700900 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -0700901 }
902
903 @Override
904 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700905 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -0700906 }
907
908 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -0700909 protected boolean checkAppOps(int uid, String opPackageName) {
910 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
911 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -0700912 }
913
914 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800915 protected List<Face> getEnrolledTemplates(int userId) {
916 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
917 }
918
919 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700920 protected void notifyClientActiveCallbacks(boolean isActive) {
921 // noop for Face.
922 }
923
Kevin Chyn7782d142019-01-18 12:51:33 -0800924 @Override
925 protected int statsModality() {
926 return BiometricsProtoEnums.MODALITY_FACE;
927 }
928
Kevin Chyna38653c2019-02-11 17:46:21 -0800929 @Override
930 protected int getLockoutMode() {
931 return mCurrentUserLockoutMode;
932 }
933
Kevin Chyna56dff72018-06-19 18:41:12 -0700934 /** Gets the face daemon */
935 private synchronized IBiometricsFace getFaceDaemon() {
936 if (mDaemon == null) {
937 Slog.v(TAG, "mDaemon was null, reconnect to face");
938 try {
939 mDaemon = IBiometricsFace.getService();
940 } catch (java.util.NoSuchElementException e) {
941 // Service doesn't exist or cannot be opened. Logged below.
942 } catch (RemoteException e) {
943 Slog.e(TAG, "Failed to get biometric interface", e);
944 }
945 if (mDaemon == null) {
946 Slog.w(TAG, "face HIDL not available");
947 return null;
948 }
949
950 mDaemon.asBinder().linkToDeath(this, 0);
951
952 try {
953 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
954 } catch (RemoteException e) {
955 Slog.e(TAG, "Failed to open face HAL", e);
956 mDaemon = null; // try again later!
957 }
958
959 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
960 if (mHalDeviceId != 0) {
961 loadAuthenticatorIds();
962 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -0800963 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -0700964 } else {
965 Slog.w(TAG, "Failed to open Face HAL!");
966 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
967 mDaemon = null;
968 }
969 }
970 return mDaemon;
971 }
972
Kevin Chynd79e24e2018-09-25 12:06:59 -0700973 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700974 IBiometricsFace daemon = getFaceDaemon();
975 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700976 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -0700977 return 0;
978 }
979 try {
Kevin Chyne46a2162018-09-20 18:43:01 -0700980 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -0700981 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700982 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -0700983 }
984 return 0;
985 }
986
Kevin Chynd79e24e2018-09-25 12:06:59 -0700987 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700988 IBiometricsFace daemon = getFaceDaemon();
989 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700990 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -0700991 return 0;
992 }
993 try {
Kevin Chyn96c92972018-08-31 16:09:31 -0700994 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -0700995 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700996 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -0700997 }
998 return 0;
999 }
1000
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001001 private void dumpInternal(PrintWriter pw) {
1002 JSONObject dump = new JSONObject();
1003 try {
1004 dump.put("service", "Face Manager");
1005
1006 JSONArray sets = new JSONArray();
1007 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1008 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001009 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001010 PerformanceStats stats = mPerformanceMap.get(userId);
1011 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1012 JSONObject set = new JSONObject();
1013 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001014 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001015 set.put("accept", (stats != null) ? stats.accept : 0);
1016 set.put("reject", (stats != null) ? stats.reject : 0);
1017 set.put("acquire", (stats != null) ? stats.acquire : 0);
1018 set.put("lockout", (stats != null) ? stats.lockout : 0);
1019 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1020 // cryptoStats measures statistics about secure face transactions
1021 // (e.g. to unlock password storage, make secure purchases, etc.)
1022 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1023 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1024 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1025 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001026 set.put("permanentLockoutCrypto",
1027 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001028 sets.put(set);
1029 }
1030
1031 dump.put("prints", sets);
1032 } catch (JSONException e) {
1033 Slog.e(TAG, "dump formatting failure", e);
1034 }
1035 pw.println(dump);
Kevin Chyn9ba99912019-01-16 16:24:36 -08001036 pw.println("HAL Deaths: " + mHALDeathCount);
1037 mHALDeathCount = 0;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001038 }
1039
1040 private void dumpProto(FileDescriptor fd) {
1041 final ProtoOutputStream proto = new ProtoOutputStream(fd);
1042 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1043 final int userId = user.getUserHandle().getIdentifier();
1044
1045 final long userToken = proto.start(FaceServiceDumpProto.USERS);
1046
1047 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001048 proto.write(FaceUserStatsProto.NUM_FACES,
1049 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001050
1051 // Normal face authentications (e.g. lockscreen)
1052 final PerformanceStats normal = mPerformanceMap.get(userId);
1053 if (normal != null) {
1054 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
1055 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
1056 proto.write(FaceActionStatsProto.REJECT, normal.reject);
1057 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
1058 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
1059 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
1060 proto.end(countsToken);
1061 }
1062
1063 // Statistics about secure face transactions (e.g. to unlock password
1064 // storage, make secure purchases, etc.)
1065 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
1066 if (crypto != null) {
1067 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
1068 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
1069 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
1070 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
1071 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
1072 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
1073 proto.end(countsToken);
1074 }
1075
1076 proto.end(userToken);
1077 }
1078 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -07001079 mPerformanceMap.clear();
1080 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001081 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001082
Joe Onoratodb396002019-04-05 19:49:27 -07001083 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001084 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1085 // production devices:
1086 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1087 // data or any data derived from it (such as embeddings) to the
1088 // Application Processor outside the context of the TEE.
1089 // As such, this API should only be enabled for testing purposes on
1090 // engineering and userdebug builds. All modules in the software stack
1091 // MUST enforce final build products do NOT have this functionality.
1092 // Additionally, the following check MUST NOT be removed.
1093 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1094 return;
1095 }
1096
Joe Onorato108413a2019-04-03 18:20:52 -07001097 // Additionally, this flag allows turning off face for a device
1098 // (either permanently through the build or on an individual device).
1099 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1100 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1101 return;
1102 }
1103
Joe Onoratodb396002019-04-05 19:49:27 -07001104 // The debug method takes two file descriptors. The first is for text
1105 // output, which we will drop. The second is for binary data, which
1106 // will be the protobuf data.
1107 final IBiometricsFace daemon = getFaceDaemon();
1108 if (daemon != null) {
1109 FileOutputStream devnull = null;
1110 try {
1111 devnull = new FileOutputStream("/dev/null");
1112 final NativeHandle handle = new NativeHandle(
1113 new FileDescriptor[] { devnull.getFD(), fd },
1114 new int[0], false);
1115 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1116 } catch (IOException | RemoteException ex) {
1117 Slog.d(TAG, "error while reading face debugging data", ex);
1118 } finally {
1119 if (devnull != null) {
1120 try {
1121 devnull.close();
1122 } catch (IOException ex) {
1123 }
1124 }
1125 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001126 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001127 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001128}