Message ID | 20231211090347.265240-1-chentao@kylinos.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6914546vqy; Mon, 11 Dec 2023 01:04:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IFOzr6UQCaApsURbTv1BNOATO3By8eDFz96gVV88qzYa9kNDweEv6RpQEy2s5e6mFjelRrG X-Received: by 2002:a05:6358:63aa:b0:170:ddbe:d060 with SMTP id k42-20020a05635863aa00b00170ddbed060mr493596rwh.63.1702285464912; Mon, 11 Dec 2023 01:04:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702285464; cv=none; d=google.com; s=arc-20160816; b=YOu4ehZ/UJvjbEa5bvZ2jDNafWJQ12E+XJNfnZbLVQRTHFHEFdvu3gM+gcsZCNSivF W7eoQiRSv+63guMsnodsITbEGB3VeKz5n5iJXC35YBNBGbRhteiWk/ImeJKCxS0ee+tp HwgC16p0lF/vtZil/E/7IVJF19KUz3hcWCO6QFWP4QwP7h1K3dI+S3uW6z8lUalIh0ax WxPCBi+PN8N9B1txQ2vgnmfvamm5L90IKHDDw2cWBmDehotHlfQEyT1NSw9ya+welWY1 KwEce9FmPPJh4PdteBujuHXLLFG7fZc3j3t7vEdBUgwx37zQLXPlZxyakzBCySAJz3aZ okBw== 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 :message-id:date:subject:cc:to:from; bh=479vtfw/z/pL01Ql2gVk9s1ITQbemLkSXz8u5h8m6bU=; fh=Ppy1WHgCqs4Jl5DoImIesOZauj3QgBlpLQBY9TqgKnE=; b=B+DDCExGdp8O9hwHsD8+cSUrwdAItUQXOvULCjxmqijOuJSpf0V7KvAHNzpiYZ7y6j oSbwuQAv1coNyH6wlL73btDqN7k5sDRA6/YXHIpKt+GgvJjA8tl6k/dkI73mDg41V/VM 2uNOSiCE6OXbi5gw1uZT9Gsmuf1voOl+S1IrMCLWHrb19oYjCt5IAt1n2iwVa3r6KPA4 ZQaOw0IDVJVzgR7kkkJ6K0CEZJPk1TLiVmJ/OyhbMQ1iIQDPAjBLY9h9k5TF7iRkagO4 vQxgEag38m9+LP+nbDA7FJAW32Aln93nY0B2q8+UGIBl5cg+hA6sXpvgoNmWHeApLNtJ Z72Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id a21-20020a631a55000000b005c687302e8esi5781524pgm.48.2023.12.11.01.04.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 01:04:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id B12BC8094D51; Mon, 11 Dec 2023 01:04:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234070AbjLKJEM (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Mon, 11 Dec 2023 04:04:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbjLKJEL (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 11 Dec 2023 04:04:11 -0500 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09E9DF2 for <linux-kernel@vger.kernel.org>; Mon, 11 Dec 2023 01:04:14 -0800 (PST) X-UUID: ddb7c8ac5a7740c39319e8105f29cf2c-20231211 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.33,REQID:1d2c04cf-5843-4420-b84c-766dfd178ec5,IP:5,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-10 X-CID-INFO: VERSION:1.1.33,REQID:1d2c04cf-5843-4420-b84c-766dfd178ec5,IP:5,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:-10 X-CID-META: VersionHash:364b77b,CLOUDID:c34d87fd-4a48-46e2-b946-12f04f20af8c,B ulkID:2312111704042F6WSKZH,BulkQuantity:0,Recheck:0,SF:17|19|44|66|38|24|1 02,TC:nil,Content:0,EDM:-3,IP:-2,URL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil ,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_FSD,TF_CID_SPAM_FSI,TF_CID_SPAM_ULN,TF_CID_SPAM_SNR, TF_CID_SPAM_FAS X-UUID: ddb7c8ac5a7740c39319e8105f29cf2c-20231211 X-User: chentao@kylinos.cn Received: from vt.. [(116.128.244.169)] by mailgw (envelope-from <chentao@kylinos.cn>) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1939800852; Mon, 11 Dec 2023 17:04:02 +0800 From: Kunwu Chan <chentao@kylinos.cn> To: will@kernel.org, mark.rutland@arm.com Cc: Ganapatrao.Kulkarni@cavium.com, suzuki.poulose@arm.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Kunwu Chan <chentao@kylinos.cn>, Kunwu Chan <kunwu.chan@hotmail.com> Subject: [PATCH] drivers/perf: Fix some null pointer dereference issues in thunderx2_pmu.c Date: Mon, 11 Dec 2023 17:03:47 +0800 Message-Id: <20231211090347.265240-1-chentao@kylinos.cn> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 11 Dec 2023 01:04:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784975683329667737 X-GMAIL-MSGID: 1784975683329667737 |
Series |
drivers/perf: Fix some null pointer dereference issues in thunderx2_pmu.c
|
|
Commit Message
Kunwu Chan
Dec. 11, 2023, 9:03 a.m. UTC
devm_kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure.
Fixes: 69c32972d593 ("drivers/perf: Add Cavium ThunderX2 SoC UNCORE PMU driver")
Cc: Kunwu Chan <kunwu.chan@hotmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
drivers/perf/thunderx2_pmu.c | 7 +++++++
1 file changed, 7 insertions(+)
Comments
On Mon, Dec 11, 2023 at 05:03:47PM +0800, Kunwu Chan wrote: > devm_kasprintf() returns a pointer to dynamically allocated memory > which can be NULL upon failure. > > Fixes: 69c32972d593 ("drivers/perf: Add Cavium ThunderX2 SoC UNCORE PMU driver") > Cc: Kunwu Chan <kunwu.chan@hotmail.com> > Signed-off-by: Kunwu Chan <chentao@kylinos.cn> > --- > drivers/perf/thunderx2_pmu.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/drivers/perf/thunderx2_pmu.c b/drivers/perf/thunderx2_pmu.c > index 1edb9c03704f..07edb174a0d7 100644 > --- a/drivers/perf/thunderx2_pmu.c > +++ b/drivers/perf/thunderx2_pmu.c > @@ -742,6 +742,8 @@ static int tx2_uncore_pmu_register( > > tx2_pmu->pmu.name = devm_kasprintf(dev, GFP_KERNEL, > "%s", name); > + if (!tx2_pmu->pmu.name) > + return -ENOMEM; > > return perf_pmu_register(&tx2_pmu->pmu, tx2_pmu->pmu.name, -1); AFAICT, perf_pmu_register() will WARN and return NULL, so I'm not sure what we gain from the additional check. > } > @@ -881,6 +883,11 @@ static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev, > return NULL; > } > > + if (!tx2_pmu->name) { > + dev_err(dev, "PMU type %d: Fail to allocate memory\n", type); > + devm_kfree(dev, tx2_pmu); > + return NULL; > + } In the _highly_ unlikely even that devm_kasprintf() failed to allocate, shouldn't we get a splat from the allocator? I don't think it's useful to print another message. Will
Thanks for your reply. After read tx2_uncore_pmu_register again. From the defination: 'char *name = tx2_pmu->name;', we could know 'tx2_pmu->pmu.name' equals 'tx2_pmu->name' The difference is that a new memory space is allocated for 'tx2_pmu->pmu.name'. If 'tx2_pmu->pmu.name' is always the same as 'tx2_pmu->name', whether we should use 'tx2_pmu->pmu.name = tx2_pmu->name;' to replace the 'devm_kasprintf'. I'm not sure it's appropriate to do that. Thanks again. On 2023/12/12 17:25, Will Deacon wrote: > On Mon, Dec 11, 2023 at 05:03:47PM +0800, Kunwu Chan wrote: >> devm_kasprintf() returns a pointer to dynamically allocated memory >> which can be NULL upon failure. >> >> Fixes: 69c32972d593 ("drivers/perf: Add Cavium ThunderX2 SoC UNCORE PMU driver") >> Cc: Kunwu Chan <kunwu.chan@hotmail.com> >> Signed-off-by: Kunwu Chan <chentao@kylinos.cn> >> --- >> drivers/perf/thunderx2_pmu.c | 7 +++++++ >> 1 file changed, 7 insertions(+) >> >> diff --git a/drivers/perf/thunderx2_pmu.c b/drivers/perf/thunderx2_pmu.c >> index 1edb9c03704f..07edb174a0d7 100644 >> --- a/drivers/perf/thunderx2_pmu.c >> +++ b/drivers/perf/thunderx2_pmu.c >> @@ -742,6 +742,8 @@ static int tx2_uncore_pmu_register( >> >> tx2_pmu->pmu.name = devm_kasprintf(dev, GFP_KERNEL, >> "%s", name); >> + if (!tx2_pmu->pmu.name) >> + return -ENOMEM; >> >> return perf_pmu_register(&tx2_pmu->pmu, tx2_pmu->pmu.name, -1); > > AFAICT, perf_pmu_register() will WARN and return NULL, so I'm not sure what > we gain from the additional check. > >> } >> @@ -881,6 +883,11 @@ static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev, >> return NULL; >> } >> >> + if (!tx2_pmu->name) { >> + dev_err(dev, "PMU type %d: Fail to allocate memory\n", type); >> + devm_kfree(dev, tx2_pmu); >> + return NULL; >> + } > > In the _highly_ unlikely even that devm_kasprintf() failed to allocate, > shouldn't we get a splat from the allocator? I don't think it's useful > to print another message. > > Will
diff --git a/drivers/perf/thunderx2_pmu.c b/drivers/perf/thunderx2_pmu.c index 1edb9c03704f..07edb174a0d7 100644 --- a/drivers/perf/thunderx2_pmu.c +++ b/drivers/perf/thunderx2_pmu.c @@ -742,6 +742,8 @@ static int tx2_uncore_pmu_register( tx2_pmu->pmu.name = devm_kasprintf(dev, GFP_KERNEL, "%s", name); + if (!tx2_pmu->pmu.name) + return -ENOMEM; return perf_pmu_register(&tx2_pmu->pmu, tx2_pmu->pmu.name, -1); } @@ -881,6 +883,11 @@ static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev, return NULL; } + if (!tx2_pmu->name) { + dev_err(dev, "PMU type %d: Fail to allocate memory\n", type); + devm_kfree(dev, tx2_pmu); + return NULL; + } return tx2_pmu; }