• 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 "appmgrrest_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "app_death_recipient.h"
22 #include "app_mgr_service_event_handler.h"
23 #include "app_process_manager.h"
24 #define private public
25 #include "app_spawn_client.h"
26 #include "app_spawn_msg_wrapper.h"
27 #undef private
28 #include "app_spawn_socket.h"
29 #include "remote_client_manager.h"
30 #include "window_focus_changed_listener.h"
31 #include "ability_record.h"
32 #include "parcel.h"
33 #include "securec.h"
34 
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::AppExecFwk;
37 
38 namespace OHOS {
39 namespace {
40 constexpr size_t FOO_MAX_LEN = 1024;
41 constexpr size_t U32_AT_SIZE = 4;
42 constexpr uint8_t ENABLE = 2;
43 constexpr size_t OFFSET_ZERO = 24;
44 constexpr size_t OFFSET_ONE = 16;
45 constexpr size_t OFFSET_TWO = 8;
46 }
GetU32Data(const char * ptr)47 uint32_t GetU32Data(const char* ptr)
48 {
49     // convert fuzz input data to an integer
50     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << OFFSET_ONE) | (ptr[2] << OFFSET_TWO) | ptr[3];
51 }
GetFuzzAbilityToken()52 sptr<Token> GetFuzzAbilityToken()
53 {
54     sptr<Token> token = nullptr;
55 
56     AbilityRequest abilityRequest;
57     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
58     abilityRequest.abilityInfo.name = "MainAbility";
59     abilityRequest.abilityInfo.type = AbilityType::PAGE;
60     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
61     if (abilityRecord) {
62         token = abilityRecord->GetToken();
63     }
64 
65     return token;
66 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)67 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
68 {
69     sptr<AppDeathRecipient> appDeathRecipient = new AppDeathRecipient();
70     wptr<IRemoteObject> remote;
71     appDeathRecipient->OnRemoteDied(remote);
72     std::shared_ptr<AMSEventHandler> handler;
73     appDeathRecipient->SetEventHandler(handler);
74     std::shared_ptr<AppMgrServiceInner> serviceInner;
75     appDeathRecipient->SetAppMgrServiceInner(serviceInner);
76     bool isRenderProcess = *data % ENABLE;
77     appDeathRecipient->SetIsRenderProcess(isRenderProcess);
78     AppProcessManager appProcessManager;
79     std::shared_ptr<AppTaskInfo> appTaskInfo;
80     appProcessManager.RemoveAppFromRecentList(appTaskInfo);
81     std::string appName(data, size);
82     std::string processName(data, size);
83     pid_t pid = static_cast<pid_t>(GetU32Data(data));
84     int32_t recordId = static_cast<int32_t>(GetU32Data(data));
85     appProcessManager.AddAppToRecentList(appName, processName, pid, recordId);
86     appProcessManager.PushAppFront(recordId);
87     appProcessManager.GetAppTaskInfoById(recordId);
88     std::string bundleName(data, size);
89     appProcessManager.GetAppTaskInfoByProcessName(appName, bundleName);
90     appProcessManager.GetRecentAppList();
91     appProcessManager.RemoveAppFromRecentListById(recordId);
92     appProcessManager.ClearRecentAppList();
93     AppSpawnClient appSpawnClient;
94     appSpawnClient.OpenConnection();
95     std::shared_ptr<AppSpawnSocket> socket;
96     appSpawnClient.SetSocket(socket);
97     appSpawnClient.PreStartNWebSpawnProcess();
98     appSpawnClient.PreStartNWebSpawnProcessImpl();
99     AppSpawnStartMsg startMsg;
100     appSpawnClient.StartProcess(startMsg, pid);
101     int status = static_cast<int>(GetU32Data(data));
102     appSpawnClient.GetRenderProcessTerminationStatus(startMsg, status);
103     appSpawnClient.QueryConnectionState();
104     appSpawnClient.CloseConnection();
105     AppSpawnMsgWrapper appSpawnMsgWrapper;
106     appSpawnMsgWrapper.AssembleMsg(startMsg);
107     appSpawnMsgWrapper.VerifyMsg(startMsg);
108     appSpawnMsgWrapper.DumpMsg();
109     appSpawnMsgWrapper.FreeMsg();
110     AppSpawnSocket appSpawnSocket;
111     std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket;
112     appSpawnSocket.SetClientSocket(clientSocket);
113     appSpawnSocket.OpenAppSpawnConnection();
114     int32_t len = appSpawnMsgWrapper.GetMsgLength();
115     appSpawnSocket.WriteMessage(appSpawnMsgWrapper.GetMsgBuf(), len);
116     appSpawnSocket.ReadMessage(reinterpret_cast<void *>(*data), len);
117     appSpawnSocket.CloseAppSpawnConnection();
118     RemoteClientManager remoteClientManager;
119     sptr<IBundleMgr> bundleManager = nullptr;
120     remoteClientManager.SetBundleManager(bundleManager);
121     std::shared_ptr<AppSpawnClient> appSpawnClientptr;
122     remoteClientManager.SetSpawnClient(appSpawnClientptr);
123     remoteClientManager.GetSpawnClient();
124     remoteClientManager.GetBundleManager();
125     remoteClientManager.GetNWebSpawnClient();
126     std::shared_ptr<AppMgrServiceInner> owner;
127     WindowFocusChangedListener windowFocusChangedListener(owner, handler);
128     sptr<Rosen::FocusChangeInfo> focusChangeInfo = nullptr;
129     windowFocusChangedListener.OnFocused(focusChangeInfo);
130     windowFocusChangedListener.OnUnfocused(focusChangeInfo);
131     return (appSpawnClient.StartProcessImpl(startMsg, pid) != 0);
132 }
133 }
134 
135 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)136 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
137 {
138     /* Run your code on data */
139     if (data == nullptr) {
140         std::cout << "invalid data" << std::endl;
141         return 0;
142     }
143 
144     /* Validate the length of size */
145     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
146         return 0;
147     }
148 
149     char* ch = (char *)malloc(size + 1);
150     if (ch == nullptr) {
151         std::cout << "malloc failed." << std::endl;
152         return 0;
153     }
154 
155     (void)memset_s(ch, size + 1, 0x00, size + 1);
156     if (memcpy_s(ch, size, data, size) != EOK) {
157         std::cout << "copy failed." << std::endl;
158         free(ch);
159         ch = nullptr;
160         return 0;
161     }
162 
163     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
164     free(ch);
165     ch = nullptr;
166     return 0;
167 }
168 
169