Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2143387pxb; Mon, 12 Apr 2021 15:56:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz8DdQYHiPBy7hOyxFcgR4dVsPBZSMmBcVzwI+Hd3crkUIxE2IxwioUbag6lfu3ZNXtsmOp X-Received: by 2002:aa7:8493:0:b029:1ee:75b2:2865 with SMTP id u19-20020aa784930000b02901ee75b22865mr26212662pfn.61.1618268168978; Mon, 12 Apr 2021 15:56:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618268168; cv=none; d=google.com; s=arc-20160816; b=Nt+IDnSh9TmmtF8s+DiYNlCMB/5uScwIeQdvpD8rgq0mE5vjHgymruqKQssHm+9Fqm j3BjTMEhzE6IjJ86wgBHcNvQHsfH7eTCTp6yGqvMp3FYHwBY31ZGETa1KJbvF7xM3AFK hVK2z25eNx94W+loobWG1llAgmWCm5zprIJXWlLvfz7sMCXiVf4MP9Gxqr1b5TOCdONo SO+9mFwxonmLf1uDIyGYt/1qCxhj+k23Vrc+QDvipQ49XUrPIhuJltDQF9Gq3QJXmMsh F9Pll1PuurFocXiITLudNmQHogLzAjHWZvIgfYXgUoH8/tkAeEzWwzd7RJpVimUmZc+z GcGQ== 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=4tZ99wGcXnMQ87wHxhqDRDSQy3qqfhZWJQPkxuj4AWA=; b=AR5euhv2WpOPDf+ZrSlR4GqJUsM+teyIXImEmMFzHhbQTq3tIraTikUphZbkhz7/tl ///Q46vCYXL6QUgc8PxE8lUhORenTePdBTBJsBILQXg8qGvzuTDAH7vLeS/fm6tKz+FR jmiALIFOynwYUncEPzaHMcvYYSwhJ/MIovVJe98Ord0+OfHSlVFi2Y1EhWtu05CHXM91 iaonROoHzb31BTyh+Q8wsOrHUwtBtGv0vmwUc2Ou8o2F7HlvR7D6OqwNgc2v2sQLZ82V +CrQvxGG7Fd/cJEI8qib8a1VYj/83rkQwJT1GCcUnN90z48kGeBdXUma5Lh1WskkHoXT nfmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wn9Wx0Fv; 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 o4si14579502pfk.309.2021.04.12.15.55.57; Mon, 12 Apr 2021 15:56:08 -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=wn9Wx0Fv; 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 S243932AbhDLJnS (ORCPT + 99 others); Mon, 12 Apr 2021 05:43:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:34436 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240886AbhDLJLI (ORCPT ); Mon, 12 Apr 2021 05:11:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C67556137B; Mon, 12 Apr 2021 09:07:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618218427; bh=STUD3UkfFGxBalbffbD/Ee9BzW6gAl390uxWQJM7NdE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wn9Wx0FvuFMXLjt4edmlqlPZU5086QBeO+Igxu8S/jhfSayTqTJNjEJvMlhO6agkW 3DSHVYGzNZf1ABX7u+XKHYtLRDuTUq38UhIZQzempEdUCAb0TgiWryhv/1g8T0O7sv ZV5eCpH5FbTBG7gNClBDCfYqkeHOUEt1AAA5BhYU= 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 5.11 167/210] clk: fix invalid usage of list cursor in unregister Date: Mon, 12 Apr 2021 10:41:12 +0200 Message-Id: <20210412084021.593885520@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210412084016.009884719@linuxfoundation.org> References: <20210412084016.009884719@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 e08274020944..571ae066e548 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -4373,32 +4373,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