Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1593982pxb; Mon, 12 Apr 2021 01:45:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrrFEQmrjK3h9rQzaJPOdaHH5A60VmuRqE9+hOLyMOLm5dHBlAVZLtz/Br8ggnaGK8svxf X-Received: by 2002:a63:485b:: with SMTP id x27mr26518304pgk.0.1618217136801; Mon, 12 Apr 2021 01:45:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618217136; cv=none; d=google.com; s=arc-20160816; b=YKZDEIFO6UpRTU1zMvewuRNN+uZv3xYB00CkqV5/TnKJ7wEZGOuxLzob4+ly4NW+bd eJuXaZ/nJvO49zxflPVf0xAoNrjJoWTxayN/fWcCKmFS5ege7O+l1SJXp/QYQsLxibMD OaQiVznPdJUONg4PnySizU75UlbvWgbWHdYNv7WJ6PzUDGgHg1GDXNosq2+vT+eoWAex jNmENPWAdHIoQSxWh+wi3Paix1zKd8HFJeCcNMSKeEDC2ffOlICq5x+V7Tcrhpy+KSti QFcYLQrRVzCGDU3vxmnKdZKxvChRbGpMBPYI09LCyuvaaEQfepbiAdTv/7vHrt01V4Kh TGlA== 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=cn2W7XJCL/iZgrhjhIHFoU1fJ5R1cYZ9vFujRnWMsro=; b=uuu/mLq1z0zAITUba567QIm92QR4f4NgPzFJCvlpt3rlKZKeTkg3s83xMhqLm99xHb XQVRRctWqGMcRE/35Gl5TXT54xDIq2Rx5MqvMS+mT9KFjpOH0DhMYE6HL4RrUCoeLtmz UaxQFMT/qIoocHpELG9e47OuuPiQ6/OhlDAS8HHtKVuReK5C9yLb2VxK0P2ngdv1CGMC Fsum7INaCzFVqF58YLNsG8sNh3T3XUiZDhuD1MjSEcXc1nBz9a3WiUbWtqonxcRi49Jx ESo3YBno7DQBYNgeGtVRDtjUrVJusshDOOs8VLdTK8DkycRoTqntmaDrDRdRQ41NLogr bbDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=E7PcwDsV; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j20si9176076pfe.165.2021.04.12.01.45.25; Mon, 12 Apr 2021 01:45:36 -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=@linuxfoundation.org header.s=korg header.b=E7PcwDsV; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237697AbhDLIoo (ORCPT + 99 others); Mon, 12 Apr 2021 04:44:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:36014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237582AbhDLIoL (ORCPT ); Mon, 12 Apr 2021 04:44:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C303761220; Mon, 12 Apr 2021 08:43:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618217032; bh=VOdPJL46jqFFcSCbPMyh1c978qeo/AG6AEolp7ePXEU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E7PcwDsV1weuMXhZAegHBSPjMqsbc6m+Astp2uG6CvRiyy9M0Gl5StR8s10IHHcSp AWhekh0AyOSGF54O4CPRehEeVN+5lAgDfRrCe2rvby2NMmugUi8Ch8hqxjXenHG9kN feowop20kDj3NNZP5LZTIWWguS8qifEZmCr0YDnk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lukasz Majczak , Lukasz Bartosik , Stephen Boyd , Sasha Levin Subject: [PATCH 4.19 45/66] clk: fix invalid usage of list cursor in unregister Date: Mon, 12 Apr 2021 10:40:51 +0200 Message-Id: <20210412083959.574106863@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210412083958.129944265@linuxfoundation.org> References: <20210412083958.129944265@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: Lukasz Bartosik [ Upstream commit 7045465500e465b09f09d6e5bdc260a9f1aab97b ] Fix invalid usage of a list_for_each_entry cursor in clk_notifier_unregister(). When list is empty or if the list is completely traversed (without breaking from the loop on one of the entries) then the list cursor does not point to a valid entry and therefore should not be used. The patch fixes a logical bug that hasn't been seen in pratice however it is analogus to the bug fixed in clk_notifier_register(). The issue was dicovered when running 5.12-rc1 kernel on x86_64 with KASAN enabled: BUG: KASAN: global-out-of-bounds in clk_notifier_register+0xab/0x230 Read of size 8 at addr ffffffffa0d10588 by task swapper/0/1 CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.12.0-rc1 #1 Hardware name: Google Caroline/Caroline, BIOS Google_Caroline.7820.430.0 07/20/2018 Call Trace: dump_stack+0xee/0x15c print_address_description+0x1e/0x2dc kasan_report+0x188/0x1ce ? clk_notifier_register+0xab/0x230 ? clk_prepare_lock+0x15/0x7b ? clk_notifier_register+0xab/0x230 clk_notifier_register+0xab/0x230 dw8250_probe+0xc01/0x10d4 ... Memory state around the buggy address: ffffffffa0d10480: 00 00 00 00 00 03 f9 f9 f9 f9 f9 f9 00 00 00 00 ffffffffa0d10500: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f9 f9 >ffffffffa0d10580: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00 ^ ffffffffa0d10600: 00 00 00 00 00 00 f9 f9 f9 f9 f9 f9 00 00 00 00 ffffffffa0d10680: 00 00 00 00 00 00 00 00 f9 f9 f9 f9 00 00 00 00 ================================================================== Fixes: b2476490ef11 ("clk: introduce the common clock framework") Reported-by: Lukasz Majczak Signed-off-by: Lukasz Bartosik Link: https://lore.kernel.org/r/20210401225149.18826-2-lb@semihalf.com Signed-off-by: Stephen Boyd Signed-off-by: Sasha Levin --- drivers/clk/clk.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 7bb1c6185f20..3806fd8fef0b 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -3665,32 +3665,28 @@ EXPORT_SYMBOL_GPL(clk_notifier_register); */ int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) { - struct clk_notifier *cn = NULL; - int ret = -EINVAL; + struct clk_notifier *cn; + int ret = -ENOENT; if (!clk || !nb) return -EINVAL; clk_prepare_lock(); - list_for_each_entry(cn, &clk_notifier_list, node) - if (cn->clk == clk) - break; - - if (cn->clk == clk) { - ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); + list_for_each_entry(cn, &clk_notifier_list, node) { + if (cn->clk == clk) { + ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); - clk->core->notifier_count--; + clk->core->notifier_count--; - /* XXX the notifier code should handle this better */ - if (!cn->notifier_head.head) { - srcu_cleanup_notifier_head(&cn->notifier_head); - list_del(&cn->node); - kfree(cn); + /* XXX the notifier code should handle this better */ + if (!cn->notifier_head.head) { + srcu_cleanup_notifier_head(&cn->notifier_head); + list_del(&cn->node); + kfree(cn); + } + break; } - - } else { - ret = -ENOENT; } clk_prepare_unlock(); -- 2.30.2