blob: 75a328f105549b2d0b9d81161e1ad3de5e420737 [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
Garrick Evanse1f11c32020-05-21 16:54:06 +09007#include <base/logging.h>
8#include <dbus/message.h>
9#include <fuzzer/FuzzedDataProvider.h>
10
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +090011#include "patchpanel/dbus/client.h"
Garrick Evanse1f11c32020-05-21 16:54:06 +090012
13namespace patchpanel {
14
15class Environment {
16 public:
17 Environment() {
18 logging::SetMinLogLevel(logging::LOG_FATAL); // <- DISABLE LOGGING.
19 }
20};
21
22class FakeObjectProxy : public dbus::ObjectProxy {
23 public:
24 explicit FakeObjectProxy(dbus::Bus* bus)
25 : dbus::ObjectProxy(bus, "svc", dbus::ObjectPath("/obj/path"), 0) {}
26
27 std::unique_ptr<dbus::Response> CallMethodAndBlockWithErrorDetails(
28 dbus::MethodCall* method_call,
29 int timeout_ms,
30 dbus::ScopedDBusError* error) override {
31 return nullptr;
32 }
33
34 std::unique_ptr<dbus::Response> CallMethodAndBlock(
35 dbus::MethodCall* method_call, int timeout_ms) override {
36 return nullptr;
37 }
38
39 void CallMethod(dbus::MethodCall* method_call,
40 int timeout_ms,
41 ResponseCallback callback) override {}
42
43 void CallMethodWithErrorResponse(dbus::MethodCall* method_call,
44 int timeout_ms,
45 ResponseOrErrorCallback callback) override {}
46
47 void CallMethodWithErrorCallback(dbus::MethodCall* method_call,
48 int timeout_ms,
49 ResponseCallback callback,
50 ErrorCallback error_callback) override {}
51
52 void ConnectToSignal(const std::string& interface_name,
53 const std::string& signal_name,
54 SignalCallback signal_callback,
55 OnConnectedCallback on_connected_callback) override {}
56
57 void WaitForServiceToBeAvailable(
58 WaitForServiceToBeAvailableCallback callback) override {}
59
60 void Detach() override {}
61};
62
63extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +090064 static Environment env;
Garrick Evanse1f11c32020-05-21 16:54:06 +090065 dbus::Bus::Options options;
66 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
67 scoped_refptr<dbus::ObjectProxy> proxy(new FakeObjectProxy(bus.get()));
68 Client client(bus, proxy.get());
69 FuzzedDataProvider provider(data, size);
70
71 while (provider.remaining_bytes() > 0) {
72 client.NotifyArcStartup(provider.ConsumeIntegral<pid_t>());
73 client.NotifyArcVmStartup(provider.ConsumeIntegral<uint32_t>());
74 client.NotifyArcVmShutdown(provider.ConsumeIntegral<uint32_t>());
75 NetworkDevice device;
Jie Jiange02d1202020-07-27 16:57:04 +090076 device.set_ifname(provider.ConsumeRandomLengthString(IFNAMSIZ * 2));
Garrick Evanse1f11c32020-05-21 16:54:06 +090077 device.set_ipv4_addr(provider.ConsumeIntegral<uint32_t>());
78 device.mutable_ipv4_subnet()->set_base_addr(
79 provider.ConsumeIntegral<uint32_t>());
80 device.mutable_ipv4_subnet()->set_prefix_len(
81 provider.ConsumeIntegral<uint32_t>());
82 IPv4Subnet subnet;
83 subnet.set_base_addr(provider.ConsumeIntegral<uint32_t>());
84 subnet.set_prefix_len(provider.ConsumeIntegral<uint32_t>());
85 client.NotifyTerminaVmStartup(provider.ConsumeIntegral<uint32_t>(), &device,
86 &subnet);
87 client.NotifyTerminaVmShutdown(provider.ConsumeIntegral<uint32_t>());
88 client.NotifyPluginVmStartup(provider.ConsumeIntegral<uint64_t>(),
89 provider.ConsumeIntegral<int>(), &device);
90 client.NotifyPluginVmShutdown(provider.ConsumeIntegral<uint64_t>());
91 // TODO(garrick): Enable the following once the memory leaks in Chrome OS
92 // DBus are resolved.
93 // client.DefaultVpnRouting(provider.ConsumeIntegral<int>());
94 // client.RouteOnVpn(provider.ConsumeIntegral<int>());
95 // client.BypassVpn(provider.ConsumeIntegral<int>());
96 client.ConnectNamespace(provider.ConsumeIntegral<pid_t>(),
97 provider.ConsumeRandomLengthString(100),
98 provider.ConsumeBool());
Jie Jiange02d1202020-07-27 16:57:04 +090099 std::set<std::string> devices_for_counters;
100 for (int i = 0; i < 10; i++) {
101 if (provider.ConsumeBool()) {
102 devices_for_counters.insert(
103 provider.ConsumeRandomLengthString(IFNAMSIZ * 2));
104 }
105 }
106 client.GetTrafficCounters(devices_for_counters);
Garrick Evanse1f11c32020-05-21 16:54:06 +0900107 }
108 bus->ShutdownAndBlock();
109 return 0;
110}
111
112} // namespace patchpanel