• 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 "token_setproc.h"
22 #include "wallpaper_manager_common_info.h"
23 #include "wallpaper_manager_kits.h"
24 #include "pixel_map.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 int32_t OFFSET = 4;
32 
GrantNativePermission()33 void GrantNativePermission()
34 {
35     const char **perms = new const char *[2];
36     perms[0] = "ohos.permission.GET_WALLPAPER";
37     perms[1] = "ohos.permission.SET_WALLPAPER";
38     TokenInfoParams infoInstance = {
39         .dcapsNum = 0,
40         .permsNum = 2,
41         .aclsNum = 0,
42         .dcaps = nullptr,
43         .perms = perms,
44         .acls = nullptr,
45         .processName = "wallpaper_service",
46         .aplStr = "system_core",
47     };
48     uint64_t tokenId = GetAccessTokenId(&infoInstance);
49     SetSelfTokenID(tokenId);
50     AccessTokenKit::ReloadNativeTokenInfo();
51     delete[] perms;
52 }
53 
54 class WallpaperColorChangeListenerFuzzTestImpl : public OHOS::WallpaperMgrService::WallpaperColorChangeListener {
55 public:
56     std::vector<uint64_t> color_;
57     int wallpaperType_;
58     WallpaperColorChangeListenerFuzzTestImpl();
~WallpaperColorChangeListenerFuzzTestImpl()59     ~WallpaperColorChangeListenerFuzzTestImpl()
60     {
61     }
62 
63     WallpaperColorChangeListenerFuzzTestImpl(const WallpaperColorChangeListenerFuzzTestImpl &) = delete;
64     WallpaperColorChangeListenerFuzzTestImpl &operator=(const WallpaperColorChangeListenerFuzzTestImpl &) = delete;
65     WallpaperColorChangeListenerFuzzTestImpl(WallpaperColorChangeListenerFuzzTestImpl &&) = delete;
66     WallpaperColorChangeListenerFuzzTestImpl &operator=(WallpaperColorChangeListenerFuzzTestImpl &&) = delete;
67 
68     // callback function will be called when the db data is changed.
69     void OnColorsChange(const std::vector<uint64_t> &color, int wallpaperType);
70 
71 private:
72     unsigned long callCount_;
73 };
74 
OnColorsChange(const std::vector<uint64_t> & color,int wallpaperType)75 void WallpaperColorChangeListenerFuzzTestImpl::OnColorsChange(const std::vector<uint64_t> &color, int wallpaperType)
76 {
77     callCount_++;
78     std::copy(color.begin(), color.end(), std::back_inserter(color_));
79     wallpaperType_ = wallpaperType;
80 }
81 
WallpaperColorChangeListenerFuzzTestImpl()82 WallpaperColorChangeListenerFuzzTestImpl::WallpaperColorChangeListenerFuzzTestImpl()
83     : 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     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetColors(wallpaperType);
101 }
102 
GetWallpaperIdFuzzTest(const uint8_t * data,size_t size)103 void GetWallpaperIdFuzzTest(const uint8_t *data, size_t size)
104 {
105     uint32_t wallpaperType = ConvertToUint32(data);
106     GrantNativePermission();
107     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetWallpaperId(wallpaperType);
108 }
109 
ResetWallpaperFuzzTest(const uint8_t * data,size_t size)110 void ResetWallpaperFuzzTest(const uint8_t *data, size_t size)
111 {
112     uint32_t wallpaperType = ConvertToUint32(data);
113     GrantNativePermission();
114     WallpaperMgrService::WallpaperManagerkits::GetInstance().ResetWallpaper(wallpaperType);
115 }
116 
SetWallpaperByUrlFuzzTest(const uint8_t * data,size_t size)117 void SetWallpaperByUrlFuzzTest(const uint8_t *data, size_t size)
118 {
119     uint32_t wallpaperType = ConvertToUint32(data);
120     data = data + OFFSET;
121     size = size - OFFSET;
122     GrantNativePermission();
123     std::string url(reinterpret_cast<const char *>(data), size);
124     auto listener = std::make_shared<WallpaperColorChangeListenerFuzzTestImpl>();
125     WallpaperMgrService::WallpaperManagerkits::GetInstance().On("colorChange", listener);
126     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(url, wallpaperType);
127     WallpaperMgrService::WallpaperManagerkits::GetInstance().Off("colorChange", listener);
128 }
129 
SetWallpaperByMapFuzzTest(const uint8_t * data,size_t size)130 void SetWallpaperByMapFuzzTest(const uint8_t *data, size_t size)
131 {
132     uint32_t wallpaperType = ConvertToUint32(data);
133     GrantNativePermission();
134     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
135     InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
136     std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
137     std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
138     auto listener = std::make_shared<WallpaperColorChangeListenerFuzzTestImpl>();
139     WallpaperMgrService::WallpaperManagerkits::GetInstance().On("colorChange", listener);
140     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(pixelMap, wallpaperType);
141     WallpaperMgrService::WallpaperManagerkits::GetInstance().Off("colorChange", listener);
142 }
143 
GetFileFuzzTest(const uint8_t * data,size_t size)144 void GetFileFuzzTest(const uint8_t *data, size_t size)
145 {
146     uint32_t wallpaperType = ConvertToUint32(data);
147     GrantNativePermission();
148     int32_t wallpaperFd = 0;
149     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetFile(wallpaperType, wallpaperFd);
150 }
151 
GetPixelMapFuzzTest(const uint8_t * data,size_t size)152 void GetPixelMapFuzzTest(const uint8_t *data, size_t size)
153 {
154     uint32_t wallpaperType = ConvertToUint32(data);
155     GrantNativePermission();
156     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
157     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetPixelMap(wallpaperType, pixelMap);
158 }
159 } // namespace OHOS
160 
161 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)162 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
163 {
164     if (data == nullptr || size < OHOS::THRESHOLD) {
165         return 0;
166     }
167     /* Run your code on data */
168     OHOS::GetColorsFuzzTest(data, size);
169     OHOS::GetWallpaperIdFuzzTest(data, size);
170     OHOS::ResetWallpaperFuzzTest(data, size);
171     OHOS::SetWallpaperByUrlFuzzTest(data, size);
172     OHOS::SetWallpaperByMapFuzzTest(data, size);
173     OHOS::GetFileFuzzTest(data, size);
174     OHOS::GetPixelMapFuzzTest(data, size);
175     return 0;
176 }