Skip to contentSkip to conversation

ipvs: fix NULL deref in ip_vs_add_service error path

PATCH Superseded Weiming Shi 2 participants 3 messages Apr 1

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipvs: fix NULL deref in ip_vs_add_service error path

When ip_vs_bind_scheduler() succeeds in ip_vs_add_service(), the local variable sched is set to NULL. If ip_vs_start_estimator() subsequently fails, the out_err cleanup calls ip_vs_unbind_scheduler(svc, sched) with sched == NULL. ip_vs_unbind_scheduler() passes the cur_sched NULL check (because svc->scheduler was set by the successful bind) but then dereferences the NULL sched parameter at sched->done_service, causing a kernel panic at offset 0x30 from NULL.

 Oops: general protection fault, probably for non-canonical address 0xdffffc0000000006: 0000 [#1] PREEMPT SMP KASAN NOPTI
 KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037]
 RIP: 0010:ip_vs_unbind_scheduler (net/netfilter/ipvs/ip_vs_sched.c:69)
 Call Trace:
  <TASK>
  ip_vs_add_service.isra.0 (net/netfilter/ipvs/ip_vs_ctl.c:1500)
  do_ip_vs_set_ctl (net/netfilter/ipvs/ip_vs_ctl.c:2809)
  nf_setsockopt (net/netfilter/nf_sockopt.c:102)
  ip_setsockopt (net/ipv4/ip_sockglue.c:1427)
  raw_setsockopt (net/ipv4/raw.c:850)
  do_sock_setsockopt (net/socket.c:2322)
  __sys_setsockopt (net/socket.c:2339)
  __x64_sys_setsockopt (net/socket.c:2350)
  do_syscall_64 (arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  </TASK>

Fix by recovering the scheduler pointer from svc->scheduler before cleanup when the local sched variable has been cleared. This also prevents a latent module refcount leak: without the recovery, ip_vs_scheduler_put(sched) receives NULL and skips the module_put(), so the scheduler module could never be unloaded if the kernel survived past the dereference.

Fixes 05f00505a89a ("ipvs: fix crash if scheduler is changed")
---
 net/netfilter/ipvs/ip_vs_ctl.c | 2 ++
 1 file changed, 2 insertions(+)
net/netfilter/ipvs/ip_vs_ctl.c +2 -0
index 35642de2a0fee..e0c978def9749 100644
@@ -1497,6 +1497,8 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
 	if (ret_hooks >= 0)
 		ip_vs_unregister_hooks(ipvs, u->af);
 	if (svc != NULL) {
+		if (!sched)
+			sched = rcu_dereference_protected(svc->scheduler, 1);
 		ip_vs_unbind_scheduler(svc, sched);
 		ip_vs_service_free(svc);
 	}
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hello,

Quoted 44 lines · Weiming Shi

When ip_vs_bind_scheduler() succeeds in ip_vs_add_service(), the local variable sched is set to NULL. If ip_vs_start_estimator() subsequently fails, the out_err cleanup calls ip_vs_unbind_scheduler(svc, sched) with sched == NULL. ip_vs_unbind_scheduler() passes the cur_sched NULL check (because svc->scheduler was set by the successful bind) but then dereferences the NULL sched parameter at sched->done_service, causing a kernel panic at offset 0x30 from NULL.

 Oops: general protection fault, probably for non-canonical address 0xdffffc0000000006: 0000 [#1] PREEMPT SMP KASAN NOPTI
 KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037]
 RIP: 0010:ip_vs_unbind_scheduler (net/netfilter/ipvs/ip_vs_sched.c:69)
 Call Trace:
  <TASK>
  ip_vs_add_service.isra.0 (net/netfilter/ipvs/ip_vs_ctl.c:1500)
  do_ip_vs_set_ctl (net/netfilter/ipvs/ip_vs_ctl.c:2809)
  nf_setsockopt (net/netfilter/nf_sockopt.c:102)
  ip_setsockopt (net/ipv4/ip_sockglue.c:1427)
  raw_setsockopt (net/ipv4/raw.c:850)
  do_sock_setsockopt (net/socket.c:2322)
  __sys_setsockopt (net/socket.c:2339)
  __x64_sys_setsockopt (net/socket.c:2350)
  do_syscall_64 (arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  </TASK>

Fix by recovering the scheduler pointer from svc->scheduler before cleanup when the local sched variable has been cleared. This also prevents a latent module refcount leak: without the recovery, ip_vs_scheduler_put(sched) receives NULL and skips the module_put(), so the scheduler module could never be unloaded if the kernel survived past the dereference.

Fixes 05f00505a89a ("ipvs: fix crash if scheduler is changed")
---
 net/netfilter/ipvs/ip_vs_ctl.c | 2 ++
 1 file changed, 2 insertions(+)
net/netfilter/ipvs/ip_vs_ctl.c +0 -0
index 35642de2a0fee..e0c978def9749 100644

net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, · line 1501

Quoted 5 lines
net/netfilter/ipvs/ip_vs_ctl.c +2 -0
@@ -1497,6 +1497,8 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
 	if (ret_hooks >= 0)
 		ip_vs_unregister_hooks(ipvs, u->af);
 	if (svc != NULL) {
+		if (!sched)
+			sched = rcu_dereference_protected(svc->scheduler, 1);

Good catch. But may be it should be enough if
we just remove the sched = NULL after successful ip_vs_bind_scheduler(), what do you think? ip_vs_unbind_scheduler() already detects if the scheduler is installed.

net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, · line 1503

Quoted 2 lines
net/netfilter/ipvs/ip_vs_ctl.c +0 -0
 		ip_vs_unbind_scheduler(svc, sched);
 		ip_vs_service_free(svc);

Regards

--
Julian Anastasov <[email protected]>

#2 lore raw
Quoted 60 lines · Julian Anastasov

Hello,

Quoted 44 lines · Weiming Shi

When ip_vs_bind_scheduler() succeeds in ip_vs_add_service(), the local variable sched is set to NULL. If ip_vs_start_estimator() subsequently fails, the out_err cleanup calls ip_vs_unbind_scheduler(svc, sched) with sched == NULL. ip_vs_unbind_scheduler() passes the cur_sched NULL check (because svc->scheduler was set by the successful bind) but then dereferences the NULL sched parameter at sched->done_service, causing a kernel panic at offset 0x30 from NULL.

 Oops: general protection fault, probably for non-canonical address 0xdffffc0000000006: 0000 [#1] PREEMPT SMP KASAN NOPTI
 KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037]
 RIP: 0010:ip_vs_unbind_scheduler (net/netfilter/ipvs/ip_vs_sched.c:69)
 Call Trace:
  <TASK>
  ip_vs_add_service.isra.0 (net/netfilter/ipvs/ip_vs_ctl.c:1500)
  do_ip_vs_set_ctl (net/netfilter/ipvs/ip_vs_ctl.c:2809)
  nf_setsockopt (net/netfilter/nf_sockopt.c:102)
  ip_setsockopt (net/ipv4/ip_sockglue.c:1427)
  raw_setsockopt (net/ipv4/raw.c:850)
  do_sock_setsockopt (net/socket.c:2322)
  __sys_setsockopt (net/socket.c:2339)
  __x64_sys_setsockopt (net/socket.c:2350)
  do_syscall_64 (arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130)
  </TASK>

Fix by recovering the scheduler pointer from svc->scheduler before cleanup when the local sched variable has been cleared. This also prevents a latent module refcount leak: without the recovery, ip_vs_scheduler_put(sched) receives NULL and skips the module_put(), so the scheduler module could never be unloaded if the kernel survived past the dereference.

Fixes 05f00505a89a ("ipvs: fix crash if scheduler is changed")
---
 net/netfilter/ipvs/ip_vs_ctl.c | 2 ++
 1 file changed, 2 insertions(+)
net/netfilter/ipvs/ip_vs_ctl.c +0 -0
index 35642de2a0fee..e0c978def9749 100644

net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, · line 1501

Quoted 5 lines
net/netfilter/ipvs/ip_vs_ctl.c +2 -0
@@ -1497,6 +1497,8 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,
 	if (ret_hooks >= 0)
 		ip_vs_unregister_hooks(ipvs, u->af);
 	if (svc != NULL) {
+		if (!sched)
+			sched = rcu_dereference_protected(svc->scheduler, 1);

Good catch. But may be it should be enough if
we just remove the sched = NULL after successful ip_vs_bind_scheduler(), what do you think? ip_vs_unbind_scheduler() already detects if the scheduler is installed.

Quoted 8 lines

net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, · line 1503

Quoted 2 lines
net/netfilter/ipvs/ip_vs_ctl.c +0 -0
 		ip_vs_unbind_scheduler(svc, sched);
 		ip_vs_service_free(svc);

Regards

--
Julian Anastasov <[email protected]>

Hi Julian,

Thanks for the review. You're right, removing the sched = NULL is simpler and sufficient

I'll send a v2 patch.

Best,
Weiming Shi