1 /*
2 * Copyright (c) 2025 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 "backupsaappincrementalfileready_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <climits>
22 #include <fuzzer/FuzzedDataProvider.h>
23 #include <vector>
24
25 #include "message_parcel.h"
26 #include "sandbox_helper.h"
27 #include "service.h"
28 #include "service_proxy.h"
29 #include "service_reverse.h"
30 #include "service_stub.h"
31 #include "securec.h"
32 #include "system_ability.h"
33
34 using namespace std;
35 using namespace OHOS::FileManagement::Backup;
36
37 namespace OHOS {
38 constexpr int32_t SERVICE_ID = 5203;
39
CmdAppIncrementalFileReadyFuzzTest(const uint8_t * data,size_t size)40 bool CmdAppIncrementalFileReadyFuzzTest(const uint8_t *data, size_t size)
41 {
42 MessageParcel datas;
43 datas.WriteInterfaceToken(ServiceStub::GetDescriptor());
44 std::string fileName(reinterpret_cast<const char *>(data), size);
45 datas.WriteString(fileName);
46 if (size >= sizeof(int)) {
47 int fd = *(reinterpret_cast<const int *>(data));
48 datas.WriteFileDescriptor(UniqueFd(fd));
49 }
50
51 if (size >= sizeof(int) + sizeof(int)) {
52 int manifestId = *(reinterpret_cast<const int *>(data + sizeof(int)));
53 datas.WriteFileDescriptor(UniqueFd(manifestId));
54 }
55 datas.RewindRead(0);
56 MessageParcel reply;
57 MessageOption option;
58
59 sptr service(new Service(SERVICE_ID));
60 uint32_t code = static_cast<uint32_t>(IServiceIpcCode::COMMAND_APP_INCREMENTAL_FILE_READY);
61 service->OnRemoteRequest(code, datas, reply, option);
62 return true;
63 }
64 } // namespace OHOS
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69 try {
70 OHOS::CmdAppIncrementalFileReadyFuzzTest(data, size);
71 } catch (OHOS::FileManagement::Backup::BError &err) {
72 HILOGE("BackupSaFuzzTest error");
73 } catch (...) {
74 HILOGE("BackupSaFuzzTest exception");
75 }
76 return 0;
77 }