/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "timeservice_fuzzer.h" #include #include #include #include "accesstoken_kit.h" #include "nativetoken_kit.h" #include "token_setproc.h" #include "time_system_ability.h" #include "message_parcel.h" using namespace OHOS::MiscServices; namespace OHOS { constexpr size_t THRESHOLD = 10; const std::u16string TIMESERVICE_INTERFACE_TOKEN = u"ohos.miscservices.time.ITimeService"; using namespace OHOS::Security::AccessToken; void GrantNativePermission() { const char **perms = new const char *[2]; perms[0] = "ohos.permission.SET_TIME"; perms[1] = "ohos.permission.SET_TIME_ZONE"; TokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 2, .aclsNum = 0, .dcaps = nullptr, .perms = perms, .acls = nullptr, .processName = "time_service", .aplStr = "system_core", }; uint64_t tokenId = GetAccessTokenId(&infoInstance); SetSelfTokenID(tokenId); AccessTokenKit::ReloadNativeTokenInfo(); delete[] perms; } bool FuzzTimeService(const uint8_t* rawData, size_t size) { uint32_t code = (*rawData) % 10 + 1; MessageParcel data; data.WriteInterfaceToken(TIMESERVICE_INTERFACE_TOKEN); data.WriteBuffer(rawData, size); data.RewindRead(0); MessageParcel reply; MessageOption option; sptr mTimess = new TimeSystemAbility(); mTimess->OnRemoteRequest(code, data, reply, option); return true; } bool FuzzTimeDump(const uint8_t *rawData, size_t size) { std::vector args; std::string str(reinterpret_cast(rawData), size); args.push_back(Str8ToStr16(str)); int fd = 0; TimeSystemAbility::GetInstance()->Dump(fd, args); return true; } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { if (size < OHOS::THRESHOLD) { return 0; } /* Run your code on data */ OHOS::FuzzTimeService(data, size); OHOS::FuzzTimeDump(data, size); return 0; }