• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <chre/version.h>
20 #include <cinttypes>
21 #include <cstring>
22 
23 #include "chre/platform/log.h"
24 
25 namespace chre {
26 
validateAppInfo(uint64_t expectedAppId,uint32_t expectedAppVersion,uint32_t expectedTargetApiVersion,const struct chreNslNanoappInfo * appInfo)27 bool validateAppInfo(uint64_t expectedAppId, uint32_t expectedAppVersion,
28                      uint32_t expectedTargetApiVersion,
29                      const struct chreNslNanoappInfo *appInfo) {
30   uint32_t ourApiMajorVersion = CHRE_EXTRACT_MAJOR_VERSION(chreGetApiVersion());
31   uint32_t targetApiMajorVersion =
32       CHRE_EXTRACT_MAJOR_VERSION(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
42          ") doesn't match internal one (0x%016" PRIx64 ")",
43          expectedAppId, appInfo->appId);
44   } else if (expectedAppVersion != appInfo->appVersion) {
45     LOGE("Expected app version (0x%" PRIx32
46          ") doesn't match internal one (0x%" PRIx32 ")",
47          expectedAppVersion, appInfo->appVersion);
48   } else if (expectedTargetApiVersion != appInfo->targetApiVersion) {
49     LOGE("Expected target API version (0x%" PRIx32
50          ") doesn't match"
51          " internal one (0x%" PRIx32 ")",
52          expectedTargetApiVersion, appInfo->targetApiVersion);
53   } else if (targetApiMajorVersion != ourApiMajorVersion) {
54     LOGE("App targets a different major API version (%" PRIu32
55          ") than what we provide (%" PRIu32 ")",
56          targetApiMajorVersion, ourApiMajorVersion);
57   } else if (strlen(appInfo->name) > CHRE_NSL_DSO_NANOAPP_STRING_MAX_LEN) {
58     LOGE("App name is too long");
59   } else if (strlen(appInfo->vendor) > CHRE_NSL_DSO_NANOAPP_STRING_MAX_LEN) {
60     LOGE("App vendor is too long");
61   } else {
62     success = true;
63   }
64 
65   return success;
66 }
67 
68 }  // namespace chre
69