Merge the 2020-05-05 SPL branch from AOSP-Partner

* security-aosp-pi-release:
  Add permission check on onKeyguardVisibilityChanged

Change-Id: I96d0e211196446c888b03d7c63c21ff52d2e9f44
diff --git a/keystore/key_store_service.cpp b/keystore/key_store_service.cpp
index bc07c21..6b26b57 100644
--- a/keystore/key_store_service.cpp
+++ b/keystore/key_store_service.cpp
@@ -2344,15 +2344,24 @@
 }
 
 Status KeyStoreService::onKeyguardVisibilityChanged(bool isShowing, int32_t userId,
-                                                    int32_t* aidl_return) {
+                                                    int32_t* _aidl_return) {
     KEYSTORE_SERVICE_LOCK;
-    enforcement_policy.set_device_locked(isShowing, userId);
-    if (!isShowing) {
+    if (isShowing) {
+        if (!checkBinderPermission(P_LOCK, UID_SELF)) {
+            LOG(WARNING) << "onKeyguardVisibilityChanged called with isShowing == true but "
+                            "without LOCK permission";
+            return AIDL_RETURN(ResponseCode::PERMISSION_DENIED);
+        }
+    } else {
+        if (!checkBinderPermission(P_UNLOCK, UID_SELF)) {
+            LOG(WARNING) << "onKeyguardVisibilityChanged called with isShowing == false but "
+                            "without UNLOCK permission";
+            return AIDL_RETURN(ResponseCode::PERMISSION_DENIED);
+        }
         mActiveUserId = userId;
     }
-    *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
-
-    return Status::ok();
+    enforcement_policy.set_device_locked(isShowing, userId);
+    return AIDL_RETURN(ResponseCode::NO_ERROR);
 }
 
 }  // namespace keystore