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