blob: ed3f5b3a5b78479839627f550c70cce49086693a [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
Jie Jiang81c84db2020-09-29 17:40:16 +09007#include <base/bind_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),
99 provider.ConsumeBool());
Jie Jiange02d1202020-07-27 16:57:04 +0900100 std::set<std::string> devices_for_counters;
101 for (int i = 0; i < 10; i++) {
102 if (provider.ConsumeBool()) {
103 devices_for_counters.insert(
104 provider.ConsumeRandomLengthString(IFNAMSIZ * 2));
105 }
106 }
Jie Jiang81c84db2020-09-29 17:40:16 +0900107 client->GetTrafficCounters(devices_for_counters, base::DoNothing());
Garrick Evanse1f11c32020-05-21 16:54:06 +0900108 }
109 bus->ShutdownAndBlock();
110 return 0;
111}
112
113} // namespace patchpanel