batman-adv: allow to specify limits for remote learned objects
Sven Eckelmann
maintainerv3 cover batman-adv: allow to specify limits for remote learned objects
There are some concerns that an external entity could spam the batman-adv related interfaces with random mac addresses. The batman-adv would use more and more resources to safe things like neighbors, originators, TT, ... and would at some point run out of resources.
One idea is to limit the number of entries for each resource type could have. Things which might need limits
* orig_node
* neigh_node
* tt_local_entry
* dat_entry
* bla_backbone_gw
* bla_claim
Things which are limited by other things (maybe)
* hardif_neigh_node
* gw_node
* orig_node_vlan
* orig_info
Unknown how to handle overly large tt_global_entries:
* tt_global_entry
This approach is also used for the bridge since commit bdb4dfda3b41 ("net:
bridge: Track and limit dynamically learned FDB entries"). And it is also disabled by default.
For the moment, I just want to demonstrate how this might work with some example code. I didn't invest any time to actually check out the other items in the list. So, please consider these lists as vague suggestions.
---
Changes in v3:
- remove RFC
- rebase
- adjust commit messages to mention correct bat_priv members
- exempt own_backbone from resource limit
- keep BLA counters in batadv_priv_bla
- keep DAT counters in batadv_priv_dat
- avoid new over-allocations by first incrementing and then deciding
whether too many items would have been allocated
- move the decrement of learned to release function (when possible)
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- rebase patches
- Link to v1: https://patch.msgid.link/[email protected]
---
Sven Eckelmann (5):
batman-adv: limit number of parallel learned neighbors
batman-adv: limit number of parallel learned originators
batman-adv: limit number of parallel learned DAT entries
batman-adv: limit number of parallel learned BLA backbones
batman-adv: limit number of parallel learned BLA claims
include/uapi/linux/batman_adv.h | 30 ++++++++++++++++++++ net/batman-adv/bat_iv_ogm.c | 15 ++++++++-- net/batman-adv/bat_v_ogm.c | 15 ++++++++-- net/batman-adv/bridge_loop_avoidance.c | 46 ++++++++++++++++++++++++++++--- net/batman-adv/distributed-arp-table.c | 17 ++++++++++-- net/batman-adv/mesh-interface.c | 15 ++++++++++ net/batman-adv/netlink.c | 50 ++++++++++++++++++++++++++++++++++ net/batman-adv/originator.c | 22 +++++++++++++-- net/batman-adv/types.h | 36 ++++++++++++++++++++++++ 9 files changed, 234 insertions(+), 12 deletions(-)
---
base-commit: 95b6f66b2659f617ce1937e790f48dc578ee014c
change-id: 20260516-resource-limit-5c1a32898f7c
Best regards,
Signature · 1 line
Sven Eckelmann <[email protected]>
Sven Eckelmann
maintainerPATCH v3 1/5 batman-adv: limit number of parallel learned neighbors
A malicious actor behind one hardif may spam the kernel with OGMs using a random source MAC address, each of which will create a neighbor node entry, a dynamic allocation in the kernel. This will at some point exhaust the available memory.
Mitigate this by maintaining a per meshif count of those automatically generated entries in neigh_learned, and a limit in neigh_max_learned. If the limit is hit, new entries are not learned anymore.
For backwards compatibility, the default setting of 0 disables the limit.
--- include/uapi/linux/batman_adv.h | 6 ++++++ net/batman-adv/mesh-interface.c | 3 +++ net/batman-adv/netlink.c | 10 ++++++++++ net/batman-adv/originator.c | 19 +++++++++++++++++-- net/batman-adv/types.h | 9 +++++++++ 5 files changed, 45 insertions(+), 2 deletions(-)
include/uapi/linux/batman_adv.h +6 -0
@@ -481,6 +481,12 @@ enum batadv_nl_attrs {*/ BATADV_ATTR_MULTICAST_FANOUT , +/** +* @BATADV_ATTR_NEIGH_MAX_LEARNED: defines the maximum number of neighbors +* which can be learned in parallel +*/ + BATADV_ATTR_NEIGH_MAX_LEARNED, +/* add attributes above here, update the policy in netlink.c */ /**
net/batman-adv/mesh-interface.c +3 -0
@@ -846,6 +846,9 @@ static int batadv_meshif_init_late(struct net_device *dev)#endif atomic_set (&bat_priv->tp_num,0 ); +atomic_set (&bat_priv->neigh_learned,0 ); +WRITE_ONCE (bat_priv->neigh_max_learned,0 ); +WRITE_ONCE (bat_priv->tt.local_changes,0 ); bat_priv->tt.last_changeset =NULL ; bat_priv->tt.last_changeset_len =0 ;
net/batman-adv/netlink.c +10 -0
@@ -154,6 +154,7 @@ static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_ORIG_INTERVAL] = { .type = NLA_U32 }, [BATADV_ATTR_ELP_INTERVAL] = { .type = NLA_U32 }, [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 }, + [BATADV_ATTR_NEIGH_MAX_LEARNED] = { .type = NLA_U32 }, };/**
@@ -355,6 +356,10 @@ static int batadv_netlink_mesh_fill(struct sk_buff *msg, READ_ONCE(bat_priv->orig_interval))) goto nla_put_failure; +if (nla_put_u32 (msg,BATADV_ATTR_NEIGH_MAX_LEARNED , +READ_ONCE (bat_priv->neigh_max_learned))) + goto nla_put_failure; +batadv_hardif_put (primary_if);genlmsg_end (msg, hdr);
@@ -600,6 +605,11 @@ static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)WRITE_ONCE (bat_priv->orig_interval, orig_interval); } +if (info->attrs[BATADV_ATTR_NEIGH_MAX_LEARNED ]) { +WRITE_ONCE (bat_priv->neigh_max_learned, +nla_get_u32 (info->attrs[BATADV_ATTR_NEIGH_MAX_LEARNED ])); + } +batadv_netlink_notify_mesh (bat_priv);return 0 ;
net/batman-adv/originator.c +17 -2
@@ -299,6 +299,7 @@ void batadv_neigh_node_release(struct kref *ref)batadv_hardif_put (neigh_node->if_incoming); +atomic_dec (&neigh_node->bat_priv->neigh_learned);kfree_rcu (neigh_node, rcu); }
@@ -675,6 +676,8 @@ batadv_neigh_node_create(struct batadv_orig_node *orig_node, {struct batadv_hardif_neigh_node *hardif_neigh =NULL ;struct batadv_neigh_node *neigh_node; +u32 neigh_max_learned; +int neigh_learned;spin_lock_bh (&orig_node->neigh_list_lock);
@@ -682,14 +685,25 @@ batadv_neigh_node_create(struct batadv_orig_node *orig_node,if (neigh_node) goto out; + neigh_max_learned =READ_ONCE (orig_node->bat_priv->neigh_max_learned); + neigh_learned =atomic_inc_return (&orig_node->bat_priv->neigh_learned); +if (neigh_max_learned && neigh_learned > neigh_max_learned) { +atomic_dec (&orig_node->bat_priv->neigh_learned); + goto out; + } + hardif_neigh =batadv_hardif_neigh_get_or_create (hard_iface, neigh_addr, orig_node); -if (!hardif_neigh) +if (!hardif_neigh) { +atomic_dec (&orig_node->bat_priv->neigh_learned); goto out; + } neigh_node =kzalloc_obj (*neigh_node,GFP_ATOMIC ); -if (!neigh_node) +if (!neigh_node) { +atomic_dec (&orig_node->bat_priv->neigh_learned); goto out; + }INIT_HLIST_NODE (&neigh_node->list);INIT_HLIST_HEAD (&neigh_node->ifinfo_list);
@@ -699,6 +713,7 @@ batadv_neigh_node_create(struct batadv_orig_node *orig_node,ether_addr_copy (neigh_node->addr, neigh_addr); neigh_node->if_incoming = hard_iface; neigh_node->last_seen = jiffies; + neigh_node->bat_priv = orig_node->bat_priv;#ifdef CONFIG_BATMAN_ADV_BATMAN_V ACCESS_PRIVATE (neigh_node, orig_node_id) = orig_node;
net/batman-adv/types.h +9 -0
@@ -679,6 +679,9 @@ struct batadv_neigh_node {/** @addr: the MAC address of the neighboring interface */ u8 addr[ETH_ALEN ]; +/** @bat_priv: pointer to the mesh object */ +struct batadv_priv *bat_priv; +/** @ifinfo_list: list for routing metrics per outgoing interface */ struct hlist_head ifinfo_list;
@@ -1700,6 +1703,12 @@ struct batadv_priv {/** @meshif_vlan_list_lock: lock protecting meshif_vlan_list */ spinlock_t meshif_vlan_list_lock; +/** @neigh_max_learned: Maximum number of neighbors */ +u32 neigh_max_learned; + +/** @neigh_learned: current number of learned neighbors */ +atomic_t neigh_learned; +#ifdef CONFIG_BATMAN_ADV_BLA /** @bla: bridge loop avoidance data */ struct batadv_priv_bla bla;
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v3 2/5 batman-adv: limit number of parallel learned originators
In reply to Sven EckelmannA malicious actor behind one hardif may spam the kernel with OGMs using a random source MAC address, each of which will create an originator node entry, a dynamic allocation in the kernel. This will at some point exhaust the available memory.
Mitigate this by maintaining a per meshif count of those automatically generated entries in orig_learned, and a limit in orig_max_learned. If the limit is hit, new entries are not learned anymore.
For backwards compatibility, the default setting of 0 disables the limit.
--- include/uapi/linux/batman_adv.h | 6 ++++++ net/batman-adv/bat_iv_ogm.c | 15 +++++++++++++-- net/batman-adv/bat_v_ogm.c | 15 +++++++++++++-- net/batman-adv/mesh-interface.c | 3 +++ net/batman-adv/netlink.c | 10 ++++++++++ net/batman-adv/originator.c | 3 +++ net/batman-adv/types.h | 6 ++++++ 7 files changed, 54 insertions(+), 4 deletions(-)
include/uapi/linux/batman_adv.h +6 -0
@@ -487,6 +487,12 @@ enum batadv_nl_attrs {*/ BATADV_ATTR_NEIGH_MAX_LEARNED , +/** +* @BATADV_ATTR_ORIG_MAX_LEARNED: defines the maximum number of originators +* which can be learned in parallel +*/ + BATADV_ATTR_ORIG_MAX_LEARNED, +/* add attributes above here, update the policy in netlink.c */ /**
net/batman-adv/bat_iv_ogm.c +13 -2
@@ -141,15 +141,26 @@ static struct batadv_orig_node * batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr) { struct batadv_orig_node *orig_node; +u32 orig_max_learned; +int orig_learned;int hash_added; orig_node =batadv_orig_hash_find (bat_priv, addr);if (orig_node)return orig_node; - orig_node =batadv_orig_node_new (bat_priv, addr); -if (!orig_node) + orig_max_learned =READ_ONCE (bat_priv->orig_max_learned); + orig_learned =atomic_inc_return (&bat_priv->orig_learned); +if (orig_max_learned && orig_learned > orig_max_learned) { +atomic_dec (&bat_priv->orig_learned);return NULL ; + } + + orig_node =batadv_orig_node_new (bat_priv, addr); +if (!orig_node) { +atomic_dec (&bat_priv->orig_learned); +return NULL ; + }spin_lock_init (&orig_node->bat_iv.ogm_cnt_lock);
net/batman-adv/bat_v_ogm.c +13 -2
@@ -56,15 +56,26 @@ struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr) {struct batadv_orig_node *orig_node; +u32 orig_max_learned; +int orig_learned;int hash_added; orig_node =batadv_orig_hash_find (bat_priv, addr);if (orig_node)return orig_node; - orig_node =batadv_orig_node_new (bat_priv, addr); -if (!orig_node) + orig_max_learned =READ_ONCE (bat_priv->orig_max_learned); + orig_learned =atomic_inc_return (&bat_priv->orig_learned); +if (orig_max_learned && orig_learned > orig_max_learned) { +atomic_dec (&bat_priv->orig_learned);return NULL ; + } + + orig_node =batadv_orig_node_new (bat_priv, addr); +if (!orig_node) { +atomic_dec (&bat_priv->orig_learned); +return NULL ; + }kref_get (&orig_node->refcount); hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
net/batman-adv/mesh-interface.c +3 -0
@@ -849,6 +849,9 @@ static int batadv_meshif_init_late(struct net_device *dev)atomic_set (&bat_priv->neigh_learned,0 );WRITE_ONCE (bat_priv->neigh_max_learned,0 ); +atomic_set (&bat_priv->orig_learned,0 ); +WRITE_ONCE (bat_priv->orig_max_learned,0 ); +WRITE_ONCE (bat_priv->tt.local_changes,0 ); bat_priv->tt.last_changeset =NULL ; bat_priv->tt.last_changeset_len =0 ;
net/batman-adv/netlink.c +10 -0
@@ -155,6 +155,7 @@ static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_ELP_INTERVAL] = { .type = NLA_U32 }, [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 }, [BATADV_ATTR_NEIGH_MAX_LEARNED] = { .type = NLA_U32 }, + [BATADV_ATTR_ORIG_MAX_LEARNED] = { .type = NLA_U32 }, };/**
@@ -360,6 +361,10 @@ static int batadv_netlink_mesh_fill(struct sk_buff *msg, READ_ONCE(bat_priv->neigh_max_learned))) goto nla_put_failure; +if (nla_put_u32 (msg,BATADV_ATTR_ORIG_MAX_LEARNED , +READ_ONCE (bat_priv->orig_max_learned))) + goto nla_put_failure; +batadv_hardif_put (primary_if);genlmsg_end (msg, hdr);
@@ -610,6 +615,11 @@ static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info) nla_get_u32(info->attrs[BATADV_ATTR_NEIGH_MAX_LEARNED])); } +if (info->attrs[BATADV_ATTR_ORIG_MAX_LEARNED ]) { +WRITE_ONCE (bat_priv->orig_max_learned, +nla_get_u32 (info->attrs[BATADV_ATTR_ORIG_MAX_LEARNED ])); + } +batadv_netlink_notify_mesh (bat_priv);return 0 ;
net/batman-adv/originator.c +3 -0
@@ -876,9 +876,11 @@ void batadv_orig_node_release(struct kref *ref)struct batadv_neigh_node *neigh_node;struct batadv_orig_node *orig_node;struct batadv_orig_node_vlan *vlan; +struct batadv_priv *bat_priv;struct hlist_node *node_tmp; orig_node = container_of(ref, struct batadv_orig_node, refcount); + bat_priv = orig_node->bat_priv;spin_lock_bh (&orig_node->neigh_list_lock);
@@ -910,6 +912,7 @@ void batadv_orig_node_release(struct kref *ref)batadv_mcast_purge_orig (orig_node); +atomic_dec (&bat_priv->orig_learned);call_rcu (&orig_node->rcu, batadv_orig_node_free_rcu); }
net/batman-adv/types.h +6 -0
@@ -1709,6 +1709,12 @@ struct batadv_priv {/** @neigh_learned: current number of learned neighbors */ atomic_t neigh_learned; +/** @orig_max_learned: Maximum number of originators */ +u32 orig_max_learned; + +/** @orig_learned: current number of learned originators */ +atomic_t orig_learned; +#ifdef CONFIG_BATMAN_ADV_BLA /** @bla: bridge loop avoidance data */ struct batadv_priv_bla bla;
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v3 3/5 batman-adv: limit number of parallel learned DAT entries
In reply to Sven EckelmannA malicious actor behind one bridge port may spam the kernel with ARP/DHCP using a random MAC address, causing the creation of a DAT entry, a dynamic allocation in the kernel. This will at some point exhaust the available memory.
Mitigate this by maintaining a per meshif count of those automatically generated entries in dat.learned, and a limit in dat.max_learned. If the limit is hit, new entries are not learned anymore.
For backwards compatibility, the default setting of 0 disables the limit.
--- include/uapi/linux/batman_adv.h | 6 ++++++ net/batman-adv/distributed-arp-table.c | 17 +++++++++++++++-- net/batman-adv/mesh-interface.c | 3 +++ net/batman-adv/netlink.c | 10 ++++++++++ net/batman-adv/types.h | 6 ++++++ 5 files changed, 40 insertions(+), 2 deletions(-)
include/uapi/linux/batman_adv.h +6 -0
@@ -493,6 +493,12 @@ enum batadv_nl_attrs {*/ BATADV_ATTR_ORIG_MAX_LEARNED , +/** +* @BATADV_ATTR_DAT_MAX_LEARNED: defines the maximum number of DAT entries +* which can be learned in parallel +*/ + BATADV_ATTR_DAT_MAX_LEARNED, +/* add attributes above here, update the policy in netlink.c */ /**
net/batman-adv/distributed-arp-table.c +15 -2
@@ -248,6 +248,7 @@ static void __batadv_dat_purge(struct batadv_priv *bat_priv,hlist_del_rcu (&dat_entry->hash_entry);batadv_dat_entry_put (dat_entry); +atomic_dec (&bat_priv->dat.learned); }spin_unlock_bh (list_lock); }
@@ -446,6 +447,8 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip, {u64 u64_mac =ether_addr_to_u64 (mac_addr);struct batadv_dat_entry *dat_entry; +u32 dat_max_learned; +int dat_learned;int hash_added; dat_entry =batadv_dat_entry_hash_find (bat_priv, ip, vid);
@@ -461,9 +464,18 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip, goto out; } - dat_entry =kmalloc_obj (*dat_entry,GFP_ATOMIC ); -if (!dat_entry) + dat_max_learned =READ_ONCE (bat_priv->dat.max_learned); + dat_learned =atomic_inc_return (&bat_priv->dat.learned); +if (dat_max_learned && dat_learned > dat_max_learned) { +atomic_dec (&bat_priv->dat.learned); goto out; + } + + dat_entry =kmalloc_obj (*dat_entry,GFP_ATOMIC ); +if (!dat_entry) { +atomic_dec (&bat_priv->dat.learned); + goto out; + } dat_entry->ip = ip; dat_entry->vid = vid;
@@ -479,6 +491,7 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,if (unlikely (hash_added !=0 )) {/* remove the reference for the hash */ batadv_dat_entry_put (dat_entry); +atomic_dec (&bat_priv->dat.learned); goto out; }
net/batman-adv/mesh-interface.c +3 -0
@@ -814,6 +814,9 @@ static int batadv_meshif_init_late(struct net_device *dev)#endif #ifdef CONFIG_BATMAN_ADV_DAT WRITE_ONCE (bat_priv->distributed_arp_table,1 ); + +atomic_set (&bat_priv->dat.learned,0 ); +WRITE_ONCE (bat_priv->dat.max_learned,0 );#endif #ifdef CONFIG_BATMAN_ADV_MCAST WRITE_ONCE (bat_priv->multicast_mode,1 );
net/batman-adv/netlink.c +10 -0
@@ -156,6 +156,7 @@ static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_THROUGHPUT_OVERRIDE] = { .type = NLA_U32 }, [BATADV_ATTR_NEIGH_MAX_LEARNED] = { .type = NLA_U32 }, [BATADV_ATTR_ORIG_MAX_LEARNED] = { .type = NLA_U32 }, + [BATADV_ATTR_DAT_MAX_LEARNED] = { .type = NLA_U32 }, };/**
@@ -305,6 +306,10 @@ static int batadv_netlink_mesh_fill(struct sk_buff *msg,if (nla_put_u8 (msg,BATADV_ATTR_DISTRIBUTED_ARP_TABLE_ENABLED , !!READ_ONCE (bat_priv->distributed_arp_table))) goto nla_put_failure; + +if (nla_put_u32 (msg,BATADV_ATTR_DAT_MAX_LEARNED , +READ_ONCE (bat_priv->dat.max_learned))) + goto nla_put_failure;#endif /* CONFIG_BATMAN_ADV_DAT */ if (nla_put_u8(msg, BATADV_ATTR_FRAGMENTATION_ENABLED,
@@ -495,6 +500,11 @@ static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info) !!nla_get_u8(attr)); batadv_dat_status_update(bat_priv->mesh_iface); } + +if (info->attrs[BATADV_ATTR_DAT_MAX_LEARNED ]) { +WRITE_ONCE (bat_priv->dat.max_learned, +nla_get_u32 (info->attrs[BATADV_ATTR_DAT_MAX_LEARNED ])); + }#endif /* CONFIG_BATMAN_ADV_DAT */ if (info->attrs[BATADV_ATTR_FRAGMENTATION_ENABLED ]) {
net/batman-adv/types.h +6 -0
@@ -1200,6 +1200,12 @@ struct batadv_priv_dat {/** @work: work queue callback item for cache purging */ struct delayed_work work; + +/** @max_learned: Maximum number of DAT entries */ +u32 max_learned; + +/** @learned: current number of learned DAT entries */ + atomic_t learned; };#endif
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v3 4/5 batman-adv: limit number of parallel learned BLA backbones
In reply to Sven EckelmannA malicious actor on the backbone Ethernet may spam the kernel with BLA announcements using a random source MAC address and a valid group ID (local group ID, or one that exists on the mesh), each of which will create a BLA backbone, a dynamic allocation in the kernel. This will at some point exhaust the available memory.
Mitigate this by maintaining a per meshif count of those automatically generated entries in bla.backbone_learned, and a limit in bla.backbone_max_learned. If the limit is hit, new entries are not learned anymore.
For backwards compatibility, the default setting of 0 disables the limit.
--- include/uapi/linux/batman_adv.h | 6 ++++++ net/batman-adv/bridge_loop_avoidance.c | 25 ++++++++++++++++++++++++- net/batman-adv/mesh-interface.c | 3 +++ net/batman-adv/netlink.c | 10 ++++++++++ net/batman-adv/types.h | 9 +++++++++ 5 files changed, 52 insertions(+), 1 deletion(-)
include/uapi/linux/batman_adv.h +6 -0
@@ -499,6 +499,12 @@ enum batadv_nl_attrs {*/ BATADV_ATTR_DAT_MAX_LEARNED , +/** +* @BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED: defines the maximum number of BLA backbones +* which can be learned in parallel +*/ + BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED, +/* add attributes above here, update the policy in netlink.c */ /**
net/batman-adv/bridge_loop_avoidance.c +24 -1
@@ -154,6 +154,9 @@ static void batadv_backbone_gw_release(struct kref *ref) backbone_gw = container_of(ref, struct batadv_bla_backbone_gw, refcount); +if (!backbone_gw->own) +atomic_dec (&backbone_gw->bat_priv->bla.backbone_learned); +kfree_rcu (backbone_gw, rcu); }
@@ -488,6 +491,8 @@ batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig, {struct batadv_bla_backbone_gw *entry;struct batadv_orig_node *orig_node; +u32 bla_backbone_max_learned; +int bla_backbone_learned;int hash_added; entry =batadv_backbone_hash_find (bat_priv, orig, vid);
@@ -499,11 +504,25 @@ batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig,"%s(): not found (%pM, %d), creating new entry\n" , __func__, orig, batadv_print_vid(vid)); +if (!own_backbone) { + bla_backbone_max_learned =READ_ONCE (bat_priv->bla.backbone_max_learned); + bla_backbone_learned =atomic_inc_return (&bat_priv->bla.backbone_learned); +if (bla_backbone_max_learned && bla_backbone_learned > bla_backbone_max_learned) { +atomic_dec (&bat_priv->bla.backbone_learned); +return NULL ; + } + } + entry =kzalloc_obj (*entry,GFP_ATOMIC ); -if (!entry) +if (!entry) { +if (!own_backbone) +atomic_dec (&bat_priv->bla.backbone_learned); +return NULL ; + } entry->vid = vid; + entry->own = own_backbone;WRITE_ONCE (entry->lasttime, jiffies); entry->crc =BATADV_BLA_CRC_INIT ; entry->bat_priv = bat_priv;
@@ -523,6 +542,10 @@ batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig,if (unlikely (hash_added !=0 )) {/* hash failed, free the structure */ kfree (entry); + +if (!own_backbone) +atomic_dec (&bat_priv->bla.backbone_learned); +return NULL ; }
net/batman-adv/mesh-interface.c +3 -0
@@ -846,6 +846,9 @@ static int batadv_meshif_init_late(struct net_device *dev)#ifdef CONFIG_BATMAN_ADV_BLA atomic_set (&bat_priv->bla.num_requests,0 );spin_lock_init (&bat_priv->bla.num_requests_lock); + +atomic_set (&bat_priv->bla.backbone_learned,0 ); +WRITE_ONCE (bat_priv->bla.backbone_max_learned,0 );#endif atomic_set (&bat_priv->tp_num,0 );
net/batman-adv/netlink.c +10 -0
@@ -157,6 +157,7 @@ static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_NEIGH_MAX_LEARNED] = { .type = NLA_U32 }, [BATADV_ATTR_ORIG_MAX_LEARNED] = { .type = NLA_U32 }, [BATADV_ATTR_DAT_MAX_LEARNED] = { .type = NLA_U32 }, + [BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED] = { .type = NLA_U32 }, };/**
@@ -300,6 +301,10 @@ static int batadv_netlink_mesh_fill(struct sk_buff *msg,if (nla_put_u8 (msg,BATADV_ATTR_BRIDGE_LOOP_AVOIDANCE_ENABLED , !!READ_ONCE (bat_priv->bridge_loop_avoidance))) goto nla_put_failure; + +if (nla_put_u32 (msg,BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED , +READ_ONCE (bat_priv->bla.backbone_max_learned))) + goto nla_put_failure;#endif /* CONFIG_BATMAN_ADV_BLA */ #ifdef CONFIG_BATMAN_ADV_DAT
@@ -490,6 +495,11 @@ static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info) !!nla_get_u8(attr)); batadv_bla_status_update(bat_priv->mesh_iface); } + +if (info->attrs[BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED ]) { +WRITE_ONCE (bat_priv->bla.backbone_max_learned, +nla_get_u32 (info->attrs[BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED ])); + }#endif /* CONFIG_BATMAN_ADV_BLA */ #ifdef CONFIG_BATMAN_ADV_DAT
net/batman-adv/types.h +9 -0
@@ -1071,6 +1071,12 @@ struct batadv_priv_bla {/** @num_requests: number of bla requests in flight */ atomic_t num_requests; +/** @backbone_max_learned: Maximum number of backbone_gw */ +u32 backbone_max_learned; + +/** @backbone_learned: current number of learned backbone_gw entries */ +atomic_t backbone_learned; +/** * @num_requests_lock: locks update num_requests + * batadv_backbone_gw::state + batadv_backbone_gw::wait_periods update
@@ -1791,6 +1797,9 @@ struct batadv_bla_backbone_gw {/** @vid: vlan id this gateway was detected on */ unsigned short vid; +/** @own: it is the local backbone gateway */ + bool own:1; +/** @hash_entry: hlist node for &batadv_priv_bla.backbone_hash */ struct hlist_node hash_entry;
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v3 5/5 batman-adv: limit number of parallel learned BLA claims
In reply to Sven EckelmannA malicious actor on the backbone Ethernet may spam the kernel with BLA claims using a random source MAC address and a valid group ID (local group ID, or one that exists on the mesh), each of which will create a BLA claim, a dynamic allocation in the kernel. This will at some point exhaust the available memory.
Mitigate this by maintaining a per meshif count of those automatically generated entries in bla.claim_learned, and a limit in bla.claim_max_learned. If the limit is hit, new entries are not learned anymore.
For backwards compatibility, the default setting of 0 disables the limit.
--- include/uapi/linux/batman_adv.h | 6 ++++++ net/batman-adv/bridge_loop_avoidance.c | 21 ++++++++++++++++++--- net/batman-adv/mesh-interface.c | 3 +++ net/batman-adv/netlink.c | 10 ++++++++++ net/batman-adv/types.h | 6 ++++++ 5 files changed, 43 insertions(+), 3 deletions(-)
include/uapi/linux/batman_adv.h +6 -0
@@ -505,6 +505,12 @@ enum batadv_nl_attrs {*/ BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED , +/** +* @BATADV_ATTR_BLA_CLAIM_MAX_LEARNED: defines the maximum number of BLA claims +* which can be learned in parallel +*/ + BATADV_ATTR_BLA_CLAIM_MAX_LEARNED, +/* add attributes above here, update the policy in netlink.c */ /**
net/batman-adv/bridge_loop_avoidance.c +18 -3
@@ -182,6 +182,7 @@ static void batadv_claim_release(struct kref *ref) {struct batadv_bla_backbone_gw *old_backbone_gw;struct batadv_bla_claim *claim; +struct batadv_priv *bat_priv; claim = container_of(ref, struct batadv_bla_claim, refcount);
@@ -194,8 +195,10 @@ static void batadv_claim_release(struct kref *ref) old_backbone_gw->crc ^=crc16 (0 , claim->addr,ETH_ALEN );spin_unlock_bh (&old_backbone_gw->crc_lock); + bat_priv = old_backbone_gw->bat_priv;batadv_backbone_gw_put (old_backbone_gw); +atomic_dec (&bat_priv->bla.claim_learned);kfree_rcu (claim, rcu); }
@@ -713,6 +716,8 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv,struct batadv_bla_backbone_gw *old_backbone_gw;struct batadv_bla_claim search_claim;struct batadv_bla_claim *claim; +u32 bla_claim_max_learned; +int bla_claim_learned;int hash_added;u16 claim_crc;bool changed;
@@ -724,9 +729,18 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv,/* create a new claim entry if it does not exist yet. */ if (!claim) { - claim =kzalloc_obj (*claim,GFP_ATOMIC ); -if (!claim) + bla_claim_max_learned =READ_ONCE (bat_priv->bla.claim_max_learned); + bla_claim_learned =atomic_inc_return (&bat_priv->bla.claim_learned); +if (bla_claim_max_learned && bla_claim_learned > bla_claim_max_learned) { +atomic_dec (&bat_priv->bla.claim_learned);return ; + } + + claim =kzalloc_obj (*claim,GFP_ATOMIC ); +if (!claim) { +atomic_dec (&bat_priv->bla.claim_learned); +return ; + }ether_addr_copy (claim->addr, mac);spin_lock_init (&claim->backbone_lock);
@@ -747,8 +761,9 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv, &claim->hash_entry);if (unlikely (hash_added !=0 )) { -/* only local changes happened. */ batadv_backbone_gw_put (backbone_gw); +atomic_dec (&bat_priv->bla.claim_learned); +/* only local changes happened. */ kfree (claim);return ; }
net/batman-adv/mesh-interface.c +3 -0
@@ -849,6 +849,9 @@ static int batadv_meshif_init_late(struct net_device *dev)atomic_set (&bat_priv->bla.backbone_learned,0 );WRITE_ONCE (bat_priv->bla.backbone_max_learned,0 ); + +atomic_set (&bat_priv->bla.claim_learned,0 ); +WRITE_ONCE (bat_priv->bla.claim_max_learned,0 );#endif atomic_set (&bat_priv->tp_num,0 );
net/batman-adv/netlink.c +10 -0
@@ -158,6 +158,7 @@ static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_ORIG_MAX_LEARNED] = { .type = NLA_U32 }, [BATADV_ATTR_DAT_MAX_LEARNED] = { .type = NLA_U32 }, [BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED] = { .type = NLA_U32 }, + [BATADV_ATTR_BLA_CLAIM_MAX_LEARNED] = { .type = NLA_U32 }, };/**
@@ -305,6 +306,10 @@ static int batadv_netlink_mesh_fill(struct sk_buff *msg,if (nla_put_u32 (msg,BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED ,READ_ONCE (bat_priv->bla.backbone_max_learned))) goto nla_put_failure; + +if (nla_put_u32 (msg,BATADV_ATTR_BLA_CLAIM_MAX_LEARNED , +READ_ONCE (bat_priv->bla.claim_max_learned))) + goto nla_put_failure;#endif /* CONFIG_BATMAN_ADV_BLA */ #ifdef CONFIG_BATMAN_ADV_DAT
@@ -500,6 +505,11 @@ static int batadv_netlink_set_mesh(struct sk_buff *skb, struct genl_info *info)WRITE_ONCE (bat_priv->bla.backbone_max_learned, nla_get_u32(info->attrs[BATADV_ATTR_BLA_BACKBONE_MAX_LEARNED])); } + +if (info->attrs[BATADV_ATTR_BLA_CLAIM_MAX_LEARNED ]) { +WRITE_ONCE (bat_priv->bla.claim_max_learned, +nla_get_u32 (info->attrs[BATADV_ATTR_BLA_CLAIM_MAX_LEARNED ])); + }#endif /* CONFIG_BATMAN_ADV_BLA */ #ifdef CONFIG_BATMAN_ADV_DAT
net/batman-adv/types.h +6 -0
@@ -1077,6 +1077,12 @@ struct batadv_priv_bla {/** @backbone_learned: current number of learned backbone_gw entries */ atomic_t backbone_learned; +/** @claim_max_learned: Maximum number of claims */ +u32 claim_max_learned; + +/** @claim_learned: current number of learned claim entries */ +atomic_t claim_learned; +/** * @num_requests_lock: locks update num_requests + * batadv_backbone_gw::state + batadv_backbone_gw::wait_periods update
Signature · 1 line
2.47.3