• 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 "addsimmessage_fuzzer.h"
17 
18 #define private public
19 #include "addsmstoken_fuzzer.h"
20 #include "i_sms_service_interface.h"
21 #include "napi_util.h"
22 #include "sms_service.h"
23 
24 using namespace OHOS::Telephony;
25 namespace OHOS {
26 static bool g_isInited = false;
27 constexpr int32_t SLOT_NUM = 2;
28 static int32_t STATUS_COUNT = 4;
29 
IsServiceInited()30 bool IsServiceInited()
31 {
32     if (!g_isInited) {
33         DelayedSingleton<SmsService>::GetInstance()->OnStart();
34         if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
35             static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
36             g_isInited = true;
37         }
38     }
39     return g_isInited;
40 }
41 
OnRemoteRequest(const uint8_t * data,size_t size)42 void OnRemoteRequest(const uint8_t *data, size_t size)
43 {
44     if (!IsServiceInited()) {
45         return;
46     }
47 
48     MessageParcel dataParcel;
49     if (!dataParcel.WriteInterfaceToken(SmsInterfaceStub::GetDescriptor())) {
50         TELEPHONY_LOGE("OnRemoteRequest WriteInterfaceToken is false");
51         return;
52     }
53 
54     MessageParcel replyParcel;
55     MessageOption option(MessageOption::TF_SYNC);
56 
57     size_t dataSize = size - sizeof(uint32_t);
58     dataParcel.WriteBuffer(data + sizeof(uint32_t), dataSize);
59     dataParcel.RewindRead(0);
60     uint32_t code = static_cast<uint32_t>(size);
61 
62     DelayedSingleton<SmsService>::GetInstance()->OnRemoteRequest(code, dataParcel, replyParcel, option);
63     return;
64 }
65 
AddSimMessage(const uint8_t * data,size_t size)66 void AddSimMessage(const uint8_t *data, size_t size)
67 {
68     if (!IsServiceInited()) {
69         return;
70     }
71 
72     MessageParcel dataParcel;
73     MessageParcel replyParcel;
74     MessageOption option(MessageOption::TF_SYNC);
75 
76     std::string smsc(reinterpret_cast<const char *>(data), size);
77     std::string pdu(reinterpret_cast<const char *>(data), size);
78     auto smscU16 = Str8ToStr16(smsc);
79     auto pduU16 = Str8ToStr16(pdu);
80     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
81     auto status = static_cast<ISmsServiceInterface::SimMessageStatus>(size % STATUS_COUNT);
82 
83     dataParcel.WriteInt32(slotId);
84     dataParcel.WriteString16(smscU16);
85     dataParcel.WriteString16(pduU16);
86     dataParcel.WriteUint32(status);
87     dataParcel.RewindRead(0);
88     DelayedSingleton<SmsService>::GetInstance()->OnAddSimMessage(dataParcel, replyParcel, option);
89 
90     std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
91     if (interfaceManager == nullptr) {
92         TELEPHONY_LOGE("interfaceManager nullptr");
93         return;
94     }
95     interfaceManager->AddSimMessage(smsc, pdu, status);
96 
97     auto smsMiscRunner = AppExecFwk::EventRunner::Create("SmsMiscRunner");
98     if (smsMiscRunner == nullptr) {
99         TELEPHONY_LOGE("failed to create SmsMiscRunner");
100         return;
101     }
102     std::shared_ptr<SmsMiscManager> smsMiscManager = std::make_shared<SmsMiscManager>(smsMiscRunner, slotId);
103     if (smsMiscManager == nullptr) {
104         TELEPHONY_LOGE("smsMiscManager nullptr");
105         return;
106     }
107     smsMiscManager->AddSimMessage(smsc, pdu, status);
108 }
109 
HasSmsCapability(const uint8_t * data,size_t size)110 void HasSmsCapability(const uint8_t *data, size_t size)
111 {
112     if (!IsServiceInited()) {
113         return;
114     }
115 
116     MessageParcel dataParcel;
117     MessageParcel replyParcel;
118     MessageOption option(MessageOption::TF_SYNC);
119 
120     dataParcel.WriteBuffer(data, size);
121     dataParcel.RewindRead(0);
122     DelayedSingleton<SmsService>::GetInstance()->OnHasSmsCapability(dataParcel, replyParcel, option);
123 
124     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
125     std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
126     if (interfaceManager == nullptr) {
127         TELEPHONY_LOGE("interfaceManager nullptr error");
128         return;
129     }
130     interfaceManager->HasSmsCapability();
131 }
132 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)133 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
134 {
135     if (data == nullptr || size == 0) {
136         return;
137     }
138 
139     OnRemoteRequest(data, size);
140     AddSimMessage(data, size);
141     HasSmsCapability(data, size);
142 }
143 } // namespace OHOS
144 
145 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)146 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
147 {
148     /* Run your code on data */
149     OHOS::AddSmsTokenFuzzer token;
150     OHOS::DoSomethingInterestingWithMyAPI(data, size);
151     return 0;
152 }
153