Skip to contentSkip to conversation

ipvs: avoid possible loop in ip_vs_dst_event on resizing

PATCH Superseded Julian Anastasov 1 participant 2 messages May 7 – May 8

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Julian Anastasov

author maintainer
#0 lore raw

PATCH v2 1/1 ipvs: avoid possible loop in ip_vs_dst_event on resizing

Sashiko points out that unprivileged user can frequently call ip_vs_flush() or ip_vs_del_service() to trigger svc_table_changes updates that can lead to infinite loop in ip_vs_dst_event(). This can also happen if the user triggers frequent table resizing without deleting all services. We should also consider the possible effects if the user triggers many NETDEV_DOWN events.

One way to solve it is to hold svc_resize_sem in ip_vs_dst_event() but this can block the dev notifier during the whole resizing process.

Instead, use new rw_semaphore svc_replace_sem to protect just the svc_table replacement which is a short code section. Then hold svc_replace_sem in ip_vs_dst_event() to serialize with replacing the svc_table. As result, loop is avoided as there is no need to repeat the table walking from the start. By this way changes in svc_table_changes can happen only when all services are removed and all dev references dropped which allows us to abort the table walking.

As IP_VS_WORK_SVC_NORESIZE is the flag used to stop the svc_resize_work under service_mutex, we should check only this flag often but not while under service_mutex.

Fixes 840aac3d900d ("ipvs: use resizable hash table for services")

---
v2:
* Do not check IP_VS_WORK_SVC_NORESIZE under service_mutex

 include/net/ip_vs.h            |  3 +-
 net/netfilter/ipvs/ip_vs_ctl.c | 70 +++++++++++++++++++++-------------
 2 files changed, 46 insertions(+), 27 deletions(-)
include/net/ip_vs.h +2 -1
index 02762ce73a0c..a02e569813d2 100644
@@ -1186,8 +1186,9 @@ struct netns_ipvs {
 	struct timer_list	dest_trash_timer; /* expiration timer */
 	struct mutex		service_mutex;    /* service reconfig */
 	struct rw_semaphore	svc_resize_sem;   /* svc_table resizing */
+	struct rw_semaphore	svc_replace_sem;  /* svc_table replace */
 	struct delayed_work	svc_resize_work;  /* resize svc_table */
-	atomic_t		svc_table_changes;/* ++ on new table */
+	atomic_t		svc_table_changes;/* ++ on table changes */
 	/* Service counters */
 	atomic_t		num_services[IP_VS_AF_MAX];   /* Services */
 	atomic_t		fwm_services[IP_VS_AF_MAX];   /* Services */
net/netfilter/ipvs/ip_vs_ctl.c +44 -26

173 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

Hello,

Quoted 21 lines · Julian Anastasov

Sashiko points out that unprivileged user can frequently call ip_vs_flush() or ip_vs_del_service() to trigger svc_table_changes updates that can lead to infinite loop in ip_vs_dst_event(). This can also happen if the user triggers frequent table resizing without deleting all services. We should also consider the possible effects if the user triggers many NETDEV_DOWN events.

One way to solve it is to hold svc_resize_sem in ip_vs_dst_event() but this can block the dev notifier during the whole resizing process.

Instead, use new rw_semaphore svc_replace_sem to protect just the svc_table replacement which is a short code section. Then hold svc_replace_sem in ip_vs_dst_event() to serialize with replacing the svc_table. As result, loop is avoided as there is no need to repeat the table walking from the start. By this way changes in svc_table_changes can happen only when all services are removed and all dev references dropped which allows us to abort the table walking.

Quoted 7 lines

As IP_VS_WORK_SVC_NORESIZE is the flag used to stop the svc_resize_work under service_mutex, we should check only this flag often but not while under service_mutex.

Fixes 840aac3d900d ("ipvs: use resizable hash table for services")

There is a way to remove the trylock in svc_resize_work,
will send v3 later today.

pw-bot: changes-requested

Regards

--
Julian Anastasov <[email protected]>