xref: /aosp_15_r20/external/selinux/libselinux/fuzzers/selinux_android_restorecon_fuzzer.cpp (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 /******************************************************************************
2  *
3  * Copyright (C) 2020 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  */
19 
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <selinux/android.h>
22 #include <string>
23 
GetFlags(FuzzedDataProvider & fdp)24 unsigned int GetFlags(FuzzedDataProvider &fdp) {
25   unsigned int flags = 0;
26   if (fdp.ConsumeBool()) {
27     flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
28   }
29   if (fdp.ConsumeBool()) {
30     flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
31   }
32   if (fdp.ConsumeBool()) {
33     flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
34   }
35   if (fdp.ConsumeBool()) {
36     flags |= SELINUX_ANDROID_RESTORECON_FORCE;
37   }
38   if (fdp.ConsumeBool()) {
39     flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
40   }
41   if (fdp.ConsumeBool()) {
42     flags |= SELINUX_ANDROID_RESTORECON_SKIPCE;
43   }
44   if (fdp.ConsumeBool()) {
45     flags |= SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS;
46   }
47   if (fdp.ConsumeBool()) {
48     flags |= SELINUX_ANDROID_RESTORECON_SKIP_SEHASH;
49   }
50   // Try adding random noise (which likely isn't a real flag).
51   if (fdp.ConsumeBool()) {
52     flags |= fdp.ConsumeIntegral<unsigned int>();
53   }
54   return flags;
55 }
56 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
58   FuzzedDataProvider fdp(data, size);
59 
60   std::string file = fdp.ConsumeRandomLengthString();
61   unsigned int flags = GetFlags(fdp);
62 
63   selinux_android_restorecon(file.c_str(), flags);
64 
65   return 0;
66 }
67