Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 5 | #include <net/if.h> |
| 6 | |
| 7 | #include <fuzzer/FuzzedDataProvider.h> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/bind.h> |
Garrick Evans | 495dfc4 | 2020-02-14 07:20:57 +0900 | [diff] [blame] | 12 | #include <base/bind_helpers.h> |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 13 | #include <base/logging.h> |
| 14 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 15 | #include "patchpanel/datapath.h" |
| 16 | #include "patchpanel/minijailed_process_runner.h" |
| 17 | #include "patchpanel/multicast_forwarder.h" |
| 18 | #include "patchpanel/net_util.h" |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 21 | |
| 22 | // Always succeeds |
| 23 | int ioctl_stub(int fd, unsigned long req, ...) { |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | class RandomProcessRunner : public MinijailedProcessRunner { |
| 28 | public: |
| 29 | explicit RandomProcessRunner(FuzzedDataProvider* data_provider) |
| 30 | : data_provider_{data_provider} {} |
| 31 | ~RandomProcessRunner() = default; |
| 32 | |
| 33 | int Run(const std::vector<std::string>& argv, bool log_failures) override { |
| 34 | return data_provider_->ConsumeBool(); |
| 35 | } |
| 36 | |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 37 | private: |
| 38 | FuzzedDataProvider* data_provider_; |
| 39 | |
| 40 | DISALLOW_COPY_AND_ASSIGN(RandomProcessRunner); |
| 41 | }; |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | class Environment { |
| 46 | public: |
| 47 | Environment() { |
| 48 | logging::SetMinLogLevel(logging::LOG_FATAL); // <- DISABLE LOGGING. |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 53 | // Turn off logging. |
| 54 | logging::SetMinLogLevel(logging::LOG_FATAL); |
| 55 | |
| 56 | FuzzedDataProvider provider(data, size); |
| 57 | RandomProcessRunner runner(&provider); |
| 58 | Datapath datapath(&runner, ioctl_stub); |
| 59 | |
| 60 | while (provider.remaining_bytes() > 0) { |
Hugo Benichi | 9ab5a05 | 2020-07-28 11:29:01 +0900 | [diff] [blame] | 61 | std::string netns_name = provider.ConsumeRandomLengthString(10); |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 62 | std::string ifname = provider.ConsumeRandomLengthString(IFNAMSIZ - 1); |
| 63 | std::string bridge = provider.ConsumeRandomLengthString(IFNAMSIZ - 1); |
Garrick Evans | 495dfc4 | 2020-02-14 07:20:57 +0900 | [diff] [blame] | 64 | uint32_t addr = provider.ConsumeIntegral<uint32_t>(); |
| 65 | std::string addr_str = IPv4AddressToString(addr); |
| 66 | uint32_t prefix_len = provider.ConsumeIntegralInRange<uint32_t>(0, 31); |
| 67 | Subnet subnet(provider.ConsumeIntegral<int32_t>(), prefix_len, |
hscham | ee9e3a1 | 2020-02-03 16:34:28 +0900 | [diff] [blame] | 68 | base::DoNothing()); |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 69 | std::unique_ptr<SubnetAddress> subnet_addr = subnet.AllocateAtOffset(0); |
| 70 | MacAddress mac; |
| 71 | std::vector<uint8_t> bytes = provider.ConsumeBytes<uint8_t>(mac.size()); |
| 72 | std::copy(std::begin(bytes), std::begin(bytes), std::begin(mac)); |
| 73 | |
Garrick Evans | 495dfc4 | 2020-02-14 07:20:57 +0900 | [diff] [blame] | 74 | datapath.AddBridge(ifname, addr, prefix_len); |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 75 | datapath.RemoveBridge(ifname); |
Garrick Evans | 495dfc4 | 2020-02-14 07:20:57 +0900 | [diff] [blame] | 76 | datapath.AddInboundIPv4DNAT(ifname, addr_str); |
| 77 | datapath.RemoveInboundIPv4DNAT(ifname, addr_str); |
Hugo Benichi | 9ab5a05 | 2020-07-28 11:29:01 +0900 | [diff] [blame] | 78 | datapath.AddVirtualInterfacePair(netns_name, ifname, bridge); |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 79 | datapath.ToggleInterface(ifname, provider.ConsumeBool()); |
| 80 | datapath.ConfigureInterface(ifname, mac, addr, prefix_len, |
| 81 | provider.ConsumeBool(), provider.ConsumeBool()); |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 82 | datapath.RemoveInterface(ifname); |
Garrick Evans | 4f9f557 | 2019-11-26 10:25:16 +0900 | [diff] [blame] | 83 | datapath.AddTAP(ifname, &mac, subnet_addr.get(), ""); |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 84 | datapath.RemoveTAP(ifname); |
Garrick Evans | 3d97a39 | 2020-02-21 15:24:37 +0900 | [diff] [blame] | 85 | datapath.AddIPv4Route(provider.ConsumeIntegral<uint32_t>(), |
| 86 | provider.ConsumeIntegral<uint32_t>(), |
| 87 | provider.ConsumeIntegral<uint32_t>()); |
Hugo Benichi | b9b93fe | 2019-10-25 23:36:01 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | } // namespace |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 94 | } // namespace patchpanel |