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 <cstdint>
17 #include <iostream>
18 
19 #include "accesstoken_kit.h"
20 #include "nativetoken_kit.h"
21 #include "pixel_map.h"
22 #include "token_setproc.h"
23 #include "wallpaper_manager_common_info.h"
24 #include "wallpaper_manager.h"
25 
26 using namespace OHOS::Security::AccessToken;
27 using namespace OHOS::Media;
28 
29 namespace OHOS {
30 constexpr size_t THRESHOLD = 10;
31 constexpr size_t LENGTH = 2;
32 constexpr int32_t OFFSET = 4;
33 
GrantNativePermission()34 void GrantNativePermission()
35 {
36     const char **perms = new const char *[2];
37     perms[0] = "ohos.permission.GET_WALLPAPER";
38     perms[1] = "ohos.permission.SET_WALLPAPER";
39     TokenInfoParams infoInstance = {
40         .dcapsNum = 0,
41         .permsNum = 2,
42         .aclsNum = 0,
43         .dcaps = nullptr,
44         .perms = perms,
45         .acls = nullptr,
46         .processName = "wallpaper_service",
47         .aplStr = "system_core",
48     };
49     uint64_t tokenId = GetAccessTokenId(&infoInstance);
50     SetSelfTokenID(tokenId);
51     AccessTokenKit::ReloadNativeTokenInfo();
52     delete[] perms;
53 }
54 
55 class WallpaperEventListenerFuzzTestImpl : public OHOS::WallpaperMgrService::WallpaperEventListener {
56 public:
57     std::vector<uint64_t> color_;
58     int32_t wallpaperType_;
59     WallpaperEventListenerFuzzTestImpl();
~WallpaperEventListenerFuzzTestImpl()60     ~WallpaperEventListenerFuzzTestImpl()
61     {
62     }
63 
64     WallpaperEventListenerFuzzTestImpl(const WallpaperEventListenerFuzzTestImpl &) = delete;
65     WallpaperEventListenerFuzzTestImpl &operator=(const WallpaperEventListenerFuzzTestImpl &) = delete;
66     WallpaperEventListenerFuzzTestImpl(WallpaperEventListenerFuzzTestImpl &&) = delete;
67     WallpaperEventListenerFuzzTestImpl &operator=(WallpaperEventListenerFuzzTestImpl &&) = delete;
68 
69     // callback function will be called when the db data is changed.
70     void OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType);
71 
72 private:
73     unsigned long callCount_;
74 };
75 
OnColorsChange(const std::vector<uint64_t> & color,int32_t wallpaperType)76 void WallpaperEventListenerFuzzTestImpl::OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType)
77 {
78     callCount_++;
79     std::copy(color.begin(), color.end(), std::back_inserter(color_));
80     wallpaperType_ = wallpaperType;
81 }
82 
WallpaperEventListenerFuzzTestImpl()83 WallpaperEventListenerFuzzTestImpl::WallpaperEventListenerFuzzTestImpl() : wallpaperType_(-1), callCount_(0)
84 {
85 }
86 
ConvertToUint32(const uint8_t * ptr)87 uint32_t ConvertToUint32(const uint8_t *ptr)
88 {
89     if (ptr == nullptr) {
90         return 0;
91     }
92     uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
93     return bigVar;
94 }
95 
GetColorsFuzzTest(const uint8_t * data,size_t size)96 void GetColorsFuzzTest(const uint8_t *data, size_t size)
97 {
98     uint32_t wallpaperType = ConvertToUint32(data);
99     GrantNativePermission();
100     if (size < LENGTH) {
101         return;
102     }
103     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
104     std::vector<uint64_t> colors;
105     WallpaperMgrService::WallpaperManager::GetInstance().GetColors(wallpaperType, apiInfo, colors);
106 }
107 
GetWallpaperIdFuzzTest(const uint8_t * data,size_t size)108 void GetWallpaperIdFuzzTest(const uint8_t *data, size_t size)
109 {
110     uint32_t wallpaperType = ConvertToUint32(data);
111     GrantNativePermission();
112     WallpaperMgrService::WallpaperManager::GetInstance().GetWallpaperId(wallpaperType);
113 }
114 
ResetWallpaperFuzzTest(const uint8_t * data,size_t size)115 void ResetWallpaperFuzzTest(const uint8_t *data, size_t size)
116 {
117     uint32_t wallpaperType = ConvertToUint32(data);
118     GrantNativePermission();
119     if (size < LENGTH) {
120         return;
121     }
122     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
123     WallpaperMgrService::WallpaperManager::GetInstance().ResetWallpaper(wallpaperType, apiInfo);
124 }
125 
SetWallpaperByUriFuzzTest(const uint8_t * data,size_t size)126 void SetWallpaperByUriFuzzTest(const uint8_t *data, size_t size)
127 {
128     uint32_t wallpaperType = ConvertToUint32(data);
129     data = data + OFFSET;
130     size = size - OFFSET;
131     GrantNativePermission();
132     std::string uri(reinterpret_cast<const char *>(data), size);
133     if (size < LENGTH) {
134         return;
135     }
136     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
137     auto listener = std::make_shared<WallpaperEventListenerFuzzTestImpl>();
138     WallpaperMgrService::WallpaperManager::GetInstance().On("colorChange", listener);
139     WallpaperMgrService::WallpaperManager::GetInstance().SetWallpaper(uri, wallpaperType, apiInfo);
140     WallpaperMgrService::WallpaperManager::GetInstance().Off("colorChange", listener);
141 }
142 
SetWallpaperByMapFuzzTest(const uint8_t * data,size_t size)143 void SetWallpaperByMapFuzzTest(const uint8_t *data, size_t size)
144 {
145     uint32_t wallpaperType = ConvertToUint32(data);
146     GrantNativePermission();
147     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
148     InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
149     std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
150     std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
151     auto listener = std::make_shared<WallpaperEventListenerFuzzTestImpl>();
152     if (size < LENGTH) {
153         return;
154     }
155     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
156     WallpaperMgrService::WallpaperManager::GetInstance().On("colorChange", listener);
157     WallpaperMgrService::WallpaperManager::GetInstance().SetWallpaper(pixelMap, wallpaperType, apiInfo);
158     WallpaperMgrService::WallpaperManager::GetInstance().Off("colorChange", listener);
159 }
160 
GetFileFuzzTest(const uint8_t * data,size_t size)161 void GetFileFuzzTest(const uint8_t *data, size_t size)
162 {
163     uint32_t wallpaperType = ConvertToUint32(data);
164     GrantNativePermission();
165     int32_t wallpaperFd = 0;
166     WallpaperMgrService::WallpaperManager::GetInstance().GetFile(wallpaperType, wallpaperFd);
167 }
168 
WallpaperManagerFuzzTest(const uint8_t * data,size_t size)169 void WallpaperManagerFuzzTest(const uint8_t *data, size_t size)
170 {
171     auto minHeight = static_cast<int32_t>(size);
172     auto minWidth = static_cast<int32_t>(size);
173     if (size < LENGTH) {
174         return;
175     }
176     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
177     GrantNativePermission();
178     WallpaperMgrService::WallpaperManager::GetInstance().GetWallpaperMinHeight(apiInfo, minHeight);
179     WallpaperMgrService::WallpaperManager::GetInstance().GetWallpaperMinWidth(apiInfo, minWidth);
180     WallpaperMgrService::WallpaperManager::GetInstance().IsChangePermitted();
181     WallpaperMgrService::WallpaperManager::GetInstance().IsOperationAllowed();
182 }
183 
GetPixelMapFuzzTest(const uint8_t * data,size_t size)184 void GetPixelMapFuzzTest(const uint8_t *data, size_t size)
185 {
186     uint32_t wallpaperType = ConvertToUint32(data);
187     GrantNativePermission();
188     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
189     if (size < LENGTH) {
190         return;
191     }
192     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
193     WallpaperMgrService::WallpaperManager::GetInstance().GetPixelMap(wallpaperType, apiInfo, pixelMap);
194 }
195 
SetVideoFuzzTest(const uint8_t * data,size_t size)196 void SetVideoFuzzTest(const uint8_t *data, size_t size)
197 {
198     uint32_t wallpaperType = ConvertToUint32(data);
199     data = data + OFFSET;
200     size = size - OFFSET;
201     GrantNativePermission();
202     std::string uri(reinterpret_cast<const char *>(data), size);
203     WallpaperMgrService::WallpaperManager::GetInstance().SetVideo(uri, wallpaperType);
204 }
205 
SendEventFuzzTest(const uint8_t * data,size_t size)206 void SendEventFuzzTest(const uint8_t *data, size_t size)
207 {
208     data = data + OFFSET;
209     size = size - OFFSET;
210     GrantNativePermission();
211     std::string eventType(reinterpret_cast<const char *>(data), size);
212     WallpaperMgrService::WallpaperManager::GetInstance().SendEvent(eventType);
213 }
214 
SetCustomWallpaperFuzzTest(const uint8_t * data,size_t size)215 void SetCustomWallpaperFuzzTest(const uint8_t *data, size_t size)
216 {
217     uint32_t wallpaperType = ConvertToUint32(data);
218     data = data + OFFSET;
219     size = size - OFFSET;
220     GrantNativePermission();
221     std::string uri(reinterpret_cast<const char *>(data), size);
222     if (size < LENGTH) {
223         return;
224     }
225     WallpaperMgrService::WallpaperManager::GetInstance().SetCustomWallpaper(uri, wallpaperType);
226 }
227 } // namespace OHOS
228 
229 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)230 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
231 {
232     if (data == nullptr || size < OHOS::THRESHOLD) {
233         return 0;
234     }
235     /* Run your code on data */
236     OHOS::GetColorsFuzzTest(data, size);
237     OHOS::GetWallpaperIdFuzzTest(data, size);
238     OHOS::ResetWallpaperFuzzTest(data, size);
239     OHOS::SetWallpaperByUriFuzzTest(data, size);
240     OHOS::SetWallpaperByMapFuzzTest(data, size);
241     OHOS::GetFileFuzzTest(data, size);
242     OHOS::WallpaperManagerFuzzTest(data, size);
243     OHOS::GetPixelMapFuzzTest(data, size);
244     OHOS::SendEventFuzzTest(data, size);
245     OHOS::SetVideoFuzzTest(data, size);
246     OHOS::SetCustomWallpaperFuzzTest(data, size);
247     return 0;
248 }