daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
8483bf
From 1035e36675e10522824476c9084ff1001039c054 Mon Sep 17 00:00:00 2001
8483bf
From: Yu Watanabe <watanabe.yu+github@gmail.com>
8483bf
Date: Fri, 1 Oct 2021 09:22:18 +0900
8483bf
Subject: [PATCH] network: drop and warn duplicated Address= settings
8483bf
8483bf
Fixes #20891.
8483bf
---
8483bf
 src/network/networkd-address.c | 43 +++++++++++++++++++++++++++++++---
8483bf
 src/network/networkd-address.h |  2 +-
8483bf
 src/network/networkd-network.c |  6 ++++-
8483bf
 3 files changed, 46 insertions(+), 5 deletions(-)
8483bf
8483bf
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
8483bf
index 7b221516d7..e1bc6aa474 100644
8483bf
--- a/src/network/networkd-address.c
8483bf
+++ b/src/network/networkd-address.c
8483bf
@@ -304,6 +304,12 @@ bool address_equal(const Address *a1, const Address *a2) {
8483bf
         return address_compare_func(a1, a2) == 0;
8483bf
 }
8483bf
 
8483bf
+DEFINE_PRIVATE_HASH_OPS(
8483bf
+        address_hash_ops_new,
8483bf
+        Address,
8483bf
+        address_hash_func,
8483bf
+        address_compare_func);
8483bf
+
8483bf
 int address_dup(const Address *src, Address **ret) {
8483bf
         _cleanup_(address_freep) Address *dest = NULL;
8483bf
         int r;
8483bf
@@ -1988,12 +1994,43 @@ static int address_section_verify(Address *address) {
8483bf
         return 0;
8483bf
 }
8483bf
 
8483bf
-void network_drop_invalid_addresses(Network *network) {
8483bf
+int network_drop_invalid_addresses(Network *network) {
8483bf
+        _cleanup_set_free_ Set *addresses = NULL;
8483bf
         Address *address;
8483bf
+        int r;
8483bf
 
8483bf
         assert(network);
8483bf
 
8483bf
-        ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section)
8483bf
-                if (address_section_verify(address) < 0)
8483bf
+        ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
8483bf
+                Address *dup;
8483bf
+
8483bf
+                if (address_section_verify(address) < 0) {
8483bf
+                        /* Drop invalid [Address] sections or Address= settings in [Network].
8483bf
+                         * Note that address_free() will drop the address from addresses_by_section. */
8483bf
                         address_free(address);
8483bf
+                        continue;
8483bf
+                }
8483bf
+
8483bf
+                /* Always use the setting specified later. So, remove the previously assigned setting. */
8483bf
+                dup = set_remove(addresses, address);
8483bf
+                if (dup) {
8483bf
+                        _cleanup_free_ char *buf = NULL;
8483bf
+
8483bf
+                        (void) in_addr_prefix_to_string(address->family, &address->in_addr, address->prefixlen, &buf;;
8483bf
+                        log_warning("%s: Duplicated address %s is specified at line %u and %u, "
8483bf
+                                    "dropping the address setting specified at line %u.",
8483bf
+                                    dup->section->filename, strna(buf), address->section->line,
8483bf
+                                    dup->section->line, dup->section->line);
8483bf
+                        /* address_free() will drop the address from addresses_by_section. */
8483bf
+                        address_free(dup);
8483bf
+                }
8483bf
+
8483bf
+                /* Do not use address_hash_ops_free here. Otherwise, all address settings will be freed. */
8483bf
+                r = set_ensure_put(&addresses, &address_hash_ops_new, address);
8483bf
+                if (r < 0)
8483bf
+                        return log_oom();
8483bf
+                assert(r > 0);
8483bf
+        }
8483bf
+
8483bf
+        return 0;
8483bf
 }
8483bf
diff --git a/src/network/networkd-address.h b/src/network/networkd-address.h
8483bf
index ff3d46abdd..55d22ad221 100644
8483bf
--- a/src/network/networkd-address.h
8483bf
+++ b/src/network/networkd-address.h
8483bf
@@ -85,7 +85,7 @@ int request_process_address(Request *req);
8483bf
 
8483bf
 int manager_rtnl_process_address(sd_netlink *nl, sd_netlink_message *message, Manager *m);
8483bf
 
8483bf
-void network_drop_invalid_addresses(Network *network);
8483bf
+int network_drop_invalid_addresses(Network *network);
8483bf
 
8483bf
 void address_hash_func(const Address *a, struct siphash *state);
8483bf
 int address_compare_func(const Address *a1, const Address *a2);
8483bf
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
8483bf
index 2a864a38b1..920f7851ba 100644
8483bf
--- a/src/network/networkd-network.c
8483bf
+++ b/src/network/networkd-network.c
8483bf
@@ -119,6 +119,8 @@ static int network_resolve_stacked_netdevs(Network *network) {
8483bf
 }
8483bf
 
8483bf
 int network_verify(Network *network) {
8483bf
+        int r;
8483bf
+
8483bf
         assert(network);
8483bf
         assert(network->filename);
8483bf
 
8483bf
@@ -252,7 +254,9 @@ int network_verify(Network *network) {
8483bf
                 network->ipv6_proxy_ndp_addresses = set_free_free(network->ipv6_proxy_ndp_addresses);
8483bf
         }
8483bf
 
8483bf
-        network_drop_invalid_addresses(network);
8483bf
+        r = network_drop_invalid_addresses(network);
8483bf
+        if (r < 0)
8483bf
+                return r;
8483bf
         network_drop_invalid_routes(network);
8483bf
         network_drop_invalid_nexthops(network);
8483bf
         network_drop_invalid_bridge_fdb_entries(network);
8483bf
-- 
8483bf
2.31.1
8483bf