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 "oaid_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20 #include "oaid_service_stub.h"
21 #include "oaid_service.h"
22 #include "oaid_hilog_wreapper.h"
23 #include "oaid_service_ipc_interface_code.h"
24 #include "accesstoken_kit.h"
25 #include "nativetoken_kit.h"
26 #include "token_setproc.h"
27 #undef private
28
29 using namespace std;
30 using namespace OHOS::Cloud;
31
32 namespace OHOS {
33 const std::u16string OAID_INTERFACE_TOKEN = u"ohos.cloud.oaid.IOAIDService";
34 const std::string OAID_TRACKING_CONSENT_PERMISSION = "ohos.permission.APP_TRACKING_CONSENT";
35 bool g_isGrant = false;
36
AddPermission()37 void AddPermission()
38 {
39 if (!g_isGrant) {
40 const char *perms[] = {
41 OAID_TRACKING_CONSENT_PERMISSION.c_str()
42 };
43 NativeTokenInfoParams infoInstance = {
44 .dcapsNum = 0,
45 .permsNum = 1,
46 .aclsNum = 0,
47 .dcaps = nullptr,
48 .perms = perms,
49 .acls = nullptr,
50 .processName = "OAIDFuzzer",
51 .aplStr = "system_basic",
52 };
53 uint64_t tokenId = GetAccessTokenId(&infoInstance);
54 SetSelfTokenID(tokenId);
55 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
56 g_isGrant = true;
57 }
58 }
59
OAIDFuzzTest(const uint8_t * rawData,size_t size)60 bool OAIDFuzzTest(const uint8_t* rawData, size_t size)
61 {
62 uint32_t startCode = static_cast<uint32_t>(OHOS::Cloud::OAIDInterfaceCode::GET_OAID);
63 uint32_t endCode = static_cast<uint32_t>(OHOS::Cloud::OAIDInterfaceCode::RESET_OAID);
64 for (uint32_t code = startCode; code <= endCode; code++) {
65 MessageParcel data;
66 data.WriteInterfaceToken(OAID_INTERFACE_TOKEN);
67 MessageParcel reply;
68 MessageOption option;
69 auto oaidService =
70 sptr<Cloud::OAIDService>(new (std::nothrow) Cloud::OAIDService());
71 oaidService->OnRemoteRequest(code, data, reply, option);
72 }
73
74 return true;
75 }
76 }
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81 /* Run your code on data */
82 OHOS::AddPermission();
83 OHOS::OAIDFuzzTest(data, size);
84 return 0;
85 }
86
87