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