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 "generatecertstub_fuzzer.h"
17 #include <iostream>
18 #include <openssl/rand.h>
19 #include <string>
20 #include <vector>
21 #include <thread>
22 #include "accesstoken_kit.h"
23 #include "dlp_permission.h"
24 #include "dlp_permission_async_stub.h"
25 #include "dlp_permission_kit.h"
26 #include "dlp_permission_log.h"
27 #include "securec.h"
28 #include "token_setproc.h"
29
30 using namespace OHOS::Security::DlpPermission;
31 using namespace OHOS::Security::AccessToken;
32 namespace OHOS {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION,
34 "GenerateCertFuzzTest" };
35
GetRandNum()36 static int GetRandNum()
37 {
38 unsigned int rand;
39 RAND_bytes(reinterpret_cast<unsigned char *>(&rand), sizeof(rand));
40 return rand;
41 }
42
FuzzTest(const uint8_t * data,size_t size)43 static void FuzzTest(const uint8_t* data, size_t size)
44 {
45 std::string name(reinterpret_cast<const char*>(data), size);
46 uint64_t curTime = static_cast<uint64_t>(
47 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count());
48 PermissionPolicy encPolicy;
49 encPolicy.ownerAccount_ = name;
50 encPolicy.ownerAccountType_ = DlpAccountType::DOMAIN_ACCOUNT;
51 encPolicy.SetIv(nullptr, 0);
52 encPolicy.SetAeskey(nullptr, 0);
53 int userNum = GetRandNum() % (size + 1) + 1;
54 DLP_LOG_INFO(LABEL, "before for:%{public}d,%{public}zu", userNum, size);
55 for (int user = 0; user < userNum; ++user) {
56 AuthUserInfo perminfo;
57 perminfo.authAccount = name;
58 perminfo.authPerm = static_cast<DLPFileAccess>(1 + GetRandNum() % 3); // perm type 1 to 3
59 perminfo.permExpiryTime = curTime + GetRandNum() % 200; // time range 0 to 200
60 perminfo.authAccountType = DlpAccountType::DOMAIN_ACCOUNT;
61 encPolicy.authUsers_.emplace_back(perminfo);
62 }
63 DlpPolicyParcel parcel;
64 parcel.policyParams_ = encPolicy;
65 MessageParcel datas;
66 if (!datas.WriteInterfaceToken(DlpPermissionStub::GetDescriptor())) {
67 return;
68 }
69 if (!datas.WriteParcelable(&parcel)) {
70 return;
71 }
72 std::shared_ptr<GenerateDlpCertificateCallback> callback = std::make_shared<ClientGenerateDlpCertificateCallback>();
73 sptr<IDlpPermissionCallback> asyncStub = new (std::nothrow) DlpPermissionAsyncStub(callback);
74 if (!datas.WriteRemoteObject(asyncStub->AsObject())) {
75 return;
76 }
77 uint32_t code = static_cast<uint32_t>(DlpPermissionServiceInterfaceCode::GENERATE_DLP_CERTIFICATE);
78 MessageParcel reply;
79 MessageOption option;
80 auto service = std::make_shared<DlpPermissionService>(SA_ID_DLP_PERMISSION_SERVICE, true);
81 service->OnRemoteRequest(code, datas, reply, option);
82 }
83
GenerateCertFuzzTest(const uint8_t * data,size_t size)84 bool GenerateCertFuzzTest(const uint8_t* data, size_t size)
85 {
86 int selfTokenId = GetSelfTokenID();
87 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.dlpmanager", 0); // user_id = 100
88 SetSelfTokenID(tokenId);
89 FuzzTest(data, size);
90 SetSelfTokenID(selfTokenId);
91 return true;
92 }
93 } // namespace OHOS
94
95 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)96 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
97 {
98 /* Run your code on data */
99 OHOS::GenerateCertFuzzTest(data, size);
100 return 0;
101 }
102