blob: ee49f5885e4a3d3318f9223693a333269352b55a [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;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020057
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070058import com.android.internal.R;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020059import com.android.internal.annotations.GuardedBy;
60import com.android.internal.logging.MetricsLogger;
61import com.android.internal.util.DumpUtils;
62import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080063import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070064import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070065import com.android.server.biometrics.BiometricUtils;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -070066import com.android.server.biometrics.ClientMonitor;
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;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070082import java.util.HashMap;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020083import java.util.List;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070084import java.util.Map;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020085
86/**
87 * A service to manage multiple clients that want to access the face HAL API.
88 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080089 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020090 *
91 * @hide
92 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070093public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070094
95 protected static final String TAG = "FaceService";
96 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020097 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020098 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070099 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -0700100 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200101
Kevin Chyndbfbed42019-06-13 17:01:30 -0700102 private static final String NOTIFICATION_TAG = "FaceService";
103 private static final int NOTIFICATION_ID = 1;
104
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700105 /**
106 * Events for bugreports.
107 */
108 public static final class AuthenticationEvent {
109 private long mStartTime;
110 private long mLatency;
111 // Only valid if mError is 0
112 private boolean mAuthenticated;
113 private int mError;
114 // Only valid if mError is ERROR_VENDOR
115 private int mVendorError;
116
117 AuthenticationEvent(long startTime, long latency, boolean authenticated, int error,
118 int vendorError) {
119 mStartTime = startTime;
120 mLatency = latency;
121 mAuthenticated = authenticated;
122 mError = error;
123 mVendorError = vendorError;
124 }
125
126 public String toString(Context context) {
127 return "Start: " + mStartTime
128 + "\tLatency: " + mLatency
129 + "\tAuthenticated: " + mAuthenticated
130 + "\tError: " + mError
131 + "\tVendorCode: " + mVendorError
132 + "\t" + FaceManager.getErrorString(context, mError, mVendorError);
133 }
134 }
135
136 /**
137 * Keep a short historical buffer of stats, with an aggregated usage time.
138 */
139 private class UsageStats {
140 static final int EVENT_LOG_SIZE = 100;
141
142 Context mContext;
143 List<AuthenticationEvent> mAuthenticationEvents;
144
145 int acceptCount;
146 int rejectCount;
147 Map<Integer, Integer> mErrorCount;
148
149 long acceptLatency;
150 long rejectLatency;
151 Map<Integer, Long> mErrorLatency;
152
153 UsageStats(Context context) {
154 mAuthenticationEvents = new ArrayList<>();
155 mErrorCount = new HashMap<>();
156 mErrorLatency = new HashMap<>();
157 mContext = context;
158 }
159
160 void addEvent(AuthenticationEvent event) {
161 if (mAuthenticationEvents.size() >= EVENT_LOG_SIZE) {
162 mAuthenticationEvents.remove(0);
163 }
164 mAuthenticationEvents.add(event);
165
166 if (event.mAuthenticated) {
167 acceptCount++;
168 acceptLatency += event.mLatency;
169 } else if (event.mError == 0) {
170 rejectCount++;
171 rejectLatency += event.mLatency;
172 } else {
173 mErrorCount.put(event.mError, mErrorCount.getOrDefault(event.mError, 0) + 1);
174 mErrorLatency.put(event.mError,
175 mErrorLatency.getOrDefault(event.mError, 0l) + event.mLatency);
176 }
177 }
178
179 void print(PrintWriter pw) {
180 pw.println("Events since last reboot: " + mAuthenticationEvents.size());
181 for (int i = 0; i < mAuthenticationEvents.size(); i++) {
182 pw.println(mAuthenticationEvents.get(i).toString(mContext));
183 }
184
185 // Dump aggregated usage stats
186 // TODO: Remove or combine with json dump in a future release
187 pw.println("Accept\tCount: " + acceptCount + "\tLatency: " + acceptLatency
188 + "\tAverage: " + (acceptCount > 0 ? acceptLatency / acceptCount : 0));
189 pw.println("Reject\tCount: " + rejectCount + "\tLatency: " + rejectLatency
190 + "\tAverage: " + (rejectCount > 0 ? rejectLatency / rejectCount : 0));
191
192 for (Integer key : mErrorCount.keySet()) {
193 final int count = mErrorCount.get(key);
194 pw.println("Error" + key + "\tCount: " + count
195 + "\tLatency: " + mErrorLatency.getOrDefault(key, 0l)
196 + "\tAverage: " + (count > 0 ? mErrorLatency.getOrDefault(key, 0l) / count
197 : 0)
198 + "\t" + FaceManager.getErrorString(mContext, key, 0 /* vendorCode */));
199 }
200 }
201 }
202
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700203 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700204 private int mLastAcquire;
205
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700206 public FaceAuthClient(Context context,
207 DaemonWrapper daemon, long halDeviceId, IBinder token,
208 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800209 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700210 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800211 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700212 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800213
214 @Override
215 protected int statsModality() {
216 return FaceService.this.statsModality();
217 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800218
219 @Override
220 public boolean shouldFrameworkHandleLockout() {
221 return false;
222 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800223
224 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700225 public boolean wasUserDetected() {
Ilya Matyukhinc90507e2019-08-20 15:38:57 -0700226 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED
227 && mLastAcquire != FaceManager.FACE_ACQUIRED_SENSOR_DIRTY;
Kevin Chyn0ce70852019-05-10 10:29:18 -0700228 }
229
230 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800231 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
232 boolean authenticated, ArrayList<Byte> token) {
233 final boolean result = super.onAuthenticated(identifier, authenticated, token);
234
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700235 mUsageStats.addEvent(new AuthenticationEvent(
236 getStartTimeMs(),
237 System.currentTimeMillis() - getStartTimeMs() /* latency */,
238 authenticated,
239 0 /* error */,
240 0 /* vendorError */));
241
Kevin Chyn56d6b072019-02-13 18:39:01 -0800242 // For face, the authentication lifecycle ends either when
243 // 1) Authenticated == true
244 // 2) Error occurred
245 // 3) Authenticated == false
246 // Fingerprint currently does not end when the third condition is met which is a bug,
247 // but let's leave it as-is for now.
248 return result || !authenticated;
249 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700250
251 @Override
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700252 public boolean onError(long deviceId, int error, int vendorCode) {
253 mUsageStats.addEvent(new AuthenticationEvent(
254 getStartTimeMs(),
255 System.currentTimeMillis() - getStartTimeMs() /* latency */,
256 false /* authenticated */,
257 error,
258 vendorCode));
259
260 return super.onError(deviceId, error, vendorCode);
261 }
262
263 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700264 public int[] getAcquireIgnorelist() {
265 if (isBiometricPrompt()) {
266 return mBiometricPromptIgnoreList;
267 } else {
268 // Keyguard
269 return mKeyguardIgnoreList;
270 }
271 }
272
273 @Override
274 public int[] getAcquireVendorIgnorelist() {
275 if (isBiometricPrompt()) {
276 return mBiometricPromptIgnoreListVendor;
277 } else {
278 // Keyguard
279 return mKeyguardIgnoreListVendor;
280 }
281 }
282
283 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700284 public boolean onAcquired(int acquireInfo, int vendorCode) {
285
Kevin Chyn0ce70852019-05-10 10:29:18 -0700286 mLastAcquire = acquireInfo;
287
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700288 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
289 final String name =
290 getContext().getString(R.string.face_recalibrate_notification_name);
291 final String title =
292 getContext().getString(R.string.face_recalibrate_notification_title);
293 final String content =
294 getContext().getString(R.string.face_recalibrate_notification_content);
295
296 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
297 intent.setPackage("com.android.settings");
298
299 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
300 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
301 UserHandle.CURRENT);
302
Kevin Chyndbfbed42019-06-13 17:01:30 -0700303 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700304
Kevin Chyndbfbed42019-06-13 17:01:30 -0700305 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700306 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700307 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700308 .setSmallIcon(R.drawable.ic_lock)
309 .setContentTitle(title)
310 .setContentText(content)
311 .setSubText(name)
312 .setOnlyAlertOnce(true)
313 .setLocalOnly(true)
314 .setAutoCancel(true)
315 .setCategory(Notification.CATEGORY_SYSTEM)
316 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700317 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700318 .build();
319
Kevin Chyndbfbed42019-06-13 17:01:30 -0700320 mNotificationManager.createNotificationChannel(channel);
321 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
322 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700323 }
324
325 return super.onAcquired(acquireInfo, vendorCode);
326 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700327 }
328
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200329 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700330 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200331 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200332 private final class FaceServiceWrapper extends IFaceService.Stub {
Curtis Belmonte45788542019-07-23 11:07:27 -0700333 private static final int ENROLL_TIMEOUT_SEC = 75;
Kevin Chyna56dff72018-06-19 18:41:12 -0700334
335 /**
336 * The following methods contain common code which is shared in biometrics/common.
337 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800338
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200339 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700340 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700341 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700342 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200343 }
344
345 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700346 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700347 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn7a207e42019-07-27 17:50:33 -0700348 mHandler.post(() -> {
349 // TODO(b/137106905): Schedule binder calls in FaceService to avoid deadlocks.
350 if (getCurrentClient() == null) {
351 // if we aren't handling any other HIDL calls (mCurrentClient == null), revoke
352 // the challenge right away.
353 startRevokeChallenge(token);
354 } else {
355 // postpone revoking the challenge until we finish processing the current HIDL
356 // call.
357 mRevokeChallengePending = true;
358 }
359 });
360 return Status.OK;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200361 }
362
363 @Override // Binder call
Kevin Chyn593e6262019-06-28 13:24:44 -0700364 public void enroll(int userId, final IBinder token, final byte[] cryptoToken,
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800365 final IFaceServiceReceiver receiver, final String opPackageName,
366 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700367 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700368 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200369
Kevin Chyndbfbed42019-06-13 17:01:30 -0700370 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
371 UserHandle.CURRENT);
372
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200373 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700374 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
375 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Curtis Belmonte45788542019-07-23 11:07:27 -0700376 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures,
377 ENROLL_TIMEOUT_SEC) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700378
379 @Override
380 public int[] getAcquireIgnorelist() {
381 return mEnrollIgnoreList;
382 }
383
384 @Override
385 public int[] getAcquireVendorIgnorelist() {
386 return mEnrollIgnoreListVendor;
387 }
388
Kevin Chyn1429a312019-01-28 16:08:09 -0800389 @Override
390 public boolean shouldVibrate() {
391 return false;
392 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800393
394 @Override
395 protected int statsModality() {
396 return FaceService.this.statsModality();
397 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800398 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200399
Kevin Chyn1a878c12019-04-04 15:50:11 -0700400 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200401 }
402
403 @Override // Binder call
404 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700405 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700406 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200407 }
408
409 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800410 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700411 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700412 final String opPackageName) {
413 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800414 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200415 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700416 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700417 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700418 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800419 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700420 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200421 }
422
423 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800424 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800425 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
426 String opPackageName, int cookie, int callingUid, int callingPid,
427 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700428 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800429 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700430 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700431 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700432 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800433 new BiometricPromptServiceListenerImpl(wrapperReceiver),
434 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800435 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700436 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
437 callingUserId);
438 }
439
440 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800441 public void startPreparedClient(int cookie) {
442 checkPermission(MANAGE_BIOMETRIC);
443 startCurrentClient(cookie);
444 }
445
446 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200447 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700448 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700449 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200450 }
451
452 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700453 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800454 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700455 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800456 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
457 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700458 }
459
460 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200461 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700462 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700463 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200464 }
465
466 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700467 public void remove(final IBinder token, final int faceId, final int userId,
Kevin Chyn593e6262019-06-28 13:24:44 -0700468 final IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700469 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700470 updateActiveGroup(userId, opPackageName);
Kevin Chyna56dff72018-06-19 18:41:12 -0700471
472 if (token == null) {
473 Slog.w(TAG, "remove(): token is null");
474 return;
475 }
476
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200477 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700478 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800479 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
480 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800481 @Override
482 protected int statsModality() {
483 return FaceService.this.statsModality();
484 }
485 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700486 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200487 }
488
Kevin Chyna56dff72018-06-19 18:41:12 -0700489 @Override
490 public void enumerate(final IBinder token, final int userId,
491 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700492 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700493
494 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700495 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800496 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
497 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800498 @Override
499 protected int statsModality() {
500 return FaceService.this.statsModality();
501 }
502 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700503 enumerateInternal(client);
504 }
505
506 @Override
507 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
508 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700509 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700510 FaceService.super.addLockoutResetCallback(callback);
511 }
512
513 @Override // Binder call
514 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
515 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
516 return;
517 }
518
519 final long ident = Binder.clearCallingIdentity();
520 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700521 if (args.length > 1 && "--hal".equals(args[0])) {
522 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Kevin Chyna56dff72018-06-19 18:41:12 -0700523 } else {
524 dumpInternal(pw);
525 }
526 } finally {
527 Binder.restoreCallingIdentity(ident);
528 }
529 }
530
531 /**
532 * The following methods don't use any common code from BiometricService
533 */
534
535 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200536 @Override // Binder call
537 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700538 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700539 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200540 Binder.getCallingUid(), Binder.getCallingPid(),
541 UserHandle.getCallingUserId())) {
542 return false;
543 }
544
545 final long token = Binder.clearCallingIdentity();
546 try {
547 IBiometricsFace daemon = getFaceDaemon();
548 return daemon != null && mHalDeviceId != 0;
549 } finally {
550 Binder.restoreCallingIdentity(token);
551 }
552 }
553
554 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700555 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700556 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700557 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
558 return;
559 }
560 mHandler.post(new Runnable() {
561 @Override
562 public void run() {
563 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
564 faceId, name);
565 }
566 });
567 }
568
569 @Override // Binder call
570 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700571 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700572 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200573 Binder.getCallingUid(), Binder.getCallingPid(),
574 UserHandle.getCallingUserId())) {
575 return null;
576 }
577
Kevin Chyn6737c572019-02-08 16:10:54 -0800578 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200579 }
580
581 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700582 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700583 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700584 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200585 Binder.getCallingUid(), Binder.getCallingPid(),
586 UserHandle.getCallingUserId())) {
587 return false;
588 }
589
Kevin Chyna56dff72018-06-19 18:41:12 -0700590 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200591 }
592
593 @Override // Binder call
594 public long getAuthenticatorId(String opPackageName) {
595 // In this method, we're not checking whether the caller is permitted to use face
596 // API because current authenticator ID is leaked (in a more contrived way) via Android
597 // Keystore (android.security.keystore package): the user of that API can create a key
598 // which requires face authentication for its use, and then query the key's
599 // characteristics (hidden API) which returns, among other things, face
600 // authenticator ID which was active at key creation time.
601 //
602 // Reason: The part of Android Keystore which runs inside an app's process invokes this
603 // method in certain cases. Those cases are not always where the developer demonstrates
604 // explicit intent to use face functionality. Thus, to avoiding throwing an
605 // unexpected SecurityException this method does not check whether its caller is
606 // permitted to use face API.
607 //
608 // The permission check should be restored once Android Keystore no longer invokes this
609 // method from inside app processes.
610
611 return FaceService.this.getAuthenticatorId(opPackageName);
612 }
613
614 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800615 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700616 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700617
joshmccloskeya4772062019-07-24 17:13:16 -0700618 mHandler.post(() -> {
619 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
620 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
621 return;
622 }
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700623
joshmccloskeya4772062019-07-24 17:13:16 -0700624 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
Kevin Chynbe67ce02019-06-10 16:14:22 -0700625
joshmccloskeya4772062019-07-24 17:13:16 -0700626 try {
627 mDaemonWrapper.resetLockout(token);
628 } catch (RemoteException e) {
629 Slog.e(getTag(), "Unable to reset lockout", e);
630 }
631 });
Kevin Chyna56dff72018-06-19 18:41:12 -0700632 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700633
634 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700635 public void setFeature(int userId, int feature, boolean enabled, final byte[] token,
636 IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700637 checkPermission(MANAGE_BIOMETRIC);
638
Kevin Chyne62749a2019-04-02 19:33:56 -0700639 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700640 if (DEBUG) {
641 Slog.d(TAG, "setFeature for user(" + userId + ")");
642 }
643 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700644 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
645 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
646 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800647 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700648
649 final ArrayList<Byte> byteToken = new ArrayList<>();
650 for (int i = 0; i < token.length; i++) {
651 byteToken.add(token[i]);
652 }
653
654 // TODO: Support multiple faces
655 final int faceId = getFirstTemplateForUser(mCurrentUserId);
656
657 if (mDaemon != null) {
658 try {
659 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
660 receiver.onFeatureSet(result == Status.OK, feature);
661 } catch (RemoteException e) {
662 Slog.e(getTag(), "Unable to set feature: " + feature
663 + " to enabled:" + enabled, e);
664 }
665 }
666 });
667
Kevin Chynd79e24e2018-09-25 12:06:59 -0700668 }
669
670 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700671 public void getFeature(int userId, int feature, IFaceServiceReceiver receiver,
672 final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700673 checkPermission(MANAGE_BIOMETRIC);
674
Kevin Chyne62749a2019-04-02 19:33:56 -0700675 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700676 if (DEBUG) {
677 Slog.d(TAG, "getFeature for user(" + userId + ")");
678 }
679 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700680 // This should ideally return tri-state, but the user isn't shown settings unless
681 // they are enrolled so it's fine for now.
682 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
683 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
684 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800685 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700686
687 // TODO: Support multiple faces
688 final int faceId = getFirstTemplateForUser(mCurrentUserId);
689
690 if (mDaemon != null) {
691 try {
692 OptionalBool result = mDaemon.getFeature(feature, faceId);
693 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
694 } catch (RemoteException e) {
695 Slog.e(getTag(), "Unable to getRequireAttention", e);
696 }
697 }
698 });
699
Kevin Chynd79e24e2018-09-25 12:06:59 -0700700 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700701
702 @Override
703 public void userActivity() {
704 checkPermission(MANAGE_BIOMETRIC);
705
706 if (mDaemon != null) {
707 try {
708 mDaemon.userActivity();
709 } catch (RemoteException e) {
710 Slog.e(getTag(), "Unable to send userActivity", e);
711 }
712 }
713 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800714
715 // TODO: Support multiple faces
716 private int getFirstTemplateForUser(int user) {
717 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
718 if (!faces.isEmpty()) {
719 return faces.get(0).getBiometricId();
720 }
721 return 0;
722 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700723 }
724
725 /**
726 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700727 * BiometricPrompt.
728 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800729 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800730 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800731 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700732 }
733
734 @Override
735 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
736 throws RemoteException {
737 /**
738 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
739 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800740 if (getWrapperReceiver() != null) {
741 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700742 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
743 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700744 }
745 }
746
747 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800748 public void onError(long deviceId, int error, int vendorCode, int cookie)
749 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800750 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800751 getWrapperReceiver().onError(cookie, error,
752 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700753 }
754 }
755 }
756
757 /**
758 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700759 * the FaceManager.
760 */
761 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700762 private IFaceServiceReceiver mFaceServiceReceiver;
763
764 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
765 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200766 }
767
768 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700769 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200770 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700771 if (mFaceServiceReceiver != null) {
772 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
773 identifier.getBiometricId(),
774 remaining);
775 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200776 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700777
778 @Override
779 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
780 throws RemoteException {
781 if (mFaceServiceReceiver != null) {
782 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
783 }
784 }
785
786 @Override
787 public void onAuthenticationSucceeded(long deviceId,
788 BiometricAuthenticator.Identifier biometric, int userId)
789 throws RemoteException {
790 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800791 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700792 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
793 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700794 } else {
795 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
796 }
797 }
798 }
799
800 @Override
801 public void onAuthenticationFailed(long deviceId) throws RemoteException {
802 if (mFaceServiceReceiver != null) {
803 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
804 }
805 }
806
807 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800808 public void onError(long deviceId, int error, int vendorCode, int cookie)
809 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700810 if (mFaceServiceReceiver != null) {
811 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
812 }
813 }
814
815 @Override
816 public void onRemoved(BiometricAuthenticator.Identifier identifier,
817 int remaining) throws RemoteException {
818 if (mFaceServiceReceiver != null) {
819 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
820 identifier.getBiometricId(), remaining);
821 }
822 }
823
824 @Override
825 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
826 throws RemoteException {
827 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800828 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
829 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700830 }
831 }
832 }
833
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700834 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700835
836 @GuardedBy("this")
837 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700838 private UsageStats mUsageStats;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -0700839 private boolean mRevokeChallengePending = false;
Kevin Chyna38653c2019-02-11 17:46:21 -0800840 // One of the AuthenticationClient constants
841 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700842
Kevin Chyndbfbed42019-06-13 17:01:30 -0700843 private NotificationManager mNotificationManager;
844
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700845 private int[] mBiometricPromptIgnoreList;
846 private int[] mBiometricPromptIgnoreListVendor;
847 private int[] mKeyguardIgnoreList;
848 private int[] mKeyguardIgnoreListVendor;
849 private int[] mEnrollIgnoreList;
850 private int[] mEnrollIgnoreListVendor;
851
Kevin Chyna56dff72018-06-19 18:41:12 -0700852 /**
853 * Receives callbacks from the HAL.
854 */
855 private IBiometricsFaceClientCallback mDaemonCallback =
856 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800857 @Override
858 public void onEnrollResult(final long deviceId, int faceId, int userId,
859 int remaining) {
860 mHandler.post(() -> {
861 final Face face = new Face(getBiometricUtils()
862 .getUniqueName(getContext(), userId), faceId, deviceId);
863 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700864
865 // Enrollment changes the authenticatorId, so update it here.
866 IBiometricsFace daemon = getFaceDaemon();
867 if (remaining == 0 && daemon != null) {
868 try {
869 mAuthenticatorIds.put(userId,
870 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
871 : 0L);
872 } catch (RemoteException e) {
873 Slog.e(TAG, "Unable to get authenticatorId", e);
874 }
875 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800876 });
877 }
878
879 @Override
880 public void onAcquired(final long deviceId, final int userId,
881 final int acquiredInfo,
882 final int vendorCode) {
883 mHandler.post(() -> {
884 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
885 });
886 }
887
888 @Override
889 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
890 ArrayList<Byte> token) {
891 mHandler.post(() -> {
892 Face face = new Face("", faceId, deviceId);
893 FaceService.super.handleAuthenticated(face, token);
894 });
895 }
896
897 @Override
898 public void onError(final long deviceId, final int userId, final int error,
899 final int vendorCode) {
900 mHandler.post(() -> {
901 FaceService.super.handleError(deviceId, error, vendorCode);
902
903 // TODO: this chunk of code should be common to all biometric services
904 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
905 // If we get HW_UNAVAILABLE, try to connect again later...
906 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
907 synchronized (this) {
908 mDaemon = null;
909 mHalDeviceId = 0;
910 mCurrentUserId = UserHandle.USER_NULL;
911 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700912 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800913 });
914 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700915
Kevin Chyn6737c572019-02-08 16:10:54 -0800916 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700917 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800918 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700919 if (!faceIds.isEmpty()) {
920 for (int i = 0; i < faceIds.size(); i++) {
921 final Face face = new Face("", faceIds.get(i), deviceId);
922 // Convert to old behavior
923 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
924 }
925 } else {
926 final Face face = new Face("", 0 /* identifier */, deviceId);
927 FaceService.super.handleRemoved(face, 0 /* remaining */);
928 }
929
Kevin Chyn6737c572019-02-08 16:10:54 -0800930 });
931 }
932
933 @Override
934 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
935 throws RemoteException {
936 mHandler.post(() -> {
937 if (!faceIds.isEmpty()) {
938 for (int i = 0; i < faceIds.size(); i++) {
939 final Face face = new Face("", faceIds.get(i), deviceId);
940 // Convert to old old behavior
941 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
942 }
943 } else {
944 // For face, the HIDL contract is to receive an empty list when there are no
945 // templates enrolled. Send a null identifier since we don't consume them
946 // anywhere, and send remaining == 0 to plumb this with existing common code.
947 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700948 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800949 });
950 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700951
Kevin Chyn6737c572019-02-08 16:10:54 -0800952 @Override
953 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800954 Slog.d(TAG, "onLockoutChanged: " + duration);
955 if (duration == 0) {
956 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
957 } else if (duration == Long.MAX_VALUE) {
958 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
959 } else {
960 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
961 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700962
Kevin Chyna38653c2019-02-11 17:46:21 -0800963 mHandler.post(() -> {
964 if (duration == 0) {
965 notifyLockoutResetMonitors();
966 }
967 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800968 }
969 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700970
971 /**
972 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
973 * can be shared between the multiple biometric services.
974 */
975 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
976 @Override
977 public int authenticate(long operationId, int groupId) throws RemoteException {
978 IBiometricsFace daemon = getFaceDaemon();
979 if (daemon == null) {
980 Slog.w(TAG, "authenticate(): no face HAL!");
981 return ERROR_ESRCH;
982 }
983 return daemon.authenticate(operationId);
984 }
985
986 @Override
987 public int cancel() throws RemoteException {
988 IBiometricsFace daemon = getFaceDaemon();
989 if (daemon == null) {
990 Slog.w(TAG, "cancel(): no face HAL!");
991 return ERROR_ESRCH;
992 }
993 return daemon.cancel();
994 }
995
996 @Override
997 public int remove(int groupId, int biometricId) throws RemoteException {
998 IBiometricsFace daemon = getFaceDaemon();
999 if (daemon == null) {
1000 Slog.w(TAG, "remove(): no face HAL!");
1001 return ERROR_ESRCH;
1002 }
1003 return daemon.remove(biometricId);
1004 }
1005
1006 @Override
1007 public int enumerate() throws RemoteException {
1008 IBiometricsFace daemon = getFaceDaemon();
1009 if (daemon == null) {
1010 Slog.w(TAG, "enumerate(): no face HAL!");
1011 return ERROR_ESRCH;
1012 }
1013 return daemon.enumerate();
1014 }
1015
1016 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001017 public int enroll(byte[] cryptoToken, int groupId, int timeout,
1018 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001019 IBiometricsFace daemon = getFaceDaemon();
1020 if (daemon == null) {
1021 Slog.w(TAG, "enroll(): no face HAL!");
1022 return ERROR_ESRCH;
1023 }
1024 final ArrayList<Byte> token = new ArrayList<>();
1025 for (int i = 0; i < cryptoToken.length; i++) {
1026 token.add(cryptoToken[i]);
1027 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001028 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -07001029 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001030
1031 @Override
1032 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1033 IBiometricsFace daemon = getFaceDaemon();
1034 if (daemon == null) {
1035 Slog.w(TAG, "resetLockout(): no face HAL!");
1036 return;
1037 }
1038 final ArrayList<Byte> token = new ArrayList<>();
1039 for (int i = 0; i < cryptoToken.length; i++) {
1040 token.add(cryptoToken[i]);
1041 }
1042 daemon.resetLockout(token);
1043 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001044 };
1045
1046
1047 public FaceService(Context context) {
1048 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001049
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001050 mUsageStats = new UsageStats(context);
1051
Kevin Chyndbfbed42019-06-13 17:01:30 -07001052 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1053
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001054 mBiometricPromptIgnoreList = getContext().getResources()
1055 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1056 mBiometricPromptIgnoreListVendor = getContext().getResources()
1057 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
1058 mKeyguardIgnoreList = getContext().getResources()
1059 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
1060 mKeyguardIgnoreListVendor = getContext().getResources()
1061 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
1062 mEnrollIgnoreList = getContext().getResources()
1063 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1064 mEnrollIgnoreListVendor = getContext().getResources()
1065 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001066 }
1067
1068 @Override
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001069 protected void removeClient(ClientMonitor client) {
1070 super.removeClient(client);
1071 if (mRevokeChallengePending) {
1072 startRevokeChallenge(null);
1073 mRevokeChallengePending = false;
1074 }
1075 }
1076
1077 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001078 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001079 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001080 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001081 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1082 // blocked
1083 SystemServerInitThreadPool.get().submit(() -> mHandler.post(this::getFaceDaemon),
1084 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001085 }
1086
1087 @Override
1088 public String getTag() {
1089 return TAG;
1090 }
1091
1092 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001093 protected DaemonWrapper getDaemonWrapper() {
1094 return mDaemonWrapper;
1095 }
1096
1097 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001098 protected BiometricUtils getBiometricUtils() {
1099 return FaceUtils.getInstance();
1100 }
1101
1102 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001103 protected Constants getConstants() {
1104 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001105 }
1106
1107 @Override
1108 protected boolean hasReachedEnrollmentLimit(int userId) {
1109 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001110 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001111 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001112 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001113 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001114 return true;
1115 }
1116 return false;
1117 }
1118
1119 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001120 public void serviceDied(long cookie) {
1121 super.serviceDied(cookie);
1122 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001123
1124 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001125 }
1126
1127 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001128 protected void updateActiveGroup(int userId, String clientPackage) {
1129 IBiometricsFace daemon = getFaceDaemon();
1130
1131 if (daemon != null) {
1132 try {
1133 userId = getUserOrWorkProfileId(clientPackage, userId);
1134 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001135 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001136 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1137 if (!faceDir.exists()) {
1138 if (!faceDir.mkdir()) {
1139 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1140 return;
1141 }
1142 // Calling mkdir() from this process will create a directory with our
1143 // permissions (inherited from the containing dir). This command fixes
1144 // the label.
1145 if (!SELinux.restorecon(faceDir)) {
1146 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1147 return;
1148 }
1149 }
1150
1151 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1152 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001153 mAuthenticatorIds.put(userId,
1154 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001155 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001156 } catch (RemoteException e) {
1157 Slog.e(TAG, "Failed to setActiveUser():", e);
1158 }
1159 }
1160 }
1161
1162 @Override
1163 protected String getLockoutResetIntent() {
1164 return ACTION_LOCKOUT_RESET;
1165 }
1166
1167 @Override
1168 protected String getLockoutBroadcastPermission() {
1169 return RESET_FACE_LOCKOUT;
1170 }
1171
1172 @Override
1173 protected long getHalDeviceId() {
1174 return mHalDeviceId;
1175 }
1176
1177 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001178 protected void handleUserSwitching(int userId) {
1179 super.handleUserSwitching(userId);
1180 // Will be updated when we get the callback from HAL
1181 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1182 }
1183
1184 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001185 protected boolean hasEnrolledBiometrics(int userId) {
1186 if (userId != UserHandle.getCallingUserId()) {
1187 checkPermission(INTERACT_ACROSS_USERS);
1188 }
1189 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1190 }
1191
1192 @Override
1193 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001194 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001195 }
1196
1197 @Override
1198 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001199 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001200 }
1201
1202 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001203 protected boolean checkAppOps(int uid, String opPackageName) {
1204 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1205 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001206 }
1207
1208 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001209 protected List<Face> getEnrolledTemplates(int userId) {
1210 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1211 }
1212
1213 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001214 protected void notifyClientActiveCallbacks(boolean isActive) {
1215 // noop for Face.
1216 }
1217
Kevin Chyn7782d142019-01-18 12:51:33 -08001218 @Override
1219 protected int statsModality() {
1220 return BiometricsProtoEnums.MODALITY_FACE;
1221 }
1222
Kevin Chyna38653c2019-02-11 17:46:21 -08001223 @Override
1224 protected int getLockoutMode() {
1225 return mCurrentUserLockoutMode;
1226 }
1227
Kevin Chyna56dff72018-06-19 18:41:12 -07001228 /** Gets the face daemon */
1229 private synchronized IBiometricsFace getFaceDaemon() {
1230 if (mDaemon == null) {
1231 Slog.v(TAG, "mDaemon was null, reconnect to face");
1232 try {
1233 mDaemon = IBiometricsFace.getService();
1234 } catch (java.util.NoSuchElementException e) {
1235 // Service doesn't exist or cannot be opened. Logged below.
1236 } catch (RemoteException e) {
1237 Slog.e(TAG, "Failed to get biometric interface", e);
1238 }
1239 if (mDaemon == null) {
1240 Slog.w(TAG, "face HIDL not available");
1241 return null;
1242 }
1243
1244 mDaemon.asBinder().linkToDeath(this, 0);
1245
1246 try {
1247 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1248 } catch (RemoteException e) {
1249 Slog.e(TAG, "Failed to open face HAL", e);
1250 mDaemon = null; // try again later!
1251 }
1252
1253 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1254 if (mHalDeviceId != 0) {
1255 loadAuthenticatorIds();
1256 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001257 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001258 } else {
1259 Slog.w(TAG, "Failed to open Face HAL!");
1260 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1261 mDaemon = null;
1262 }
1263 }
1264 return mDaemon;
1265 }
1266
Kevin Chynd79e24e2018-09-25 12:06:59 -07001267 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001268 IBiometricsFace daemon = getFaceDaemon();
1269 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001270 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001271 return 0;
1272 }
1273 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001274 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001275 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001276 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001277 }
1278 return 0;
1279 }
1280
Kevin Chynd79e24e2018-09-25 12:06:59 -07001281 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001282 IBiometricsFace daemon = getFaceDaemon();
1283 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001284 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001285 return 0;
1286 }
1287 try {
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001288 final int res = daemon.revokeChallenge();
1289 if (res != Status.OK) {
1290 Slog.e(TAG, "revokeChallenge returned " + res);
1291 }
1292 return res;
Kevin Chyna56dff72018-06-19 18:41:12 -07001293 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001294 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001295 }
1296 return 0;
1297 }
1298
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001299 private void dumpInternal(PrintWriter pw) {
1300 JSONObject dump = new JSONObject();
1301 try {
1302 dump.put("service", "Face Manager");
1303
1304 JSONArray sets = new JSONArray();
1305 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1306 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001307 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001308 PerformanceStats stats = mPerformanceMap.get(userId);
1309 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1310 JSONObject set = new JSONObject();
1311 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001312 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001313 set.put("accept", (stats != null) ? stats.accept : 0);
1314 set.put("reject", (stats != null) ? stats.reject : 0);
1315 set.put("acquire", (stats != null) ? stats.acquire : 0);
1316 set.put("lockout", (stats != null) ? stats.lockout : 0);
1317 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1318 // cryptoStats measures statistics about secure face transactions
1319 // (e.g. to unlock password storage, make secure purchases, etc.)
1320 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1321 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1322 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1323 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001324 set.put("permanentLockoutCrypto",
1325 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001326 sets.put(set);
1327 }
1328
1329 dump.put("prints", sets);
1330 } catch (JSONException e) {
1331 Slog.e(TAG, "dump formatting failure", e);
1332 }
1333 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001334 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1335
1336 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001337 }
1338
Joe Onoratodb396002019-04-05 19:49:27 -07001339 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001340 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1341 // production devices:
1342 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1343 // data or any data derived from it (such as embeddings) to the
1344 // Application Processor outside the context of the TEE.
1345 // As such, this API should only be enabled for testing purposes on
1346 // engineering and userdebug builds. All modules in the software stack
1347 // MUST enforce final build products do NOT have this functionality.
1348 // Additionally, the following check MUST NOT be removed.
1349 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1350 return;
1351 }
1352
Joe Onorato108413a2019-04-03 18:20:52 -07001353 // Additionally, this flag allows turning off face for a device
1354 // (either permanently through the build or on an individual device).
1355 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1356 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1357 return;
1358 }
1359
Joe Onoratodb396002019-04-05 19:49:27 -07001360 // The debug method takes two file descriptors. The first is for text
1361 // output, which we will drop. The second is for binary data, which
1362 // will be the protobuf data.
1363 final IBiometricsFace daemon = getFaceDaemon();
1364 if (daemon != null) {
1365 FileOutputStream devnull = null;
1366 try {
1367 devnull = new FileOutputStream("/dev/null");
1368 final NativeHandle handle = new NativeHandle(
1369 new FileDescriptor[] { devnull.getFD(), fd },
1370 new int[0], false);
1371 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1372 } catch (IOException | RemoteException ex) {
1373 Slog.d(TAG, "error while reading face debugging data", ex);
1374 } finally {
1375 if (devnull != null) {
1376 try {
1377 devnull.close();
1378 } catch (IOException ex) {
1379 }
1380 }
1381 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001382 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001383 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001384}