1 /* 2 * Copyright (C) 2017 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 17 #include "chre/platform/shared/nanoapp_dso_util.h" 18 19 #include <cinttypes> 20 #include <cstring> 21 #include <chre/version.h> 22 23 #include "chre/platform/log.h" 24 25 namespace chre { 26 27 bool validateAppInfo(uint64_t expectedAppId, uint32_t expectedAppVersion, 28 const struct chreNslNanoappInfo *appInfo, 29 bool skipVersionValidation) { 30 uint32_t ourApiMajorVersion = CHRE_EXTRACT_MAJOR_VERSION(chreGetApiVersion()); 31 uint32_t targetApiMajorVersion = CHRE_EXTRACT_MAJOR_VERSION( 32 appInfo->targetApiVersion); 33 34 bool success = false; 35 if (appInfo->magic != CHRE_NSL_NANOAPP_INFO_MAGIC) { 36 LOGE("Invalid app info magic: got 0x%08" PRIx32 " expected 0x%08" PRIx32, 37 appInfo->magic, static_cast<uint32_t>(CHRE_NSL_NANOAPP_INFO_MAGIC)); 38 } else if (appInfo->appId == 0) { 39 LOGE("Rejecting invalid app ID 0"); 40 } else if (expectedAppId != 0 && expectedAppId != appInfo->appId) { 41 LOGE("Expected app ID (0x%016" PRIx64 ") doesn't match internal one (0x%016" 42 PRIx64 ")", expectedAppId, appInfo->appId); 43 } else if (!skipVersionValidation 44 && expectedAppVersion != appInfo->appVersion) { 45 LOGE("Expected app version (0x%" PRIx32 ") doesn't match internal one (0x%" 46 PRIx32 ")", expectedAppVersion, appInfo->appVersion); 47 } else if (targetApiMajorVersion != ourApiMajorVersion) { 48 LOGE("App targets a different major API version (%" PRIu32 ") than what we " 49 "provide (%" PRIu32 ")", targetApiMajorVersion, ourApiMajorVersion); 50 } else if (strlen(appInfo->name) > CHRE_NSL_DSO_NANOAPP_STRING_MAX_LEN) { 51 LOGE("App name is too long"); 52 } else if (strlen(appInfo->vendor) > CHRE_NSL_DSO_NANOAPP_STRING_MAX_LEN) { 53 LOGE("App vendor is too long"); 54 } else { 55 success = true; 56 } 57 58 return success; 59 } 60 61 } // namespace chre 62