Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3743254pxk; Tue, 29 Sep 2020 05:18:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyE6QDYuOv54+ybmDh9i8uPhzX7xJ03lnZ5sGs8G+4i1v72rXqGh2oUeCl2fa7F5AlL5eCa X-Received: by 2002:a17:906:934f:: with SMTP id p15mr3602620ejw.497.1601381908511; Tue, 29 Sep 2020 05:18:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601381908; cv=none; d=google.com; s=arc-20160816; b=JGL4fIYXZXkBMNi2umOD66Shop3rtH1TvcDJSKfvskFVoHpDY9gyEeyCBYnYkaawkg eVhDdD00m3QLY0oRIXtwPswzcHllfr6bJScZZuET35bzjD5DzJplyfXaOgtNGFbT2r48 +6xKSg1QjJf7nnbUF43yG9hqPkKy6ElfOrcbQ7xrHxcMo2y3xfbo/RE7ANZ8S+PTBlNc kLIbSApQ8tIaCo9XyarZhzrCimWGuucI96GeGRwV6+YIoFDRBisULzGjl6dZuQdh8ZbD trBx2e6lGdgNn+qlW6s4xon8jYK1MYukwK3rQ4VNrEuss0FQ3S4kR6tKXpEuDGSDQe4G pbfw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Ybi2MSLEtxVLYKC91YjyLVsWGhySvIegc3SahzjkN0M=; b=0y67yPAOJhef0mXcAYfM2LNrKIXpgcoxTxcTzhGF0KQbXvBA+wT+d8BtLolJXadrmR 4mj6dTmllKnNifljeOmRkkyq6vrfGyLy2AlArpt6jy1EQnijRZZrAPrTRngOa7aI3LUb 9QYvhu8S5LsJLEvVNg7DMqfOp+FgyTgDG28tdYVQM08wsWN3G00jEa4xRnLCx9Q7+1h0 vRZfkYLFdzf7XNZpmBoEnHUPiMuBR6OMWOQtbjizZlcxm+Zl5LXrL0KhKFJXTf14fBSE bdhGwXS/XGa4nk2ZTcjIrESP1VdBK5e0REV0C2KDvYhpObCll/S4uh8MMKcw6ztVoiva G5aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=y+XR4WAi; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r11si2392049eju.461.2020.09.29.05.18.05; Tue, 29 Sep 2020 05:18:28 -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=y+XR4WAi; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731901AbgI2MPI (ORCPT + 99 others); Tue, 29 Sep 2020 08:15:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:58078 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728388AbgI2Lh2 (ORCPT ); Tue, 29 Sep 2020 07:37:28 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0D3523B21; Tue, 29 Sep 2020 11:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601379226; bh=kNFZsopbVyVAJ8OKMfB9Qlukt+X2Zi5I8g5+zko9C/A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y+XR4WAilYBvma41mD61p+qAc5yn3qPoMoOcUC29Yc790Pf8YAsWFbYhjkABh68DT TSpK1RGwAD1v2HXRkXH5R+BOMYRtVKVOF3q1AreJGrjlnpJ1owE/5dcLcFocyzL7Qf M2ZNVE0KvU6A6J8DJ20RAOUNqCDdySnnDftwqzhM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, 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 5.4 069/388] kernel/notifier.c: intercept duplicate registrations to avoid infinite loops Date: Tue, 29 Sep 2020 12:56:40 +0200 Message-Id: <20200929110013.833689836@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200929110010.467764689@linuxfoundation.org> References: <20200929110010.467764689@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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