• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "fuzztest_helper.h"
17 
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21 
22 namespace OHOS {
23 namespace UpdateEngine {
24 constexpr uint32_t CHAR_TO_INT_INDEX0 = 0;
25 constexpr uint32_t CHAR_TO_INT_INDEX1 = 1;
26 constexpr uint32_t CHAR_TO_INT_INDEX2 = 2;
27 constexpr uint32_t CHAR_TO_INT_INDEX3 = 3;
28 
29 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT0 = 0;
30 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT1 = 8;
31 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT2 = 16;
32 constexpr uint32_t CHAR_TO_INT_MOVE_LEFT3 = 24;
33 
34 constexpr uint32_t COUNT_BOOL_TYPE = 2;
35 
36 constexpr uint32_t FUZZ_HEAD_DATA = 0;
37 constexpr uint32_t FUZZ_INT_LEN_DATA = 4;
38 constexpr uint32_t FUZZ_CHAR_ARRAY_LEN_DATA = 64;
39 
FuzztestHelper()40 FuzztestHelper::FuzztestHelper()
41 {
42     static const char *perms[] = {
43         "ohos.permission.UPDATE_SYSTEM",
44         "ohos.permission.FACTORY_RESET"
45     };
46     NativeTokenInfoParams infoInstance = {
47         .dcapsNum = 0,
48         .permsNum = 2,
49         .aclsNum = 0,
50         .dcaps = nullptr,
51         .perms = perms,
52         .acls = nullptr,
53         .processName = "updateservice_fuzztest",
54         .aplStr = "system_basic",
55     };
56     uint64_t tokenId = GetAccessTokenId(&infoInstance);
57     if (tokenId == 0) {
58         return;
59     }
60     SetSelfTokenID(tokenId);
61     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
62 }
63 
~FuzztestHelper()64 FuzztestHelper::~FuzztestHelper()
65 {
66 }
67 
FtCheckNewVersionDone(const BusinessError & businessError,const CheckResult & checkResult)68 static void FtCheckNewVersionDone(const BusinessError &businessError, const CheckResult &checkResult)
69 {
70 }
71 
FtOnEvent(const EventInfo & eventInfo)72 static void FtOnEvent(const EventInfo &eventInfo)
73 {
74 }
75 
TrySetData(const uint8_t * data,size_t size)76 bool FuzztestHelper::TrySetData(const uint8_t* data, size_t size)
77 {
78     if (size < FUZZ_DATA_LEN) {
79         return false;
80     }
81 
82     if (memcpy_s(data_, sizeof(data_), data, sizeof(data_)) != EOK) {
83         return false;
84     }
85 
86     return true;
87 }
88 
GetInt()89 int32_t FuzztestHelper::GetInt()
90 {
91     int32_t number;
92     if (index_ + FUZZ_INT_LEN_DATA > FUZZ_DATA_LEN) {
93         index_ = FUZZ_HEAD_DATA;
94     }
95     number = static_cast<int32_t>(
96         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX0]) <<
97         CHAR_TO_INT_MOVE_LEFT3) +
98         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX1]) <<
99         CHAR_TO_INT_MOVE_LEFT2) +
100         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX2]) <<
101         CHAR_TO_INT_MOVE_LEFT1) +
102         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX3]) <<
103         CHAR_TO_INT_MOVE_LEFT0));
104     index_ += FUZZ_INT_LEN_DATA;
105     return number;
106 }
107 
BuildBusinessType()108 BusinessType FuzztestHelper::BuildBusinessType()
109 {
110     BusinessType businessType;
111     businessType.vendor = BusinessVendor::PUBLIC;
112     businessType.subType = BusinessSubType::FIRMWARE;
113     return businessType;
114 }
115 
BuildUpdateCallbackInfo()116 UpdateCallbackInfo FuzztestHelper::BuildUpdateCallbackInfo()
117 {
118     UpdateCallbackInfo cb;
119     cb.checkNewVersionDone = FtCheckNewVersionDone;
120     cb.onEvent = FtOnEvent;
121     return cb;
122 }
123 
BuildUpgradeInfo()124 UpgradeInfo FuzztestHelper::BuildUpgradeInfo()
125 {
126     UpgradeInfo upgradeInfo;
127     char upgradeApp[FUZZ_CHAR_ARRAY_LEN_DATA];
128     GetCharArray(upgradeApp, FUZZ_CHAR_ARRAY_LEN_DATA);
129     upgradeInfo.upgradeApp = upgradeApp;
130 
131     upgradeInfo.businessType = BuildBusinessType();
132 
133     char upgradeDevId[FUZZ_CHAR_ARRAY_LEN_DATA];
134     GetCharArray(upgradeDevId, FUZZ_CHAR_ARRAY_LEN_DATA);
135     upgradeInfo.upgradeDevId = upgradeDevId;
136 
137     char controlDevId[FUZZ_CHAR_ARRAY_LEN_DATA];
138     GetCharArray(controlDevId, FUZZ_CHAR_ARRAY_LEN_DATA);
139     upgradeInfo.controlDevId = controlDevId;
140     return upgradeInfo;
141 }
142 
BuildUpgradePolicy()143 UpgradePolicy FuzztestHelper::BuildUpgradePolicy()
144 {
145     UpgradePolicy upgradePolicy;
146     upgradePolicy.downloadStrategy = static_cast<bool>(GetUInt() % COUNT_BOOL_TYPE);
147     upgradePolicy.autoUpgradeStrategy = static_cast<bool>(GetUInt() % COUNT_BOOL_TYPE);
148     upgradePolicy.autoUpgradePeriods[0].start = GetUInt();
149     upgradePolicy.autoUpgradePeriods[0].end = GetUInt();
150     upgradePolicy.autoUpgradePeriods[1].start = GetUInt();
151     upgradePolicy.autoUpgradePeriods[1].end = GetUInt();
152     return upgradePolicy;
153 }
154 
BuildVersionDigestInfo()155 VersionDigestInfo FuzztestHelper::BuildVersionDigestInfo()
156 {
157     VersionDigestInfo versionDigestInfo;
158     versionDigestInfo.versionDigest = "versionDigest";
159     return versionDigestInfo;
160 }
161 
GetCharArray(char * charArray,uint32_t arraySize)162 void FuzztestHelper::GetCharArray(char *charArray, uint32_t arraySize)
163 {
164     if (index_ + arraySize > FUZZ_DATA_LEN) {
165         index_ = FUZZ_HEAD_DATA;
166     }
167     for (uint32_t i = 0; i < arraySize; i++) {
168         charArray[i] = static_cast<char>(data_[i + index_]);
169     }
170     index_ += arraySize;
171 }
172 
GetUInt()173 uint32_t FuzztestHelper::GetUInt()
174 {
175     uint32_t number;
176     if (index_ + FUZZ_INT_LEN_DATA > FUZZ_DATA_LEN) {
177         index_ = FUZZ_HEAD_DATA;
178     }
179     number = (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX0]) <<
180         CHAR_TO_INT_MOVE_LEFT3) +
181         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX1]) <<
182         CHAR_TO_INT_MOVE_LEFT2) +
183         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX2]) <<
184         CHAR_TO_INT_MOVE_LEFT1) +
185         (static_cast<uint32_t>(data_[index_ + CHAR_TO_INT_INDEX3]) <<
186         CHAR_TO_INT_MOVE_LEFT0);
187     index_ += FUZZ_INT_LEN_DATA;
188     return number;
189 }
190 } // namespace UpdateEngine
191 } // namespace OHOS
192