Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp881442pxk; Thu, 17 Sep 2020 20:13:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzA2VyKBZVDH5Jv1k9yyEri/n7mIA2J9KKsfrwhQ981PFtA7u3xnqb8X7cBgsttRcKUPKmf X-Received: by 2002:a05:6402:1109:: with SMTP id u9mr36785633edv.74.1600398789470; Thu, 17 Sep 2020 20:13:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398789; cv=none; d=google.com; s=arc-20160816; b=TOZtkgJJNgB/21zyOtoqZmhtddB3Ynlv4GKSg2Rn/XAAOaU8nZi94nU7cdXbEHff4M 8XLW9XlEBgEMn8qYSzmi4QroZMH8jz0Ev/rEKXcLp/sfg3fMSv3eBfhJDeUXzn99sCut X1NBhW1M6Mpynhl+mPt2YFXwV4ABpV8BZYpJM3rG6vdUAEUnadm5xZyEaJp7frbNtrwl U4dxGaOGCJhcUIgn1aQEQQe4sauryEdB/Nhpl7JUcrTPkYhHk34CsDj39baqK7/hmNyc JlnP2DpEXFiy0Z1iO4iKneuX3pug/bybu7Muzd5Zl79wiMsJ82x4wo3NCXCayTocJ4r6 7jzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ybi2MSLEtxVLYKC91YjyLVsWGhySvIegc3SahzjkN0M=; b=HCtxhqQoo0i/PWSHmit+6JOaIwBglaSDPqayQ/LJ16qaXmVPDoETkio+2M5NDTHJDH dSYyAHmxepWAcoEdvQCZaPDP0TTubaGKI7RGgN6VD74+NpI/CcjKHc15x3BDxWBZuy0E 16Un/7Io2O0x4VflxfvcvHDVI7YeljCptGBVB/A1QoqKfr1R+5wUavfIKZmwtebj4hTQ FEtPeC7LB23CifE8+2KcBanHULzpORNGMBeufZ4uVge6WsVUM5Aq2bGvovR+jdeMWQRF 1LrYHbqz0GvsEku29lf2Pj+xuaIR2W3cnZcIf9zeJYwvHymOBOnZVK/gOvhqe//LD/8b nMfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cXJG1FJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oa23si1227503ejb.708.2020.09.17.20.12.46; Thu, 17 Sep 2020 20:13:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cXJG1FJf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727433AbgIRDLA (ORCPT + 99 others); Thu, 17 Sep 2020 23:11:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:47970 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726822AbgIRCCj (ORCPT ); Thu, 17 Sep 2020 22:02:39 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4A19A208DB; Fri, 18 Sep 2020 02:02:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394558; bh=kNFZsopbVyVAJ8OKMfB9Qlukt+X2Zi5I8g5+zko9C/A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cXJG1FJf52NWQKRws/DivkesxnzcwRmdJXvR5LObG1xih9DnIRF9F+Tmkbm5bWagc YJTs7OW8fOCHTmUJ8zIyUbLoWfK5+YrxAYlwbU/bUgU3oYhnhBkhwP3nZG4l2KNofn t96fhYLDWQSiX4OvZmVqIrmxQhLEtEGrHcoXzCDY= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Xiaoming Ni , Vasily Averin , Andrew Morton , Alexey Dobriyan , Anna Schumaker , Arjan van de Ven , "J . Bruce Fields" , Chuck Lever , "David S . Miller" , Jeff Layton , Andy Lutomirski , Ingo Molnar , Nadia Derbey , "Paul E. McKenney" , Sam Protsenko , Alan Stern , Thomas Gleixner , Trond Myklebust , Viresh Kumar , YueHaibing , Linus Torvalds , Sasha Levin Subject: [PATCH AUTOSEL 5.4 072/330] kernel/notifier.c: intercept duplicate registrations to avoid infinite loops Date: Thu, 17 Sep 2020 21:56:52 -0400 Message-Id: <20200918020110.2063155-72-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xiaoming Ni [ Upstream commit 1a50cb80f219c44adb6265f5071b81fc3c1deced ] 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(), intercepting duplicate registrations to avoid infinite loops Link: http://lkml.kernel.org/r/1568861888-34045-2-git-send-email-nixiaoming@huawei.com Signed-off-by: Xiaoming Ni Reviewed-by: Vasily Averin Reviewed-by: Andrew Morton Cc: Alexey Dobriyan Cc: Anna Schumaker Cc: Arjan van de Ven Cc: J. Bruce Fields Cc: Chuck Lever Cc: David S. Miller Cc: Jeff Layton Cc: Andy Lutomirski Cc: Ingo Molnar Cc: Nadia Derbey Cc: "Paul E. McKenney" Cc: Sam Protsenko Cc: Alan Stern Cc: Thomas Gleixner Cc: Trond Myklebust Cc: Viresh Kumar Cc: Xiaoming Ni Cc: YueHaibing Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- kernel/notifier.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/notifier.c b/kernel/notifier.c index 157d7c29f7207..f6d5ffe4e72ec 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); -- 2.25.1