Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1894735ybi; Thu, 20 Jun 2019 05:49:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqxck6PY9pVkxD7LrsYWup3rWGeu6OhxnfxKdQei0NHGLBxOEvQKrFsi4uosiDrYfpkCWG8r X-Received: by 2002:a63:fa0d:: with SMTP id y13mr12735813pgh.258.1561034959357; Thu, 20 Jun 2019 05:49:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561034959; cv=none; d=google.com; s=arc-20160816; b=U5Iv25qsx++t4Kfko5KRCpMJsSUTYtXYH7D9VxP0uYc06ku0qRKu2ebtsiYTROQSbm ATU0lqjeCbNu/2YFnS0r+adq8wMNPk9rPkXJJf/U0Il9WNsrZoIcDH1Vs14f5UFrvNyo 6lVS/SJlmuNl1GwG+1ifbj+IQLa6AI4N5HM+pBrGKGL9yq2iyASmt3Uq1WkM+gDyH9Qg PqKKAmVvatakWk2AQuLihD//pOfqhnhODbJx1cV2T7xCmeGN8yVOhirSp68svk3CV0uh hkdpvhFg7v05PfOJ0YexWRgNdn3rBbjpXX7sYy2rp/aU/d/ZZ6qSL5jfpedioy0Ykc+x roig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=7+NjyR43KG0M6pWQ5JRk0F/rsU4n19pWfIJg1sF+QA0=; b=gQx1TEcvhqxsX4ozOw0lSiycRSTAICCqeQe8++vnEFr+3Ukib+gcYalay0XY6Ltgtx uI1FU0ApgwchVh3ZZPY4ZhXWJR6pHQX0qgKqqWkMjCCDIiTh3p0DwKZ9EYqnCqEkaarS hcxQfkG0vE7t3n9LwWYTM/sOtO2mZlNPHBPMpzDP1DOOU5Xe6XJ9Z9YWHyUsV5uy5igL hLuhL96ZM8BHzhdbKTuq4X8pXNOP8ZyC2rDK3piQPAwa+1a1m23/umFWmHNZQmmNnLnR eC6cwmSJMfjN60MrwF6vdY5PpevHDP/Ues+y6bIlWAV4eWhPgQuxv76iI5R8VP3iXJhV xcvw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x3si5815180pgk.413.2019.06.20.05.49.04; Thu, 20 Jun 2019 05:49:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731893AbfFTMs4 (ORCPT + 99 others); Thu, 20 Jun 2019 08:48:56 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:34034 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726757AbfFTMsy (ORCPT ); Thu, 20 Jun 2019 08:48:54 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 017E69432391C7476A9E; Thu, 20 Jun 2019 20:48:48 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.439.0; Thu, 20 Jun 2019 20:48:38 +0800 From: Xiaoming Ni To: , , , , , , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v2 1/3] kernel/notifier.c: avoid duplicate registration Date: Thu, 20 Jun 2019 20:48:32 +0800 Message-ID: <1561034914-106990-1-git-send-email-nixiaoming@huawei.com> X-Mailer: git-send-email 1.8.5.6 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.189.174] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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