Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8633254ybi; Tue, 9 Jul 2019 20:09:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqw/Tyjs4QBx+Wfh6IGBRkgnGk6phnnkXXEY+eFRRCCKCFl1SE4TGSzbS/trAA4F3rPnBoh+ X-Received: by 2002:a17:90a:384d:: with SMTP id l13mr4090339pjf.86.1562728198484; Tue, 09 Jul 2019 20:09:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562728198; cv=none; d=google.com; s=arc-20160816; b=YrYzshV1wnXq15QuFzYxG0DmFq2hqzynDvUH9KhBfYBfdhzGFcAjKXHZdMedk/Fx1i /mfFq+MG8BcLzPpeiytq28raWSQfkhz144WxazwOHGh5PK1seYlEaUb+mhj0lqyBwQ2d xB89LARoP1LFhisze+buOK+rPPMwmRgy1wcXorPrM63/6g0HFXu/fsigHpyBMs6lJmmY FclcbnKNOsF/1sUeTWs4iDXY+JAYv9Po0EFpnjC2+1JLYjLhlCJkbcnHhvN/QRhqTJt6 vnhXM1DK1ARrFZR3TOkgsq5N1vb0+jbrh6kRhzivn7hOpcjI3g8m0sBDeztehBr2NImM 91LA== 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=UGTKfPO0oXUDFfIzD6IbhZSRszhzee60xonpFfDsSmH2rLIHsXolYIWuW6ds/RLg+A /sdM2QIundCsnTlph+6mI/vqDF9liZ3FUyR68WxCC1y0MntjlR20VC3ZLh8ywW6CuiY9 WZir1VEE+zHJFBCQTxiC612MUwOhLKPaeQ7N0Th6KdQfqxaXhBQh6YXOBx4RCya5iTWy 4pJAgcQ/GwWpg7W61lfHiUJ4WyVfnd0ef86u3ugSNBLb3Av3Zer7BVN+XPTNRgI8N7Ow psLI+4hwliOL8YAyqakLHqEg/EL9Abwy9Bs/lAnbShgFGqjEIG3Xi6ooB+1qMjVhFH2P kIzQ== 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 b93si839053plb.11.2019.07.09.20.09.42; Tue, 09 Jul 2019 20:09:58 -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 S1726820AbfGJDJZ (ORCPT + 99 others); Tue, 9 Jul 2019 23:09:25 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:2248 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725941AbfGJDJY (ORCPT ); Tue, 9 Jul 2019 23:09:24 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 942637021D84FE204553; Wed, 10 Jul 2019 11:09:18 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.439.0; Wed, 10 Jul 2019 11:09:12 +0800 From: Xiaoming Ni To: , , , , , , , , , , , , , , , , , , , , CC: , , , , , Subject: [PATCH v3 1/3] kernel/notifier.c: avoid duplicate registration Date: Wed, 10 Jul 2019 11:09:10 +0800 Message-ID: <1562728150-30301-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