Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2051687pxb; Mon, 12 Apr 2021 13:03:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkcdn0R6fIxulukMCSfQafrPjG8Ng6bUQMzP8oXO98NIsfSblZSzedZedMX3/xdO3Ye3Ig X-Received: by 2002:a62:f210:0:b029:24a:c8f:c1bf with SMTP id m16-20020a62f2100000b029024a0c8fc1bfmr10813741pfh.47.1618257788276; Mon, 12 Apr 2021 13:03:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618257788; cv=none; d=google.com; s=arc-20160816; b=Zsr2W08jmT8jhZZV1RN8Rgmqv+v+ScjepMG2ujtsou7chA6QCySPK5FJsrIGEtQI/4 ZHhwUgnt8WLrkW4PWRhrPnzqpIVQiKkqZCjgHr1am1xeVRP7KnT4HKUt2Mn3CNxbh10v acxgO1EMbVP0w9K98Lr/5LVa5pJ5Ddl/ckPqWYpGpl3r101Qx9kjGoFLbvQGU34wVsBM YHFCDKqLKtM+3wAfsvoMC5pkZ8RklQIPFYSXPiYh9Ujlk0/n53Oos78GqJnzP6etah0y JV3eHmwNG4/3uPl2BD8jp2PZJ/wBqYoFlG+M+xFGS1O24H30hJOs0x2GOFiTWvIbmA8j gDZg== 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=Si685bCpn4jYUyTSJYeEMrn1Bd8I6KAGv22R4tBMD5k=; b=A9RSJJdno59dbowXMOZ5pLzDKcViJgivUhXSFdm/L/jw3W6IH26VSQaiorWzImuc05 hKrgZi3F5mpd2qIYvLubkb3z4cIjWMXMKCPokkOabSlj0TuErxzVshYD4tnBBZPZlbrw di5rISMYXS68rc1nLOFZfiXUJcimKyIuTrwTG7rSEEQsFwexS+X70p2dWvWsI0F0sw64 TbSp+Pcb+HvLMA8jclfOMb+yhqvdTSxyeIMsn8tzA/b2Ds1c+kq5J92dte/gdzjLPtWu dmAT04PRFrbFkPOUnLS4Qp6ubWQRITSFtc0QvzBvANfjjlvbRD/X/sjBxVK/72y8Mgs9 AFwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QaO6vGjH; 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 h6si444051pjq.104.2021.04.12.13.02.50; Mon, 12 Apr 2021 13:03: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=QaO6vGjH; 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 S237620AbhDLIob (ORCPT + 99 others); Mon, 12 Apr 2021 04:44:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:35980 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237606AbhDLIoH (ORCPT ); Mon, 12 Apr 2021 04:44:07 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EC3E960241; Mon, 12 Apr 2021 08:43:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618217029; bh=yPuLew2KJnZSOlDbpM4PPAXm/fTfgspcnIv60BuCp+4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QaO6vGjHWXG8aH4yoZbX8tkQhD/Vlc1U5o1CLs/uYtq6k8GPfSRtq97YqSB00I1Ek r/JKga3+7sYbqCkMQ4zOAepYguhOKY4KG6F26LnQ4ZjVzwqoE3Ht/twSCnm3VvVPtK h2ITtf+NpES7rLr2HQ7d2ZqT384CLHtxUR2MVDTQ= 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 44/66] clk: fix invalid usage of list cursor in register Date: Mon, 12 Apr 2021 10:40:50 +0200 Message-Id: <20210412083959.543477984@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 8d3c0c01cb2e36b2bf3c06a82b18b228d0c8f5d0 ] Fix invalid usage of a list_for_each_entry cursor in clk_notifier_register(). 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 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-1-lb@semihalf.com Signed-off-by: Stephen Boyd Signed-off-by: Sasha Levin --- drivers/clk/clk.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index c5cf9e77fe86..7bb1c6185f20 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -3628,20 +3628,19 @@ int clk_notifier_register(struct clk *clk, struct notifier_block *nb) /* search the list of notifiers for this clk */ list_for_each_entry(cn, &clk_notifier_list, node) if (cn->clk == clk) - break; + goto found; /* if clk wasn't in the notifier list, allocate new clk_notifier */ - if (cn->clk != clk) { - cn = kzalloc(sizeof(*cn), GFP_KERNEL); - if (!cn) - goto out; + cn = kzalloc(sizeof(*cn), GFP_KERNEL); + if (!cn) + goto out; - cn->clk = clk; - srcu_init_notifier_head(&cn->notifier_head); + cn->clk = clk; + srcu_init_notifier_head(&cn->notifier_head); - list_add(&cn->node, &clk_notifier_list); - } + list_add(&cn->node, &clk_notifier_list); +found: ret = srcu_notifier_chain_register(&cn->notifier_head, nb); clk->core->notifier_count++; -- 2.30.2