1 /*
2 * Copyright (c) 2022 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 "uripermissionmanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21
22 #include "uri_permission_manager_stub.h"
23 #include "message_parcel.h"
24 #include "securec.h"
25
26 using namespace OHOS::AAFwk;
27
28 namespace OHOS {
29 namespace {
30 constexpr size_t FOO_MAX_LEN = 1024;
31 constexpr size_t U32_AT_SIZE = 4;
32 const std::u16string ABILITYMGR_INTERFACE_TOKEN = u"ohos.aafwk.UriPermissionManager";
33 }
34
35 class UriPermissionManagerStubFuzzTest : public UriPermissionManagerStub {
36 public:
37 UriPermissionManagerStubFuzzTest() = default;
~UriPermissionManagerStubFuzzTest()38 virtual ~UriPermissionManagerStubFuzzTest()
39 {}
GrantUriPermission(const Uri & uri,unsigned int flag,const Security::AccessToken::AccessTokenID fromTokenId,const Security::AccessToken::AccessTokenID targetTokenId)40 bool GrantUriPermission(const Uri &uri, unsigned int flag,
41 const Security::AccessToken::AccessTokenID fromTokenId,
42 const Security::AccessToken::AccessTokenID targetTokenId) override
43 {
44 return true;
45 }
VerifyUriPermission(const Uri & uri,unsigned int flag,const Security::AccessToken::AccessTokenID tokenId)46 bool VerifyUriPermission(const Uri &uri, unsigned int flag,
47 const Security::AccessToken::AccessTokenID tokenId) override
48 {
49 return true;
50 }
RemoveUriPermission(const Security::AccessToken::AccessTokenID tokenId)51 void RemoveUriPermission(const Security::AccessToken::AccessTokenID tokenId) override
52 {}
53 };
54
GetU32Data(const char * ptr)55 uint32_t GetU32Data(const char* ptr)
56 {
57 // convert fuzz input data to an integer
58 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
59 }
60
DoSomethingInterestingWithMyAPI(const char * data,size_t size)61 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
62 {
63 uint32_t code = GetU32Data(data);
64
65 MessageParcel parcel;
66 parcel.WriteInterfaceToken(ABILITYMGR_INTERFACE_TOKEN);
67 parcel.WriteBuffer(data, size);
68 parcel.RewindRead(0);
69 MessageParcel reply;
70 MessageOption option;
71 sptr<UriPermissionManagerStub> uripermissionMgr = new UriPermissionManagerStubFuzzTest();
72 uripermissionMgr->OnRemoteRequest(code, parcel, reply, option);
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 if (data == nullptr) {
83 std::cout << "invalid data" << std::endl;
84 return 0;
85 }
86
87 /* Validate the length of size */
88 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
89 return 0;
90 }
91
92 char* ch = (char *)malloc(size + 1);
93 if (ch == nullptr) {
94 std::cout << "malloc failed." << std::endl;
95 return 0;
96 }
97
98 (void)memset_s(ch, size + 1, 0x00, size + 1);
99 if (memcpy_s(ch, size, data, size) != EOK) {
100 std::cout << "copy failed." << std::endl;
101 free(ch);
102 ch = nullptr;
103 return 0;
104 }
105
106 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
107 free(ch);
108 ch = nullptr;
109 return 0;
110 }
111
112