Message ID | 20230919-fdb_limit-v4-0-39f0293807b8@avm.de |
---|---|
Headers | show |
Series | bridge: Add a limit on learned FDB entries | expand |
On Wed, Sep 20, 2023 at 01:50:32PM +0300, Nikolay Aleksandrov wrote: > On 9/19/23 11:12, Johannes Nixdorf wrote: > > [...] > > diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c > > index 505683ef9a26..f5d49a05e61b 100644 > > --- a/net/bridge/br_netlink.c > > +++ b/net/bridge/br_netlink.c > > @@ -1267,6 +1267,8 @@ static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = { > > [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 }, > > [IFLA_BR_MULTI_BOOLOPT] = > > NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)), > > + [IFLA_BR_FDB_N_LEARNED] = { .type = NLA_U32 }, > > hmm? I thought this one was RO. You are right. I set this to NLA_REJECT locally for v5 now, analogously to how IFLA_BRPORT_MCAST_N_GROUPS is specified. > > [...]
On Wed, Sep 20, 2023 at 02:00:27PM +0300, Nikolay Aleksandrov wrote: > On 9/19/23 11:12, Johannes Nixdorf wrote: > > Add a Kconfig option to configure a default FDB learning limit system > > wide, so a distributor building a special purpose kernel can limit all > > created bridges by default. > > > > The limit is only a soft default setting and overrideable on a per bridge > > basis using netlink. > > > > Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de> > > --- > > net/bridge/Kconfig | 13 +++++++++++++ > > net/bridge/br_device.c | 2 ++ > > 2 files changed, 15 insertions(+) > > > > diff --git a/net/bridge/Kconfig b/net/bridge/Kconfig > > index 3c8ded7d3e84..c0d9c08088c4 100644 > > --- a/net/bridge/Kconfig > > +++ b/net/bridge/Kconfig > > @@ -84,3 +84,16 @@ config BRIDGE_CFM > > Say N to exclude this support and reduce the binary size. > > If unsure, say N. > > + > > +config BRIDGE_DEFAULT_FDB_MAX_LEARNED > > + int "Default FDB learning limit" > > + default 0 > > + depends on BRIDGE > > + help > > + Sets a default limit on the number of learned FDB entries on > > + new bridges. This limit can be overwritten via netlink on a > > + per bridge basis. > > + > > + The default of 0 disables the limit. > > + > > + If unsure, say 0. > > diff --git a/net/bridge/br_device.c b/net/bridge/br_device.c > > index 9a5ea06236bd..3214391c15a0 100644 > > --- a/net/bridge/br_device.c > > +++ b/net/bridge/br_device.c > > @@ -531,6 +531,8 @@ void br_dev_setup(struct net_device *dev) > > br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME; > > dev->max_mtu = ETH_MAX_MTU; > > + br->fdb_max_learned = CONFIG_BRIDGE_DEFAULT_FDB_MAX_LEARNED; > > + > > br_netfilter_rtable_init(br); > > br_stp_timer_init(br); > > br_multicast_init(br); > > > > This one I'm not sure about at all. Distributions can just create the bridge > with a predefined limit. This is not flexible and just adds > one more kconfig option that is rather unnecessary. Why having a kconfig > knob is better than bridge creation time limit setting? You still have > to create the bridge, so why not set the limit then? The problem I'm trying to solve here are unaware applications. Assuming this change lands in the next Linux release there will still be quite some time until the major applications that create bridges (distribution specific or common network management tools, the container solution of they day, for embedded some random vendor tools, etc.) will pick it up. In this series I chose a default of 0 to not break existing setups that rely on some arbitrary amount of FDB entries, so those unaware applications will create bridges without limits. I added the Kconfig setting so someone who knows their use cases can still set a more fitting default limit. More specifically to our use case as an embedded vendor that builds their own kernels and knows they have no use case that requires huge FDB tables, the kernel config allows us to set a safe default limit before starting to teach all our applications and our upstream vendors' code about the new netlink attribute. As this patch is relatively simple, we can also keep it downstream if there is opposition to it here though.
On 9/21/23 11:06, Johannes Nixdorf wrote: > On Wed, Sep 20, 2023 at 02:00:27PM +0300, Nikolay Aleksandrov wrote: >> On 9/19/23 11:12, Johannes Nixdorf wrote: >>> Add a Kconfig option to configure a default FDB learning limit system >>> wide, so a distributor building a special purpose kernel can limit all >>> created bridges by default. >>> >>> The limit is only a soft default setting and overrideable on a per bridge >>> basis using netlink. >>> >>> Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de> >>> --- >>> net/bridge/Kconfig | 13 +++++++++++++ >>> net/bridge/br_device.c | 2 ++ >>> 2 files changed, 15 insertions(+) >>> >>> diff --git a/net/bridge/Kconfig b/net/bridge/Kconfig >>> index 3c8ded7d3e84..c0d9c08088c4 100644 >>> --- a/net/bridge/Kconfig >>> +++ b/net/bridge/Kconfig >>> @@ -84,3 +84,16 @@ config BRIDGE_CFM >>> Say N to exclude this support and reduce the binary size. >>> If unsure, say N. >>> + >>> +config BRIDGE_DEFAULT_FDB_MAX_LEARNED >>> + int "Default FDB learning limit" >>> + default 0 >>> + depends on BRIDGE >>> + help >>> + Sets a default limit on the number of learned FDB entries on >>> + new bridges. This limit can be overwritten via netlink on a overwritten doesn't sound good, how about This limit can be set (or changed) >>> + per bridge basis. >>> + >>> + The default of 0 disables the limit. >>> + >>> + If unsure, say 0. >>> diff --git a/net/bridge/br_device.c b/net/bridge/br_device.c >>> index 9a5ea06236bd..3214391c15a0 100644 >>> --- a/net/bridge/br_device.c >>> +++ b/net/bridge/br_device.c >>> @@ -531,6 +531,8 @@ void br_dev_setup(struct net_device *dev) >>> br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME; >>> dev->max_mtu = ETH_MAX_MTU; >>> + br->fdb_max_learned = CONFIG_BRIDGE_DEFAULT_FDB_MAX_LEARNED; >>> + >>> br_netfilter_rtable_init(br); >>> br_stp_timer_init(br); >>> br_multicast_init(br); >>> >> >> This one I'm not sure about at all. Distributions can just create the bridge >> with a predefined limit. This is not flexible and just adds >> one more kconfig option that is rather unnecessary. Why having a kconfig >> knob is better than bridge creation time limit setting? You still have >> to create the bridge, so why not set the limit then? > > The problem I'm trying to solve here are unaware applications. Assuming > this change lands in the next Linux release there will still be quite > some time until the major applications that create bridges (distribution > specific or common network management tools, the container solution of > they day, for embedded some random vendor tools, etc.) will pick it > up. In this series I chose a default of 0 to not break existing setups > that rely on some arbitrary amount of FDB entries, so those unaware > applications will create bridges without limits. I added the Kconfig > setting so someone who knows their use cases can still set a more fitting > default limit. > > More specifically to our use case as an embedded vendor that builds their > own kernels and knows they have no use case that requires huge FDB tables, > the kernel config allows us to set a safe default limit before starting > to teach all our applications and our upstream vendors' code about the > new netlink attribute. As this patch is relatively simple, we can also > keep it downstream if there is opposition to it here though. I'm not strongly against, just IMO it is unnecessary. I won't block the set because of this, but it would be nice to get input from others as well. If you can recompile your kernel to set a limit, it should be easier to change your app to set the same limit via netlink, but I'm not familiar with your use case.
On 9/21/23 15:41, Nikolay Aleksandrov wrote: > On 9/19/23 11:12, Johannes Nixdorf wrote: >> The previous patch added accounting and a limit for the number of >> dynamically learned FDB entries per bridge. However it did not provide >> means to actually configure those bounds or read back the count. This >> patch does that. >> >> Two new netlink attributes are added for the accounting and limit of >> dynamically learned FDB entries: >> - IFLA_BR_FDB_N_LEARNED (RO) for the number of entries accounted for >> a single bridge. >> - IFLA_BR_FDB_MAX_LEARNED (RW) for the configured limit of entries for >> the bridge. >> >> The new attributes are used like this: >> >> # ip link add name br up type bridge fdb_max_learned 256 >> # ip link add name v1 up master br type veth peer v2 >> # ip link set up dev v2 >> # mausezahn -a rand -c 1024 v2 >> 0.01 seconds (90877 packets per second >> # bridge fdb | grep -v permanent | wc -l >> 256 >> # ip -d link show dev br >> 13: br: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 [...] >> [...] fdb_n_learned 256 fdb_max_learned 256 >> >> Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de> >> --- >> include/uapi/linux/if_link.h | 2 ++ >> net/bridge/br_netlink.c | 15 ++++++++++++++- >> 2 files changed, 16 insertions(+), 1 deletion(-) [snip] >> @@ -1670,7 +1680,10 @@ static int br_fill_info(struct sk_buff *skb, >> const struct net_device *brdev) >> nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED, >> br->topology_change_detected) || >> nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) || >> - nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm)) >> + nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm) || >> + nla_put_u32(skb, IFLA_BR_FDB_N_LEARNED, >> + atomic_read(&br->fdb_n_learned)) || >> + nla_put_u32(skb, IFLA_BR_FDB_MAX_LEARNED, br->fdb_max_learned)) >> return -EMSGSIZE; >> #ifdef CONFIG_BRIDGE_VLAN_FILTERING >> > > Actually you're using atomic for counting, but using a u32 for the > limit, you should cap it because the count can overflow. Or you should > use atomic64 for the counting. > Scratch all that, I'm speaking nonsense. Need to refresh my mind. :) EVerything's alright. Sorry for the noise.
On Tue, Sep 19, 2023 at 10:12:50AM +0200, Johannes Nixdorf wrote: > A malicious actor behind one bridge port may spam the kernel with packets > with a random source MAC address, each of which will create an FDB entry, > each of which is a dynamic allocation in the kernel. > > There are roughly 2^48 different MAC addresses, further limited by the > rhashtable they are stored in to 2^31. Each entry is of the type struct > net_bridge_fdb_entry, which is currently 128 bytes big. This means the > maximum amount of memory allocated for FDB entries is 2^31 * 128B = > 256GiB, which is too much for most computers. > > Mitigate this by maintaining a per bridge count of those automatically > generated entries in fdb_n_learned, and a limit in fdb_max_learned. If > the limit is hit new entries are not learned anymore. > > For backwards compatibility the default setting of 0 disables the limit. > > User-added entries by netlink or from bridge or bridge port addresses > are never blocked and do not count towards that limit. > > Introduce a new fdb entry flag BR_FDB_DYNAMIC_LEARNED to keep track of > whether an FDB entry is included in the count. The flag is enabled for > dynamically learned entries, and disabled for all other entries. This > should be equivalent to BR_FDB_ADDED_BY_USER and BR_FDB_LOCAL being unset, > but contrary to the two flags it can be toggled atomically. > > Atomicity is required here, as there are multiple callers that modify the > flags, but are not under a common lock (br_fdb_update is the exception > for br->hash_lock, br_fdb_external_learn_add for RTNL). > > Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de> Reviewed-by: Ido Schimmel <idosch@nvidia.com>
On Thu, Sep 21, 2023 at 01:19:44PM +0300, Nikolay Aleksandrov wrote: > I'm not strongly against, just IMO it is unnecessary. I won't block the set > because of this, but it would be nice to get input from others as > well. If you can recompile your kernel to set a limit, it should be easier > to change your app to set the same limit via netlink, but I'm not familiar > with your use case. I agree with keeping it out. We don't have it for similar knobs (e.g., MDB limits) and it would create a precedence for other bridge options instead of simply using netlink and improving user space applications.
Introduce a limit on the amount of learned FDB entries on a bridge, configured by netlink with a build time default on bridge creation in the kernel config. For backwards compatibility the kernel config default is disabling the limit (0). Without any limit a malicious actor may OOM a kernel by spamming packets with changing MAC addresses on their bridge port, so allow the bridge creator to limit the number of entries. Currently the manual entries are identified by the bridge flags BR_FDB_LOCAL or BR_FDB_ADDED_BY_USER, atomically bundled under the new flag BR_FDB_DYNAMIC_LEARNED. This means the limit also applies to entries created with BR_FDB_ADDED_BY_EXT_LEARN but none of BR_FDB_LOCAL or BR_FDB_ADDED_BY_USER, e.g. ones added by SWITCHDEV_FDB_ADD_TO_BRIDGE. Link to the corresponding iproute2 changes: https://lore.kernel.org/netdev/20230919-fdb_limit-v4-1-b4d2dc4df30f@avm.de/ Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de> --- Changes in v4: - Added the new test to the Makefile. (from review) - Removed _entries from the names. (from iproute2 review, in some places only for consistency) - Wrapped the lines at 80 chars, except when longer lines are consistent with neighbouring code. (from review) - Fixed a race in fdb_delete. (from review) - Link to v3: https://lore.kernel.org/r/20230905-fdb_limit-v3-0-7597cd500a82@avm.de Changes in v3: - Fixed the flags for fdb_create in fdb_add_entry to use BIT(...). Previously we passed garbage. (from review) - Set strict_start_type for br_policy. (from review) - Split out the combined accounting and limit patch, and the netlink patch from the combined patch in v2. (from review) - Count atomically, remove the newly introduced lock. (from review) - Added the new attributes to br_policy. (from review) - Added a selftest for the new feature. (from review) - Link to v2: https://lore.kernel.org/netdev/20230619071444.14625-1-jnixdorf-oss@avm.de/ Changes in v2: - Added BR_FDB_ADDED_BY_USER earlier in fdb_add_entry to ensure the limit is not applied. - Do not initialize fdb_*_entries to 0. (from review) - Do not skip decrementing on 0. (from review) - Moved the counters to a conditional hole in struct net_bridge to avoid growing the struct. (from review, it still grows the struct as there are 2 32-bit values) - Add IFLA_BR_FDB_CUR_LEARNED_ENTRIES (from review) - Fix br_get_size() with the added attributes. - Only limit learned entries, rename to *_(CUR|MAX)_LEARNED_ENTRIES. (from review) - Added a default limit in Kconfig. (deemed acceptable in review comments, helps with embedded use-cases where a special purpose kernel is built anyways) - Added an iproute2 patch for easier testing. - Link to v1: https://lore.kernel.org/netdev/20230515085046.4457-1-jnixdorf-oss@avm.de/ Obsolete v1 review comments: - Return better errors to users: Due to limiting the limit to automatically created entries, netlink fdb add requests and changing bridge ports are never rejected, so they do not yet need a more friendly error returned. --- Johannes Nixdorf (6): net: bridge: Set BR_FDB_ADDED_BY_USER early in fdb_add_entry net: bridge: Set strict_start_type for br_policy net: bridge: Track and limit dynamically learned FDB entries net: bridge: Add netlink knobs for number / max learned FDB entries net: bridge: Add a configurable default FDB learning limit selftests: forwarding: bridge_fdb_learning_limit: Add a new selftest include/uapi/linux/if_link.h | 2 + net/bridge/Kconfig | 13 + net/bridge/br_device.c | 2 + net/bridge/br_fdb.c | 42 ++- net/bridge/br_netlink.c | 17 +- net/bridge/br_private.h | 4 + tools/testing/selftests/net/forwarding/Makefile | 3 +- .../net/forwarding/bridge_fdb_learning_limit.sh | 283 +++++++++++++++++++++ 8 files changed, 359 insertions(+), 7 deletions(-) --- base-commit: ce9ecca0238b140b88f43859b211c9fdfd8e5b70 change-id: 20230904-fdb_limit-fae5bbf16c88 Best regards,