• 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 <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_kits.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     int 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, int wallpaperType);
71 
72 private:
73     unsigned long callCount_;
74 };
75 
OnColorsChange(const std::vector<uint64_t> & color,int wallpaperType)76 void WallpaperEventListenerFuzzTestImpl::OnColorsChange(const std::vector<uint64_t> &color, int wallpaperType)
77 {
78     callCount_++;
79     std::copy(color.begin(), color.end(), std::back_inserter(color_));
80     wallpaperType_ = wallpaperType;
81 }
82 
WallpaperEventListenerFuzzTestImpl()83 WallpaperEventListenerFuzzTestImpl::WallpaperEventListenerFuzzTestImpl()
84     : wallpaperType_(-1), callCount_(0)
85 {
86 }
87 
ConvertToUint32(const uint8_t * ptr)88 uint32_t ConvertToUint32(const uint8_t *ptr)
89 {
90     if (ptr == nullptr) {
91         return 0;
92     }
93     uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
94     return bigVar;
95 }
96 
GetColorsFuzzTest(const uint8_t * data,size_t size)97 void GetColorsFuzzTest(const uint8_t *data, size_t size)
98 {
99     uint32_t wallpaperType = ConvertToUint32(data);
100     GrantNativePermission();
101     if (size < LENGTH) {
102         return;
103     }
104     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
105     std::vector<uint64_t> colors;
106     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetColors(wallpaperType, apiInfo, colors);
107 }
108 
GetWallpaperIdFuzzTest(const uint8_t * data,size_t size)109 void GetWallpaperIdFuzzTest(const uint8_t *data, size_t size)
110 {
111     uint32_t wallpaperType = ConvertToUint32(data);
112     GrantNativePermission();
113     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetWallpaperId(wallpaperType);
114 }
115 
ResetWallpaperFuzzTest(const uint8_t * data,size_t size)116 void ResetWallpaperFuzzTest(const uint8_t *data, size_t size)
117 {
118     uint32_t wallpaperType = ConvertToUint32(data);
119     GrantNativePermission();
120     if (size < LENGTH) {
121         return;
122     }
123     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
124     WallpaperMgrService::WallpaperManagerkits::GetInstance().ResetWallpaper(wallpaperType, apiInfo);
125 }
126 
SetWallpaperByUriFuzzTest(const uint8_t * data,size_t size)127 void SetWallpaperByUriFuzzTest(const uint8_t *data, size_t size)
128 {
129     uint32_t wallpaperType = ConvertToUint32(data);
130     data = data + OFFSET;
131     size = size - OFFSET;
132     GrantNativePermission();
133     std::string uri(reinterpret_cast<const char *>(data), size);
134     if (size < LENGTH) {
135         return;
136     }
137     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
138     auto listener = std::make_shared<WallpaperEventListenerFuzzTestImpl>();
139     WallpaperMgrService::WallpaperManagerkits::GetInstance().On("colorChange", listener);
140     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(uri, wallpaperType, apiInfo);
141     WallpaperMgrService::WallpaperManagerkits::GetInstance().Off("colorChange", listener);
142 }
143 
SetWallpaperByMapFuzzTest(const uint8_t * data,size_t size)144 void SetWallpaperByMapFuzzTest(const uint8_t *data, size_t size)
145 {
146     uint32_t wallpaperType = ConvertToUint32(data);
147     GrantNativePermission();
148     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
149     InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
150     std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
151     std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
152     auto listener = std::make_shared<WallpaperEventListenerFuzzTestImpl>();
153     if (size < LENGTH) {
154         return;
155     }
156     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
157     WallpaperMgrService::WallpaperManagerkits::GetInstance().On("colorChange", listener);
158     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(pixelMap, wallpaperType, apiInfo);
159     WallpaperMgrService::WallpaperManagerkits::GetInstance().Off("colorChange", listener);
160 }
161 
GetFileFuzzTest(const uint8_t * data,size_t size)162 void GetFileFuzzTest(const uint8_t *data, size_t size)
163 {
164     uint32_t wallpaperType = ConvertToUint32(data);
165     GrantNativePermission();
166     int32_t wallpaperFd = 0;
167     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetFile(wallpaperType, wallpaperFd);
168 }
169 
WallpaperManagerFuzzTest(const uint8_t * data,size_t size)170 void WallpaperManagerFuzzTest(const uint8_t *data, size_t size)
171 {
172     auto minHeight = static_cast<int32_t>(size);
173     auto minWidth = static_cast<int32_t>(size);
174     if (size < LENGTH) {
175         return;
176     }
177     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
178     GrantNativePermission();
179     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetWallpaperMinHeight(apiInfo, minHeight);
180     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetWallpaperMinWidth(apiInfo, minWidth);
181     WallpaperMgrService::WallpaperManagerkits::GetInstance().IsChangePermitted();
182     WallpaperMgrService::WallpaperManagerkits::GetInstance().IsOperationAllowed();
183 }
184 
GetPixelMapFuzzTest(const uint8_t * data,size_t size)185 void GetPixelMapFuzzTest(const uint8_t *data, size_t size)
186 {
187     uint32_t wallpaperType = ConvertToUint32(data);
188     GrantNativePermission();
189     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
190     if (size < LENGTH) {
191         return;
192     }
193     WallpaperMgrService::ApiInfo apiInfo{ static_cast<bool>(data[0] % 2), static_cast<bool>(data[1] % 2) };
194     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetPixelMap(wallpaperType, apiInfo, pixelMap);
195 }
196 
SetVideoFuzzTest(const uint8_t * data,size_t size)197 void SetVideoFuzzTest(const uint8_t *data, size_t size)
198 {
199     uint32_t wallpaperType = ConvertToUint32(data);
200     data = data + OFFSET;
201     size = size - OFFSET;
202     GrantNativePermission();
203     std::string uri(reinterpret_cast<const char *>(data), size);
204     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetVideo(uri, wallpaperType);
205 }
206 
SendEventFuzzTest(const uint8_t * data,size_t size)207 void SendEventFuzzTest(const uint8_t *data, size_t size)
208 {
209     data = data + OFFSET;
210     size = size - OFFSET;
211     GrantNativePermission();
212     std::string eventType(reinterpret_cast<const char *>(data), size);
213     WallpaperMgrService::WallpaperManagerkits::GetInstance().SendEvent(eventType);
214 }
215 
SetCustomWallpaperFuzzTest(const uint8_t * data,size_t size)216 void SetCustomWallpaperFuzzTest(const uint8_t *data, size_t size)
217 {
218     uint32_t wallpaperType = ConvertToUint32(data);
219     data = data + OFFSET;
220     size = size - OFFSET;
221     GrantNativePermission();
222     std::string uri(reinterpret_cast<const char *>(data), size);
223     if (size < LENGTH) {
224         return;
225     }
226     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetCustomWallpaper(uri, wallpaperType);
227 }
228 } // namespace OHOS
229 
230 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)231 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
232 {
233     if (data == nullptr || size < OHOS::THRESHOLD) {
234         return 0;
235     }
236     /* Run your code on data */
237     OHOS::GetColorsFuzzTest(data, size);
238     OHOS::GetWallpaperIdFuzzTest(data, size);
239     OHOS::ResetWallpaperFuzzTest(data, size);
240     OHOS::SetWallpaperByUriFuzzTest(data, size);
241     OHOS::SetWallpaperByMapFuzzTest(data, size);
242     OHOS::GetFileFuzzTest(data, size);
243     OHOS::WallpaperManagerFuzzTest(data, size);
244     OHOS::GetPixelMapFuzzTest(data, size);
245     OHOS::SendEventFuzzTest(data, size);
246     OHOS::SetVideoFuzzTest(data, size);
247     OHOS::SetCustomWallpaperFuzzTest(data, size);
248     return 0;
249 }