1 /*
2  * Copyright (c) 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 "cleanpersistpolicybypath_fuzzer.h"
17 
18 #include <vector>
19 #include <cstdint>
20 #include <string>
21 #include "accesstoken_kit.h"
22 #include "fuzz_common.h"
23 #include "sandbox_manager_kit.h"
24 #include "token_setproc.h"
25 
26 using namespace OHOS::AccessControl::SandboxManager;
27 
28 namespace OHOS {
29 namespace {
30 static uint32_t SELF_TOKEN = 0;
31 static uint32_t FILE_MANAGER_TOKEN = 0;
32 };
CheckPersistPolicyFuzzTest(const uint8_t * data,size_t size)33     bool CheckPersistPolicyFuzzTest(const uint8_t *data, size_t size)
34     {
35         if ((data == nullptr) || (size == 0)) {
36             return false;
37         }
38         FILE_MANAGER_TOKEN = Security::AccessToken::AccessTokenKit::GetNativeTokenId(
39             "file_manager_service");
40         SELF_TOKEN = GetSelfTokenID();
41         SetSelfTokenID(FILE_MANAGER_TOKEN);
42 
43         std::vector<std::string> pathList;
44         PolicyInfoRandomGenerator gen(data, size);
45         gen.GenerateStringVec(pathList);
46 
47         SandboxManagerKit::CleanPersistPolicyByPath(pathList);
48         SetSelfTokenID(SELF_TOKEN);
49         return true;
50     }
51 }
52 
53 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56     /* Run your code on data */
57     OHOS::CheckPersistPolicyFuzzTest(data, size);
58     return 0;
59 }