• 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 "timeservice_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_ex.h>
21 
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 #include "time_system_ability.h"
26 #include "message_parcel.h"
27 
28 using namespace OHOS::MiscServices;
29 
30 namespace OHOS {
31 constexpr size_t THRESHOLD = 10;
32 const std::u16string TIMESERVICE_INTERFACE_TOKEN = u"ohos.miscservices.time.ITimeService";
33 
34 using namespace OHOS::Security::AccessToken;
35 
GrantNativePermission()36 void GrantNativePermission()
37 {
38     const char **perms = new const char *[2];
39     perms[0] = "ohos.permission.SET_TIME";
40     perms[1] = "ohos.permission.SET_TIME_ZONE";
41     TokenInfoParams infoInstance = {
42         .dcapsNum = 0,
43         .permsNum = 2,
44         .aclsNum = 0,
45         .dcaps = nullptr,
46         .perms = perms,
47         .acls = nullptr,
48         .processName = "time_service",
49         .aplStr = "system_core",
50     };
51     uint64_t tokenId = GetAccessTokenId(&infoInstance);
52     SetSelfTokenID(tokenId);
53     AccessTokenKit::ReloadNativeTokenInfo();
54     delete[] perms;
55 }
56 
FuzzTimeService(const uint8_t * rawData,size_t size)57 bool FuzzTimeService(const uint8_t* rawData, size_t size)
58 {
59     uint32_t code = (*rawData) % 10 + 1;
60 
61     MessageParcel data;
62     data.WriteInterfaceToken(TIMESERVICE_INTERFACE_TOKEN);
63     data.WriteBuffer(rawData, size);
64     data.RewindRead(0);
65     MessageParcel reply;
66     MessageOption option;
67 
68     sptr<TimeSystemAbility> mTimess = new TimeSystemAbility();
69     mTimess->OnRemoteRequest(code, data, reply, option);
70 
71     return true;
72 }
73 
FuzzTimeDump(const uint8_t * rawData,size_t size)74 bool FuzzTimeDump(const uint8_t *rawData, size_t size)
75 {
76     std::vector<std::u16string> args;
77     std::string str(reinterpret_cast<const char *>(rawData), size);
78     args.push_back(Str8ToStr16(str));
79     int fd = 0;
80     TimeSystemAbility::GetInstance()->Dump(fd, args);
81     return true;
82 }
83 }
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87     if (size < OHOS::THRESHOLD) {
88         return 0;
89     }
90     /* Run your code on data */
91     OHOS::FuzzTimeService(data, size);
92     OHOS::FuzzTimeDump(data, size);
93     return 0;
94 }