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
16 #include "filesystemcrypto_fuzzer.h"
17 #include "crypto/filesystem_crypto.h"
18 #include "storage_service_errno.h"
19 #include "storage_service_log.h"
20 #include "vector"
21
22 namespace OHOS {
23 namespace StorageManager {
24
25 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)26 T TypeCast(const uint8_t *data, int *pos = nullptr)
27 {
28 if (pos) {
29 *pos += sizeof(T);
30 }
31 return *(reinterpret_cast<const T*>(data));
32 }
33
34 auto &fileSystem = FileSystemCrypto::GetInstance();
35
GenerateUserKeysFuzzTest(const uint8_t * data,size_t size)36 bool GenerateUserKeysFuzzTest(const uint8_t *data, size_t size)
37 {
38 if (data == nullptr || size <= sizeof(uint32_t) + sizeof(uint32_t)) {
39 return false;
40 }
41
42 int pos = 0;
43 uint32_t userId = TypeCast<uint32_t>(data, &pos);
44 uint32_t flags = TypeCast<uint32_t>(data + pos);
45
46 int32_t result = fileSystem.GenerateUserKeys(userId, flags);
47 if (result != E_OK) {
48 LOGI("file system crypto fuzz test of interface FileSystemCrypto::GenerateUserKeysTest failed!");
49 return false;
50 }
51 return true;
52 }
53
DeleteUserKeysFuzzTest(const uint8_t * data,size_t size)54 bool DeleteUserKeysFuzzTest(const uint8_t *data, size_t size)
55 {
56 if (data == nullptr || size <= sizeof(uint32_t)) {
57 return false;
58 }
59
60 int pos = 0;
61 uint32_t userId = TypeCast<uint32_t>(data, &pos);
62
63 int32_t result = fileSystem.DeleteUserKeys(userId);
64 if (result != E_OK) {
65 LOGI("file system crypto fuzz test of interface FileSystemCrypto::DeleteUserKeys failed!");
66 return false;
67 }
68 return true;
69 }
70
UpdateUserAuthFuzzTest(const uint8_t * data,size_t size)71 bool UpdateUserAuthFuzzTest(const uint8_t *data, size_t size)
72 {
73 if (data == nullptr || size <= sizeof(uint32_t) + sizeof(uint64_t)) {
74 return false;
75 }
76
77 int pos = 0;
78 uint32_t userId = TypeCast<uint32_t>(data, &pos);
79 uint64_t secureUid = TypeCast<uint64_t>(data + pos);
80
81 std::vector<uint8_t> token;
82 std::vector<uint8_t> oldSecret;
83 std::vector<uint8_t> newSecret;
84 token.push_back(*data);
85 oldSecret.push_back(*data);
86 newSecret.push_back(*data);
87
88 int32_t result = fileSystem.UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
89 if (result != E_OK) {
90 LOGI("file system crypto fuzz test of interface FileSystemCrypto::UpdateUserAuth failed!");
91 return false;
92 }
93 return true;
94 }
95
ActiveUserKeyFuzzTest(const uint8_t * data,size_t size)96 bool ActiveUserKeyFuzzTest(const uint8_t *data, size_t size)
97 {
98 if ((data == nullptr) || (size <= sizeof(uint32_t))) {
99 return false;
100 }
101
102 int pos = 0;
103 uint32_t userId = TypeCast<uint32_t>(data, &pos);
104
105 std::vector<uint8_t> token;
106 std::vector<uint8_t> secret;
107 token.push_back(*data);
108 secret.push_back(*data);
109
110 int32_t result = fileSystem.ActiveUserKey(userId, token, secret);
111 if (result != E_OK) {
112 LOGI("file system crypto fuzz test of interface FileSystemCrypto::ActiveUserKey failed!");
113 return false;
114 }
115 return true;
116 }
117
InactiveUserKeyFuzzTest(const uint8_t * data,size_t size)118 bool InactiveUserKeyFuzzTest(const uint8_t *data, size_t size)
119 {
120 if ((data == nullptr) || (size <= sizeof(uint32_t))) {
121 return false;
122 }
123
124 int pos = 0;
125 uint32_t userId = TypeCast<uint32_t>(data, &pos);
126
127 int32_t result = fileSystem.InactiveUserKey(userId);
128 if (result != E_OK) {
129 LOGI("file system crypto fuzz test of interface FileSystemCrypto::InactiveUserKey failed!");
130 return false;
131 }
132 return true;
133 }
134
UpdateKeyContextFuzzTest(const uint8_t * data,size_t size)135 bool UpdateKeyContextFuzzTest(const uint8_t *data, size_t size)
136 {
137 if ((data == nullptr) || (size <= sizeof(uint32_t))) {
138 return false;
139 }
140
141 int pos = 0;
142 uint32_t userId = TypeCast<uint32_t>(data, &pos);
143
144 int32_t result = fileSystem.UpdateKeyContext(userId);
145 if (result != E_OK) {
146 LOGI("file system crypto fuzz test of interface FileSystemCrypto::UpdateKeyContext failed!");
147 return false;
148 }
149 return true;
150 }
151
LockUserScreenFuzzTest(const uint8_t * data,size_t size)152 bool LockUserScreenFuzzTest(const uint8_t *data, size_t size)
153 {
154 if ((data == nullptr) || (size <= sizeof(uint32_t))) {
155 return false;
156 }
157
158 int pos = 0;
159 uint32_t userId = TypeCast<uint32_t>(data, &pos);
160
161 int32_t result = fileSystem.LockUserScreen(userId);
162 if (result != E_OK) {
163 LOGI("file system crypto fuzz test of interface FileSystemCrypto::LockUserScreen failed!");
164 return false;
165 }
166 return true;
167 }
168
UnlockUserScreenFuzzTest(const uint8_t * data,size_t size)169 bool UnlockUserScreenFuzzTest(const uint8_t *data, size_t size)
170 {
171 if ((data == nullptr) || (size <= sizeof(uint32_t))) {
172 return false;
173 }
174
175 int pos = 0;
176 uint32_t userId = TypeCast<uint32_t>(data, &pos);
177
178 int32_t result = fileSystem.UnlockUserScreen(userId, {}, {});
179 if (result != E_OK) {
180 LOGI("file system crypto fuzz test of interface FileSystemCrypto::UnlockUserScreen failed!");
181 return false;
182 }
183 return true;
184 }
185
GetLockScreenStatusFuzzTest(const uint8_t * data,size_t size)186 bool GetLockScreenStatusFuzzTest(const uint8_t *data, size_t size)
187 {
188 if (data == nullptr || size <= sizeof(uint32_t) + sizeof(bool)) {
189 return false;
190 }
191
192 int pos = 0;
193 uint32_t userId = TypeCast<uint32_t>(data, &pos);
194 bool lockScreenStatus = TypeCast<bool>(data + pos);
195
196 int32_t result = fileSystem.GetLockScreenStatus(userId, lockScreenStatus);
197 if (result != E_OK) {
198 LOGI("file system crypto fuzz test of interface FileSystemCrypto::GetLockScreenStatus failed!");
199 return false;
200 }
201 return true;
202 }
203
GenerateAppkeyFuzzTest(const uint8_t * data,size_t size)204 bool GenerateAppkeyFuzzTest(const uint8_t *data, size_t size)
205 {
206 if (data == nullptr || size <= sizeof(uint32_t) + sizeof(uint32_t)) {
207 return false;
208 }
209
210 int pos = 0;
211 uint32_t hashId = TypeCast<uint32_t>(data, &pos);
212 uint32_t userId = TypeCast<uint32_t>(data + pos);
213 std::string keyId;
214 int32_t result = fileSystem.GenerateAppkey(hashId, userId, keyId);
215 if (result != E_OK) {
216 LOGI("file system crypto fuzz test of interface FileSystemCrypto::GenerateAppkey failed!");
217 return false;
218 }
219 return true;
220 }
221
DeleteAppkeyFuzzTest(const uint8_t * data,size_t size)222 bool DeleteAppkeyFuzzTest(const uint8_t *data, size_t size)
223 {
224 if (data == nullptr) {
225 return false;
226 }
227 const std::string keyId(reinterpret_cast<const char *>(data), size);
228 int32_t result = fileSystem.DeleteAppkey(keyId);
229 if (result != E_OK) {
230 LOGI("file system crypto fuzz test of interface FileSystemCrypto::DeleteAppkey failed!");
231 return false;
232 }
233 return true;
234 }
235 } // namespace StorageManager
236 } // namespace OHOS
237
238 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)239 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
240 {
241 /* Run your code on data */
242 OHOS::StorageManager::GenerateUserKeysFuzzTest(data, size);
243 OHOS::StorageManager::DeleteUserKeysFuzzTest(data, size);
244 OHOS::StorageManager::UpdateUserAuthFuzzTest(data, size);
245 OHOS::StorageManager::ActiveUserKeyFuzzTest(data, size);
246 OHOS::StorageManager::InactiveUserKeyFuzzTest(data, size);
247 OHOS::StorageManager::UpdateKeyContextFuzzTest(data, size);
248 OHOS::StorageManager::LockUserScreenFuzzTest(data, size);
249 OHOS::StorageManager::UnlockUserScreenFuzzTest(data, size);
250 OHOS::StorageManager::GetLockScreenStatusFuzzTest(data, size);
251 OHOS::StorageManager::GenerateAppkeyFuzzTest(data, size);
252 OHOS::StorageManager::DeleteAppkeyFuzzTest(data, size);
253
254 return 0;
255 }
256