ipvs: avoid possible loop in ip_vs_dst_event on resizing
Subsystems (4)
Julian Anastasov
maintainerPATCH 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.
---
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
@@ -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
Julian Anastasov
maintainerHello,
Quoted 21 lines · Julian Anastasov
Sashiko points out that unprivileged user can frequently call
ip_vs_flush()orip_vs_del_service()to triggersvc_table_changesupdates that can lead to infinite loop inip_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 manyNETDEV_DOWNevents.One way to solve it is to hold
svc_resize_seminip_vs_dst_event()but this can block the dev notifier during the whole resizing process.Instead, use new
rw_semaphoresvc_replace_semto protect just thesvc_tablereplacement which is a short code section. Then holdsvc_replace_seminip_vs_dst_event()to serialize with replacing thesvc_table. As result, loop is avoided as there is no need to repeat the table walking from the start. By this way changes insvc_table_changescan 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_NORESIZEis the flag used to stop thesvc_resize_workunderservice_mutex, we should check only this flag often but not while underservice_mutex.Fixes 840aac3d900d ("ipvs: use resizable hash table for services")Signed-off-by Julian Anastasov <[email protected]>
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]>