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 "systemabilitytokencallbackstub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "system_ability_token_callback_stub.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24 
25 using namespace OHOS::AAFwk;
26 
27 namespace OHOS {
28 namespace {
29 constexpr size_t FOO_MAX_LEN = 1024;
30 constexpr size_t U32_AT_SIZE = 4;
31 const std::u16string ABILITYMGR_INTERFACE_TOKEN = u"ohos.aafwk.ISystemAbilityTokenCallback";
32 }
33 
34 class SystemAbilityTokenCallbackStubFuzzTest : public SystemAbilityTokenCallbackStub {
35 public:
36     SystemAbilityTokenCallbackStubFuzzTest() = default;
~SystemAbilityTokenCallbackStubFuzzTest()37     virtual ~SystemAbilityTokenCallbackStubFuzzTest()
38     {}
SendResult(OHOS::AAFwk::Want & want,int32_t callerUid,int32_t requestCode,uint32_t accessToken,int32_t resultCode)39     int32_t SendResult(OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode,
40         uint32_t accessToken, int32_t resultCode) override
41     {
42         return 0;
43     }
44 };
45 
GetU32Data(const char * ptr)46 uint32_t GetU32Data(const char* ptr)
47 {
48     // convert fuzz input data to an integer
49     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
50 }
51 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)52 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
53 {
54     uint32_t code = GetU32Data(data);
55 
56     MessageParcel parcel;
57     parcel.WriteInterfaceToken(ABILITYMGR_INTERFACE_TOKEN);
58     parcel.WriteBuffer(data, size);
59     parcel.RewindRead(0);
60     MessageParcel reply;
61     MessageOption option;
62 
63     std::shared_ptr<SystemAbilityTokenCallbackStub> systemabilitytokencallbackstub
64         = std::make_shared<SystemAbilityTokenCallbackStubFuzzTest>();
65 
66     systemabilitytokencallbackstub->OnRemoteRequest(code, parcel, reply, option);
67 
68     return true;
69 }
70 }
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     /* Run your code on data */
76     if (data == nullptr) {
77         std::cout << "invalid data" << std::endl;
78         return 0;
79     }
80 
81     /* Validate the length of size */
82     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
83         return 0;
84     }
85 
86     char* ch = static_cast<char*>(malloc(size + 1));
87     if (ch == nullptr) {
88         std::cout << "malloc failed." << std::endl;
89         return 0;
90     }
91 
92     (void)memset_s(ch, size + 1, 0x00, size + 1);
93     if (memcpy_s(ch, size, data, size) != EOK) {
94         std::cout << "copy failed." << std::endl;
95         free(ch);
96         ch = nullptr;
97         return 0;
98     }
99 
100     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
101     free(ch);
102     ch = nullptr;
103     return 0;
104 }