xref: /aosp_15_r20/external/selinux/libselinux/fuzzers/selabel_lookup_fuzzer.cpp (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker /******************************************************************************
2*2d543d20SAndroid Build Coastguard Worker  *
3*2d543d20SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
4*2d543d20SAndroid Build Coastguard Worker  *
5*2d543d20SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*2d543d20SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*2d543d20SAndroid Build Coastguard Worker  * You may obtain a copy of the License at:
8*2d543d20SAndroid Build Coastguard Worker  *
9*2d543d20SAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
10*2d543d20SAndroid Build Coastguard Worker  *
11*2d543d20SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*2d543d20SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*2d543d20SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*2d543d20SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*2d543d20SAndroid Build Coastguard Worker  * limitations under the License.
16*2d543d20SAndroid Build Coastguard Worker  *
17*2d543d20SAndroid Build Coastguard Worker  *****************************************************************************
18*2d543d20SAndroid Build Coastguard Worker  */
19*2d543d20SAndroid Build Coastguard Worker 
20*2d543d20SAndroid Build Coastguard Worker #include <fuzzer/FuzzedDataProvider.h>
21*2d543d20SAndroid Build Coastguard Worker #include <stdint.h>
22*2d543d20SAndroid Build Coastguard Worker #include <selinux/android.h>
23*2d543d20SAndroid Build Coastguard Worker #include <string>
24*2d543d20SAndroid Build Coastguard Worker 
GetHandle(FuzzedDataProvider & fdp)25*2d543d20SAndroid Build Coastguard Worker selabel_handle *GetHandle(FuzzedDataProvider &fdp) {
26*2d543d20SAndroid Build Coastguard Worker   switch (fdp.ConsumeIntegralInRange(0, 4)) {
27*2d543d20SAndroid Build Coastguard Worker   case 0: return selinux_android_file_context_handle();
28*2d543d20SAndroid Build Coastguard Worker   case 1: return selinux_android_service_context_handle();
29*2d543d20SAndroid Build Coastguard Worker   case 2: return selinux_android_hw_service_context_handle();
30*2d543d20SAndroid Build Coastguard Worker   case 3: return selinux_android_vendor_service_context_handle();
31*2d543d20SAndroid Build Coastguard Worker   case 4: return selinux_android_keystore2_key_context_handle();
32*2d543d20SAndroid Build Coastguard Worker   default: return selinux_android_file_context_handle();
33*2d543d20SAndroid Build Coastguard Worker   }
34*2d543d20SAndroid Build Coastguard Worker }
35*2d543d20SAndroid Build Coastguard Worker 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)36*2d543d20SAndroid Build Coastguard Worker extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
37*2d543d20SAndroid Build Coastguard Worker   FuzzedDataProvider fdp(data, size);
38*2d543d20SAndroid Build Coastguard Worker 
39*2d543d20SAndroid Build Coastguard Worker   std::string str = fdp.ConsumeRandomLengthString();
40*2d543d20SAndroid Build Coastguard Worker   static auto handle = GetHandle(fdp);
41*2d543d20SAndroid Build Coastguard Worker   char *conn = NULL;
42*2d543d20SAndroid Build Coastguard Worker   int type = fdp.ConsumeIntegral<int>();
43*2d543d20SAndroid Build Coastguard Worker 
44*2d543d20SAndroid Build Coastguard Worker   selabel_lookup(handle, &conn, str.data(), type);
45*2d543d20SAndroid Build Coastguard Worker 
46*2d543d20SAndroid Build Coastguard Worker   free(conn);
47*2d543d20SAndroid Build Coastguard Worker 
48*2d543d20SAndroid Build Coastguard Worker   return 0;
49*2d543d20SAndroid Build Coastguard Worker }
50