[RHEL7,COMMIT] nfsd: race of nfsd inetaddr notifiers with nn->nfsd_serv change

Submitted by Konstantin Khorenko on Oct. 31, 2017, 10:16 a.m.

Details

Message ID 201710311016.v9VAGKAK017896@finist_ce7.work
State New
Series "race of nfsd inetaddr notifiers with nn->nfsd_serv change"
Headers show

Commit Message

Konstantin Khorenko Oct. 31, 2017, 10:16 a.m.
The commit is pushed to "branch-rh7-3.10.0-693.1.1.vz7.37.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-693.1.1.vz7.37.20
------>
commit 5902c9124c2f1d74b9986f4b2d7d48fc0e01e783
Author: Vasily Averin <vvs@virtuozzo.com>
Date:   Tue Oct 31 13:16:20 2017 +0300

    nfsd: race of nfsd inetaddr notifiers with nn->nfsd_serv change
    
    nfsd_inet[6]addr_event use nn->nfsd_serv without taken nfsd_mutex,
    pointer can be changed during execution of notifiers and crash the host.
    
    Moreover if notifiers was enabled in one net namespace it will be enabled
    in all other net namespaces, from its creation until its destroy.
    
    Patch allows notifiers to access to nn->nfsd_serv only when the pointer was
    correctly initialized and delays its cleanup until notifiers are in use.
    
    https://jira.sw.ru/browse/PSBM-76103
    Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
---
 fs/nfsd/netns.h  |  3 +++
 fs/nfsd/nfsctl.c |  3 +++
 fs/nfsd/nfssvc.c | 14 +++++++++++---
 3 files changed, 17 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/fs/nfsd/netns.h b/fs/nfsd/netns.h
index ee36efd..94cd7b8 100644
--- a/fs/nfsd/netns.h
+++ b/fs/nfsd/netns.h
@@ -119,6 +119,9 @@  struct nfsd_net {
 	u32 clverifier_counter;
 
 	struct svc_serv *nfsd_serv;
+
+	wait_queue_head_t ntf_wq;
+	atomic_t ntf_refcnt;
 };
 
 /* Simple check to find out if a given net was properly initialized */
diff --git a/fs/nfsd/nfsctl.c b/fs/nfsd/nfsctl.c
index 7b5c926..44a9446 100644
--- a/fs/nfsd/nfsctl.c
+++ b/fs/nfsd/nfsctl.c
@@ -1232,6 +1232,9 @@  static __net_init int nfsd_init_net(struct net *net)
 	nn->nfsd4_grace = 90;
 	nn->clverifier_counter = prandom_u32();
 	nn->clientid_counter = prandom_u32();
+
+	atomic_set(&nn->ntf_refcnt, 0);
+	init_waitqueue_head(&nn->ntf_wq);
 	return 0;
 
 out_idmap_error:
diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c
index 15497cb..8b72c0e 100644
--- a/fs/nfsd/nfssvc.c
+++ b/fs/nfsd/nfssvc.c
@@ -319,7 +319,8 @@  static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 	struct sockaddr_in sin;
 
-	if (event != NETDEV_DOWN)
+	if ((event != NETDEV_DOWN) ||
+	    !atomic_inc_not_zero(&nn->ntf_refcnt))
 		goto out;
 
 	if (nn->nfsd_serv) {
@@ -328,6 +329,8 @@  static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
 		sin.sin_addr.s_addr = ifa->ifa_local;
 		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
 	}
+	atomic_dec(&nn->ntf_refcnt);
+	wake_up(&nn->ntf_wq);
 
 out:
 	return NOTIFY_DONE;
@@ -347,7 +350,8 @@  static int nfsd_inet6addr_event(struct notifier_block *this,
 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 	struct sockaddr_in6 sin6;
 
-	if (event != NETDEV_DOWN)
+	if ((event != NETDEV_DOWN) ||
+	    !atomic_inc_not_zero(&nn->ntf_refcnt))
 		goto out;
 
 	if (nn->nfsd_serv) {
@@ -356,7 +360,8 @@  static int nfsd_inet6addr_event(struct notifier_block *this,
 		sin6.sin6_addr = ifa->addr;
 		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
 	}
-
+	atomic_dec(&nn->ntf_refcnt);
+	wake_up(&nn->ntf_wq);
 out:
 	return NOTIFY_DONE;
 }
@@ -373,6 +378,7 @@  static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
 {
 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 
+	atomic_dec(&nn->ntf_refcnt);
 	/* check if the notifier still has clients */
 	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
 		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
@@ -380,6 +386,7 @@  static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
 		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
 #endif
 	}
+	wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
 
 	/*
 	 * write_ports can create the server without actually starting
@@ -502,6 +509,7 @@  int nfsd_create_serv(struct net *net)
 		register_inet6addr_notifier(&nfsd_inet6addr_notifier);
 #endif
 	}
+	atomic_inc(&nn->ntf_refcnt);
 	do_gettimeofday(&nn->nfssvc_boot);		/* record boot time */
 	return 0;
 }