linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 1/3] kernel/notifier.c: avoid duplicate registration
@ 2019-07-10  3:09 Xiaoming Ni
  2019-07-17 11:19 ` Vasily Averin
  0 siblings, 1 reply; 2+ messages in thread
From: Xiaoming Ni @ 2019-07-10  3:09 UTC (permalink / raw)
  To: adobriyan, akpm, anna.schumaker, arjan, bfields, chuck.lever,
	davem, gregkh, jlayton, luto, mingo, Nadia.Derbey, paulmck,
	semen.protsenko, stable, stern, tglx, torvalds, trond.myklebust,
	viresh.kumar, vvs
  Cc: alex.huangjianhui, dylix.dailei, nixiaoming, linux-kernel,
	linux-nfs, netdev

Registering the same notifier to a hook repeatedly can cause the hook
list to form a ring or lose other members of the list.

case1: An infinite loop in notifier_chain_register() can cause soft lockup
        atomic_notifier_chain_register(&test_notifier_list, &test1);
        atomic_notifier_chain_register(&test_notifier_list, &test1);
        atomic_notifier_chain_register(&test_notifier_list, &test2);

case2: An infinite loop in notifier_chain_register() can cause soft lockup
        atomic_notifier_chain_register(&test_notifier_list, &test1);
        atomic_notifier_chain_register(&test_notifier_list, &test1);
        atomic_notifier_call_chain(&test_notifier_list, 0, NULL);

case3: lose other hook test2
        atomic_notifier_chain_register(&test_notifier_list, &test1);
        atomic_notifier_chain_register(&test_notifier_list, &test2);
        atomic_notifier_chain_register(&test_notifier_list, &test1);

case4: Unregister returns 0, but the hook is still in the linked list,
        and it is not really registered. If you call notifier_call_chain
        after ko is unloaded, it will trigger oops.

If the system is configured with softlockup_panic and the same
hook is repeatedly registered on the panic_notifier_list, it
will cause a loop panic.

Add a check in notifier_chain_register() to avoid duplicate registration

Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com>
---
 kernel/notifier.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/notifier.c b/kernel/notifier.c
index d9f5081..30bedb8 100644
--- a/kernel/notifier.c
+++ b/kernel/notifier.c
@@ -23,7 +23,10 @@ static int notifier_chain_register(struct notifier_block **nl,
 		struct notifier_block *n)
 {
 	while ((*nl) != NULL) {
-		WARN_ONCE(((*nl) == n), "double register detected");
+		if (unlikely((*nl) == n)) {
+			WARN(1, "double register detected");
+			return 0;
+		}
 		if (n->priority > (*nl)->priority)
 			break;
 		nl = &((*nl)->next);
-- 
1.8.5.6


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3 1/3] kernel/notifier.c: avoid duplicate registration
  2019-07-10  3:09 [PATCH v3 1/3] kernel/notifier.c: avoid duplicate registration Xiaoming Ni
@ 2019-07-17 11:19 ` Vasily Averin
  0 siblings, 0 replies; 2+ messages in thread
From: Vasily Averin @ 2019-07-17 11:19 UTC (permalink / raw)
  To: Xiaoming Ni, adobriyan, akpm, anna.schumaker, arjan, bfields,
	chuck.lever, davem, gregkh, jlayton, luto, mingo, Nadia.Derbey,
	paulmck, semen.protsenko, stable, stern, tglx, torvalds,
	trond.myklebust, viresh.kumar
  Cc: alex.huangjianhui, dylix.dailei, linux-kernel, linux-nfs, netdev

On 7/10/19 6:09 AM, Xiaoming Ni wrote:
> Registering the same notifier to a hook repeatedly can cause the hook
> list to form a ring or lose other members of the list.
> 
> case1: An infinite loop in notifier_chain_register() can cause soft lockup
>         atomic_notifier_chain_register(&test_notifier_list, &test1);
>         atomic_notifier_chain_register(&test_notifier_list, &test1);
>         atomic_notifier_chain_register(&test_notifier_list, &test2);
> 
> case2: An infinite loop in notifier_chain_register() can cause soft lockup
>         atomic_notifier_chain_register(&test_notifier_list, &test1);
>         atomic_notifier_chain_register(&test_notifier_list, &test1);
>         atomic_notifier_call_chain(&test_notifier_list, 0, NULL);
> 
> case3: lose other hook test2
>         atomic_notifier_chain_register(&test_notifier_list, &test1);
>         atomic_notifier_chain_register(&test_notifier_list, &test2);
>         atomic_notifier_chain_register(&test_notifier_list, &test1);
> 
> case4: Unregister returns 0, but the hook is still in the linked list,
>         and it is not really registered. If you call notifier_call_chain
>         after ko is unloaded, it will trigger oops.
> 
> If the system is configured with softlockup_panic and the same
> hook is repeatedly registered on the panic_notifier_list, it
> will cause a loop panic.
> 
> Add a check in notifier_chain_register() to avoid duplicate registration
> 
> Signed-off-by: Xiaoming Ni <nixiaoming@huawei.com>
Reviewed-by: Vasily Averin <vvs@virtuozzo.com>
> ---
>  kernel/notifier.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/notifier.c b/kernel/notifier.c
> index d9f5081..30bedb8 100644
> --- a/kernel/notifier.c
> +++ b/kernel/notifier.c
> @@ -23,7 +23,10 @@ static int notifier_chain_register(struct notifier_block **nl,
>  		struct notifier_block *n)
>  {
>  	while ((*nl) != NULL) {
> -		WARN_ONCE(((*nl) == n), "double register detected");
> +		if (unlikely((*nl) == n)) {
> +			WARN(1, "double register detected");
> +			return 0;
> +		}
>  		if (n->priority > (*nl)->priority)
>  			break;
>  		nl = &((*nl)->next);
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-17 11:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-10  3:09 [PATCH v3 1/3] kernel/notifier.c: avoid duplicate registration Xiaoming Ni
2019-07-17 11:19 ` Vasily Averin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).