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