blob: 202b89496007d23c0c894a9a4ae993f6eb9b91fe [file] [log] [blame]
Rubin Xu75431fb2016-01-07 21:12:14 +00001/*
2 * Copyright (C) 2016 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
Michal Karpinski6235a942016-03-15 12:07:23 +000017package android.app.admin;
Rubin Xu75431fb2016-01-07 21:12:14 +000018
19import android.annotation.IntDef;
Naomi Musgravedb980f42017-11-28 10:56:47 +000020import android.annotation.TestApi;
Pavel Grafovce72ef02018-01-10 17:14:11 +000021import android.content.ComponentName;
Rubin Xu75431fb2016-01-07 21:12:14 +000022import android.os.Parcel;
23import android.os.Parcelable;
24import android.os.SystemProperties;
25import android.util.EventLog.Event;
26
27import java.io.IOException;
28import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30import java.util.Collection;
Naomi Musgravedb980f42017-11-28 10:56:47 +000031import java.util.Objects;
Rubin Xu75431fb2016-01-07 21:12:14 +000032
Pavel Grafov73f747b2017-04-10 19:29:41 +010033/**
34 * Definitions for working with security logs.
35 *
36 * <p>Device owner apps can control the logging with
37 * {@link DevicePolicyManager#setSecurityLoggingEnabled}. When security logs are enabled, device
38 * owner apps receive periodic callbacks from {@link DeviceAdminReceiver#onSecurityLogsAvailable},
39 * at which time new batch of logs can be collected via
40 * {@link DevicePolicyManager#retrieveSecurityLogs}. {@link SecurityEvent} describes the type and
41 * format of security logs being collected.
42 */
Rubin Xu75431fb2016-01-07 21:12:14 +000043public class SecurityLog {
44
45 private static final String PROPERTY_LOGGING_ENABLED = "persist.logd.security";
46
47 /** @hide */
48 @Retention(RetentionPolicy.SOURCE)
Jeff Sharkeyce8db992017-12-13 20:05:05 -070049 @IntDef(prefix = { "TAG_" }, value = {
50 TAG_ADB_SHELL_INTERACTIVE,
51 TAG_ADB_SHELL_CMD,
52 TAG_SYNC_RECV_FILE,
53 TAG_SYNC_SEND_FILE,
54 TAG_APP_PROCESS_START,
55 TAG_KEYGUARD_DISMISSED,
56 TAG_KEYGUARD_DISMISS_AUTH_ATTEMPT,
Pavel Grafovce72ef02018-01-10 17:14:11 +000057 TAG_KEYGUARD_SECURED,
58 TAG_OS_STARTUP,
59 TAG_OS_SHUTDOWN,
60 TAG_LOGGING_STARTED,
61 TAG_LOGGING_STOPPED,
62 TAG_MEDIA_MOUNT,
63 TAG_MEDIA_UNMOUNT,
64 TAG_LOG_BUFFER_SIZE_CRITICAL,
65 TAG_PASSWORD_EXPIRATION_SET,
66 TAG_PASSWORD_COMPLEXITY_SET,
67 TAG_PASSWORD_HISTORY_LENGTH_SET,
68 TAG_MAX_SCREEN_LOCK_TIMEOUT_SET,
69 TAG_MAX_PASSWORD_ATTEMPTS_SET,
70 TAG_KEYGUARD_DISABLED_FEATURES_SET,
71 TAG_REMOTE_LOCK,
72 TAG_USER_RESTRICTION_ADDED,
73 TAG_USER_RESTRICTION_REMOVED,
74 TAG_WIPE_FAILURE,
75 TAG_KEY_GENERATED,
76 TAG_KEY_IMPORT,
77 TAG_KEY_DESTRUCTION,
78 TAG_CERT_AUTHORITY_INSTALLED,
79 TAG_CERT_AUTHORITY_REMOVED,
Pavel Grafovb7455402018-01-30 21:17:08 +000080 TAG_CRYPTO_SELF_TEST_COMPLETED,
Jeff Sharkeyce8db992017-12-13 20:05:05 -070081 })
82 public @interface SecurityLogTag {}
Rubin Xu75431fb2016-01-07 21:12:14 +000083
Pavel Grafovce72ef02018-01-10 17:14:11 +000084 /** @hide */
85 @Retention(RetentionPolicy.SOURCE)
86 @IntDef(prefix = { "LEVEL_" }, value = {
87 LEVEL_INFO,
88 LEVEL_WARNING,
89 LEVEL_ERROR
90 })
91 public @interface SecurityLogLevel {}
92
Rubin Xu75431fb2016-01-07 21:12:14 +000093 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +000094 * Indicates that an ADB interactive shell was opened via "adb shell".
Rubin Xu75431fb2016-01-07 21:12:14 +000095 * There is no extra payload in the log event.
96 */
97 public static final int TAG_ADB_SHELL_INTERACTIVE =
98 SecurityLogTags.SECURITY_ADB_SHELL_INTERACTIVE;
Pavel Grafovce72ef02018-01-10 17:14:11 +000099
Rubin Xu75431fb2016-01-07 21:12:14 +0000100 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000101 * Indicates that a shell command was issued over ADB via {@code adb shell <command>}
102 * The log entry contains a {@code String} payload containing the shell command, accessible
103 * via {@link SecurityEvent#getData()}.
Rubin Xu75431fb2016-01-07 21:12:14 +0000104 */
105 public static final int TAG_ADB_SHELL_CMD = SecurityLogTags.SECURITY_ADB_SHELL_COMMAND;
Pavel Grafovce72ef02018-01-10 17:14:11 +0000106
Rubin Xu75431fb2016-01-07 21:12:14 +0000107 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000108 * Indicates that a file was pulled from the device via the adb daemon, for example via
109 * {@code adb pull}. The log entry contains a {@code String} payload containing the path of the
110 * pulled file on the device, accessible via {@link SecurityEvent#getData()}.
Rubin Xu75431fb2016-01-07 21:12:14 +0000111 */
112 public static final int TAG_SYNC_RECV_FILE = SecurityLogTags.SECURITY_ADB_SYNC_RECV;
Pavel Grafovce72ef02018-01-10 17:14:11 +0000113
Rubin Xu75431fb2016-01-07 21:12:14 +0000114 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000115 * Indicates that a file was pushed to the device via the adb daemon, for example via
116 * {@code adb push}. The log entry contains a {@code String} payload containing the destination
117 * path of the pushed file, accessible via {@link SecurityEvent#getData()}.
Rubin Xu75431fb2016-01-07 21:12:14 +0000118 */
119 public static final int TAG_SYNC_SEND_FILE = SecurityLogTags.SECURITY_ADB_SYNC_SEND;
Pavel Grafovce72ef02018-01-10 17:14:11 +0000120
Rubin Xu75431fb2016-01-07 21:12:14 +0000121 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000122 * Indicates that an app process was started. The log entry contains the following
Rubin Xu232990d2016-02-18 15:55:21 +0000123 * information about the process encapsulated in an {@link Object} array, accessible via
124 * {@link SecurityEvent#getData()}:
Pavel Grafovce72ef02018-01-10 17:14:11 +0000125 * <li> [0] process name ({@code String})
126 * <li> [1] exact start time in milliseconds according to {@code System.currentTimeMillis()}
127 * ({@code Long})
128 * <li> [2] app uid ({@code Integer})
129 * <li> [3] app pid ({@code Integer})
130 * <li> [4] seinfo tag ({@code String})
131 * <li> [5] SHA-256 hash of the base APK in hexadecimal ({@code String})
Rubin Xu75431fb2016-01-07 21:12:14 +0000132 */
133 public static final int TAG_APP_PROCESS_START = SecurityLogTags.SECURITY_APP_PROCESS_START;
Pavel Grafovce72ef02018-01-10 17:14:11 +0000134
Rubin Xu75431fb2016-01-07 21:12:14 +0000135 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000136 * Indicates that keyguard has been dismissed.
Michal Karpinski31502d32016-01-25 16:43:07 +0000137 * There is no extra payload in the log event.
Rubin Xu75431fb2016-01-07 21:12:14 +0000138 */
Pavel Grafovce72ef02018-01-10 17:14:11 +0000139 public static final int TAG_KEYGUARD_DISMISSED = SecurityLogTags.SECURITY_KEYGUARD_DISMISSED;
140
Michal Karpinski31502d32016-01-25 16:43:07 +0000141 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000142 * Indicates that there has been an authentication attempt to dismiss the keyguard. The log
143 * entry contains the following information about the attempt encapsulated in an {@link Object}
144 * array, accessible via {@link SecurityEvent#getData()}:
145 * <li> [0] attempt result ({@code Integer}, 1 for successful, 0 for unsuccessful)
146 * <li> [1] strength of authentication method ({@code Integer}, 1 if strong authentication
147 * method was used, 0 otherwise)
Michal Karpinski31502d32016-01-25 16:43:07 +0000148 */
149 public static final int TAG_KEYGUARD_DISMISS_AUTH_ATTEMPT =
150 SecurityLogTags.SECURITY_KEYGUARD_DISMISS_AUTH_ATTEMPT;
Pavel Grafovce72ef02018-01-10 17:14:11 +0000151
Rubin Xu75431fb2016-01-07 21:12:14 +0000152 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000153 * Indicates that the device has been locked, either by the user or by a timeout. There is no
154 * extra payload in the log event.
Rubin Xu75431fb2016-01-07 21:12:14 +0000155 */
Michal Karpinski31502d32016-01-25 16:43:07 +0000156 public static final int TAG_KEYGUARD_SECURED = SecurityLogTags.SECURITY_KEYGUARD_SECURED;
Rubin Xu75431fb2016-01-07 21:12:14 +0000157
158 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000159 * Indicates that the Android OS has started. The log entry contains the following information
160 * about the startup time software integrity check encapsulated in an {@link Object} array,
161 * accessible via {@link SecurityEvent#getData()}:
162 * <li> [0] Verified Boot state ({@code String})
163 * <li> [1] dm-verity mode ({@code String}).
164 * <p>Verified Boot state can be one of the following:
165 * <li> {@code green} indicates that there is a full chain of trust extending from the
166 * bootloader to verified partitions including the bootloader, boot partition, and all verified
167 * partitions.
168 * <li> {@code yellow} indicates that the boot partition has been verified using the embedded
169 * certificate and the signature is valid.
170 * <li> {@code orange} indicates that the device may be freely modified. Device integrity is
171 * left to the user to verify out-of-band.
172 * <p>dm-verity mode can be one of the following:
173 * <li> {@code enforcing} indicates that the device will be restarted when corruption is
174 * detected.
175 * <li> {@code eio} indicates that an I/O error will be returned for an attempt to read
176 * corrupted data blocks.
177 * For details see Verified Boot documentation.
178 */
179 public static final int TAG_OS_STARTUP = SecurityLogTags.SECURITY_OS_STARTUP;
180
181 /**
182 * Indicates that the Android OS has shutdown. There is no extra payload in the log event.
183 */
184 public static final int TAG_OS_SHUTDOWN = SecurityLogTags.SECURITY_OS_SHUTDOWN;
185
186 /**
187 * Indicates start-up of audit logging. There is no extra payload in the log event.
188 */
189 public static final int TAG_LOGGING_STARTED = SecurityLogTags.SECURITY_LOGGING_STARTED;
190
191 /**
192 * Indicates shutdown of audit logging. There is no extra payload in the log event.
193 */
194 public static final int TAG_LOGGING_STOPPED = SecurityLogTags.SECURITY_LOGGING_STOPPED;
195
196 /**
197 * Indicates that removable media has been mounted on the device. The log entry contains the
198 * following information about the event, encapsulated in an {@link Object} array and
199 * accessible via {@link SecurityEvent#getData()}:
200 * <li> [0] mount point ({@code String})
201 * <li> [1] volume label ({@code String}).
202 */
203 public static final int TAG_MEDIA_MOUNT = SecurityLogTags.SECURITY_MEDIA_MOUNTED;
204
205 /**
206 * Indicates that removable media was unmounted from the device. The log entry contains the
207 * following information about the event, encapsulated in an {@link Object} array and
208 * accessible via {@link SecurityEvent#getData()}:
209 * <li> [0] mount point ({@code String})
210 * <li> [1] volume label ({@code String}).
211 */
212 public static final int TAG_MEDIA_UNMOUNT = SecurityLogTags.SECURITY_MEDIA_UNMOUNTED;
213
214 /**
215 * Indicates that the audit log buffer has reached 90% of its capacity. There is no extra
216 * payload in the log event.
217 */
218 public static final int TAG_LOG_BUFFER_SIZE_CRITICAL =
219 SecurityLogTags.SECURITY_LOG_BUFFER_SIZE_CRITICAL;
220
221 /**
222 * Indicates that an admin has set a password expiration timeout. The log entry contains the
223 * following information about the event, encapsulated in an {@link Object} array and accessible
224 * via {@link SecurityEvent#getData()}:
225 * <li> [0] admin package name ({@code String})
226 * <li> [1] admin user ID ({@code Integer})
227 * <li> [2] target user ID ({@code Integer})
228 * <li> [3] new password expiration timeout in milliseconds ({@code Long}).
229 * @see DevicePolicyManager#setPasswordExpirationTimeout(ComponentName, long)
230 */
231 public static final int TAG_PASSWORD_EXPIRATION_SET =
232 SecurityLogTags.SECURITY_PASSWORD_EXPIRATION_SET;
233
234 /**
235 * Indicates that an admin has set a requirement for password complexity. The log entry contains
236 * the following information about the event, encapsulated in an {@link Object} array and
237 * accessible via {@link SecurityEvent#getData()}:
238 * <li> [0] admin package name ({@code String})
239 * <li> [1] admin user ID ({@code Integer})
240 * <li> [2] target user ID ({@code Integer})
241 * <li> [3] minimum password length ({@code Integer})
242 * <li> [4] password quality constraint ({@code Integer})
243 * <li> [5] minimum number of letters ({@code Integer})
244 * <li> [6] minimum number of non-letters ({@code Integer})
245 * <li> [7] minimum number of digits ({@code Integer})
246 * <li> [8] minimum number of uppercase letters ({@code Integer})
247 * <li> [9] minimum number of lowercase letters ({@code Integer})
248 * <li> [10] minimum number of symbols ({@code Integer})
249 *
250 * @see DevicePolicyManager#setPasswordMinimumLength(ComponentName, int)
251 * @see DevicePolicyManager#setPasswordQuality(ComponentName, int)
252 * @see DevicePolicyManager#setPasswordMinimumLetters(ComponentName, int)
253 * @see DevicePolicyManager#setPasswordMinimumNonLetter(ComponentName, int)
254 * @see DevicePolicyManager#setPasswordMinimumLowerCase(ComponentName, int)
255 * @see DevicePolicyManager#setPasswordMinimumUpperCase(ComponentName, int)
256 * @see DevicePolicyManager#setPasswordMinimumNumeric(ComponentName, int)
257 * @see DevicePolicyManager#setPasswordMinimumSymbols(ComponentName, int)
258 */
259 public static final int TAG_PASSWORD_COMPLEXITY_SET =
260 SecurityLogTags.SECURITY_PASSWORD_COMPLEXITY_SET;
261
262 /**
263 * Indicates that an admin has set a password history length. The log entry contains the
264 * following information about the event encapsulated in an {@link Object} array, accessible
265 * via {@link SecurityEvent#getData()}:
266 * <li> [0] admin package name ({@code String})
267 * <li> [1] admin user ID ({@code Integer})
268 * <li> [2] target user ID ({@code Integer})
269 * <li> [3] new password history length value ({@code Integer})
270 * @see DevicePolicyManager#setPasswordHistoryLength(ComponentName, int)
271 */
272 public static final int TAG_PASSWORD_HISTORY_LENGTH_SET =
273 SecurityLogTags.SECURITY_PASSWORD_HISTORY_LENGTH_SET;
274
275 /**
276 * Indicates that an admin has set a maximum screen lock timeout. The log entry contains the
277 * following information about the event encapsulated in an {@link Object} array, accessible
278 * via {@link SecurityEvent#getData()}:
279 * <li> [0] admin package name ({@code String})
280 * <li> [1] admin user ID ({@code Integer})
281 * <li> [2] target user ID ({@code Integer})
282 * <li> [3] new screen lock timeout in milliseconds ({@code Long})
283 * @see DevicePolicyManager#setMaximumTimeToLock(ComponentName, long)
284 */
285 public static final int TAG_MAX_SCREEN_LOCK_TIMEOUT_SET =
286 SecurityLogTags.SECURITY_MAX_SCREEN_LOCK_TIMEOUT_SET;
287
288 /**
289 * Indicates that an admin has set a maximum number of failed password attempts before wiping
290 * data. The log entry contains the following information about the event encapsulated in an
291 * {@link Object} array, accessible via {@link SecurityEvent#getData()}:
292 * <li> [0] admin package name ({@code String})
293 * <li> [1] admin user ID ({@code Integer})
294 * <li> [2] target user ID ({@code Integer})
295 * <li> [3] new maximum number of failed password attempts ({@code Integer})
296 * @see DevicePolicyManager#setMaximumTimeToLock(ComponentName, long)
297 */
298 public static final int TAG_MAX_PASSWORD_ATTEMPTS_SET =
299 SecurityLogTags.SECURITY_MAX_PASSWORD_ATTEMPTS_SET;
300
301 /**
302 * Indicates that an admin has set disabled keyguard features. The log entry contains the
303 * following information about the event encapsulated in an {@link Object} array, accessible via
304 * {@link SecurityEvent#getData()}:
305 * <li> [0] admin package name ({@code String})
306 * <li> [1] admin user ID ({@code Integer})
307 * <li> [2] target user ID ({@code Integer})
308 * <li> [3] disabled keyguard feature mask ({@code Integer}).
309 * @see DevicePolicyManager#setKeyguardDisabledFeatures(ComponentName, int)
310 */
311 public static final int TAG_KEYGUARD_DISABLED_FEATURES_SET =
312 SecurityLogTags.SECURITY_KEYGUARD_DISABLED_FEATURES_SET;
313
314 /**
315 * Indicates that an admin remotely locked the device or profile. The log entry contains the
316 * following information about the event encapsulated in an {@link Object} array, accessible via
317 * {@link SecurityEvent#getData()}:
318 * <li> [0] admin package name ({@code String}),
319 * <li> [1] admin user ID ({@code Integer}).
320 */
321 public static final int TAG_REMOTE_LOCK = SecurityLogTags.SECURITY_REMOTE_LOCK;
322
323 /**
324 * Indicates a failure to wipe device or user data. There is no extra payload in the log event.
325 */
326 public static final int TAG_WIPE_FAILURE = SecurityLogTags.SECURITY_WIPE_FAILED;
327
328 /**
329 * Indicates that an authentication key was generated. The log entry contains the following
330 * information about the event, encapsulated in an {@link Object} array and accessible via
331 * {@link SecurityEvent#getData()}:
332 * <li> [0] result ({@code Integer}, 0 if operation failed, 1 if succeeded)
333 * <li> [1] alias of the key ({@code String})
334 * <li> [2] requesting process uid ({@code Integer}).
335 */
336 public static final int TAG_KEY_GENERATED =
337 SecurityLogTags.SECURITY_KEY_GENERATED;
338
339 /**
340 * Indicates that a cryptographic key was imported. The log entry contains the following
341 * information about the event, encapsulated in an {@link Object} array and accessible via
342 * {@link SecurityEvent#getData()}:
343 * <li> [0] result ({@code Integer}, 0 if operation failed, 1 if succeeded)
344 * <li> [1] alias of the key ({@code String})
345 * <li> [2] requesting process uid ({@code Integer}).
346 */
347 public static final int TAG_KEY_IMPORT = SecurityLogTags.SECURITY_KEY_IMPORTED;
348
349 /**
350 * Indicates that a cryptographic key was destroyed. The log entry contains the following
351 * information about the event, encapsulated in an {@link Object} array and accessible via
352 * {@link SecurityEvent#getData()}:
353 * <li> [0] result ({@code Integer}, 0 if operation failed, 1 if succeeded)
354 * <li> [1] alias of the key ({@code String})
355 * <li> [2] requesting process uid ({@code Integer}).
356 */
357 public static final int TAG_KEY_DESTRUCTION = SecurityLogTags.SECURITY_KEY_DESTROYED;
358
359 /**
360 * Indicates that a new root certificate has been installed into system's trusted credential
361 * storage. The log entry contains the following information about the event, encapsulated in an
362 * {@link Object} array and accessible via {@link SecurityEvent#getData()}:
363 * <li> [0] result ({@code Integer}, 0 if operation failed, 1 if succeeded)
364 * <li> [1] subject of the certificate ({@code String}).
365 */
366 public static final int TAG_CERT_AUTHORITY_INSTALLED =
367 SecurityLogTags.SECURITY_CERT_AUTHORITY_INSTALLED;
368
369 /**
370 * Indicates that a new oot certificate has been removed from system's trusted credential
371 * storage. The log entry contains the following information about the event, encapsulated in an
372 * {@link Object} array and accessible via {@link SecurityEvent#getData()}:
373 * <li> [0] result ({@code Integer}, 0 if operation failed, 1 if succeeded)
374 * <li> [1] subject of the certificate ({@code String}).
375 */
376 public static final int TAG_CERT_AUTHORITY_REMOVED =
377 SecurityLogTags.SECURITY_CERT_AUTHORITY_REMOVED;
378
379 /**
380 * Indicates that an admin has set a user restriction. The log entry contains the following
381 * information about the event, encapsulated in an {@link Object} array and accessible via
382 * {@link SecurityEvent#getData()}:
383 * <li> [0] admin package name ({@code String})
384 * <li> [1] admin user ID ({@code Integer})
385 * <li> [2] user restriction ({@code String})
386 * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
387 */
388 public static final int TAG_USER_RESTRICTION_ADDED =
389 SecurityLogTags.SECURITY_USER_RESTRICTION_ADDED;
390
391 /**
392 * Indicates that an admin has removed a user restriction. The log entry contains the following
393 * information about the event, encapsulated in an {@link Object} array and accessible via
394 * {@link SecurityEvent#getData()}:
395 * <li> [0] admin package name ({@code String})
396 * <li> [1] admin user ID ({@code Integer})
397 * <li> [2] user restriction ({@code String})
398 * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
399 */
400 public static final int TAG_USER_RESTRICTION_REMOVED =
401 SecurityLogTags.SECURITY_USER_RESTRICTION_REMOVED;
402
403 /**
Pavel Grafovb7455402018-01-30 21:17:08 +0000404 * Indicates that cryptographic functionality self test has completed. The log entry contains an
405 * {@code Integer} payload, indicating the result of the test (0 if the test failed, 1 if
406 * succeeded) and accessible via {@link SecurityEvent#getData()}.
407 */
408 public static final int TAG_CRYPTO_SELF_TEST_COMPLETED =
409 SecurityLogTags.SECURITY_CRYPTO_SELF_TEST_COMPLETED;
410
411 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000412 * Event severity level indicating that the event corresponds to normal workflow.
413 */
414 public static final int LEVEL_INFO = 1;
415
416 /**
417 * Event severity level indicating that the event may require admin attention.
418 */
419 public static final int LEVEL_WARNING = 2;
420
421 /**
422 * Event severity level indicating that the event requires urgent admin action.
423 */
424 public static final int LEVEL_ERROR = 3;
425
426 /**
Michal Karpinski6235a942016-03-15 12:07:23 +0000427 * Returns if security logging is enabled. Log producers should only write new logs if this is
Rubin Xu75431fb2016-01-07 21:12:14 +0000428 * true. Under the hood this is the logical AND of whether device owner exists and whether
429 * it enables logging by setting the system property {@link #PROPERTY_LOGGING_ENABLED}.
430 * @hide
431 */
432 public static native boolean isLoggingEnabled();
433
434 /**
435 * @hide
436 */
437 public static void setLoggingEnabledProperty(boolean enabled) {
438 SystemProperties.set(PROPERTY_LOGGING_ENABLED, enabled ? "true" : "false");
439 }
440
441 /**
442 * @hide
443 */
444 public static boolean getLoggingEnabledProperty() {
445 return SystemProperties.getBoolean(PROPERTY_LOGGING_ENABLED, false);
446 }
447
448 /**
449 * A class representing a security event log entry.
450 */
Jeff Sharkey50d1c042016-02-29 16:34:46 -0700451 public static final class SecurityEvent implements Parcelable {
Rubin Xu75431fb2016-01-07 21:12:14 +0000452 private Event mEvent;
Naomi Musgravedb980f42017-11-28 10:56:47 +0000453 private long mId;
454
455 /**
456 * Constructor used by native classes to generate SecurityEvent instances.
457 * @hide
458 */
459 /* package */ SecurityEvent(byte[] data) {
460 this(0, data);
461 }
462
463 /**
464 * Constructor used by Parcelable.Creator to generate SecurityEvent instances.
465 * @hide
466 */
467 /* package */ SecurityEvent(Parcel source) {
468 this(source.readLong(), source.createByteArray());
469 }
Rubin Xu75431fb2016-01-07 21:12:14 +0000470
471 /** @hide */
Naomi Musgravedb980f42017-11-28 10:56:47 +0000472 @TestApi
473 public SecurityEvent(long id, byte[] data) {
474 mId = id;
Rubin Xu75431fb2016-01-07 21:12:14 +0000475 mEvent = Event.fromBytes(data);
476 }
477
478 /**
479 * Returns the timestamp in nano seconds when this event was logged.
480 */
481 public long getTimeNanos() {
482 return mEvent.getTimeNanos();
483 }
484
485 /**
486 * Returns the tag of this log entry, which specifies entry's semantics.
Rubin Xu75431fb2016-01-07 21:12:14 +0000487 */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700488 public @SecurityLogTag int getTag() {
Rubin Xu75431fb2016-01-07 21:12:14 +0000489 return mEvent.getTag();
490 }
491
492 /**
Pavel Grafov739cda82017-02-02 12:41:20 +0000493 * Returns the payload contained in this log entry or {@code null} if there is no payload.
Rubin Xu75431fb2016-01-07 21:12:14 +0000494 */
495 public Object getData() {
496 return mEvent.getData();
497 }
498
Naomi Musgravedb980f42017-11-28 10:56:47 +0000499 /**
500 * @hide
501 */
502 public void setId(long id) {
503 this.mId = id;
504 }
505
506 /**
507 * Returns the id of the event, where the id monotonically increases for each event. The id
508 * is reset when the device reboots, and when security logging is enabled.
509 */
510 public long getId() {
511 return mId;
512 }
513
Pavel Grafovce72ef02018-01-10 17:14:11 +0000514 /**
515 * Returns severity level for the event.
516 */
517 public @SecurityLogLevel int getLogLevel() {
518 switch (mEvent.getTag()) {
519 case TAG_ADB_SHELL_INTERACTIVE:
520 case TAG_ADB_SHELL_CMD:
521 case TAG_SYNC_RECV_FILE:
522 case TAG_SYNC_SEND_FILE:
523 case TAG_APP_PROCESS_START:
524 case TAG_KEYGUARD_DISMISSED:
525 case TAG_KEYGUARD_SECURED:
526 case TAG_OS_STARTUP:
527 case TAG_OS_SHUTDOWN:
528 case TAG_LOGGING_STARTED:
529 case TAG_LOGGING_STOPPED:
530 case TAG_MEDIA_MOUNT:
531 case TAG_MEDIA_UNMOUNT:
532 case TAG_PASSWORD_EXPIRATION_SET:
533 case TAG_PASSWORD_COMPLEXITY_SET:
534 case TAG_PASSWORD_HISTORY_LENGTH_SET:
535 case TAG_MAX_SCREEN_LOCK_TIMEOUT_SET:
536 case TAG_MAX_PASSWORD_ATTEMPTS_SET:
537 case TAG_USER_RESTRICTION_ADDED:
538 case TAG_USER_RESTRICTION_REMOVED:
539 return LEVEL_INFO;
540 case TAG_CERT_AUTHORITY_REMOVED:
Pavel Grafovb7455402018-01-30 21:17:08 +0000541 case TAG_CRYPTO_SELF_TEST_COMPLETED:
Pavel Grafovce72ef02018-01-10 17:14:11 +0000542 return getSuccess() ? LEVEL_INFO : LEVEL_ERROR;
543 case TAG_CERT_AUTHORITY_INSTALLED:
544 case TAG_KEYGUARD_DISMISS_AUTH_ATTEMPT:
545 case TAG_KEY_IMPORT:
546 case TAG_KEY_DESTRUCTION:
547 case TAG_KEY_GENERATED:
548 return getSuccess() ? LEVEL_INFO : LEVEL_WARNING;
549 case TAG_LOG_BUFFER_SIZE_CRITICAL:
550 case TAG_WIPE_FAILURE:
551 return LEVEL_ERROR;
552 default:
553 return LEVEL_INFO;
554 }
555 }
556
557 // Success/failure if present is encoded as an integer in the first (0th) element of data.
558 private boolean getSuccess() {
559 final Object data = getData();
560 if (data == null || !(data instanceof Object[])) {
561 return false;
562 }
563
564 final Object[] array = (Object[]) data;
565 return array.length >= 1 && array[0] instanceof Integer && (Integer) array[0] != 0;
566 }
567
568
Rubin Xu75431fb2016-01-07 21:12:14 +0000569 @Override
570 public int describeContents() {
571 return 0;
572 }
573
574 @Override
575 public void writeToParcel(Parcel dest, int flags) {
Naomi Musgravedb980f42017-11-28 10:56:47 +0000576 dest.writeLong(mId);
Rubin Xu75431fb2016-01-07 21:12:14 +0000577 dest.writeByteArray(mEvent.getBytes());
578 }
579
580 public static final Parcelable.Creator<SecurityEvent> CREATOR =
581 new Parcelable.Creator<SecurityEvent>() {
582 @Override
583 public SecurityEvent createFromParcel(Parcel source) {
Naomi Musgravedb980f42017-11-28 10:56:47 +0000584 return new SecurityEvent(source);
Rubin Xu75431fb2016-01-07 21:12:14 +0000585 }
586
587 @Override
588 public SecurityEvent[] newArray(int size) {
589 return new SecurityEvent[size];
590 }
591 };
Pavel Grafov4ce59d42017-02-25 19:45:43 +0000592
593 /**
594 * @hide
595 */
596 @Override
597 public boolean equals(Object o) {
598 if (this == o) return true;
599 if (o == null || getClass() != o.getClass()) return false;
600 SecurityEvent other = (SecurityEvent) o;
Naomi Musgravedb980f42017-11-28 10:56:47 +0000601 return mEvent.equals(other.mEvent) && mId == other.mId;
Pavel Grafov4ce59d42017-02-25 19:45:43 +0000602 }
603
604 /**
605 * @hide
606 */
607 @Override
608 public int hashCode() {
Naomi Musgravedb980f42017-11-28 10:56:47 +0000609 return Objects.hash(mEvent, mId);
Pavel Grafov4ce59d42017-02-25 19:45:43 +0000610 }
Rubin Xu75431fb2016-01-07 21:12:14 +0000611 }
612 /**
613 * Retrieve all security logs and return immediately.
614 * @hide
615 */
616 public static native void readEvents(Collection<SecurityEvent> output) throws IOException;
617
618 /**
619 * Retrieve all security logs since the given timestamp in nanoseconds and return immediately.
620 * @hide
621 */
622 public static native void readEventsSince(long timestamp, Collection<SecurityEvent> output)
623 throws IOException;
624
625 /**
626 * Retrieve all security logs before the last reboot. May return corrupted data due to
627 * unreliable pstore.
628 * @hide
629 */
630 public static native void readPreviousEvents(Collection<SecurityEvent> output)
631 throws IOException;
632
633 /**
Pavel Grafovce72ef02018-01-10 17:14:11 +0000634 * Retrieve all security logs whose timestamp is equal to or greater than the given timestamp in
635 * nanoseconds. This method will block until either the last log earlier than the given
Rubin Xu75431fb2016-01-07 21:12:14 +0000636 * timestamp is about to be pruned, or after a 2-hour timeout has passed.
637 * @hide
638 */
639 public static native void readEventsOnWrapping(long timestamp, Collection<SecurityEvent> output)
640 throws IOException;
641
642 /**
643 * Write a log entry to the underlying storage, with a string payload.
644 * @hide
645 */
646 public static native int writeEvent(int tag, String str);
647
648 /**
649 * Write a log entry to the underlying storage, with several payloads.
650 * Supported types of payload are: integer, long, float, string plus array of supported types.
651 * @hide
652 */
653 public static native int writeEvent(int tag, Object... payloads);
654}