blob: 35ef0efb76023b4d074875262854a2c942851082 [file] [log] [blame]
Garrick Evanse1f11c32020-05-21 16:54:06 +09001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jie Jiange02d1202020-07-27 16:57:04 +09005#include <net/if.h>
6
hscham4ce3c992021-02-19 16:37:23 +09007#include <base/callback_helpers.h>
Garrick Evanse1f11c32020-05-21 16:54:06 +09008#include <base/logging.h>
9#include <dbus/message.h>
10#include <fuzzer/FuzzedDataProvider.h>
11
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +090012#include "patchpanel/dbus/client.h"
Garrick Evanse1f11c32020-05-21 16:54:06 +090013
14namespace patchpanel {
15
16class Environment {
17 public:
18 Environment() {
hschame6711302020-11-20 17:08:14 +090019 logging::SetMinLogLevel(logging::LOGGING_FATAL); // <- DISABLE LOGGING.
Garrick Evanse1f11c32020-05-21 16:54:06 +090020 }
21};
22
23class FakeObjectProxy : public dbus::ObjectProxy {
24 public:
25 explicit FakeObjectProxy(dbus::Bus* bus)
26 : dbus::ObjectProxy(bus, "svc", dbus::ObjectPath("/obj/path"), 0) {}
27
28 std::unique_ptr<dbus::Response> CallMethodAndBlockWithErrorDetails(
29 dbus::MethodCall* method_call,
30 int timeout_ms,
31 dbus::ScopedDBusError* error) override {
32 return nullptr;
33 }
34
35 std::unique_ptr<dbus::Response> CallMethodAndBlock(
36 dbus::MethodCall* method_call, int timeout_ms) override {
37 return nullptr;
38 }
39
40 void CallMethod(dbus::MethodCall* method_call,
41 int timeout_ms,
42 ResponseCallback callback) override {}
43
44 void CallMethodWithErrorResponse(dbus::MethodCall* method_call,
45 int timeout_ms,
46 ResponseOrErrorCallback callback) override {}
47
48 void CallMethodWithErrorCallback(dbus::MethodCall* method_call,
49 int timeout_ms,
50 ResponseCallback callback,
51 ErrorCallback error_callback) override {}
52
53 void ConnectToSignal(const std::string& interface_name,
54 const std::string& signal_name,
55 SignalCallback signal_callback,
56 OnConnectedCallback on_connected_callback) override {}
57
58 void WaitForServiceToBeAvailable(
59 WaitForServiceToBeAvailableCallback callback) override {}
60
61 void Detach() override {}
62};
63
64extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +090065 static Environment env;
Garrick Evanse1f11c32020-05-21 16:54:06 +090066 dbus::Bus::Options options;
67 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
68 scoped_refptr<dbus::ObjectProxy> proxy(new FakeObjectProxy(bus.get()));
Jie Jiang81c84db2020-09-29 17:40:16 +090069 auto client = Client::New(bus, proxy.get());
Garrick Evanse1f11c32020-05-21 16:54:06 +090070 FuzzedDataProvider provider(data, size);
71
72 while (provider.remaining_bytes() > 0) {
Jie Jiang81c84db2020-09-29 17:40:16 +090073 client->NotifyArcStartup(provider.ConsumeIntegral<pid_t>());
74 client->NotifyArcVmStartup(provider.ConsumeIntegral<uint32_t>());
75 client->NotifyArcVmShutdown(provider.ConsumeIntegral<uint32_t>());
Garrick Evanse1f11c32020-05-21 16:54:06 +090076 NetworkDevice device;
Jie Jiange02d1202020-07-27 16:57:04 +090077 device.set_ifname(provider.ConsumeRandomLengthString(IFNAMSIZ * 2));
Garrick Evanse1f11c32020-05-21 16:54:06 +090078 device.set_ipv4_addr(provider.ConsumeIntegral<uint32_t>());
79 device.mutable_ipv4_subnet()->set_base_addr(
80 provider.ConsumeIntegral<uint32_t>());
81 device.mutable_ipv4_subnet()->set_prefix_len(
82 provider.ConsumeIntegral<uint32_t>());
83 IPv4Subnet subnet;
84 subnet.set_base_addr(provider.ConsumeIntegral<uint32_t>());
85 subnet.set_prefix_len(provider.ConsumeIntegral<uint32_t>());
Jie Jiang81c84db2020-09-29 17:40:16 +090086 client->NotifyTerminaVmStartup(provider.ConsumeIntegral<uint32_t>(),
87 &device, &subnet);
88 client->NotifyTerminaVmShutdown(provider.ConsumeIntegral<uint32_t>());
89 client->NotifyPluginVmStartup(provider.ConsumeIntegral<uint64_t>(),
90 provider.ConsumeIntegral<int>(), &device);
91 client->NotifyPluginVmShutdown(provider.ConsumeIntegral<uint64_t>());
Garrick Evanse1f11c32020-05-21 16:54:06 +090092 // TODO(garrick): Enable the following once the memory leaks in Chrome OS
93 // DBus are resolved.
Jie Jiang81c84db2020-09-29 17:40:16 +090094 // client->DefaultVpnRouting(provider.ConsumeIntegral<int>());
95 // client->RouteOnVpn(provider.ConsumeIntegral<int>());
96 // client->BypassVpn(provider.ConsumeIntegral<int>());
97 client->ConnectNamespace(provider.ConsumeIntegral<pid_t>(),
98 provider.ConsumeRandomLengthString(100),
Garrick Evans58697022020-12-03 12:41:13 +090099 provider.ConsumeBool(), provider.ConsumeBool(),
100 TrafficCounter::SYSTEM);
Jie Jiange02d1202020-07-27 16:57:04 +0900101 std::set<std::string> devices_for_counters;
102 for (int i = 0; i < 10; i++) {
103 if (provider.ConsumeBool()) {
104 devices_for_counters.insert(
105 provider.ConsumeRandomLengthString(IFNAMSIZ * 2));
106 }
107 }
Jie Jiang81c84db2020-09-29 17:40:16 +0900108 client->GetTrafficCounters(devices_for_counters, base::DoNothing());
Garrick Evanse1f11c32020-05-21 16:54:06 +0900109 }
110 bus->ShutdownAndBlock();
111 return 0;
112}
113
114} // namespace patchpanel