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 "systemabilitymanager_fuzzer.h"
17
18 #include "if_system_ability_manager.h"
19 #include "sam_mock_permission.h"
20 #include "system_ability_manager.h"
21
22 #include <cstddef>
23 #include <cstdint>
24 #include <unistd.h>
25 #include <cstdlib>
26 #include <fcntl.h>
27
28 namespace OHOS {
29 namespace Samgr {
30 namespace {
31 constexpr size_t THRESHOLD = 10;
32 constexpr uint8_t MAX_CALL_TRANSACTION = 64;
33 constexpr int32_t OFFSET = 4;
34 constexpr int32_t INIT_TIME = 3;
35 const std::u16string SAMGR_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
36 bool flag_ = false;
37 }
38
Convert2Uint32(const uint8_t * ptr)39 uint32_t Convert2Uint32(const uint8_t* ptr)
40 {
41 if (ptr == nullptr) {
42 return 0;
43 }
44 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); // this is a general method of converting in fuzz
45 }
46
FuzzSystemAbilityManager(const uint8_t * rawData,size_t size)47 void FuzzSystemAbilityManager(const uint8_t* rawData, size_t size)
48 {
49 SamMockPermission::MockPermission();
50 uint32_t code = Convert2Uint32(rawData);
51 rawData = rawData + OFFSET;
52 size = size - OFFSET;
53 MessageParcel data;
54 data.WriteInterfaceToken(SAMGR_INTERFACE_TOKEN);
55 data.WriteBuffer(rawData, size);
56 data.RewindRead(0);
57 MessageParcel reply;
58 MessageOption option;
59 sptr<SystemAbilityManager> manager = SystemAbilityManager::GetInstance();
60 if (!flag_) {
61 sleep(INIT_TIME);
62 flag_ = true;
63 } else {
64 manager->SetFfrt();
65 }
66 manager->OnRemoteRequest(code % MAX_CALL_TRANSACTION, data, reply, option);
67 manager->CleanFfrt();
68 }
69 }
70 }
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75 if (size < OHOS::Samgr::THRESHOLD) {
76 return 0;
77 }
78
79 OHOS::Samgr::FuzzSystemAbilityManager(data, size);
80 return 0;
81 }
82
83