Garrick Evans | e1f11c3 | 2020-05-21 16:54:06 +0900 | [diff] [blame] | 1 | // 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 Jiang | e02d120 | 2020-07-27 16:57:04 +0900 | [diff] [blame] | 5 | #include <net/if.h> |
| 6 | |
Garrick Evans | e1f11c3 | 2020-05-21 16:54:06 +0900 | [diff] [blame] | 7 | #include <base/logging.h> |
| 8 | #include <dbus/message.h> |
| 9 | #include <fuzzer/FuzzedDataProvider.h> |
| 10 | |
Jason Jeremy Iman | adffbcb | 2020-08-31 13:21:36 +0900 | [diff] [blame] | 11 | #include "patchpanel/dbus/client.h" |
Garrick Evans | e1f11c3 | 2020-05-21 16:54:06 +0900 | [diff] [blame] | 12 | |
| 13 | namespace patchpanel { |
| 14 | |
| 15 | class Environment { |
| 16 | public: |
| 17 | Environment() { |
| 18 | logging::SetMinLogLevel(logging::LOG_FATAL); // <- DISABLE LOGGING. |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | class 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 | |
| 63 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
Jason Jeremy Iman | adffbcb | 2020-08-31 13:21:36 +0900 | [diff] [blame] | 64 | static Environment env; |
Garrick Evans | e1f11c3 | 2020-05-21 16:54:06 +0900 | [diff] [blame] | 65 | 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 Jiang | e02d120 | 2020-07-27 16:57:04 +0900 | [diff] [blame] | 76 | device.set_ifname(provider.ConsumeRandomLengthString(IFNAMSIZ * 2)); |
Garrick Evans | e1f11c3 | 2020-05-21 16:54:06 +0900 | [diff] [blame] | 77 | 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 Jiang | e02d120 | 2020-07-27 16:57:04 +0900 | [diff] [blame] | 99 | 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 Evans | e1f11c3 | 2020-05-21 16:54:06 +0900 | [diff] [blame] | 107 | } |
| 108 | bus->ShutdownAndBlock(); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | } // namespace patchpanel |