Message ID | 20240301095514.3598280-1-haowenchao2@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-88211-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp970755dyb; Fri, 1 Mar 2024 01:58:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUyCtJHN+Vvbp7CMGNmqVCB9fVz6QN+1n+3M9rEU2NhO24EujNuBWPBxg/rwA5a9ZgEPJ5OZ8Qge70uRKsfZbPsySv/fg== X-Google-Smtp-Source: AGHT+IHjGc5u8vKhsGbdwP/WigREB6ft0orzN1iQn9EQmQFMQpA9GD+RyW5tzMbqXdG4afGpm701 X-Received: by 2002:a0c:f30a:0:b0:68f:edbc:5db1 with SMTP id j10-20020a0cf30a000000b0068fedbc5db1mr1222306qvl.42.1709287085963; Fri, 01 Mar 2024 01:58:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709287085; cv=pass; d=google.com; s=arc-20160816; b=uUCOrt90uRMfhjyhJOk34I6//GfxAxkITv6Hlgh+5R94vTLdLsoZzPdmoZyKRRuwhn VxkvQvpW2TNGdp0OHDdEj27J9OhyYSPqdKpxLaPU6pc4HLLrxB41sMiZnuT7jf3YB3L1 wPAkbZvPZT3JgM2n+E3x2xEwFtnk3Vz3XLpGHUd+fA0HKMXfGfj8EA8hZ/8RHUErXDVs 5syhEkFTieTYaJHG172lH8OUZyatLlh1JN8MxmCecPI9WdeXPaT4kx6lQhyLQCJlmQPu +TxrsVCLQMR22OTBbMViAWc46Ub3+OnuqHn37MmtoRddY0vuU8ZeE9zi/nPYqoF98uYx 00fQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from; bh=JgBTVxSzt00j6BeWRouxJ7WfuR2EWKSLKVSf+1vidpw=; fh=2JMmeDp4qG+pAcNCvb/IG7V7Pj1jav73Bzg7Rt6X0u0=; b=m7sccKzl299rR6A1HWkMes5UcqTvoFpSTVw9hzPyuTe4TIMmR5IOd1pTDE4gdxC9Hy d2sV2TLkywrr7zxOaR+2+OX6sfCRoGBkgy1YKgfmB8IPbyT6nbZyAu/AEZcGlKWg7Bv/ 5n9F9/S37sISVuu6h3fYflXoEkoJSvKNYritR1sZx4/4Mh91tmUbXR1Bu7IP0xImlPan Fz/zrHa8xwXDQw0H1B/NlSZJ2BGKv5v+Skof476jiwXl4o9Nlur16B+EusjnEPSpb6I1 3dmumHAHoSQx2IC6rUXpBEFgSmjpWRMmEAkyKkeL+8o1A/mEyX3NcsoxbAMbaGcDlmrR 2TJw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-88211-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88211-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id fq5-20020a056214258500b0068f57292ab9si3188564qvb.123.2024.03.01.01.58.05 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 01:58:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88211-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-88211-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88211-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B55D01C219E8 for <ouuuleilei@gmail.com>; Fri, 1 Mar 2024 09:58:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4429A6AFA0; Fri, 1 Mar 2024 09:57:51 +0000 (UTC) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBCBA42A8B; Fri, 1 Mar 2024 09:57:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709287069; cv=none; b=neqBLCdd73td6B5eikHPE1szN7QP8F4+q4BMO0SlN++Qq0yDBNbcVsmMelhJ/CuU5YiuUd+74HwOcXrOmj1XdCeqOgR0yg9x3/uaE4RAmNM6zvmscSqaPUNTd/roc4bYRoZav+C0GZs6XvX/cU96gWwSNeEUzR7RkXF4swXLZdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709287069; c=relaxed/simple; bh=7i7qkNtRTTPOCju0zKhcOsfkVU1pD4TeLBCBwyMYrNc=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=MUuEddCE1+ZEUQb5Ilp6nRPzSmfIlOF9y3y9ECNn4oV9wjsENgNRvOhES0BOgVv/6aP+TSY1QmG9ICaTRN2FwAsobCl29Tv4VULZJNShoBOldavKdJ0YaPFc7CuBOwSIfssWvEtC0/kcvk14jS/4AKAznQ+q9NftsH5jBGYTXpA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4TmNm86Gd3zNp35; Fri, 1 Mar 2024 17:56:56 +0800 (CST) Received: from kwepemm600012.china.huawei.com (unknown [7.193.23.74]) by mail.maildlp.com (Postfix) with ESMTPS id 0259418007D; Fri, 1 Mar 2024 17:57:37 +0800 (CST) Received: from build.huawei.com (10.175.101.6) by kwepemm600012.china.huawei.com (7.193.23.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 1 Mar 2024 17:57:36 +0800 From: Wenchao Hao <haowenchao2@huawei.com> To: Jason Gunthorpe <jgg@ziepe.ca>, Leon Romanovsky <leon@kernel.org>, Wenchao Hao <haowenchao2@huawei.com>, <linux-rdma@vger.kernel.org>, <linux-kernel@vger.kernel.org> Subject: [PATCH] RDMA/restrack: Fix potential invalid address access Date: Fri, 1 Mar 2024 17:55:15 +0800 Message-ID: <20240301095514.3598280-1-haowenchao2@huawei.com> X-Mailer: git-send-email 2.32.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600012.china.huawei.com (7.193.23.74) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792317415045920733 X-GMAIL-MSGID: 1792317415045920733 |
Series |
RDMA/restrack: Fix potential invalid address access
|
|
Commit Message
Wenchao Hao
March 1, 2024, 9:55 a.m. UTC
struct rdma_restrack_entry's kern_name was set to KBUILD_MODNAME
in ib_create_cq(), while if the module exited but forgot del this
rdma_restrack_entry, it would cause a invalid address access in
rdma_restrack_clean() when print the owner of this rdma_restrack_entry.
Fix this issue by using kstrdup() to set rdma_restrack_entry's
kern_name.
Signed-off-by: Wenchao Hao <haowenchao2@huawei.com>
---
drivers/infiniband/core/restrack.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
Comments
On Fri, Mar 01, 2024 at 05:55:15PM +0800, Wenchao Hao wrote: > struct rdma_restrack_entry's kern_name was set to KBUILD_MODNAME > in ib_create_cq(), while if the module exited but forgot del this > rdma_restrack_entry, it would cause a invalid address access in > rdma_restrack_clean() when print the owner of this rdma_restrack_entry. How is it possible to exit owner module without cleaning the resources? Thanks > > Fix this issue by using kstrdup() to set rdma_restrack_entry's > kern_name. > > Signed-off-by: Wenchao Hao <haowenchao2@huawei.com> > --- > drivers/infiniband/core/restrack.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/drivers/infiniband/core/restrack.c b/drivers/infiniband/core/restrack.c > index 01a499a8b88d..6605011c4edc 100644 > --- a/drivers/infiniband/core/restrack.c > +++ b/drivers/infiniband/core/restrack.c > @@ -177,7 +177,8 @@ static void rdma_restrack_attach_task(struct rdma_restrack_entry *res, > void rdma_restrack_set_name(struct rdma_restrack_entry *res, const char *caller) > { > if (caller) { > - res->kern_name = caller; > + kfree(res->kern_name); > + res->kern_name = kstrdup(caller, GFP_KERNEL); > return; > } > > @@ -195,7 +196,7 @@ void rdma_restrack_parent_name(struct rdma_restrack_entry *dst, > const struct rdma_restrack_entry *parent) > { > if (rdma_is_kernel_res(parent)) > - dst->kern_name = parent->kern_name; > + dst->kern_name = kstrdup(parent->kern_name, GFP_KERNEL); > else > rdma_restrack_attach_task(dst, parent->task); > } > @@ -306,6 +307,7 @@ static void restrack_release(struct kref *kref) > put_task_struct(res->task); > res->task = NULL; > } > + kfree(res->kern_name); > complete(&res->comp); > } > > -- > 2.32.0 >
On 2024/3/3 20:57, Leon Romanovsky wrote: > On Fri, Mar 01, 2024 at 05:55:15PM +0800, Wenchao Hao wrote: >> struct rdma_restrack_entry's kern_name was set to KBUILD_MODNAME >> in ib_create_cq(), while if the module exited but forgot del this >> rdma_restrack_entry, it would cause a invalid address access in >> rdma_restrack_clean() when print the owner of this rdma_restrack_entry. > > How is it possible to exit owner module without cleaning the resources? > I meet this issue with one of our product who develop their owner kernel modules based on ib_core, and there are terrible logic with the exit code which cause resource leak. Of curse it's bug of module who did not clear resource when exit, but I think ib_core should avoid accessing memory of other modules directly to provides better stability. What's more, from the context of rdma_restrack_clean() when print "restack: %s %s object allocated by %s is not freed ...", it seems designed for the above scene where client has bug to alerts there are resource leak, so we should not panic on this log print. > Thanks > >> >> Fix this issue by using kstrdup() to set rdma_restrack_entry's >> kern_name. >> >> Signed-off-by: Wenchao Hao <haowenchao2@huawei.com> >> --- >> drivers/infiniband/core/restrack.c | 6 ++++-- >> 1 file changed, 4 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/infiniband/core/restrack.c b/drivers/infiniband/core/restrack.c >> index 01a499a8b88d..6605011c4edc 100644 >> --- a/drivers/infiniband/core/restrack.c >> +++ b/drivers/infiniband/core/restrack.c >> @@ -177,7 +177,8 @@ static void rdma_restrack_attach_task(struct rdma_restrack_entry *res, >> void rdma_restrack_set_name(struct rdma_restrack_entry *res, const char *caller) >> { >> if (caller) { >> - res->kern_name = caller; >> + kfree(res->kern_name); >> + res->kern_name = kstrdup(caller, GFP_KERNEL); >> return; >> } >> >> @@ -195,7 +196,7 @@ void rdma_restrack_parent_name(struct rdma_restrack_entry *dst, >> const struct rdma_restrack_entry *parent) >> { >> if (rdma_is_kernel_res(parent)) >> - dst->kern_name = parent->kern_name; >> + dst->kern_name = kstrdup(parent->kern_name, GFP_KERNEL); >> else >> rdma_restrack_attach_task(dst, parent->task); >> } >> @@ -306,6 +307,7 @@ static void restrack_release(struct kref *kref) >> put_task_struct(res->task); >> res->task = NULL; >> } >> + kfree(res->kern_name); >> complete(&res->comp); >> } >> >> -- >> 2.32.0 >>
On Mon, Mar 04, 2024 at 11:21:19AM +0800, Wenchao Hao wrote: > On 2024/3/3 20:57, Leon Romanovsky wrote: > > On Fri, Mar 01, 2024 at 05:55:15PM +0800, Wenchao Hao wrote: > > > struct rdma_restrack_entry's kern_name was set to KBUILD_MODNAME > > > in ib_create_cq(), while if the module exited but forgot del this > > > rdma_restrack_entry, it would cause a invalid address access in > > > rdma_restrack_clean() when print the owner of this rdma_restrack_entry. > > > > How is it possible to exit owner module without cleaning the resources? > > > > I meet this issue with one of our product who develop their owner kernel > modules based on ib_core, and there are terrible logic with the exit > code which cause resource leak. > > Of curse it's bug of module who did not clear resource when exit, but > I think ib_core should avoid accessing memory of other modules directly > to provides better stability. > > What's more, from the context of rdma_restrack_clean() when print > "restack: %s %s object allocated by %s is not freed ...", it seems > designed for the above scene where client has bug to alerts there > are resource leak, so we should not panic on this log print. Can you please share the kernel panic? Thanks
On 2024/3/4 15:35, Leon Romanovsky wrote: > On Mon, Mar 04, 2024 at 11:21:19AM +0800, Wenchao Hao wrote: >> On 2024/3/3 20:57, Leon Romanovsky wrote: >>> On Fri, Mar 01, 2024 at 05:55:15PM +0800, Wenchao Hao wrote: >>>> struct rdma_restrack_entry's kern_name was set to KBUILD_MODNAME >>>> in ib_create_cq(), while if the module exited but forgot del this >>>> rdma_restrack_entry, it would cause a invalid address access in >>>> rdma_restrack_clean() when print the owner of this rdma_restrack_entry. >>> >>> How is it possible to exit owner module without cleaning the resources? >>> >> >> I meet this issue with one of our product who develop their owner kernel >> modules based on ib_core, and there are terrible logic with the exit >> code which cause resource leak. >> >> Of curse it's bug of module who did not clear resource when exit, but >> I think ib_core should avoid accessing memory of other modules directly >> to provides better stability. >> >> What's more, from the context of rdma_restrack_clean() when print >> "restack: %s %s object allocated by %s is not freed ...", it seems >> designed for the above scene where client has bug to alerts there >> are resource leak, so we should not panic on this log print. > > Can you please share the kernel panic? > Sorry, there is no stack or panic info recorded. This is because another issue of "printk". It seems printk would cause a deadlock when printk access invalid address with our kernel. Actually, I found this issue with ftrace/kprobe but not printk, that's why it takes me a long time to address it. BTW, I am not developer of rdma, after found the issue, I think it's better to enhance, so send this patch, and the patch has been tested with the origin scene. > Thanks
diff --git a/drivers/infiniband/core/restrack.c b/drivers/infiniband/core/restrack.c index 01a499a8b88d..6605011c4edc 100644 --- a/drivers/infiniband/core/restrack.c +++ b/drivers/infiniband/core/restrack.c @@ -177,7 +177,8 @@ static void rdma_restrack_attach_task(struct rdma_restrack_entry *res, void rdma_restrack_set_name(struct rdma_restrack_entry *res, const char *caller) { if (caller) { - res->kern_name = caller; + kfree(res->kern_name); + res->kern_name = kstrdup(caller, GFP_KERNEL); return; } @@ -195,7 +196,7 @@ void rdma_restrack_parent_name(struct rdma_restrack_entry *dst, const struct rdma_restrack_entry *parent) { if (rdma_is_kernel_res(parent)) - dst->kern_name = parent->kern_name; + dst->kern_name = kstrdup(parent->kern_name, GFP_KERNEL); else rdma_restrack_attach_task(dst, parent->task); } @@ -306,6 +307,7 @@ static void restrack_release(struct kref *kref) put_task_struct(res->task); res->task = NULL; } + kfree(res->kern_name); complete(&res->comp); }