• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "usb_ddk_permission.h"
16 #include <hdf_base.h>
17 #include <hdf_log.h>
18 #include <dlfcn.h>
19 #include <mutex>
20 
21 #include "ipc_skeleton.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Usb {
26 namespace Ddk {
27 using VerifyAccessTokenFunc = int(*)(uint32_t callerToken, const std::string &permissionName);
28 using GetApiVersion = int(*)(uint32_t callerToken, int32_t &apiVersion);
29 static constexpr int PERMISSION_GRANTED = 0;
30 
31 static void *g_libHandle = nullptr;
32 static VerifyAccessTokenFunc g_verifyAccessToken = nullptr;
33 static GetApiVersion g_getApiVersion = nullptr;
34 static std::mutex g_mutex;
35 
InitVerifyAccessToken()36 static void InitVerifyAccessToken()
37 {
38     if (g_verifyAccessToken != nullptr && g_verifyAccessToken != nullptr) {
39         return;
40     }
41 
42     g_libHandle = dlopen("libusb_ddk_dynamic_library_wrapper.z.so", RTLD_LAZY);
43     if (g_libHandle == nullptr) {
44         HDF_LOGE("%{public}s dlopen failed: %{public}s", __func__, dlerror());
45         return;
46     }
47 
48     void *funcPtr = dlsym(g_libHandle, "VerifyAccessToken");
49     if (funcPtr == nullptr) {
50         HDF_LOGE("%{public}s dlsym failed: %{public}s", __func__, dlerror());
51         dlclose(g_libHandle);
52         g_libHandle = nullptr;
53         return;
54     }
55 
56     g_verifyAccessToken = reinterpret_cast<VerifyAccessTokenFunc>(funcPtr);
57 
58     void *getApiVersionPtr = dlsym(g_libHandle, "GetApiVersion");
59     if (getApiVersionPtr == nullptr) {
60         HDF_LOGE("%{public}s dlsym getApiVersionPtr failed: %{public}s", __func__, dlerror());
61         dlclose(g_libHandle);
62         g_libHandle = nullptr;
63         return;
64     }
65     g_getApiVersion = reinterpret_cast<GetApiVersion>(getApiVersionPtr);
66 }
67 
Reset()68 void DdkPermissionManager::Reset()
69 {
70     std::lock_guard<std::mutex> lock(g_mutex);
71     g_verifyAccessToken = nullptr;
72     if (g_libHandle != nullptr) {
73         dlclose(g_libHandle);
74         g_libHandle = nullptr;
75     }
76 }
77 
VerifyPermission(const std::string & permissionName)78 bool DdkPermissionManager::VerifyPermission(const std::string &permissionName)
79 {
80     std::lock_guard<std::mutex> lock(g_mutex);
81     InitVerifyAccessToken();
82     if (g_verifyAccessToken == nullptr) {
83         return false;
84     }
85 
86     uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
87     int result = g_verifyAccessToken(callerToken, permissionName);
88     HDF_LOGI("%{public}s VerifyAccessToken: %{public}d", __func__, result);
89     return result == PERMISSION_GRANTED;
90 }
91 
GetHapApiVersion(int32_t & apiVersion)92 int32_t DdkPermissionManager::GetHapApiVersion(int32_t &apiVersion)
93 {
94     std::lock_guard<std::mutex> lock(g_mutex);
95     InitVerifyAccessToken();
96     if (g_getApiVersion == nullptr) {
97         return HDF_FAILURE;
98     }
99 
100     uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
101     return g_getApiVersion(callerToken, apiVersion);
102 }
103 } // namespace Ddk
104 } // namespace Usb
105 } // namespace HDI
106 } // namespace OHOS