blob: 8afac97f647f76a5136de680b818b71d9e31b906 [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;
30import android.hardware.biometrics.IBiometricPromptReceiver;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070031import android.hardware.biometrics.IBiometricPromptServiceReceiver;
Kevin Chyna56dff72018-06-19 18:41:12 -070032import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020033import android.hardware.biometrics.face.V1_0.IBiometricsFace;
34import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
35import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070036import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020037import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020038import android.hardware.face.IFaceServiceReceiver;
39import android.os.Binder;
40import android.os.Bundle;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020041import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020042import android.os.IBinder;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020043import android.os.RemoteException;
44import android.os.SELinux;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020045import android.os.UserHandle;
46import android.os.UserManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020047import android.util.Slog;
48import android.util.proto.ProtoOutputStream;
49
50import com.android.internal.annotations.GuardedBy;
51import com.android.internal.logging.MetricsLogger;
52import com.android.internal.util.DumpUtils;
53import com.android.server.SystemServerInitThreadPool;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070054import com.android.server.biometrics.BiometricService;
55import com.android.server.biometrics.BiometricUtils;
56import com.android.server.biometrics.Metrics;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020057
58import org.json.JSONArray;
59import org.json.JSONException;
60import org.json.JSONObject;
61
62import java.io.File;
63import java.io.FileDescriptor;
64import java.io.PrintWriter;
65import java.util.ArrayList;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020066import java.util.List;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020067
68/**
69 * A service to manage multiple clients that want to access the face HAL API.
70 * The service is responsible for maintaining a list of clients and dispatching all
71 * face -related events.
72 *
73 * @hide
74 */
Kevin Chyna56dff72018-06-19 18:41:12 -070075public class FaceService extends BiometricService {
76
77 protected static final String TAG = "FaceService";
78 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020079 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020080 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070081 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyna56dff72018-06-19 18:41:12 -070082 private static final int MAX_FAILED_ATTEMPTS_LOCKOUT_TIMED = 3;
83 private static final int MAX_FAILED_ATTEMPTS_LOCKOUT_PERMANENT = 12;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020084
85 /**
Kevin Chyna56dff72018-06-19 18:41:12 -070086 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020087 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +020088 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -070089
90 /**
91 * The following methods contain common code which is shared in biometrics/common.
92 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +020093 @Override // Binder call
94 public long preEnroll(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -070095 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +020096 return startPreEnroll(token);
97 }
98
99 @Override // Binder call
100 public int postEnroll(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700101 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200102 return startPostEnroll(token);
103 }
104
105 @Override // Binder call
106 public void enroll(final IBinder token, final byte[] cryptoToken, final int userId,
107 final IFaceServiceReceiver receiver, final int flags,
108 final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700109 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200110
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200111 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700112 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
113 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
114 0 /* groupId */, cryptoToken, restricted, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200115
Kevin Chyna56dff72018-06-19 18:41:12 -0700116 enrollInternal(client, userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200117 }
118
119 @Override // Binder call
120 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700121 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700122 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200123 }
124
125 @Override // Binder call
126 public void authenticate(final IBinder token, final long opId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700127 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700128 final String opPackageName) {
129 checkPermission(USE_BIOMETRIC_INTERNAL);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200130 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700131 final AuthenticationClientImpl client = new AuthenticationClientImpl(getContext(),
132 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700133 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700134 null /* bundle */, null /* dialogReceiver */, mStatusBarService, mFaceManager);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200135
Kevin Chyna56dff72018-06-19 18:41:12 -0700136 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200137 }
138
139 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700140 public void authenticateFromService(IBinder token, long opId, int groupId,
141 IBiometricPromptServiceReceiver receiver, int flags, String opPackageName,
142 Bundle bundle, IBiometricPromptReceiver dialogReceiver,
143 int callingUid, int callingPid, int callingUserId) {
144 checkPermission(USE_BIOMETRIC_INTERNAL);
145 final boolean restricted = true; // BiometricPrompt is always restricted
146 final AuthenticationClientImpl client = new AuthenticationClientImpl(getContext(),
147 mDaemonWrapper, mHalDeviceId, token,
148 new BiometricPromptServiceListenerImpl(receiver),
149 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700150 bundle, dialogReceiver, mStatusBarService, mFaceManager);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700151 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
152 callingUserId);
153 }
154
155 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200156 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700157 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700158 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200159 }
160
161 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700162 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
163 int callingUid, int callingPid, int callingUserId) {
164 checkPermission(USE_BIOMETRIC_INTERNAL);
165 cancelAuthenticationInternal(token, opPackageName,
166 callingUid, callingPid, callingUserId);
167 }
168
169 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200170 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700171 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700172 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200173 }
174
175 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700176 public void remove(final IBinder token, final int faceId, final int userId,
177 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700178 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700179
180 if (token == null) {
181 Slog.w(TAG, "remove(): token is null");
182 return;
183 }
184
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200185 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700186 final RemovalClientImpl client = new RemovalClientImpl(getContext(), mDaemonWrapper,
187 mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId, 0 /* groupId */,
188 userId, restricted, token.toString());
189 client.setShouldNotifyUserActivity(true);
190 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200191 }
192
Kevin Chyna56dff72018-06-19 18:41:12 -0700193 @Override
194 public void enumerate(final IBinder token, final int userId,
195 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700196 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700197
198 final boolean restricted = isRestricted();
199 final EnumerateClientImpl client = new EnumerateClientImpl(getContext(), mDaemonWrapper,
200 mHalDeviceId, token, new ServiceListenerImpl(receiver), userId, userId,
201 restricted, getContext().getOpPackageName());
202 enumerateInternal(client);
203 }
204
205 @Override
206 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
207 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700208 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700209 FaceService.super.addLockoutResetCallback(callback);
210 }
211
212 @Override // Binder call
213 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
214 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
215 return;
216 }
217
218 final long ident = Binder.clearCallingIdentity();
219 try {
220 if (args.length > 0 && "--proto".equals(args[0])) {
221 dumpProto(fd);
222 } else {
223 dumpInternal(pw);
224 }
225 } finally {
226 Binder.restoreCallingIdentity(ident);
227 }
228 }
229
230 /**
231 * The following methods don't use any common code from BiometricService
232 */
233
234 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200235 @Override // Binder call
236 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700237 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700238 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200239 Binder.getCallingUid(), Binder.getCallingPid(),
240 UserHandle.getCallingUserId())) {
241 return false;
242 }
243
244 final long token = Binder.clearCallingIdentity();
245 try {
246 IBiometricsFace daemon = getFaceDaemon();
247 return daemon != null && mHalDeviceId != 0;
248 } finally {
249 Binder.restoreCallingIdentity(token);
250 }
251 }
252
253 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700254 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700255 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700256 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
257 return;
258 }
259 mHandler.post(new Runnable() {
260 @Override
261 public void run() {
262 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
263 faceId, name);
264 }
265 });
266 }
267
268 @Override // Binder call
269 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700270 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700271 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200272 Binder.getCallingUid(), Binder.getCallingPid(),
273 UserHandle.getCallingUserId())) {
274 return null;
275 }
276
Kevin Chyna56dff72018-06-19 18:41:12 -0700277 return FaceService.this.getEnrolledFaces(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200278 }
279
280 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700281 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700282 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700283 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200284 Binder.getCallingUid(), Binder.getCallingPid(),
285 UserHandle.getCallingUserId())) {
286 return false;
287 }
288
Kevin Chyna56dff72018-06-19 18:41:12 -0700289 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200290 }
291
292 @Override // Binder call
293 public long getAuthenticatorId(String opPackageName) {
294 // In this method, we're not checking whether the caller is permitted to use face
295 // API because current authenticator ID is leaked (in a more contrived way) via Android
296 // Keystore (android.security.keystore package): the user of that API can create a key
297 // which requires face authentication for its use, and then query the key's
298 // characteristics (hidden API) which returns, among other things, face
299 // authenticator ID which was active at key creation time.
300 //
301 // Reason: The part of Android Keystore which runs inside an app's process invokes this
302 // method in certain cases. Those cases are not always where the developer demonstrates
303 // explicit intent to use face functionality. Thus, to avoiding throwing an
304 // unexpected SecurityException this method does not check whether its caller is
305 // permitted to use face API.
306 //
307 // The permission check should be restored once Android Keystore no longer invokes this
308 // method from inside app processes.
309
310 return FaceService.this.getAuthenticatorId(opPackageName);
311 }
312
313 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700314 public void resetTimeout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700315 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200316 // TODO: confirm security token when we move timeout management into the HAL layer.
Kevin Chyna56dff72018-06-19 18:41:12 -0700317 mHandler.post(mResetFailedAttemptsForCurrentUserRunnable);
318 }
319 }
320
321 /**
322 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700323 * BiometricPrompt.
324 */
325 private class BiometricPromptServiceListenerImpl implements ServiceListener {
326
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700327 private IBiometricPromptServiceReceiver mBiometricPromptServiceReceiver;
328
329 public BiometricPromptServiceListenerImpl(IBiometricPromptServiceReceiver receiver) {
330 mBiometricPromptServiceReceiver = receiver;
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700331 }
332
333 @Override
334 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
335 throws RemoteException {
336 /**
337 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
338 */
339 if (mBiometricPromptServiceReceiver != null) {
340 mBiometricPromptServiceReceiver.onAcquired(deviceId,
341 mFaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
342 mFaceManager.getAcquiredString(acquiredInfo, vendorCode));
343 }
344 }
345
346 @Override
347 public void onAuthenticationSucceeded(long deviceId,
348 BiometricAuthenticator.Identifier biometric, int userId) throws RemoteException {
349 if (mBiometricPromptServiceReceiver != null) {
350 mBiometricPromptServiceReceiver.onAuthenticationSucceeded(deviceId);
351 }
352 }
353
354 @Override
355 public void onAuthenticationFailed(long deviceId) throws RemoteException {
356 if (mBiometricPromptServiceReceiver != null) {
357 mBiometricPromptServiceReceiver.onAuthenticationFailed(deviceId);
358 }
359 }
360
361 @Override
362 public void onError(long deviceId, int error, int vendorCode) throws RemoteException {
363 if (mBiometricPromptServiceReceiver != null) {
364 mBiometricPromptServiceReceiver.onError(deviceId, error,
365 mFaceManager.getErrorString(error, vendorCode));
366 }
367 }
368 }
369
370 /**
371 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700372 * the FaceManager.
373 */
374 private class ServiceListenerImpl implements ServiceListener {
375
376 private IFaceServiceReceiver mFaceServiceReceiver;
377
378 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
379 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200380 }
381
382 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700383 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200384 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700385 if (mFaceServiceReceiver != null) {
386 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
387 identifier.getBiometricId(),
388 remaining);
389 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200390 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700391
392 @Override
393 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
394 throws RemoteException {
395 if (mFaceServiceReceiver != null) {
396 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
397 }
398 }
399
400 @Override
401 public void onAuthenticationSucceeded(long deviceId,
402 BiometricAuthenticator.Identifier biometric, int userId)
403 throws RemoteException {
404 if (mFaceServiceReceiver != null) {
405 if (biometric instanceof Face) {
406 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face)biometric);
407 } else {
408 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
409 }
410 }
411 }
412
413 @Override
414 public void onAuthenticationFailed(long deviceId) throws RemoteException {
415 if (mFaceServiceReceiver != null) {
416 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
417 }
418 }
419
420 @Override
421 public void onError(long deviceId, int error, int vendorCode) throws RemoteException {
422 if (mFaceServiceReceiver != null) {
423 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
424 }
425 }
426
427 @Override
428 public void onRemoved(BiometricAuthenticator.Identifier identifier,
429 int remaining) throws RemoteException {
430 if (mFaceServiceReceiver != null) {
431 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
432 identifier.getBiometricId(), remaining);
433 }
434 }
435
436 @Override
437 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
438 throws RemoteException {
439 if (mFaceServiceReceiver != null) {
440
441 }
442 }
443 }
444
445 private final FaceMetrics mFaceMetrics = new FaceMetrics();
446
447 @GuardedBy("this")
448 private IBiometricsFace mDaemon;
Kevin Chyna56dff72018-06-19 18:41:12 -0700449 private long mHalDeviceId;
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700450 // Use FaceManager to get strings, so BiometricPrompt interface is cleaner
451 private FaceManager mFaceManager;
Kevin Chyna56dff72018-06-19 18:41:12 -0700452
453 /**
454 * Receives callbacks from the HAL.
455 */
456 private IBiometricsFaceClientCallback mDaemonCallback =
457 new IBiometricsFaceClientCallback.Stub() {
458 @Override
459 public void onEnrollResult(final long deviceId, int faceId, int userId,
460 int remaining) {
461 mHandler.post(() -> {
462 final Face face = new Face(getBiometricUtils()
463 .getUniqueName(getContext(), userId), faceId, deviceId);
464 FaceService.super.handleEnrollResult(face, remaining);
465 });
466 }
467
468 @Override
469 public void onAcquired(final long deviceId, final int userId,
470 final int acquiredInfo,
471 final int vendorCode) {
472 mHandler.post(() -> {
473 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
474 });
475 }
476
477 @Override
478 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
479 ArrayList<Byte> token) {
480 mHandler.post(() -> {
Kevin Chynb528d692018-07-20 11:53:14 -0700481 Face face = new Face("", faceId, deviceId);
482 FaceService.super.handleAuthenticated(face, token);
Kevin Chyna56dff72018-06-19 18:41:12 -0700483 });
484 }
485
486 @Override
487 public void onError(final long deviceId, final int userId, final int error,
488 final int vendorCode) {
489 mHandler.post(() -> {
490 FaceService.super.handleError(deviceId, error, vendorCode);
491
492 // TODO: this chunk of code should be common to all biometric services
493 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
494 // If we get HW_UNAVAILABLE, try to connect again later...
495 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
496 synchronized (this) {
497 mDaemon = null;
498 mHalDeviceId = 0;
499 mCurrentUserId = UserHandle.USER_NULL;
500 }
501 }
502 });
503 }
504
505 @Override
506 public void onRemoved(final long deviceId, final int faceId, final int userId,
507 final int remaining) {
508 mHandler.post(() -> {
509 final Face face = new Face("", faceId, deviceId);
510 FaceService.super.handleRemoved(face, remaining);
511 });
512 }
513
514 @Override
515 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
516 throws RemoteException {
517 // TODO
518 }
519 };
520
521 /**
522 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
523 * can be shared between the multiple biometric services.
524 */
525 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
526 @Override
527 public int authenticate(long operationId, int groupId) throws RemoteException {
528 IBiometricsFace daemon = getFaceDaemon();
529 if (daemon == null) {
530 Slog.w(TAG, "authenticate(): no face HAL!");
531 return ERROR_ESRCH;
532 }
533 return daemon.authenticate(operationId);
534 }
535
536 @Override
537 public int cancel() throws RemoteException {
538 IBiometricsFace daemon = getFaceDaemon();
539 if (daemon == null) {
540 Slog.w(TAG, "cancel(): no face HAL!");
541 return ERROR_ESRCH;
542 }
543 return daemon.cancel();
544 }
545
546 @Override
547 public int remove(int groupId, int biometricId) throws RemoteException {
548 IBiometricsFace daemon = getFaceDaemon();
549 if (daemon == null) {
550 Slog.w(TAG, "remove(): no face HAL!");
551 return ERROR_ESRCH;
552 }
553 return daemon.remove(biometricId);
554 }
555
556 @Override
557 public int enumerate() throws RemoteException {
558 IBiometricsFace daemon = getFaceDaemon();
559 if (daemon == null) {
560 Slog.w(TAG, "enumerate(): no face HAL!");
561 return ERROR_ESRCH;
562 }
563 return daemon.enumerate();
564 }
565
566 @Override
567 public int enroll(byte[] cryptoToken, int groupId, int timeout) throws RemoteException {
568 IBiometricsFace daemon = getFaceDaemon();
569 if (daemon == null) {
570 Slog.w(TAG, "enroll(): no face HAL!");
571 return ERROR_ESRCH;
572 }
573 final ArrayList<Byte> token = new ArrayList<>();
574 for (int i = 0; i < cryptoToken.length; i++) {
575 token.add(cryptoToken[i]);
576 }
Kevin Chyna0e920f2018-08-31 15:16:55 -0700577 // TODO: plumb requireAttention down from framework
578 return daemon.enroll(token, timeout, true /* requireAttention */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700579 }
580 };
581
582
583 public FaceService(Context context) {
584 super(context);
Kevin Chyna56dff72018-06-19 18:41:12 -0700585 }
586
587 @Override
588 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700589 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700590 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
591 SystemServerInitThreadPool.get().submit(this::getFaceDaemon, TAG + ".onStart");
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700592 mFaceManager = (FaceManager) getContext().getSystemService(Context.FACE_SERVICE);
Kevin Chyna56dff72018-06-19 18:41:12 -0700593 }
594
595 @Override
596 public String getTag() {
597 return TAG;
598 }
599
600 @Override
601 protected BiometricUtils getBiometricUtils() {
602 return FaceUtils.getInstance();
603 }
604
605 @Override
606 protected int getFailedAttemptsLockoutTimed() {
607 return MAX_FAILED_ATTEMPTS_LOCKOUT_TIMED;
608 }
609
610 @Override
611 protected int getFailedAttemptsLockoutPermanent() {
612 return MAX_FAILED_ATTEMPTS_LOCKOUT_PERMANENT;
613 }
614
615 @Override
616 protected Metrics getMetrics() {
617 return mFaceMetrics;
618 }
619
620 @Override
621 protected boolean hasReachedEnrollmentLimit(int userId) {
622 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700623 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyna56dff72018-06-19 18:41:12 -0700624 final int enrolled = FaceService.this.getEnrolledFaces(userId).size();
625 if (enrolled >= limit) {
626 Slog.w(TAG, "Too many faces registered");
627 return true;
628 }
629 return false;
630 }
631
632 @Override
633 protected void updateActiveGroup(int userId, String clientPackage) {
634 IBiometricsFace daemon = getFaceDaemon();
635
636 if (daemon != null) {
637 try {
638 userId = getUserOrWorkProfileId(clientPackage, userId);
639 if (userId != mCurrentUserId) {
640 final File baseDir = Environment.getDataVendorDeDirectory(userId);
641 final File faceDir = new File(baseDir, FACE_DATA_DIR);
642 if (!faceDir.exists()) {
643 if (!faceDir.mkdir()) {
644 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
645 return;
646 }
647 // Calling mkdir() from this process will create a directory with our
648 // permissions (inherited from the containing dir). This command fixes
649 // the label.
650 if (!SELinux.restorecon(faceDir)) {
651 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
652 return;
653 }
654 }
655
656 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
657 mCurrentUserId = userId;
658 }
659 mAuthenticatorIds.put(userId,
660 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
661 } catch (RemoteException e) {
662 Slog.e(TAG, "Failed to setActiveUser():", e);
663 }
664 }
665 }
666
667 @Override
668 protected String getLockoutResetIntent() {
669 return ACTION_LOCKOUT_RESET;
670 }
671
672 @Override
673 protected String getLockoutBroadcastPermission() {
674 return RESET_FACE_LOCKOUT;
675 }
676
677 @Override
678 protected long getHalDeviceId() {
679 return mHalDeviceId;
680 }
681
682 @Override
683 protected void handleUserSwitching(int userId) {
684 updateActiveGroup(userId, null);
685 }
686
687 @Override
688 protected boolean hasEnrolledBiometrics(int userId) {
689 if (userId != UserHandle.getCallingUserId()) {
690 checkPermission(INTERACT_ACROSS_USERS);
691 }
692 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
693 }
694
695 @Override
696 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700697 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -0700698 }
699
700 @Override
701 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700702 // noop for Face. The permission checks are all done on the incoming binder call.
703 // TODO: Perhaps do the same in FingerprintService
Kevin Chyna56dff72018-06-19 18:41:12 -0700704 }
705
706 @Override
707 protected int getAppOp() {
708 return AppOpsManager.OP_USE_FACE;
709 }
710
711 @Override
712 protected void notifyClientActiveCallbacks(boolean isActive) {
713 // noop for Face.
714 }
715
716 /** Gets the face daemon */
717 private synchronized IBiometricsFace getFaceDaemon() {
718 if (mDaemon == null) {
719 Slog.v(TAG, "mDaemon was null, reconnect to face");
720 try {
721 mDaemon = IBiometricsFace.getService();
722 } catch (java.util.NoSuchElementException e) {
723 // Service doesn't exist or cannot be opened. Logged below.
724 } catch (RemoteException e) {
725 Slog.e(TAG, "Failed to get biometric interface", e);
726 }
727 if (mDaemon == null) {
728 Slog.w(TAG, "face HIDL not available");
729 return null;
730 }
731
732 mDaemon.asBinder().linkToDeath(this, 0);
733
734 try {
735 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
736 } catch (RemoteException e) {
737 Slog.e(TAG, "Failed to open face HAL", e);
738 mDaemon = null; // try again later!
739 }
740
741 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
742 if (mHalDeviceId != 0) {
743 loadAuthenticatorIds();
744 updateActiveGroup(ActivityManager.getCurrentUser(), null);
745 } else {
746 Slog.w(TAG, "Failed to open Face HAL!");
747 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
748 mDaemon = null;
749 }
750 }
751 return mDaemon;
752 }
753
754 private long startPreEnroll(IBinder token) {
755 IBiometricsFace daemon = getFaceDaemon();
756 if (daemon == null) {
757 Slog.w(TAG, "startPreEnroll: no face HAL!");
758 return 0;
759 }
760 try {
Kevin Chyn96c92972018-08-31 16:09:31 -0700761 return daemon.generateChallenge().value;
Kevin Chyna56dff72018-06-19 18:41:12 -0700762 } catch (RemoteException e) {
763 Slog.e(TAG, "startPreEnroll failed", e);
764 }
765 return 0;
766 }
767
768 private int startPostEnroll(IBinder token) {
769 IBiometricsFace daemon = getFaceDaemon();
770 if (daemon == null) {
771 Slog.w(TAG, "startPostEnroll: no face HAL!");
772 return 0;
773 }
774 try {
Kevin Chyn96c92972018-08-31 16:09:31 -0700775 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -0700776 } catch (RemoteException e) {
777 Slog.e(TAG, "startPostEnroll failed", e);
778 }
779 return 0;
780 }
781
782 private List<Face> getEnrolledFaces(int userId) {
783 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200784 }
785
786 private void dumpInternal(PrintWriter pw) {
787 JSONObject dump = new JSONObject();
788 try {
789 dump.put("service", "Face Manager");
790
791 JSONArray sets = new JSONArray();
792 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
793 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -0700794 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200795 PerformanceStats stats = mPerformanceMap.get(userId);
796 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
797 JSONObject set = new JSONObject();
798 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700799 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200800 set.put("accept", (stats != null) ? stats.accept : 0);
801 set.put("reject", (stats != null) ? stats.reject : 0);
802 set.put("acquire", (stats != null) ? stats.acquire : 0);
803 set.put("lockout", (stats != null) ? stats.lockout : 0);
804 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
805 // cryptoStats measures statistics about secure face transactions
806 // (e.g. to unlock password storage, make secure purchases, etc.)
807 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
808 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
809 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
810 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700811 set.put("permanentLockoutCrypto",
812 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200813 sets.put(set);
814 }
815
816 dump.put("prints", sets);
817 } catch (JSONException e) {
818 Slog.e(TAG, "dump formatting failure", e);
819 }
820 pw.println(dump);
821 }
822
823 private void dumpProto(FileDescriptor fd) {
824 final ProtoOutputStream proto = new ProtoOutputStream(fd);
825 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
826 final int userId = user.getUserHandle().getIdentifier();
827
828 final long userToken = proto.start(FaceServiceDumpProto.USERS);
829
830 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700831 proto.write(FaceUserStatsProto.NUM_FACES,
832 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200833
834 // Normal face authentications (e.g. lockscreen)
835 final PerformanceStats normal = mPerformanceMap.get(userId);
836 if (normal != null) {
837 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
838 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
839 proto.write(FaceActionStatsProto.REJECT, normal.reject);
840 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
841 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
842 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
843 proto.end(countsToken);
844 }
845
846 // Statistics about secure face transactions (e.g. to unlock password
847 // storage, make secure purchases, etc.)
848 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
849 if (crypto != null) {
850 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
851 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
852 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
853 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
854 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
855 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
856 proto.end(countsToken);
857 }
858
859 proto.end(userToken);
860 }
861 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -0700862 mPerformanceMap.clear();
863 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200864 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700865}