/src/logging-log4cxx/src/fuzzers/cpp/PropertyConfiguratorFuzzer.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. 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 | | // Fuzzer for the log4j-style properties configuration surface. |
20 | | // |
21 | | // The input is treated as a .properties configuration file and handed to |
22 | | // PropertyConfigurator, mirroring what DOMConfiguratorFuzzer does for the |
23 | | // XML configuration surface. This is the sibling attack surface: a |
24 | | // program's logging configuration drives class-registry instantiation |
25 | | // (appenders, layouts, filters, rolling and triggering policies, error |
26 | | // handlers), option conversion (OptionConverter variable substitution, |
27 | | // integer/level/file-size parsing through helpers::Integer) and the |
28 | | // log4j-properties text parser itself (helpers::Properties). |
29 | | // |
30 | | // After configuration, a bounded number of events is emitted through the |
31 | | // configured appenders so that activateOptions()/append() paths execute |
32 | | // (filter decisions, rollover triggers, syslog datagram send, console |
33 | | // writers) rather than only the parsing front end. |
34 | | // |
35 | | // PropertyConfigurator::configure intentionally does not reset an existing |
36 | | // configuration, so the harness resets the repository before and after to |
37 | | // keep per-iteration resources (threads, sockets, file handles) bounded. |
38 | | // |
39 | | // Expected defect classes: memory safety in the properties parser and the |
40 | | // class-registry instantiation path, integer parsing overflow |
41 | | // (MaxFileSize/BufferSize/facility numbers), unbounded allocation from |
42 | | // size-like options, and error-handling paths on malformed configurations. |
43 | | // |
44 | | |
45 | | #include <algorithm> |
46 | | #include <cstdio> |
47 | | #include <fuzzer/FuzzedDataProvider.h> |
48 | | #include <log4cxx/logger.h> |
49 | | #include <log4cxx/logmanager.h> |
50 | | #include <log4cxx/mdc.h> |
51 | | #include <log4cxx/ndc.h> |
52 | | #include <log4cxx/net/syslogappender.h> |
53 | | #include <log4cxx/simplelayout.h> |
54 | | #include <log4cxx/spi/loggerrepository.h> |
55 | | #include <log4cxx/propertyconfigurator.h> |
56 | | |
57 | | using namespace log4cxx; |
58 | | |
59 | | namespace |
60 | | { |
61 | | const size_t MaximumFileByteCount = 100000; |
62 | | |
63 | | /** |
64 | | * The configuration under test may leave an appender without a layout |
65 | | * (e.g. a mistyped layout class name); SyslogAppender::append() |
66 | | * dereferences the null layout and crashes (upstream issue log4cxx-1, |
67 | | * see the findings directory). Install a SimpleLayout on any layout-less |
68 | | * appender so the events emitted below exercise the append paths rather |
69 | | * than a known null-dereference. |
70 | | */ |
71 | | void giveLayoutlessAppendersALayout() |
72 | 5.39k | { |
73 | 5.39k | spi::LoggerRepositoryPtr repo = LogManager::getLoggerRepository(); |
74 | 5.39k | if (!repo) |
75 | 0 | { |
76 | 0 | return; |
77 | 0 | } |
78 | 5.39k | std::vector<LoggerPtr> loggers = repo->getCurrentLoggers(); |
79 | 5.39k | loggers.push_back(repo->getRootLogger()); |
80 | 5.39k | for (auto const& logger : loggers) |
81 | 2.66M | { |
82 | 2.66M | if (!logger) |
83 | 0 | { |
84 | 0 | continue; |
85 | 0 | } |
86 | 2.66M | AppenderList appenders = logger->getAllAppenders(); |
87 | 2.66M | for (auto const& appender : appenders) |
88 | 50.1k | { |
89 | 50.1k | if (appender && !appender->getLayout()) |
90 | 1.76k | { |
91 | 1.76k | appender->setLayout(std::make_shared<SimpleLayout>()); |
92 | 1.76k | } |
93 | 50.1k | } |
94 | 2.66M | } |
95 | 5.39k | } |
96 | | } |
97 | | |
98 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
99 | 5.39k | { |
100 | 5.39k | char filename[16] = "conf.properties"; |
101 | | |
102 | | // configure() does not reset the existing configuration; reset it here so |
103 | | // iterations cannot accumulate appenders (see propertyconfigurator.h). |
104 | 5.39k | LogManager::resetConfiguration(); |
105 | | |
106 | 5.39k | FILE *fp = fopen(filename, "wb"); |
107 | 5.39k | if (!fp) |
108 | 0 | { |
109 | 0 | return 0; |
110 | 0 | } |
111 | 5.39k | fwrite(data, std::min(size, MaximumFileByteCount), 1, fp); |
112 | 5.39k | fclose(fp); |
113 | | |
114 | 5.39k | PropertyConfigurator::configure(filename); |
115 | | |
116 | | // Route around the known null-layout crash (findings/log4cxx-1) before |
117 | | // emitting events, so the gate validates the non-crashing remainder. |
118 | 5.39k | giveLayoutlessAppendersALayout(); |
119 | | |
120 | | // Emit a bounded number of events through whatever was configured. |
121 | 5.39k | log4cxx::LoggerPtr logger = log4cxx::Logger::getRootLogger(); |
122 | 5.39k | log4cxx::MDC::put(LOG4CXX_STR("fuzzkey"), LOG4CXX_STR("fuzzvalue")); |
123 | 5.39k | log4cxx::NDC::push(LOG4CXX_STR("fuzzndc")); |
124 | 5.39k | LOG4CXX_INFO(logger, "property configurator fuzzer"); |
125 | 5.39k | LOG4CXX_DEBUG(logger, "second fuzz event"); |
126 | 5.39k | log4cxx::NDC::pop(); |
127 | 5.39k | log4cxx::MDC::remove(LOG4CXX_STR("fuzzkey")); |
128 | | |
129 | 5.39k | LogManager::resetConfiguration(); |
130 | 5.39k | std::remove(filename); |
131 | 5.39k | return 0; |
132 | 5.39k | } |