]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ipvs: Guard access of HK_TYPE_KTHREAD cpumask with RCU
authorWaiman Long <longman@redhat.com>
Thu, 30 Apr 2026 07:44:19 +0000 (10:44 +0300)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 4 May 2026 23:52:55 +0000 (01:52 +0200)
The ip_vs_ctl.c file and the associated ip_vs.h file are the only places
in the kernel where HK_TYPE_KTHREAD cpumask is being retrieved and used.
Now that HK_TYPE_KTHREAD/HK_TYPE_DOMAIN cpumask can be changed at run
time. We need to use RCU to guard access to this cpumask to avoid a
potential UAF problem as the returned cpumask may be freed before it
is being used.

We can replace HK_TYPE_KTHREAD by HK_TYPE_DOMAIN as they are aliases
of each other, but keeping the HK_TYPE_KTHREAD name can highlight the
fact that it is the kthread initiated by ipvs that is being controlled.

Fixes: 03ff73510169 ("cpuset: Update HK_TYPE_DOMAIN cpumask from cpuset")
Signed-off-by: Waiman Long <longman@redhat.com>
Signed-off-by: Julian Anastasov <ja@ssi.bg>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/net/ip_vs.h
net/netfilter/ipvs/ip_vs_ctl.c

index d28ad8a0541fe55c96eb736e6ac03de898c79243..02762ce73a0ce4dbe99572b4a1d27c27a84653fd 100644 (file)
@@ -1412,7 +1412,7 @@ static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
        return ipvs->sysctl_run_estimation;
 }
 
-static inline const struct cpumask *sysctl_est_cpulist(struct netns_ipvs *ipvs)
+static inline const struct cpumask *__sysctl_est_cpulist(struct netns_ipvs *ipvs)
 {
        if (ipvs->est_cpulist_valid)
                return ipvs->sysctl_est_cpulist;
@@ -1530,7 +1530,7 @@ static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
        return 1;
 }
 
-static inline const struct cpumask *sysctl_est_cpulist(struct netns_ipvs *ipvs)
+static inline const struct cpumask *__sysctl_est_cpulist(struct netns_ipvs *ipvs)
 {
        return housekeeping_cpumask(HK_TYPE_KTHREAD);
 }
@@ -1565,6 +1565,18 @@ static inline int sysctl_svc_lfactor(struct netns_ipvs *ipvs)
        return READ_ONCE(ipvs->sysctl_svc_lfactor);
 }
 
+static inline bool sysctl_est_cpulist_empty(struct netns_ipvs *ipvs)
+{
+       guard(rcu)();
+       return cpumask_empty(__sysctl_est_cpulist(ipvs));
+}
+
+static inline unsigned int sysctl_est_cpulist_weight(struct netns_ipvs *ipvs)
+{
+       guard(rcu)();
+       return cpumask_weight(__sysctl_est_cpulist(ipvs));
+}
+
 /* IPVS core functions
  * (from ip_vs_core.c)
  */
@@ -1904,7 +1916,7 @@ static inline void ip_vs_est_stopped_recalc(struct netns_ipvs *ipvs)
        /* Stop tasks while cpulist is empty or if disabled with flag */
        ipvs->est_stopped = !sysctl_run_estimation(ipvs) ||
                            (ipvs->est_cpulist_valid &&
-                            cpumask_empty(sysctl_est_cpulist(ipvs)));
+                            sysctl_est_cpulist_empty(ipvs));
 #endif
 }
 
@@ -1920,7 +1932,7 @@ static inline bool ip_vs_est_stopped(struct netns_ipvs *ipvs)
 static inline int ip_vs_est_max_threads(struct netns_ipvs *ipvs)
 {
        unsigned int limit = IPVS_EST_CPU_KTHREADS *
-                            cpumask_weight(sysctl_est_cpulist(ipvs));
+                            sysctl_est_cpulist_weight(ipvs);
 
        return max(1U, limit);
 }
index 5c9f8e0e238f7a4e1cbb8cdbd33d593095c1508f..c7c7f6a7a9f6dfc8e513d17a1c3fa813a23bf497 100644 (file)
@@ -2394,11 +2394,14 @@ static int ipvs_proc_est_cpumask_get(const struct ctl_table *table,
 
        mutex_lock(&ipvs->est_mutex);
 
-       if (ipvs->est_cpulist_valid)
-               mask = *valp;
-       else
-               mask = (struct cpumask *)housekeeping_cpumask(HK_TYPE_KTHREAD);
-       ret = scnprintf(buffer, size, "%*pbl\n", cpumask_pr_args(mask));
+       /* HK_TYPE_KTHREAD cpumask needs RCU protection */
+       scoped_guard(rcu) {
+               if (ipvs->est_cpulist_valid)
+                       mask = *valp;
+               else
+                       mask = (struct cpumask *)housekeeping_cpumask(HK_TYPE_KTHREAD);
+               ret = scnprintf(buffer, size, "%*pbl\n", cpumask_pr_args(mask));
+       }
 
        mutex_unlock(&ipvs->est_mutex);