1 /*
2  * Copyright (c) 2023-2024 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 "cmdqueryohosaccountinfobyuseridstub_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 #define private public
21 #include "account_mgr_service.h"
22 #undef private
23 #include "fuzz_data.h"
24 #include "iaccount.h"
25 #include "src/callbacks.h"
26 
27 using namespace std;
28 using namespace OHOS::AccountSA;
29 
30 namespace OHOS {
31 namespace {
32 const std::u16string ACCOUNT_TOKEN = u"ohos.accountfwk.IAccount";
33 static pthread_once_t g_fcOnce = PTHREAD_ONCE_INIT;
34 }
35 
SelinuxLog(int logLevel,const char * fmt,...)36 static int SelinuxLog(int logLevel, const char *fmt, ...)
37 {
38     (void)logLevel;
39     (void)fmt;
40     return 0;
41 }
42 
SelinuxSetCallback()43 static void SelinuxSetCallback()
44 {
45     union selinux_callback cb;
46     cb.func_log = SelinuxLog;
47     selinux_set_callback(SELINUX_CB_LOG, cb);
48 }
49 
CmdQueryOhosAccountInfoByUserIdStubFuzzTest(const uint8_t * data,size_t size)50 bool CmdQueryOhosAccountInfoByUserIdStubFuzzTest(const uint8_t* data, size_t size)
51 {
52     __selinux_once(g_fcOnce, SelinuxSetCallback);
53     if ((data == nullptr) || (size == 0)) {
54         return false;
55     }
56     MessageParcel dataTemp;
57     FuzzData fuzzData(data, size);
58     int32_t userId = fuzzData.GetData<int32_t>();
59     if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
60         return false;
61     }
62     if (!dataTemp.WriteInt32(userId)) {
63         return false;
64     }
65     MessageParcel reply;
66     MessageOption option;
67     uint32_t code = static_cast<uint32_t>(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID);
68     DelayedRefSingleton<AccountMgrService>::GetInstance().state_ = ServiceRunningState::STATE_RUNNING;
69     DelayedRefSingleton<AccountMgrService>::GetInstance().OnRemoteRequest(code, dataTemp, reply, option);
70 
71     return true;
72 }
73 }
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     OHOS::CmdQueryOhosAccountInfoByUserIdStubFuzzTest(data, size);
80     return 0;
81 }
82 
83