1 /*
2 * Copyright (c) 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
16 #include "huks_apply_permission_test_common.h"
17
18 #include "accesstoken_kit.h"
19 #include "dlp_permission_log.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22
23 #include <thread>
24 #include <iostream>
25
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
28 LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "HuksApplyPermissionCommon" };
29 }
30
31 static constexpr uint32_t WAIT_FOR_ACCESS_TOKEN_START = 500;
32 #define AC_TKN_SVC "accesstoken_service"
33 #define SVC_CTRL "service_control"
34 static constexpr char PID_OF_ACCESS_TOKEN_SERVICE[] = "pidof " AC_TKN_SVC;
35 static constexpr char HUKS_SKIP_RESTART_ACCESS_TOKEN_SERVICE[] = "HUKS_SKIP_RESTART_ACCESS_TOKEN_SERVICE";
36
RestartAccessTokenService()37 static void RestartAccessTokenService()
38 {
39 std::cout << PID_OF_ACCESS_TOKEN_SERVICE << std::endl;
40 std::system(PID_OF_ACCESS_TOKEN_SERVICE);
41
42 std::system(SVC_CTRL " stop " AC_TKN_SVC);
43
44 std::cout << PID_OF_ACCESS_TOKEN_SERVICE << std::endl;
45 std::system(PID_OF_ACCESS_TOKEN_SERVICE);
46
47 std::system(SVC_CTRL " start " AC_TKN_SVC);
48
49 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_ACCESS_TOKEN_START));
50
51 std::cout << PID_OF_ACCESS_TOKEN_SERVICE << std::endl;
52 std::system(PID_OF_ACCESS_TOKEN_SERVICE);
53 }
54
SetIdsTokenForAcrossAccountsPermissionInner()55 int32_t SetIdsTokenForAcrossAccountsPermissionInner()
56 {
57 uint64_t tokenId;
58 const char *acls[] = {
59 "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS",
60 };
61 const char *perms[] = {
62 "ohos.permission.PLACE_CALL", // system_basic
63 "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS",
64 };
65 NativeTokenInfoParams infoInstance = {
66 .dcapsNum = 0,
67 .permsNum = 2,
68 .dcaps = nullptr,
69 .perms = perms,
70 .aplStr = "system_basic",
71 };
72 infoInstance.acls = acls;
73 infoInstance.aclsNum = 1;
74 infoInstance.processName = "test_across_local_account";
75 tokenId = GetAccessTokenId(&infoInstance);
76 int32_t ret = SetSelfTokenID(tokenId);
77 if (ret != 0) {
78 DLP_LOG_ERROR(LABEL, "SetSelfTokenID fail, ret is %{public}d", ret);
79 return ret;
80 }
81 ret = OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
82 return ret;
83 }
84
SetIdsTokenForAcrossAccountsPermission()85 int32_t SetIdsTokenForAcrossAccountsPermission()
86 {
87 int32_t ret = SetIdsTokenForAcrossAccountsPermissionInner();
88 if (std::getenv(HUKS_SKIP_RESTART_ACCESS_TOKEN_SERVICE) != nullptr) {
89 return ret;
90 }
91 RestartAccessTokenService();
92 return SetIdsTokenForAcrossAccountsPermissionInner();
93 }