• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "fusion_security.h"
17 
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "nocopyable.h"
25 #include "softbus_bus_center.h"
26 #include "token_setproc.h"
27 
28 #include "devicestatus_define.h"
29 
30 using namespace OHOS;
31 using namespace OHOS::Security::AccessToken;
32 
33 namespace {
34 constexpr HiviewDFX::HiLogLabel LABEL { LOG_CORE, Msdp::MSDP_DOMAIN_ID, "FusionSecurity" };
35 } // namespace
36 
SetAceessTokenPermission(const std::string & processName,const char ** perms,size_t permCount)37 static void SetAceessTokenPermission(const std::string &processName, const char** perms, size_t permCount)
38 {
39     if (perms == nullptr || permCount == 0) {
40         FI_HILOGE("perms is nullptr or permCount is 0");
41         return;
42     }
43     uint64_t tokenId;
44     NativeTokenInfoParams infoInstance = {
45         .dcapsNum = 0,
46         .permsNum = permCount,
47         .aclsNum = 0,
48         .dcaps = nullptr,
49         .perms = perms,
50         .acls = nullptr,
51         .processName = processName.c_str(),
52         .aplStr = "system_basic",
53     };
54     tokenId = GetAccessTokenId(&infoInstance);
55     SetSelfTokenID(tokenId);
56     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
57 }
58 
GetAccessToken()59 void GetAccessToken()
60 {
61     const char* perms[] {
62         "ohos.permission.CAPTURE_SCREEN",
63         "ohos.permission.DISTRIBUTED_DATASYNC",
64     };
65     size_t permCount = 2;
66     SetAceessTokenPermission("fusion_device_profile_rust", perms, permCount);
67 }
68 
69 struct CString : public CIString {
70     std::string str;
71 
72     explicit CString(const char* s);
73     DISALLOW_MOVE(CString);
74     CString(const CString &other);
75     ~CString() = default;
76     CString& operator=(const CString &other) = delete;
77 
78     static CIString* Clone(CIString* target);
79     static void Destruct(CIString* target);
80     static const char* GetData(CIString* target);
81 };
82 
CString(const char * s)83 CString::CString(const char* s)
84     : str(s != nullptr ? s : std::string())
85 {
86     clone = &CString::Clone;
87     destruct = &CString::Destruct;
88     data = &CString::GetData;
89 }
90 
CString(const CString & other)91 CString::CString(const CString &other)
92     : str(other.str)
93 {
94     clone = &CString::Clone;
95     destruct = &CString::Destruct;
96     data = &CString::GetData;
97 }
98 
Clone(CIString * target)99 CIString* CString::Clone(CIString* target)
100 {
101     CString* t = static_cast<CString*>(target);
102     CHKPP(t);
103     return new (std::nothrow) CString(*t);
104 }
105 
Destruct(CIString * target)106 void CString::Destruct(CIString* target)
107 {
108     CString* t = static_cast<CString*>(target);
109     CHKPV(t);
110     delete t;
111 }
112 
GetData(CIString * target)113 const char* CString::GetData(CIString* target)
114 {
115     CString* t = static_cast<CString*>(target);
116     CHKPP(t);
117     return t->str.c_str();
118 }
119 
GetLocalNetworkId()120 CIString* GetLocalNetworkId()
121 {
122     CALL_DEBUG_ENTER;
123     NodeBasicInfo node;
124     int32_t ret = GetLocalNodeDeviceInfo(FI_PKG_NAME, &node);
125     if (ret != RET_OK) {
126         FI_HILOGE("GetLocalNodeDeviceInfo ret:%{public}d", ret);
127         return nullptr;
128     }
129     return new (std::nothrow) CString(node.networkId);
130 }
131