• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "backupsa_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 
22 #include <climits>
23 #include "message_parcel.h"
24 #include "service_stub.h"
25 #include "service.h"
26 #include "securec.h"
27 #include "system_ability.h"
28 
29 #include "filemgmt_libhilog.h"
30 
31 using namespace OHOS::FileManagement::Backup;
32 
33 namespace OHOS {
34 constexpr size_t U32_AT_SIZE = 4;
35 constexpr uint8_t MAX_CALL_TRANSACTION = 24;
36 constexpr int32_t SERVICE_ID = 5203;
37 
GetU32Data(const char * ptr)38 uint32_t GetU32Data(const char* ptr)
39 {
40     // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
41     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
42 }
43 
BackupSaFuzzTest(const uint8_t * data,size_t size)44 bool BackupSaFuzzTest(const uint8_t *data, size_t size)
45 {
46     if (data == nullptr || size < U32_AT_SIZE) {
47         return true;
48     }
49 
50     sptr service = sptr(new Service(SERVICE_ID));
51     uint32_t code = GetU32Data(reinterpret_cast<const char*>(data));
52     if (code == 0) {
53         return true;
54     }
55     MessageParcel datas;
56     datas.WriteInterfaceToken(ServiceStub::GetDescriptor());
57     datas.WriteBuffer(reinterpret_cast<const char*>(data + U32_AT_SIZE), size - U32_AT_SIZE);
58     datas.RewindRead(0);
59     MessageParcel reply;
60     MessageOption option;
61     service->OnRemoteRequest(code % MAX_CALL_TRANSACTION, datas, reply, option);
62     service = nullptr;
63     return true;
64 }
65 } // namespace OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t dataSize)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
69 {
70     OHOS::BackupSaFuzzTest(data, dataSize);
71     return 0;
72 }