Skip to contentSkip to conversation

NFSD: notifiers registration cleanup

PATCH In review Vasily Averin 3 participants 4 messages Sep 21, 2016 – Sep 22, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 NFSD: notifiers registration cleanup

By design notifier can be registered once only,
however nfsd registers the same inetaddr notifiers per net-namespace. When this happen it corrupts list of notifiers,
as result some notifiers can be not called on proper event, traverse on list can be cycled forever,
and second unregister can access already freed memory.

Fixes 36684996 ("nfsd: Register callbacks on the inetaddr_chain and inet6addr_chain")
---
 fs/nfsd/nfssvc.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
fs/nfsd/nfssvc.c +13 -4
index 45007ac..7f8914f 100644
@@ -366,14 +366,20 @@ static struct notifier_block nfsd_inet6addr_notifier = {
 };
 #endif
 
+static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
+
 static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
 {
 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
 
-	unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
+	/* check if the notifier still has clients */
+	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
+		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
 #if IS_ENABLED(CONFIG_IPV6)
-	unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
+		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
 #endif
+	}
+
 	/*
 	 * write_ports can create the server without actually starting
 	 * any threads--if we get shut down before any threads are
@@ -488,10 +494,13 @@ int nfsd_create_serv(struct net *net)
 	}
 
 	set_max_drc();
-	register_inetaddr_notifier(&nfsd_inetaddr_notifier);
+	/* check if the notifier is already set */
+	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
+		register_inetaddr_notifier(&nfsd_inetaddr_notifier);
 #if IS_ENABLED(CONFIG_IPV6)
-	register_inet6addr_notifier(&nfsd_inet6addr_notifier);
+		register_inet6addr_notifier(&nfsd_inet6addr_notifier);
 #endif
+	}
 	do_gettimeofday(&nn->nfssvc_boot);		/* record boot time */
 	return 0;
 }
Signature · 1 line
1.9.1
#1 lore raw
Quoted 49 lines · Vasily Averin

By design notifier can be registered once only,
however nfsd registers the same inetaddr notifiers per net-namespace. When this happen it corrupts list of notifiers,
as result some notifiers can be not called on proper event, traverse on list can be cycled forever,
and second unregister can access already freed memory.

Fixes 36684996 ("nfsd: Register callbacks on the inetaddr_chain and inet6addr_chain")
Quoted 1 line

---
 fs/nfsd/nfssvc.c | 17 +++++++++++++----

 1 file changed, 13 insertions(+), 4 deletions(-)
fs/nfsd/nfssvc.c +0 -0
index 45007ac..7f8914f 100644
@@ -366,14 +366,20 @@ static struct notifier_block nfsd_inet6addr_notifier = {
  };
  #endif

fs/nfsd/nfssvc.c +2 -0
+static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
+

 static void nfsd_last_thread(struct svc_serv *serv, struct net *net) {

Quoted 1 line

  struct nfsd_net *nn = net_generic(net, nfsd_net_id);

fs/nfsd/nfssvc.c static struct notifier_block nfsd_inet6addr_notifier = { · line 377

Quoted 4 lines
fs/nfsd/nfssvc.c +3 -1
-	unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
+	/* check if the notifier still has clients */
+	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
+		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);

 #if IS_ENABLED(CONFIG_IPV6)

fs/nfsd/nfssvc.c static struct notifier_block nfsd_inet6addr_notifier = { · line 379

Quoted 2 lines
fs/nfsd/nfssvc.c +1 -1
-	unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
+		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);

 #endif

Review

Quoted 1 line
(quoted diff) +1 -0
+	}

+

Quoted 3 lines

  /*
   * write_ports can create the server without actually starting
   * any threads--if we get shut down before any threads are

fs/nfsd/nfssvc.c +0 -0
@@ -488,10 +494,13 @@ int nfsd_create_serv(struct net *net)
 >  	}

fs/nfsd/nfssvc.c int nfsd_create_serv(struct net *net) · line 499

Quoted 5 lines

  set_max_drc();

fs/nfsd/nfssvc.c +3 -1
-	register_inetaddr_notifier(&nfsd_inetaddr_notifier);
+	/* check if the notifier is already set */
+	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
+		register_inetaddr_notifier(&nfsd_inetaddr_notifier);

 #if IS_ENABLED(CONFIG_IPV6)

Quoted 7 lines

fs/nfsd/nfssvc.c int nfsd_create_serv(struct net *net) · line 501

Quoted 2 lines
fs/nfsd/nfssvc.c +1 -1
-	register_inet6addr_notifier(&nfsd_inet6addr_notifier);
+		register_inet6addr_notifier(&nfsd_inet6addr_notifier);

 #endif

Quoted 3 lines
(quoted diff) +1 -0
+	}
Quoted 1 line

  do_gettimeofday(&nn->nfssvc_boot); /* record boot time */

  return 0;

 }

Good catch. I'm not very fond of the refcounting this here but it should
serve the purpose and I don't have anything better to suggest. FWIW, I think the nfsd_mutex is held during all of these operations so we probably don't need atomics for the refcount.

--
Jeff Layton <[email protected]>

#2 lore raw
Quoted 5 lines · Jeff Layton

Good catch. I'm not very fond of the refcounting this here but it should
serve the purpose and I don't have anything better to suggest. FWIW, I think the nfsd_mutex is held during all of these operations so we probably don't need atomics for the refcount.

I've copied idea from netfilters,
look at using of masquerade_notifier_refcount

Perhaps someday in future we'll replace all such cases by some common primitive.

#3 lore raw
In reply to Jeff Layton
Quoted 55 lines · Jeff Layton
Quoted 54 lines · Vasily Averin

By design notifier can be registered once only,
however nfsd registers the same inetaddr notifiers per net-namespace. When this happen it corrupts list of notifiers,
as result some notifiers can be not called on proper event, traverse on list can be cycled forever,
and second unregister can access already freed memory.

Fixes 36684996 ("nfsd: Register callbacks on the inetaddr_chain and inet6addr_chain")
Quoted 1 line

---
 fs/nfsd/nfssvc.c | 17 +++++++++++++----

 1 file changed, 13 insertions(+), 4 deletions(-)
fs/nfsd/nfssvc.c +0 -0
index 45007ac..7f8914f 100644
@@ -366,14 +366,20 @@ static struct notifier_block nfsd_inet6addr_notifier = {
  };
  #endif

fs/nfsd/nfssvc.c +2 -0
+static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
+

 static void nfsd_last_thread(struct svc_serv *serv, struct net *net) {

Quoted 1 line

  struct nfsd_net *nn = net_generic(net, nfsd_net_id);

fs/nfsd/nfssvc.c static struct notifier_block nfsd_inet6addr_notifier = { · line 377

Quoted 4 lines
fs/nfsd/nfssvc.c +3 -1
-	unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
+	/* check if the notifier still has clients */
+	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
+		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);

 #if IS_ENABLED(CONFIG_IPV6)

fs/nfsd/nfssvc.c static struct notifier_block nfsd_inet6addr_notifier = { · line 379

Quoted 2 lines
fs/nfsd/nfssvc.c +1 -1
-	unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
+		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);

 #endif

Review

Quoted 1 line
(quoted diff) +1 -0
+	}

+

Quoted 3 lines

  /*
   * write_ports can create the server without actually starting
   * any threads--if we get shut down before any threads are

fs/nfsd/nfssvc.c +0 -0
@@ -488,10 +494,13 @@ int nfsd_create_serv(struct net *net)
 >  	}

fs/nfsd/nfssvc.c int nfsd_create_serv(struct net *net) · line 499

Quoted 5 lines

  set_max_drc();

fs/nfsd/nfssvc.c +3 -1
-	register_inetaddr_notifier(&nfsd_inetaddr_notifier);
+	/* check if the notifier is already set */
+	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
+		register_inetaddr_notifier(&nfsd_inetaddr_notifier);

 #if IS_ENABLED(CONFIG_IPV6)

fs/nfsd/nfssvc.c int nfsd_create_serv(struct net *net) · line 501

Quoted 2 lines
fs/nfsd/nfssvc.c +1 -1
-	register_inet6addr_notifier(&nfsd_inet6addr_notifier);
+		register_inet6addr_notifier(&nfsd_inet6addr_notifier);

 #endif

Quoted 2 lines
(quoted diff) +1 -0
+	}
Quoted 1 line

  do_gettimeofday(&nn->nfssvc_boot); /* record boot time */

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

  return 0;

 }

Good catch. I'm not very fond of the refcounting this here but it should
serve the purpose and I don't have anything better to suggest. FWIW, I think the nfsd_mutex is held during all of these operations so we probably don't need atomics for the refcount.

Inclined to apply with a note like:

	/* Only used under nfsd_mutex, so this atomic may be overkill: */
	static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
	...

--b.

Quoted 3 lines

--
Jeff Layton <[email protected]>