net: bridge: vlan: fix memory leak & null pointer deref
Subsystems (3)
Nikolay Aleksandrov
maintainercover net: bridge: vlan: fix memory leak & null pointer deref
Hi,
These are two issues I found while reworking the bridge vlan fast-path. I need to fix them before I can post my larger patch-sets because they will make these bugs more visible and easy to trigger.
Patch 01 fixes memory leaks on the vlan flush path (teardown). These have existed since I converted the VLAN code to use rhashtables, but are hard to hit because they require __vlan_vid_del or switch dev error to hit. It is ok to abort vlan deletion when removing a single vlan, but we must free their resources on teardown otherwise they're left dangling.
Patch 02 fixes a NULL pointer deref that is a consequence of patch 01's bug. If the vlan delete fails when removing a bridge device vlan, the error printing will try to dereference the port which is NULL.
Thanks,
Nik
Nikolay Aleksandrov (2):
net: bridge: vlan: fix leaks on switchdev deletion errors
net: bridge: vlan: avoid NULL dereference on flush errors
net/bridge/br_vlan.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.3
Nikolay Aleksandrov
maintainerPATCH 1/2 net: bridge: vlan: fix leaks on switchdev deletion errors
__vlan_del() stops the software deletion when the switchdev operation fails which is ok for an explicit VLAN deletion because the VLAN remains configured but not ok when its VLAN group is being destroyed and everything is being freed. __vlan_flush() always destroys the VLAN group after walking it regardless of individual deletion errors, so aborting the software vlan delete leaks the VLAN object's memory (and potentially its master VLAN, due to references).
Allow teardown callers to finish the software deletion while preserving error for reporting. Save the VLAN id before deleting because the VLAN object can already be freed (queued for freeing by call_rcu).
--- net/bridge/br_vlan.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
net/bridge/br_vlan.c +11 -9
@@ -387,7 +387,7 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags, goto out; } -static int __vlan_del (struct net_bridge_vlan *v) +static int __vlan_del (struct net_bridge_vlan *v,bool teardown) {struct net_bridge_vlan *masterv = v;struct net_bridge_vlan_group *vg;
@@ -405,13 +405,14 @@ static int __vlan_del(struct net_bridge_vlan *v)__vlan_delete_pvid (vg, v->vid);if (p) { err =__vlan_vid_del (p->dev, p->br, v); -if (err) +if (err && !teardown) goto out; }else { err =br_switchdev_port_vlan_del (v->br->dev, v->vid); -if (err && err != -EOPNOTSUPP ) +if (err == -EOPNOTSUPP ) + err =0 ; +else if (err && !teardown) goto out; - err =0 ; }if (br_vlan_should_use (v)) {
@@ -448,7 +449,7 @@ static void __vlan_flush(const struct net_bridge *br, struct net_bridge_vlan_group *vg) {struct net_bridge_vlan *vlan, *tmp; -u16 v_start =0 , v_end =0 ; +u16 v_start =0 , v_end =0 , vid;int err;__vlan_delete_pvid (vg, vg->pvid);
@@ -463,12 +464,13 @@ static void __vlan_flush(const struct net_bridge *br, } v_end = vlan->vid; - err =__vlan_del (vlan); + vid = vlan->vid; + err =__vlan_del (vlan, true);if (err) {br_err (br,"port %u(%s) failed to delete vlan %d: %pe\n" , (unsigned int ) p->port_no, p->dev->name, - vlan->vid,ERR_PTR (err)); + vid,ERR_PTR (err)); } }
@@ -838,7 +840,7 @@ int br_vlan_delete(struct net_bridge *br, u16 vid)vlan_tunnel_info_del (vg, v); -return __vlan_del (v); +return __vlan_del (v, false); }void br_vlan_flush (struct net_bridge *br)
@@ -1369,7 +1371,7 @@ int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)br_fdb_find_delete_local (port->br, port, port->dev->dev_addr, vid);br_fdb_delete_by_port (port->br, port, vid,0 ); -return __vlan_del (v); +return __vlan_del (v, false); }void nbp_vlan_flush (struct net_bridge_port *port)
Signature · 1 line
2.47.3
Ido Schimmel
maintainerRe: [PATCH net 1/2] net: bridge: vlan: fix leaks on switchdev deletion errors
Quoted 11 lines · Nikolay Aleksandrov
__vlan_del()stops the software deletion when the switchdev operation fails which is ok for an explicit VLAN deletion because the VLAN remains configured but not ok when its VLAN group is being destroyed and everything is being freed.__vlan_flush()always destroys the VLAN group after walking it regardless of individual deletion errors, so aborting the software vlan delete leaks the VLAN object's memory (and potentially its master VLAN, due to references).Allow teardown callers to finish the software deletion while preserving error for reporting. Save the VLAN id before deleting because the VLAN object can already be freed (queued for freeing by
call_rcu).
I think we should only log an error instead of aborting the operation.
It's not really valid for a driver to refuse VLAN deletion and it's inconsistent with the 8021q deletion path (i.e., __vlan_vid_del() -> vlan_vid_del()) which doesn't abort the operation.
Also, by the time the switchdev deletion fails, the affected FDB entries were already flushed and the PVID was already cleared, so the state is inconsistent.
Something like:
net/bridge/br_vlan.c +14 -17
@@ -387,12 +387,12 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags, goto out; } -static int __vlan_del (struct net_bridge_vlan *v) +static void __vlan_del (struct net_bridge_vlan *v) {struct net_bridge_vlan *masterv = v;struct net_bridge_vlan_group *vg;struct net_bridge_port *p =NULL ; -int err =0 ; +int err;if (br_vlan_is_master (v)) { vg =br_vlan_group (v->br);
@@ -406,12 +406,16 @@ static int __vlan_del(struct net_bridge_vlan *v)if (p) { err =__vlan_vid_del (p->dev, p->br, v);if (err) - goto out; +br_warn (p->br, +"port %u(%s) failed to delete vlan %u from switchdev: %pe\n" , + (unsigned int )p->port_no, p->dev->name, + v->vid,ERR_PTR (err)); }else { err =br_switchdev_port_vlan_del (v->br->dev, v->vid);if (err && err != -EOPNOTSUPP ) - goto out; - err =0 ; +br_warn (v->br, +"failed to delete bridge vlan %u from switchdev: %pe\n" , + v->vid,ERR_PTR (err)); }if (br_vlan_should_use (v)) {
@@ -431,8 +435,6 @@ static int __vlan_del(struct net_bridge_vlan *v) }br_vlan_put_master (masterv); -out: -return err; }static void __vlan_group_free (struct net_bridge_vlan_group *vg)
@@ -449,7 +451,6 @@ static void __vlan_flush(const struct net_bridge *br, {struct net_bridge_vlan *vlan, *tmp;u16 v_start =0 , v_end =0 ; -int err;__vlan_delete_pvid (vg, vg->pvid);list_for_each_entry_safe (vlan, tmp, &vg->vlan_list, vlist) {
@@ -463,13 +464,7 @@ static void __vlan_flush(const struct net_bridge *br, } v_end = vlan->vid; - err =__vlan_del (vlan); -if (err) { -br_err (br, -"port %u(%s) failed to delete vlan %d: %pe\n" , - (unsigned int ) p->port_no, p->dev->name, - vlan->vid,ERR_PTR (err)); - } +__vlan_del (vlan); }/* notify about the last/whole vlan range */
@@ -837,8 +832,9 @@ int br_vlan_delete(struct net_bridge *br, u16 vid)br_fdb_delete_by_port (br,NULL , vid,0 );vlan_tunnel_info_del (vg, v); +__vlan_del (v); -return __vlan_del (v); +return 0 ; }void br_vlan_flush (struct net_bridge *br)
@@ -1368,8 +1364,9 @@ int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)return -ENOENT ;br_fdb_find_delete_local (port->br, port, port->dev->dev_addr, vid);br_fdb_delete_by_port (port->br, port, vid,0 ); +__vlan_del (v); -return __vlan_del (v); +return 0 ; }void nbp_vlan_flush (struct net_bridge_port *port)
Nikolay Aleksandrov
maintainerRe: [PATCH net 1/2] net: bridge: vlan: fix leaks on switchdev deletion errors
Quoted 15 lines · Ido Schimmel
Quoted 11 lines · Nikolay Aleksandrov
__vlan_del()stops the software deletion when the switchdev operation fails which is ok for an explicit VLAN deletion because the VLAN remains configured but not ok when its VLAN group is being destroyed and everything is being freed.__vlan_flush()always destroys the VLAN group after walking it regardless of individual deletion errors, so aborting the software vlan delete leaks the VLAN object's memory (and potentially its master VLAN, due to references).Allow teardown callers to finish the software deletion while preserving error for reporting. Save the VLAN id before deleting because the VLAN object can already be freed (queued for freeing by
call_rcu).I think we should only log an error instead of aborting the operation.
Quoted 8 lines
It's not really valid for a driver to refuse VLAN deletion and it's inconsistent with the 8021q deletion path (i.e.,
__vlan_vid_del()->vlan_vid_del()) which doesn't abort the operation.Also, by the time the switchdev deletion fails, the affected FDB entries were already flushed and the PVID was already cleared, so the state is inconsistent.
Ack, sounds good. I'll just report the error instead of failing the delete. That makes it even easier and drops the need for the second patch. :)
Cheers,
Nik
Nikolay Aleksandrov
maintainerPATCH 2/2 net: bridge: vlan: avoid NULL dereference on flush errors
In reply to Nikolay Aleksandrov__vlan_flush() is used for both port and bridge VLAN groups. The error path unconditionally dereferences the port argument even though br_vlan_flush() calls it with a NULL port. Any error (e.g. switchdev) while deleting a bridge VLAN can result in a NULL pointer dereference. Use a bridge-specific error message when called for the bridge device.
--- net/bridge/br_vlan.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
net/bridge/br_vlan.c +9 -4
@@ -467,10 +467,15 @@ static void __vlan_flush(const struct net_bridge *br, vid = vlan->vid; err =__vlan_del (vlan, true);if (err) { -br_err (br, -"port %u(%s) failed to delete vlan %d: %pe\n" , - (unsigned int ) p->port_no, p->dev->name, - vid,ERR_PTR (err)); +if (p) +br_err (br, +"port %u(%s) failed to delete vlan %d: %pe\n" , + (unsigned int )p->port_no, p->dev->name, + vid,ERR_PTR (err)); +else +br_err (br, +"failed to delete bridge vlan %d: %pe\n" , + vid,ERR_PTR (err)); } }
Signature · 1 line
2.47.3
Nikolay Aleksandrov
maintainerRe: [PATCH net 2/2] net: bridge: vlan: avoid NULL dereference on flush errors
Quoted 26 lines · Nikolay Aleksandrov
__vlan_flush()is used for both port and bridge VLAN groups. The error path unconditionally dereferences the port argument even thoughbr_vlan_flush()calls it with a NULL port. Any error (e.g. switchdev) while deleting a bridge VLAN can result in a NULL pointer dereference. Use a bridge-specific error message when called for the bridge device.Fixes 5454f5c28eca ("net: bridge: vlan: check for errors from __vlan_del in __vlan_flush")Signed-off-by Nikolay Aleksandrov <[email protected]>--- net/bridge/br_vlan.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)net/bridge/br_vlan.c +2
-4@@ -467,10 +467,15 @@ static void __vlan_flush(const struct net_bridge *br, vid = vlan->vid; err =__vlan_del (vlan, true);if (err) { -br_err (br, -"port %u(%s) failed to delete vlan %d: %pe\n" , - (unsigned int ) p->port_no, p->dev->name, - vid,ERR_PTR (err)); +if (p) + br_err(br,
net/bridge/br_vlan.c static void __vlan_flush(const struct net_bridge *br, · line 481
Quoted 10 lines
net/bridge/br_vlan.c +7
-0+"port %u(%s) failed to delete vlan %d: %pe\n" , + (unsigned int )p->port_no, p->dev->name, + vid, ERR_PTR(err)); + else + br_err(br, +"failed to delete bridge vlan %d: %pe\n" , + vid, ERR_PTR(err)); } }
Note that currently only switchdev can err out and on the br_vlan_flush() path that is unlikely because all ports have already been removed. But that relies only on the facts that no swdev driver will return an error at that time and that __vlan_del returns an error only from swdev (currently), these are not hard contracts or APIs anyone follows, it is by mere chance that we haven't hit it. It would be best to handle the br/port cases separately as usual and check the port pointer before dereferencing. I think this belongs with the previous fix for the memory leaks.
Cheers,
Nik