1/*
2 * Copyright (c) 2024 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
17#include <hdf_log.h>
18#include <dlfcn.h>
19#include <mutex>
20
21#include "ipc_skeleton.h"
22
23namespace OHOS {
24namespace HDI {
25namespace Usb {
26namespace Ddk {
27namespace V1_0 {
28using VerifyAccessTokenFunc = int(*)(uint32_t callerToken, const std::string &permissionName);
29
30static constexpr int PERMISSION_GRANTED = 0;
31
32static void *g_libHandle = nullptr;
33static VerifyAccessTokenFunc g_verifyAccessToken = nullptr;
34
35static std::mutex g_mutex;
36
37static void InitVerifyAccessToken()
38{
39    if (g_verifyAccessToken != nullptr) {
40        return;
41    }
42
43    g_libHandle = dlopen("libusb_ddk_dynamic_library_wrapper.z.so", RTLD_LAZY);
44    if (g_libHandle == nullptr) {
45        HDF_LOGE("%{public}s dlopen failed: %{public}s", __func__, dlerror());
46        return;
47    }
48
49    void *funcPtr = dlsym(g_libHandle, "VerifyAccessToken");
50    if (funcPtr == nullptr) {
51        HDF_LOGE("%{public}s dlsym failed: %{public}s", __func__, dlerror());
52        dlclose(g_libHandle);
53        g_libHandle = nullptr;
54        return;
55    }
56
57    g_verifyAccessToken = reinterpret_cast<VerifyAccessTokenFunc>(funcPtr);
58}
59
60void DdkPermissionManager::Reset()
61{
62    std::lock_guard<std::mutex> lock(g_mutex);
63    g_verifyAccessToken = nullptr;
64    if (g_libHandle != nullptr) {
65        dlclose(g_libHandle);
66        g_libHandle = nullptr;
67    }
68}
69
70bool DdkPermissionManager::VerifyPermission(const std::string &permissionName)
71{
72    std::lock_guard<std::mutex> lock(g_mutex);
73    InitVerifyAccessToken();
74    if (g_verifyAccessToken == nullptr) {
75        return false;
76    }
77
78    uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
79    int result = g_verifyAccessToken(callerToken, permissionName);
80    HDF_LOGI("%{public}s VerifyAccessToken: %{public}d", __func__, result);
81    return result == PERMISSION_GRANTED;
82}
83} // namespace V1_0
84} // namespace Ddk
85} // namespace Usb
86} // namespace HDI
87} // namespace OHOS