Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp303155rwd; Tue, 30 May 2023 21:02:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7j1Mx79yDr5tPbDMNFF3OzpMOoDiFI9yMLM4Db+fW6mPT1tmQttGMIv0/ZMpe9rBspieCl X-Received: by 2002:aca:6501:0:b0:398:416e:22e9 with SMTP id m1-20020aca6501000000b00398416e22e9mr2291238oim.59.1685505732364; Tue, 30 May 2023 21:02:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685505732; cv=none; d=google.com; s=arc-20160816; b=ybtr82DKW5cherAaN+dFLdjHW7WE7zYrd+iGCVkrA71mutLuYCFBRRxE9DKteHaK// 9oz7ZMpmm3eGve6jcZmjzue/PtLcu37WboNvcmvp0wsVjP7Ru2COcJYQbqoW2Fr7yB5T bCCqZiy1zVzTGkAKKsPs15BN4QZCp/juSEVMYso34fXA8r0AJWQyvTPnCaQ0WcDeNlsC 6ZpttEwQDyBV7xiuTzP+NjREq2+i0ktYgFfdTYTu9wW+mgxjGK8dKzU/rxwypYhoEMRb WmLbHJxN2gDEMbJWwRdufALDbg03TAHxw+GoEn8ec5SgHcyQ2lCh347KomNze8Q9BFXX 1Pmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=mc749fSoT7HRuZHMhc7vPNXSe/JLAydSHJGmkjEZ7Fw=; b=ByCkvOhunMftF5We+WeWr5elydjKBgmSclTRSKTDIwAaRloJyPTSPrJD9bW0QQRvbO 7/g5R+TV3yRyuL7M0huveztr6ikj99ehmbsNExpDLkrpRtZye/pxteR1knI9Y5p4BJFg /rHhLMQEw9Sqt6dRn0AaAJTYhIETlYp/sq4nuGoq37ZRcyG3D1MXeipOZNOqnnRI8pka ZSMJFkeCUygnLxI3od3zzVJb5na48c65XePRK8uJTyzApgX4UCdNkthvQmwJaxc2vJ/c PbM+IYPYWvC+eLNB5O+nQRWRaT/jv3fODJCMt3n7uMEBZUyokprpHIm1B+iNkZ6PfPBe HozA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Tyw1GlPk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv125-20020a632e83000000b00537d3adaa10si236502pgb.597.2023.05.30.21.02.00; Tue, 30 May 2023 21:02:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Tyw1GlPk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232067AbjEaDv0 (ORCPT + 99 others); Tue, 30 May 2023 23:51:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229904AbjEaDvY (ORCPT ); Tue, 30 May 2023 23:51:24 -0400 Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEC42FC for ; Tue, 30 May 2023 20:51:21 -0700 (PDT) Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6af93a6166fso4108534a34.3 for ; Tue, 30 May 2023 20:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1685505081; x=1688097081; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=mc749fSoT7HRuZHMhc7vPNXSe/JLAydSHJGmkjEZ7Fw=; b=Tyw1GlPkBOpYbtCaPr1gWiNTs0bxcS6ipEd5PkOHCkTMrkh1ohHRoksZR0UIArJ3rg hsYeEAhYA0cXPOqUq2M+3QFgZEUXxlYNU3ZxeyfnqnulkOIDEX6fkmRpLDxIAp4GZghe EyJvK5eEVUeQWu0uZDXJAPY0yJA5p49qVhRCucigkF0nbqGHcvxIq9lT1RDydxJbZzhi E35zaf4AdqKIrM2DyWkr6PIX4uEONBZrlOGqQUGSE8Ad6C8xMhOFypJr9FUPLlCSHJkJ Fwntqrm4vyIeOcD6Ec3g0RvqurFYAQRtgu0K3r3vSHcorv11AxDERz5EaCwgbad/0rIe GEHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685505081; x=1688097081; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=mc749fSoT7HRuZHMhc7vPNXSe/JLAydSHJGmkjEZ7Fw=; b=LrdBmd8CgQ55zv54QZ5bhMzY0O/h3tBoUzJEusTFIwKqc+VzyzlqV10kMweARyzDID K3E9zuhlN0BKLOU/P1QiBm1h3Z7AhDV5BJnaGUJruX6XT2yIbu+ivnABFDrOM4xLXNNP 5iZgHGe+dXp7+eBg3Zk/nI0UewnP6efa1svfUeqgYkZIxGwdp1T6cb55GLxfrBj9pj60 MYnHXt+9qlgSXIwGvRkkJfrzK9jbGnlP4XDqrb4qmnlDa5smKI1JbCeuj9HAOLIJbxxx qSG0l9DBX6qYaq7g0cCpaRdGkOUuoXo4mLKUvqRJwOgvvzA0tdGmLQNi7swrNKkLtgKC aeJw== X-Gm-Message-State: AC+VfDxN9Kg0rrd7XmiUS5hgWVO2eTSxzJvKdULRvCFxKqGuzU/DYUv1 XoL5gcu3cI10JwL8EKCrBNj+xA== X-Received: by 2002:a05:6358:7241:b0:123:3133:1899 with SMTP id i1-20020a056358724100b0012331331899mr590034rwa.1.1685505081036; Tue, 30 May 2023 20:51:21 -0700 (PDT) Received: from localhost ([122.172.87.195]) by smtp.gmail.com with ESMTPSA id i10-20020a17090332ca00b001af98dcf958sm81371plr.288.2023.05.30.20.51.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 20:51:20 -0700 (PDT) Date: Wed, 31 May 2023 09:21:18 +0530 From: Viresh Kumar To: Stephan Gerhold Cc: Viresh Kumar , Nishanth Menon , Stephen Boyd , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2] opp: Fix use-after-free in lazy_opp_tables after probe deferral Message-ID: <20230531035118.p3iogejcmatd7nql@vireshk-i7> References: <20230524-opp-lazy-uaf-v2-1-c26304544a47@kernkonzept.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230524-opp-lazy-uaf-v2-1-c26304544a47@kernkonzept.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 30-05-23, 17:54, Stephan Gerhold wrote: > When dev_pm_opp_of_find_icc_paths() in _allocate_opp_table() returns > -EPROBE_DEFER, the opp_table is freed again, to wait until all the > interconnect paths are available. > > However, if the OPP table is using required-opps then it may already > have been added to the global lazy_opp_tables list. The error path > does not remove the opp_table from the list again. > > This can cause crashes later when the provider of the required-opps > is added, since we will iterate over OPP tables that have already been > freed. E.g.: > > Unable to handle kernel NULL pointer dereference when read > CPU: 0 PID: 7 Comm: kworker/0:0 Not tainted 6.4.0-rc3 > PC is at _of_add_opp_table_v2 (include/linux/of.h:949 > drivers/opp/of.c:98 drivers/opp/of.c:344 drivers/opp/of.c:404 > drivers/opp/of.c:1032) -> lazy_link_required_opp_table() > > Fix this by calling _of_clear_opp_table() to remove the opp_table from > the list and clear other allocated resources. While at it, also add the > missing mutex_destroy() calls in the error path. > > Cc: stable@vger.kernel.org > Suggested-by: Viresh Kumar > Fixes: 7eba0c7641b0 ("opp: Allow lazy-linking of required-opps") > Signed-off-by: Stephan Gerhold > --- > Changes in v2: > - Call _of_clear_opp_table() as suggested by Viresh > - Also add missing mutex_destroy() calls in the error path > - Link to v1: https://lore.kernel.org/r/20230524-opp-lazy-uaf-v1-1-f5f95cb4b6de@kernkonzept.com > --- > This fixes the crash I ran into after adding an OPP table with > both "required-opps" and interconnect paths (opp-peak-kBps). > --- > drivers/opp/core.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/drivers/opp/core.c b/drivers/opp/core.c > index 85cbc8de407c..7046487dc6f4 100644 > --- a/drivers/opp/core.c > +++ b/drivers/opp/core.c > @@ -1358,7 +1358,10 @@ static struct opp_table *_allocate_opp_table(struct device *dev, int index) > return opp_table; > > remove_opp_dev: > + _of_clear_opp_table(opp_table); > _remove_opp_dev(opp_dev, opp_table); > + mutex_destroy(&opp_table->genpd_virt_dev_lock); > + mutex_destroy(&opp_table->lock); > err: > kfree(opp_table); > return ERR_PTR(ret); > Applied. Thanks. -- viresh