bonding: no longer rely on RTNL in bond_fill_info()
Subsystems (3)
PATCH v3 1/1 bonding: no longer rely on RTNL in bond_fill_info()
Add READ_ONCE()/WRITE_ONCE() annotations on port->is_enabled. While this field is written under bond->mode_lock protection, is is read without this lock being held.
Change bond_fill_info() to acquire RCU and use READ_ONCE() to read bond->params fields that can be updated concurrently from sysfs/procfs/rtnetlink.
Add const qualifiers to bond_uses_primary(), __agg_active_ports(), bond_option_active_slave_get_rcu(), bond_3ad_get_active_agg_info(), __bond_3ad_get_active_agg_info() helpers.
---
Cc: Jay Vosburgh <[email protected]>
Cc: Andrew Lunn <[email protected]>
---
v3: Use net-next in patch title to not confuse patchwork/CI.
v2: addressed sashiko.dev feedback.
drivers/net/bonding/bond_3ad.c | 18 ++--- drivers/net/bonding/bond_netlink.c | 107 ++++++++++++++++------------- drivers/net/bonding/bond_options.c | 6 +- include/net/bond_3ad.h | 4 +- include/net/bonding.h | 8 +-- 5 files changed, 78 insertions(+), 65 deletions(-)
drivers/net/bonding/bond_3ad.c +9 -9
@@ -745,14 +745,14 @@ static void __set_agg_ports_ready(struct aggregator *aggregator, int val) } } -static int __agg_active_ports (struct aggregator *agg) +static int __agg_active_ports (const struct aggregator *agg) { -struct port *port; +const struct port *port;int active =0 ;for (port = agg->lag_ports; port; port = port->next_port_in_aggregator) { -if (port->is_enabled) +if (READ_ONCE (port->is_enabled)) active++; }
@@ -2782,11 +2782,11 @@ void bond_3ad_handle_link_change(struct slave *slave, char link)* some of he adaptors(ce1000.lan) report. */ if (link ==BOND_LINK_UP ) { - port->is_enabled = true; +WRITE_ONCE (port->is_enabled, true);ad_update_actor_keys (port, false); }else {/* link has failed */ - port->is_enabled = false; +WRITE_ONCE (port->is_enabled, false);ad_update_actor_keys (port, true); } agg =__get_first_agg (port);
@@ -2857,13 +2857,13 @@ int bond_3ad_set_carrier(struct bonding *bond)* Returns: 0 on success * < 0 on error */ -int __bond_3ad_get_active_agg_info (struct bonding *bond, +int __bond_3ad_get_active_agg_info (const struct bonding *bond,struct ad_info *ad_info) { -struct aggregator *aggregator =NULL , *tmp; +const struct aggregator *aggregator =NULL , *tmp; +const struct port *port;struct list_head *iter;struct slave *slave; -struct port *port;bond_for_each_slave_rcu (bond, slave, iter) { port = &(SLAVE_AD_INFO (slave)->port);
@@ -2886,7 +2886,7 @@ int __bond_3ad_get_active_agg_info(struct bonding *bond,return 0 ; } -int bond_3ad_get_active_agg_info (struct bonding *bond,struct ad_info *ad_info) +int bond_3ad_get_active_agg_info (const struct bonding *bond,struct ad_info *ad_info) {int ret;
drivers/net/bonding/bond_netlink.c +60 -47
240 lines not on this page · raw message
drivers/net/bonding/bond_options.c +3 -3
@@ -918,14 +918,14 @@ static int bond_option_mode_set(struct bonding *bond,/* don't cache arp_validate between modes */ WRITE_ONCE (bond->params.arp_validate,BOND_ARP_VALIDATE_NONE ); - bond->params.mode = newval->value; +WRITE_ONCE (bond->params.mode, newval->value);/* When changing mode, the bond device is down, we may reduce * the bond_bcast_neigh_enabled in bond_close() if broadcast_neighbor * enabled in 8023ad mode. Therefore, only clear broadcast_neighbor * to 0. */ - bond->params.broadcast_neighbor =0 ; +WRITE_ONCE (bond->params.broadcast_neighbor,0 );if (bond->dev->reg_state ==NETREG_REGISTERED ) {bool update = false;
@@ -1900,7 +1900,7 @@ static int bond_option_broadcast_neigh_set(struct bonding *bond,if (bond->params.broadcast_neighbor == newval->value)return 0 ; - bond->params.broadcast_neighbor = newval->value; +WRITE_ONCE (bond->params.broadcast_neighbor, newval->value);if (bond->dev->flags &IFF_UP ) {if (bond->params.broadcast_neighbor)static_branch_inc (&bond_bcast_neigh_enabled);
include/net/bond_3ad.h +2 -2
@@ -302,8 +302,8 @@ void bond_3ad_state_machine_handler(struct work_struct *);void bond_3ad_initiate_agg_selection (struct bonding *bond,int timeout);void bond_3ad_adapter_speed_duplex_changed (struct slave *slave);void bond_3ad_handle_link_change (struct slave *slave,char link); -int bond_3ad_get_active_agg_info (struct bonding *bond,struct ad_info *ad_info); -int __bond_3ad_get_active_agg_info (struct bonding *bond, +int bond_3ad_get_active_agg_info (const struct bonding *bond,struct ad_info *ad_info); +int __bond_3ad_get_active_agg_info (const struct bonding *bond,struct ad_info *ad_info);int bond_3ad_lacpdu_recv (const struct sk_buff *skb,struct bonding *bond,struct slave *slave);
include/net/bonding.h +4 -4
@@ -344,14 +344,14 @@ static inline bool bond_mode_uses_primary(int mode) mode ==BOND_MODE_ALB ; } -static inline bool bond_uses_primary (struct bonding *bond) +static inline bool bond_uses_primary (const struct bonding *bond) {return bond_mode_uses_primary (BOND_MODE (bond)); } -static inline struct net_device *bond_option_active_slave_get_rcu (struct bonding *bond) +static inline struct net_device *bond_option_active_slave_get_rcu (const struct bonding *bond) { -struct slave *slave =rcu_dereference_rtnl (bond->curr_active_slave); +const struct slave *slave =rcu_dereference_rtnl (bond->curr_active_slave);return bond_uses_primary (bond) && slave ? slave->dev :NULL ; }
@@ -702,7 +702,7 @@ void bond_setup(struct net_device *bond_dev);unsigned int bond_get_num_tx_queues (void );int bond_netlink_init (void );void bond_netlink_fini (void ); -struct net_device *bond_option_active_slave_get_rcu (struct bonding *bond); +struct net_device *bond_option_active_slave_get_rcu (const struct bonding *bond);const char *bond_slave_link_status (s8 link); struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev, struct net_device *end_dev,
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Jakub Kicinski
maintainerThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
bonding: no longer rely on RTNL in bond_fill_info()
This commit removes the RTNL lock dependency in bond_fill_info(), replacing it with RCU protection and READ_ONCE()/WRITE_ONCE() annotations for reading bond configuration fields.
Quoted 4 lines
drivers/net/bonding/bond_netlink.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/bonding/bond_netlink.c +0
-0@@ -674,53 +674,58 @@ static size_t bond_get_size(const struct net_device *bond_dev)
[ ... ]
Quoted 15 lines
static int bond_fill_info(struct
sk_buff*skb,drivers/net/bonding/bond_netlink.c +5
-5const struct net_device *bond_dev) { -struct bonding *bond =netdev_priv (bond_dev); -unsigned int packets_per_slave; -int ifindex, i, targets_added; +const struct bonding *bond =netdev_priv (bond_dev); +int i, targets_added, miimon, mode; +const struct slave *primary;struct nlattr *targets; -struct slave *primary; -if (nla_put_u8 (skb,IFLA_BOND_MODE ,BOND_MODE (bond))) +rcu_read_lock (); + mode =READ_ONCE (bond->params.mode);
[ ... ]
Quoted 12 lines
drivers/net/bonding/bond_netlink.c +2
-3-if (BOND_MODE (bond) ==BOND_MODE_8023AD ) { +if (mode ==BOND_MODE_8023AD ) {struct ad_info info;if (capable (CAP_NET_ADMIN )) {if (nla_put_u16 (skb,IFLA_BOND_AD_ACTOR_SYS_PRIO , - bond->params.ad_actor_sys_prio)) +READ_ONCE (bond->params.ad_actor_sys_prio))) goto nla_put_failure; if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY, - bond->params.ad_user_port_key))
drivers/net/bonding/bond_netlink.c static int bond_fill_info(struct sk_buff *skb, · line 868
Quoted 10 lines
drivers/net/bonding/bond_netlink.c +3
-1+ READ_ONCE(bond->params.ad_user_port_key))) goto nla_put_failure; +/* Small race here, this is a minor trade off. */ if (nla_put (skb,IFLA_BOND_AD_ACTOR_SYSTEM ,ETH_ALEN , &bond->params.ad_actor_system)) goto nla_put_failure; } -if (!bond_3ad_get_active_agg_info (bond, &info)) { +if (!__bond_3ad_get_active_agg_info (bond, &info)) {
^^^^
[Severity: Critical]
Is it possible for a concurrent mode change to cause a NULL pointer dereference here?
Since bond_fill_info() no longer holds the RTNL lock, another thread could concurrently change the bonding mode from BOND_MODE_8023AD to BOND_MODE_ACTIVEBACKUP and immediately enslave a new device.
During bond_alloc_slave(), if the new mode is ACTIVEBACKUP, slave->ad_info is not allocated and remains NULL. The slave is then added to the RCU-protected bond->slave_list.
Because bond_fill_info() cached the mode earlier as BOND_MODE_8023AD:
mode = READ_ONCE(bond->params.mode);
It will still enter this block and call __bond_3ad_get_active_agg_info(). That function iterates over the slave list and attempts to dereference the newly added slave's ad_info without checking if it is NULL:
drivers/net/bonding/bond_3ad.c:__bond_3ad_get_active_agg_info() {
...
bond_for_each_slave_rcu(bond, slave, iter) {
port = &(SLAVE_AD_INFO(slave)->port);
tmp = rcu_dereference(port->aggregator);
...
}Could this be addressed by adding a NULL check for SLAVE_AD_INFO(slave) in __bond_3ad_get_active_agg_info(), or by re-evaluating the mode?
Quoted 3 lines
drivers/net/bonding/bond_netlink.c +0
-0struct nlattr *nest; nest =nla_nest_start_noflag (skb,IFLA_BOND_AD_INFO );
Signature · 1 line
pw-bot: cr