• 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_common.h"
24 #include "wallpaper_manager_common_info.h"
25 #include "wallpaper_manager_kits.h"
26 
27 using namespace OHOS::Security::AccessToken;
28 using namespace OHOS::Media;
29 
30 namespace OHOS {
31 constexpr size_t THRESHOLD = 10;
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 WallpaperColorChangeListenerFuzzTestImpl : public OHOS::WallpaperMgrService::WallpaperColorChangeListener {
56 public:
57     std::vector<uint64_t> color_;
58     int wallpaperType_;
59     WallpaperColorChangeListenerFuzzTestImpl();
~WallpaperColorChangeListenerFuzzTestImpl()60     ~WallpaperColorChangeListenerFuzzTestImpl()
61     {
62     }
63 
64     WallpaperColorChangeListenerFuzzTestImpl(const WallpaperColorChangeListenerFuzzTestImpl &) = delete;
65     WallpaperColorChangeListenerFuzzTestImpl &operator=(const WallpaperColorChangeListenerFuzzTestImpl &) = delete;
66     WallpaperColorChangeListenerFuzzTestImpl(WallpaperColorChangeListenerFuzzTestImpl &&) = delete;
67     WallpaperColorChangeListenerFuzzTestImpl &operator=(WallpaperColorChangeListenerFuzzTestImpl &&) = 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 WallpaperColorChangeListenerFuzzTestImpl::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 
WallpaperColorChangeListenerFuzzTestImpl()83 WallpaperColorChangeListenerFuzzTestImpl::WallpaperColorChangeListenerFuzzTestImpl()
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     WallpaperMgrService::ApiInfo apiInfo{ false, false };
102     std::vector<uint64_t> colors;
103     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetColors(wallpaperType, apiInfo, colors);
104 }
105 
GetWallpaperIdFuzzTest(const uint8_t * data,size_t size)106 void GetWallpaperIdFuzzTest(const uint8_t *data, size_t size)
107 {
108     uint32_t wallpaperType = ConvertToUint32(data);
109     GrantNativePermission();
110     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetWallpaperId(wallpaperType);
111 }
112 
ResetWallpaperFuzzTest(const uint8_t * data,size_t size)113 void ResetWallpaperFuzzTest(const uint8_t *data, size_t size)
114 {
115     uint32_t wallpaperType = ConvertToUint32(data);
116     GrantNativePermission();
117     WallpaperMgrService::ApiInfo apiInfo{ false, false };
118     WallpaperMgrService::WallpaperManagerkits::GetInstance().ResetWallpaper(wallpaperType, apiInfo);
119 }
120 
SetWallpaperByUrlFuzzTest(const uint8_t * data,size_t size)121 void SetWallpaperByUrlFuzzTest(const uint8_t *data, size_t size)
122 {
123     uint32_t wallpaperType = ConvertToUint32(data);
124     data = data + OFFSET;
125     size = size - OFFSET;
126     GrantNativePermission();
127 
128     std::string uri(reinterpret_cast<const char *>(data), size);
129     WallpaperMgrService::ApiInfo apiInfo{ false, false };
130     auto listener = std::make_shared<WallpaperColorChangeListenerFuzzTestImpl>();
131     WallpaperMgrService::WallpaperManagerkits::GetInstance().On("colorChange", listener);
132     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(uri, wallpaperType, apiInfo);
133     WallpaperMgrService::WallpaperManagerkits::GetInstance().Off("colorChange", listener);
134 }
135 
SetWallpaperByMapFuzzTest(const uint8_t * data,size_t size)136 void SetWallpaperByMapFuzzTest(const uint8_t *data, size_t size)
137 {
138     uint32_t wallpaperType = ConvertToUint32(data);
139     GrantNativePermission();
140     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
141     InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
142     std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
143     std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
144     auto listener = std::make_shared<WallpaperColorChangeListenerFuzzTestImpl>();
145     WallpaperMgrService::ApiInfo apiInfo{ false, false };
146     WallpaperMgrService::WallpaperManagerkits::GetInstance().On("colorChange", listener);
147     WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(pixelMap, wallpaperType, apiInfo);
148     WallpaperMgrService::WallpaperManagerkits::GetInstance().Off("colorChange", listener);
149 }
150 
GetFileFuzzTest(const uint8_t * data,size_t size)151 void GetFileFuzzTest(const uint8_t *data, size_t size)
152 {
153     uint32_t wallpaperType = ConvertToUint32(data);
154     GrantNativePermission();
155     int32_t wallpaperFd = 0;
156     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetFile(wallpaperType, wallpaperFd);
157 }
158 
GetPixelMapFuzzTest(const uint8_t * data,size_t size)159 void GetPixelMapFuzzTest(const uint8_t *data, size_t size)
160 {
161     uint32_t wallpaperType = ConvertToUint32(data);
162     GrantNativePermission();
163     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
164     WallpaperMgrService::ApiInfo apiInfo{ false, false };
165     WallpaperMgrService::WallpaperManagerkits::GetInstance().GetPixelMap(wallpaperType, apiInfo, pixelMap);
166 }
167 } // namespace OHOS
168 
169 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)170 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
171 {
172     if (data == nullptr || size < OHOS::THRESHOLD) {
173         return 0;
174     }
175     /* Run your code on data */
176     OHOS::GetColorsFuzzTest(data, size);
177     OHOS::GetWallpaperIdFuzzTest(data, size);
178     OHOS::ResetWallpaperFuzzTest(data, size);
179     OHOS::SetWallpaperByUrlFuzzTest(data, size);
180     OHOS::SetWallpaperByMapFuzzTest(data, size);
181     OHOS::GetFileFuzzTest(data, size);
182     OHOS::GetPixelMapFuzzTest(data, size);
183     return 0;
184 }