• 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 "fingerprint_auth_interface_stub_fuzzer.h"
17 
18 #include "iam_logger.h"
19 
20 #include "fingerprint_auth_hdi.h"
21 
22 #undef LOG_TAG
23 #define LOG_TAG "FINGERPRINT_AUTH_IMPL"
24 
25 #undef private
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace FingerprintAuth {
30 namespace {
31 constexpr uint32_t FINGERPRINT_AUTH_INTERFACE_STUB_CODE_MIN = 0;
32 constexpr uint32_t FINGERPRINT_AUTH_INTERFACE_STUB_CODE_MAX = 3;
33 constexpr uint32_t FINGERPRINT_AUTH_INTERFACE_STUB_CODE_MIN_V1_1 = 2;
34 const std::u16string FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN_V1_0 =
35     u"ohos.hdi.fingerprint_auth.v1_0.IFingerprintAuthInterface";
36 const std::u16string FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN_V1_1 =
37     u"ohos.hdi.fingerprint_auth.v2_0.IFingerprintAuthInterface";
38 
FingerprintAuthInterfaceStubFuzzTest(const uint8_t * rawData,size_t size)39 bool FingerprintAuthInterfaceStubFuzzTest(const uint8_t *rawData, size_t size)
40 {
41     IAM_LOGI("start");
42     if (rawData == nullptr) {
43         IAM_LOGE("%{public}s:rawData is null.", __func__);
44         return false;
45     }
46     sptr<IFingerprintAuthInterface> serviceImpl = IFingerprintAuthInterface::Get(true);
47     if (serviceImpl == nullptr) {
48         IAM_LOGE("%{public}s:get serviceImpl failed.", __func__);
49         return false;
50     }
51     sptr<FingerprintAuthInterfaceStub> fingerprintAuthInterfaceStub = new FingerprintAuthInterfaceStub(serviceImpl);
52     if (fingerprintAuthInterfaceStub == nullptr) {
53         IAM_LOGE("%{public}s:new IFingerprintAuthInterfaceStub failed.", __func__);
54         return false;
55     }
56 
57     for (uint32_t code = FINGERPRINT_AUTH_INTERFACE_STUB_CODE_MIN; code < FINGERPRINT_AUTH_INTERFACE_STUB_CODE_MAX;
58          code++) {
59         MessageParcel data;
60         MessageParcel reply;
61         MessageOption optionSync = MessageOption::TF_SYNC;
62         MessageOption optionAsync = MessageOption::TF_ASYNC;
63         std::u16string FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN;
64         if (code < FINGERPRINT_AUTH_INTERFACE_STUB_CODE_MIN_V1_1) {
65             FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN = FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN_V1_0;
66         } else {
67             FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN = FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN_V1_1;
68         }
69         // Sync
70         data.WriteInterfaceToken(FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN);
71         data.WriteBuffer(rawData, size);
72         data.RewindRead(0);
73         (void)fingerprintAuthInterfaceStub->OnRemoteRequest(code, data, reply, optionSync);
74         // Async
75         data.WriteInterfaceToken(FINGERPRINT_AUTH_INTERFACE_STUB_TOKEN);
76         data.WriteBuffer(rawData, size);
77         data.RewindRead(0);
78         (void)fingerprintAuthInterfaceStub->OnRemoteRequest(code, data, reply, optionAsync);
79     }
80     return true;
81 }
82 } // namespace
83 } // namespace FingerprintAuth
84 } // namespace HDI
85 } // namespace OHOS
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
89 {
90     OHOS::HDI::FingerprintAuth::FingerprintAuthInterfaceStubFuzzTest(data, size);
91     return 0;
92 }
93