am b124c9e8: Fix unchecked length in Blob creation

* commit 'b124c9e86a5f8466f527501c6677b4b1b165c0b1':
  Fix unchecked length in Blob creation
diff --git a/keystore-engine/Android.mk b/keystore-engine/Android.mk
index e7cab53..bd86b6a 100644
--- a/keystore-engine/Android.mk
+++ b/keystore-engine/Android.mk
@@ -20,7 +20,7 @@
 
 LOCAL_MODULE_TAGS := optional
 
-LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/ssl/engines
+LOCAL_MODULE_RELATIVE_PATH := ssl/engines
 
 LOCAL_SRC_FILES := \
 	eng_keystore.cpp \
diff --git a/keystore-engine/dsa_meth.cpp b/keystore-engine/dsa_meth.cpp
index 6adfa2d..372d5c4 100644
--- a/keystore-engine/dsa_meth.cpp
+++ b/keystore-engine/dsa_meth.cpp
@@ -23,7 +23,7 @@
  *
  */
 
-#include <utils/UniquePtr.h>
+#include <UniquePtr.h>
 
 //#define LOG_NDEBUG 0
 #define LOG_TAG "OpenSSL-keystore-dsa"
diff --git a/keystore-engine/ecdsa_meth.cpp b/keystore-engine/ecdsa_meth.cpp
index 7b673a2..a059b54 100644
--- a/keystore-engine/ecdsa_meth.cpp
+++ b/keystore-engine/ecdsa_meth.cpp
@@ -23,7 +23,7 @@
  *
  */
 
-#include <utils/UniquePtr.h>
+#include <UniquePtr.h>
 
 //#define LOG_NDEBUG 0
 #define LOG_TAG "OpenSSL-keystore-ecdsa"
diff --git a/keystore-engine/eng_keystore.cpp b/keystore-engine/eng_keystore.cpp
index 6f5b01a..6feb0f9 100644
--- a/keystore-engine/eng_keystore.cpp
+++ b/keystore-engine/eng_keystore.cpp
@@ -23,7 +23,7 @@
  *
  */
 
-#include <utils/UniquePtr.h>
+#include <UniquePtr.h>
 
 #include <sys/socket.h>
 #include <stdarg.h>
diff --git a/keystore-engine/rsa_meth.cpp b/keystore-engine/rsa_meth.cpp
index b949fa4..da9e45f 100644
--- a/keystore-engine/rsa_meth.cpp
+++ b/keystore-engine/rsa_meth.cpp
@@ -23,7 +23,7 @@
  *
  */
 
-#include <utils/UniquePtr.h>
+#include <UniquePtr.h>
 
 //#define LOG_NDEBUG 0
 #define LOG_TAG "OpenSSL-keystore-rsa"
diff --git a/keystore/keystore.cpp b/keystore/keystore.cpp
index 031f4c8..605df3b 100644
--- a/keystore/keystore.cpp
+++ b/keystore/keystore.cpp
@@ -44,8 +44,8 @@
 
 #include <keymaster/softkeymaster.h>
 
+#include <UniquePtr.h>
 #include <utils/String8.h>
-#include <utils/UniquePtr.h>
 #include <utils/Vector.h>
 
 #include <keystore/IKeystoreService.h>
@@ -1506,7 +1506,7 @@
         Blob keyBlob(item, itemLength, NULL, 0, ::TYPE_GENERIC);
         keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED);
 
-        return mKeyStore->put(filename.string(), &keyBlob, callingUid);
+        return mKeyStore->put(filename.string(), &keyBlob, targetUid);
     }
 
     int32_t del(const String16& name, int targetUid) {
@@ -1527,7 +1527,7 @@
 
         Blob keyBlob;
         ResponseCode responseCode = mKeyStore->get(filename.string(), &keyBlob, TYPE_GENERIC,
-                callingUid);
+                targetUid);
         if (responseCode != ::NO_ERROR) {
             return responseCode;
         }
@@ -1825,7 +1825,7 @@
             rsa_params.modulus_size = keySize;
 
             if (args->size() > 1) {
-                ALOGI("invalid number of arguments: %d", args->size());
+                ALOGI("invalid number of arguments: %zu", args->size());
                 return ::SYSTEM_ERROR;
             } else if (args->size() == 1) {
                 sp<KeystoreArg> pubExpBlob = args->itemAt(0);
@@ -1882,7 +1882,7 @@
             return ::PERMISSION_DENIED;
         }
 
-        State state = mKeyStore->getState(callingUid);
+        State state = mKeyStore->getState(targetUid);
         if ((flags & KEYSTORE_FLAG_ENCRYPTED) && !isKeystoreUnlocked(state)) {
             ALOGD("calling import in state: %d", state);
             return state;
@@ -1891,7 +1891,7 @@
         String8 name8(name);
         String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid));
 
-        return mKeyStore->importKey(data, length, filename.string(), callingUid, flags);
+        return mKeyStore->importKey(data, length, filename.string(), targetUid, flags);
     }
 
     int32_t sign(const String16& name, const uint8_t* data, size_t length, uint8_t** out,
@@ -2063,11 +2063,11 @@
         }
 
         String8 name8(name);
-        String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid));
+        String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid));
 
         Blob keyBlob;
         ResponseCode responseCode = mKeyStore->get(filename.string(), &keyBlob, ::TYPE_KEY_PAIR,
-                callingUid);
+                targetUid);
         if (responseCode != ::NO_ERROR) {
             return responseCode;
         }
@@ -2214,7 +2214,7 @@
         String8 sourceFile(mKeyStore->getKeyNameForUidWithDir(source8, srcUid));
 
         String8 target8(destKey);
-        String8 targetFile(mKeyStore->getKeyNameForUidWithDir(target8, srcUid));
+        String8 targetFile(mKeyStore->getKeyNameForUidWithDir(target8, destUid));
 
         if (access(targetFile.string(), W_OK) != -1 || errno != ENOENT) {
             ALOGD("destination already exists: %s", targetFile.string());
@@ -2223,19 +2223,20 @@
 
         Blob keyBlob;
         ResponseCode responseCode = mKeyStore->get(sourceFile.string(), &keyBlob, TYPE_ANY,
-                callingUid);
+                srcUid);
         if (responseCode != ::NO_ERROR) {
             return responseCode;
         }
 
-        return mKeyStore->put(targetFile.string(), &keyBlob, callingUid);
+        return mKeyStore->put(targetFile.string(), &keyBlob, destUid);
     }
 
     int32_t is_hardware_backed(const String16& keyType) {
         return mKeyStore->isHardwareBacked(keyType) ? 1 : 0;
     }
 
-    int32_t clear_uid(int64_t targetUid) {
+    int32_t clear_uid(int64_t targetUid64) {
+        uid_t targetUid = static_cast<uid_t>(targetUid64);
         uid_t callingUid = IPCThreadState::self()->getCallingUid();
         if (!has_permission(callingUid, P_CLEAR_UID)) {
             ALOGW("permission denied for %d: clear_uid", callingUid);
@@ -2248,13 +2249,19 @@
             return state;
         }
 
+        if (targetUid64 == -1) {
+            targetUid = callingUid;
+        } else if (!is_granted_to(callingUid, targetUid)) {
+            return ::PERMISSION_DENIED;
+        }
+
         const keymaster_device_t* device = mKeyStore->getDevice();
         if (device == NULL) {
             ALOGW("can't get keymaster device");
             return ::SYSTEM_ERROR;
         }
 
-        UserState* userState = mKeyStore->getUserState(callingUid);
+        UserState* userState = mKeyStore->getUserState(targetUid);
         DIR* dir = opendir(userState->getUserDirName());
         if (!dir) {
             ALOGW("can't open user directory: %s", strerror(errno));
@@ -2262,7 +2269,7 @@
         }
 
         char prefix[NAME_MAX];
-        int n = snprintf(prefix, NAME_MAX, "%u_", static_cast<uid_t>(targetUid));
+        int n = snprintf(prefix, NAME_MAX, "%u_", targetUid);
 
         ResponseCode rc = ::NO_ERROR;
 
@@ -2284,7 +2291,7 @@
 
             String8 filename(String8::format("%s/%s", userState->getUserDirName(), file->d_name));
             Blob keyBlob;
-            if (mKeyStore->get(filename.string(), &keyBlob, ::TYPE_ANY, callingUid)
+            if (mKeyStore->get(filename.string(), &keyBlob, ::TYPE_ANY, targetUid)
                     != ::NO_ERROR) {
                 ALOGW("couldn't open %s", filename.string());
                 continue;
diff --git a/softkeymaster/Android.mk b/softkeymaster/Android.mk
index 0064d01..7eee9c5 100644
--- a/softkeymaster/Android.mk
+++ b/softkeymaster/Android.mk
@@ -16,7 +16,7 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := keystore.default
-LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_SRC_FILES := module.cpp
 LOCAL_C_INCLUDES := \
 	system/security/keystore \
diff --git a/softkeymaster/keymaster_openssl.cpp b/softkeymaster/keymaster_openssl.cpp
index 4aaaea2..783148e 100644
--- a/softkeymaster/keymaster_openssl.cpp
+++ b/softkeymaster/keymaster_openssl.cpp
@@ -28,7 +28,7 @@
 #include <openssl/err.h>
 #include <openssl/x509.h>
 
-#include <utils/UniquePtr.h>
+#include <UniquePtr.h>
 
 // For debugging
 //#define LOG_NDEBUG 0
@@ -191,7 +191,7 @@
         publicLen = (publicLen << 8) | *p++;
     }
     if (p + publicLen > end) {
-        ALOGE("public key length encoding error: size=%ld, end=%d", publicLen, end - p);
+        ALOGE("public key length encoding error: size=%ld, end=%td", publicLen, end - p);
         return NULL;
     }
 
@@ -204,7 +204,7 @@
         privateLen = (privateLen << 8) | *p++;
     }
     if (p + privateLen > end) {
-        ALOGE("private key length encoding error: size=%ld, end=%d", privateLen, end - p);
+        ALOGE("private key length encoding error: size=%ld, end=%td", privateLen, end - p);
         return NULL;
     }
 
diff --git a/softkeymaster/module.cpp b/softkeymaster/module.cpp
index 758dfe7..cba3a79 100644
--- a/softkeymaster/module.cpp
+++ b/softkeymaster/module.cpp
@@ -26,7 +26,7 @@
 
 #include <openssl/err.h>
 
-#include <utils/UniquePtr.h>
+#include <UniquePtr.h>
 
 // For debugging
 //#define LOG_NDEBUG 0
@@ -78,20 +78,20 @@
 }
 
 static struct hw_module_methods_t keystore_module_methods = {
-    open: openssl_open,
+    .open = openssl_open,
 };
 
 struct keystore_module HAL_MODULE_INFO_SYM
 __attribute__ ((visibility ("default"))) = {
-    common: {
-        tag: HARDWARE_MODULE_TAG,
-        module_api_version: KEYMASTER_MODULE_API_VERSION_0_2,
-        hal_api_version: HARDWARE_HAL_API_VERSION,
-        id: KEYSTORE_HARDWARE_MODULE_ID,
-        name: "Keymaster OpenSSL HAL",
-        author: "The Android Open Source Project",
-        methods: &keystore_module_methods,
-        dso: 0,
-        reserved: {},
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .module_api_version = KEYMASTER_MODULE_API_VERSION_0_2,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
+        .id = KEYSTORE_HARDWARE_MODULE_ID,
+        .name = "Keymaster OpenSSL HAL",
+        .author = "The Android Open Source Project",
+        .methods = &keystore_module_methods,
+        .dso = 0,
+        .reserved = {},
     },
 };