File: init_property_fuzzer.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (140 lines) | stat: -rw-r--r-- 5,018 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <persistent_properties.h>
#include <property_type.h>
#include <sys/stat.h>
#include <fstream>
#include "fuzzer/FuzzedDataProvider.h"

using namespace android;
using namespace android::init;
using android::init::persistent_property_filename;

const std::string kTempDir = "/data/local/tmp/";
const std::string kFuzzerPropertyFile = kTempDir + "persistent_properties";
constexpr int32_t kMaxPropertyLength = 10;
const std::string kPrefix = "persist.";
const std::string kPropertyName = kPrefix + "sys.timezone";
const std::string kPropertyValue = "America/Los_Angeles";
const std::string kLegacyPropertyFile = "/data/property/persist.properties";
const std::string kSizeSuffix[3] = {"g", "k", "m"};
constexpr int32_t kMinNumStrings = 1;
constexpr int32_t kMaxNumStrings = 10;

enum PropertyType { STRING, BOOL, INT, UINT, DOUBLE, SIZE, ENUM, RANDOM, kMaxValue = RANDOM };

class InitPropertyFuzzer {
  public:
    InitPropertyFuzzer(const uint8_t* data, size_t size) : fdp_(data, size){};
    void process();

  private:
    void InvokeCheckType();
    void InvokeWritePersistentProperty();
    void RemoveFiles();
    void CreateFuzzerPropertyFile(const std::string property_file);
    FuzzedDataProvider fdp_;
};

void InitPropertyFuzzer::InvokeCheckType() {
    std::string property_type;
    std::string value;
    int type = fdp_.ConsumeEnum<PropertyType>();
    switch (type) {
        case STRING:
            value = fdp_.ConsumeRandomLengthString(kMaxPropertyLength);
            property_type = "string";
            break;
        case BOOL:
            value = fdp_.ConsumeBool();
            property_type = "bool";
            break;
        case INT:
            value = fdp_.ConsumeIntegral<int>();
            property_type = "int";
            break;
        case UINT:
            value = fdp_.ConsumeIntegral<uint_t>();
            property_type = "uint";
            break;
        case DOUBLE:
            value = fdp_.ConsumeFloatingPoint<double>();
            property_type = "double";
            break;
        case SIZE:
            value = fdp_.ConsumeIntegral<uint_t>();
            value = value.append(fdp_.PickValueInArray(kSizeSuffix));
            property_type = "size";
            break;
        case ENUM:
            value = fdp_.ConsumeIntegral<uint_t>();
            property_type = "enum";
            break;
        case RANDOM:
            value = fdp_.ConsumeRandomLengthString(kMaxPropertyLength);
            property_type = fdp_.ConsumeRandomLengthString(kMaxPropertyLength);
            break;
    }

    CheckType(property_type, value);
}

void InitPropertyFuzzer::InvokeWritePersistentProperty() {
    if (fdp_.ConsumeBool()) {
        WritePersistentProperty(kPropertyName, kPropertyValue);
    } else {
        WritePersistentProperty((kPrefix + fdp_.ConsumeRandomLengthString(kMaxPropertyLength)),
                                fdp_.ConsumeRandomLengthString(kMaxPropertyLength));
    }
}

void InitPropertyFuzzer::RemoveFiles() {
    remove(kFuzzerPropertyFile.c_str());
    remove(kLegacyPropertyFile.c_str());
}

void InitPropertyFuzzer::CreateFuzzerPropertyFile(const std::string property_file) {
    std::ofstream out;
    out.open(property_file, std::ios::binary | std::ofstream::trunc);
    chmod(property_file.c_str(), S_IRWXU);
    const int32_t numStrings = fdp_.ConsumeIntegralInRange(kMinNumStrings, kMaxNumStrings);
    for (int32_t i = 0; i < numStrings; ++i) {
        out << fdp_.ConsumeRandomLengthString(kMaxPropertyLength) << "\n";
    }
    out.close();
}

void InitPropertyFuzzer::process() {
    persistent_property_filename = kFuzzerPropertyFile;
    /* Property and legacy files are created using createFuzzerPropertyFile() and */
    /* are used in the below APIs. Hence createFuzzerPropertyFile() is not a part */
    /* of the lambda construct. */
    CreateFuzzerPropertyFile(kFuzzerPropertyFile);
    CreateFuzzerPropertyFile(kLegacyPropertyFile);
    auto property_type = fdp_.PickValueInArray<const std::function<void()>>({
            [&]() { InvokeCheckType(); },
            [&]() { InvokeWritePersistentProperty(); },
            [&]() { LoadPersistentProperties(); },
    });
    property_type();
    RemoveFiles();
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    InitPropertyFuzzer initPropertyFuzzer(data, size);
    initPropertyFuzzer.process();
    return 0;
}