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 "cmonremoterequest_fuzzer.h"
17
18 #include "cm_fuzz_test_common.h"
19 #include "cm_sa.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22
23 using namespace CmFuzzTest;
24 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)25 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
26 {
27 uint32_t minSize = sizeof(uint32_t) + sizeof(uint32_t) + sizeof(struct CmBlob);
28 uint8_t *myData = nullptr;
29 if (!CopyMyData(data, size, minSize, &myData)) {
30 return false;
31 }
32
33 uint32_t remainSize = static_cast<uint32_t>(size);
34 uint32_t offset = 0;
35
36 // get code
37 uint32_t code;
38 if (!GetUintFromBuffer(myData, &remainSize, &offset, &code)) {
39 CmFree(myData);
40 return false;
41 }
42 code = (code % static_cast<uint32_t>(CM_MSG_MAX - CM_MSG_BASE) + static_cast<uint32_t>(CM_MSG_BASE));
43
44 // get data
45 uint32_t outSize;
46 if (!GetUintFromBuffer(myData, &remainSize, &offset, &outSize)) {
47 CmFree(myData);
48 return false;
49 }
50 struct CmParamSet *paramSet = nullptr;
51 if (ConstructParamSet(myData, &remainSize, &offset,
52 static_cast<CertManagerInterfaceCode>(code), ¶mSet) == false) {
53 CmFree(myData);
54 return false;
55 }
56 struct CmBlob srcDataBlob = { paramSet->paramSetSize, reinterpret_cast<uint8_t *>(paramSet) };
57
58 Security::CertManager::CertManagerService &myService = Security::CertManager::CertManagerService::GetInstance();
59
60 std::u16string descriptor = myService.GetDescriptor();
61 MessageParcel messageData;
62 messageData.WriteInterfaceToken(descriptor);
63 messageData.WriteUint32(outSize);
64 messageData.WriteUint32(srcDataBlob.size);
65 messageData.WriteBuffer(srcDataBlob.data, static_cast<size_t>(srcDataBlob.size));
66
67 MessageParcel reply;
68 MessageOption option;
69 SetATPermission();
70 SystemAbilityOnDemandReason reason;
71 (void)myService.OnStart(reason);
72 (void)myService.OnRemoteRequest(code, messageData, reply, option);
73
74 CmFree(myData);
75 CmFreeParamSet(¶mSet);
76 return true;
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::DoSomethingInterestingWithMyAPI(data, size);
85 return 0;
86 }
87