Message ID | 20231119164705.1991375-1-phind.uet@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp1739437vqn; Sun, 19 Nov 2023 08:54:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCFs1IJsVu4SJFSIRcVYFr5HpNlhLr0SOL++3ACNwfoADp4BX+fc1Emg1GSnj9YuA62i3c X-Received: by 2002:a05:6808:1494:b0:3a5:a78b:f773 with SMTP id e20-20020a056808149400b003a5a78bf773mr7929955oiw.6.1700412847333; Sun, 19 Nov 2023 08:54:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700412847; cv=none; d=google.com; s=arc-20160816; b=WFLSvjLfRIGpy5yl0531QmDQHPQOgQ/dGm8DdQRhF78iCWhSNQsXlVGQWZOhy5Y4aU MbGwqVfKb1b6uGcTyFY1VELY/cK0UQ91paMkmDoDE9FuKP30OxmOso0mzqaPLFMbcEgE PGORR/AxQcIhPGYvzCK0Ocv3xmU3wJoy+KLYt4tsJFxwVbttyoOZS8UTCuZWI0BV898r szYePVzj5uxCp/NJhpTUGint+5IYofOA/SL8Zo8hftALCrF1TRc3h5XYQwZow9fGyyVF aeVpkUWZtp9yWZuzFhF1KfhP4dbXc0knCDbUcPXaNk47yuEnm9acxuxoKyQiSkqQ0u8s sHrg== 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:dkim-signature; bh=l/ttRIHKXMZ13uQ7+QQurcFDXbvyi0K7tHJbOIbLlt0=; fh=u0h/yA9S51V6c0KfDOJlcd0qKsxIeX8Nm8emlt0WgOk=; b=RfAnwEwBtzEjvhpeDnExq+IqhAThnOZQiv4AVdJ6BqhScMuUbtCmuxfNYCUmhbnJeq DS5OWk0SKA7FjZ4VU91j6xVCBHwcDD0rfKSh8k7U6NyffPQzCutBeMN8encFCOkdKVsr 9kuMPeoQXLWJHv1zSlvsXiPI3kibaaMKX4orTdV5mxcGUI8igFYewE69Vb5rze6pgga1 liZBIgUUzDkyPymZPjTko1Osd5fkLkAJYVa74744p1k8IQuVEFXTH3QX4UFr751fCowr TInz2p6CAtK3fWXiznY0OGVnDvsi6KbXLl+xnINDK5+H31kpYQDVubugzbfLGKrAU3fA QVWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=iuAt1wQi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id v67-20020a632f46000000b005c1b2e0b12fsi6076494pgv.372.2023.11.19.08.54.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 08:54:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=iuAt1wQi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id DFB42805D5DF; Sun, 19 Nov 2023 08:54:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231422AbjKSQrP (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Sun, 19 Nov 2023 11:47:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjKSQrO (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 19 Nov 2023 11:47:14 -0500 Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB7D1F9; Sun, 19 Nov 2023 08:47:11 -0800 (PST) Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6ce322b62aeso2040037a34.3; Sun, 19 Nov 2023 08:47:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700412430; x=1701017230; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=l/ttRIHKXMZ13uQ7+QQurcFDXbvyi0K7tHJbOIbLlt0=; b=iuAt1wQiZHwWJ/QU32Pq8k3wMjoQI6C4nmSLDYnCEiz3erLad7CDafoWfB9FggfZLS idlczmX2ynU9710DQyi/25tid9tITNfD8AxOfNnA9Oh6KSDlQl0Jq1Eg15V+x/tfGCCE eZvaEtZB1a/LQO6BR5BXT2njXz8jCcMWbO1oWija7rKpU76yHwFv2i5JLgW0iHLxuCQq VxgNPbelsvRESBs4zdToJPzBr7rody+LDTsJZneEXYqp8OHppf0iHUfbQKFtv0ishK1u tmvfAtgAesHAIU/Ny5WNKONcxktVnSzepNYabvcpuNcg5qTdgINEpRKN5+989JNHLxfF EipA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700412430; x=1701017230; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=l/ttRIHKXMZ13uQ7+QQurcFDXbvyi0K7tHJbOIbLlt0=; b=ZArgaBfWK4RAIkqCfeDXictQxK/a+lUiC3yCJp2VkvblysEsmUc+Kt9BQdwTn6GpBg ph+5Ue09fxnB68Fu1flZg847PucICmcGLgLg0nS7zMgEsMALvcLu40uMR+BiXTcPd2vD UVjBPLwWao8dh5/5KsZNqXep21Yh0OQ2h4A7bva0cf3ri2O+x20wRWjsKSQ8o03ifCfD 0J8UNOzOxzjIaj6s0jcIfBZoeN8+4wpCk5nQB5kMmrLK6ytNBBUARPDU0n2x+iN8Uvhx 96kvtQYaawZCbpeD27PHqKjD+QNH1jLRU1Iicjbe1YuaL7KjzgBk2RQedLstd1So6mmk HvvA== X-Gm-Message-State: AOJu0Yxp93ltDT+ueKaPrqRl7Yya3f+HmhzWlIhg6J0n9o8W1V5oN4C3 qK2VPlu2jHNi4RIB32lagcw= X-Received: by 2002:a05:6871:152:b0:1f4:ae6e:a4e1 with SMTP id z18-20020a056871015200b001f4ae6ea4e1mr6674088oab.56.1700412430539; Sun, 19 Nov 2023 08:47:10 -0800 (PST) Received: from phi.. (bb220-255-254-193.singnet.com.sg. [220.255.254.193]) by smtp.gmail.com with ESMTPSA id x4-20020aa79184000000b0068790c41ca2sm4519398pfa.27.2023.11.19.08.47.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 08:47:10 -0800 (PST) From: Nguyen Dinh Phi <phind.uet@gmail.com> To: bongsu.jeon@samsung.com, krzysztof.kozlowski@linaro.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Nguyen Dinh Phi <phind.uet@gmail.com>, syzbot+6eb09d75211863f15e3e@syzkaller.appspotmail.com Subject: [PATCH] nfc: virtual_ncidev: Add variable to check if ndev is running Date: Mon, 20 Nov 2023 00:47:05 +0800 Message-Id: <20231119164705.1991375-1-phind.uet@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Sun, 19 Nov 2023 08:54:01 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783012101996326610 X-GMAIL-MSGID: 1783012101996326610 |
Series |
nfc: virtual_ncidev: Add variable to check if ndev is running
|
|
Commit Message
Nguyen Dinh Phi
Nov. 19, 2023, 4:47 p.m. UTC
syzbot reported an memory leak that happens when an skb is add to
send_buff after virtual nci closed.
This patch adds a variable to track if the ndev is running before
handling new skb in send function.
Reported-by: syzbot+6eb09d75211863f15e3e@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/00000000000075472b06007df4fb@google.com
Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com>
---
drivers/nfc/virtual_ncidev.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
Comments
On 20/11/2023 01:47, Nguyen Dinh Phi wrote: > syzbot reported an memory leak that happens when an skb is add to > send_buff after virtual nci closed. > This patch adds a variable to track if the ndev is running before > handling new skb in send function. > > Reported-by: syzbot+6eb09d75211863f15e3e@syzkaller.appspotmail.com > Closes: https://lore.kernel.org/lkml/00000000000075472b06007df4fb@google.com > Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com> > --- > drivers/nfc/virtual_ncidev.c | 9 +++++++-- > 1 file changed, 7 insertions(+), 2 deletions(-) > > diff --git a/drivers/nfc/virtual_ncidev.c b/drivers/nfc/virtual_ncidev.c > index b027be0b0b6f..ac8226db54e2 100644 > --- a/drivers/nfc/virtual_ncidev.c > +++ b/drivers/nfc/virtual_ncidev.c > @@ -20,26 +20,31 @@ > NFC_PROTO_ISO14443_MASK | \ > NFC_PROTO_ISO14443_B_MASK | \ > NFC_PROTO_ISO15693_MASK) > +#define NCIDEV_RUNNING 0 This define isn't used. > > struct virtual_nci_dev { > struct nci_dev *ndev; > struct mutex mtx; > struct sk_buff *send_buff; > struct wait_queue_head wq; > + bool running; > }; > > static int virtual_nci_open(struct nci_dev *ndev) > { > + struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); > + > + vdev->running = true; > return 0; > } > > static int virtual_nci_close(struct nci_dev *ndev) > { > struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); > - > mutex_lock(&vdev->mtx); > kfree_skb(vdev->send_buff); > vdev->send_buff = NULL; > + vdev->running = false; > mutex_unlock(&vdev->mtx); > > return 0; > @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) > struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); > > mutex_lock(&vdev->mtx); > - if (vdev->send_buff) { > + if (vdev->send_buff || !vdev->running) { Dear Krzysztof, I agree this defensive code. But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) Could you check this? Best regards, Bongsu
On 20/11/2023 05:47, Bongsu Jeon wrote: > > On 20/11/2023 01:47, Nguyen Dinh Phi wrote: > >> syzbot reported an memory leak that happens when an skb is add to >> send_buff after virtual nci closed. >> This patch adds a variable to track if the ndev is running before >> handling new skb in send function. >> >> Reported-by: syzbot+6eb09d75211863f15e3e@syzkaller.appspotmail.com >> Closes: https://lore.kernel.org/lkml/00000000000075472b06007df4fb@google.com >> Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com> >> --- >> drivers/nfc/virtual_ncidev.c | 9 +++++++-- >> 1 file changed, 7 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/nfc/virtual_ncidev.c b/drivers/nfc/virtual_ncidev.c >> index b027be0b0b6f..ac8226db54e2 100644 >> --- a/drivers/nfc/virtual_ncidev.c >> +++ b/drivers/nfc/virtual_ncidev.c >> @@ -20,26 +20,31 @@ >> NFC_PROTO_ISO14443_MASK | \ >> NFC_PROTO_ISO14443_B_MASK | \ >> NFC_PROTO_ISO15693_MASK) >> +#define NCIDEV_RUNNING 0 > This define isn't used. > >> >> struct virtual_nci_dev { >> struct nci_dev *ndev; >> struct mutex mtx; >> struct sk_buff *send_buff; >> struct wait_queue_head wq; >> + bool running; >> }; >> >> static int virtual_nci_open(struct nci_dev *ndev) >> { >> + struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >> + >> + vdev->running = true; >> return 0; >> } >> >> static int virtual_nci_close(struct nci_dev *ndev) >> { >> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >> - >> mutex_lock(&vdev->mtx); >> kfree_skb(vdev->send_buff); >> vdev->send_buff = NULL; >> + vdev->running = false; >> mutex_unlock(&vdev->mtx); >> >> return 0; >> @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) >> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >> >> mutex_lock(&vdev->mtx); >> - if (vdev->send_buff) { >> + if (vdev->send_buff || !vdev->running) { > > Dear Krzysztof, > > I agree this defensive code. > But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) > Could you check this? This code looks not effective. At this point vdev->send_buff is always false, so the additional check would not bring any value. I don't see this fixing anything. Syzbot also does not seem to agree. Nguyen, please test your patches against syzbot *before* sending them. If you claim this fixes the report, please provide me the link to syzbot test results confirming it is fixed. I looked at syzbot dashboard and do not see this issue fixed with this patch. Best regards, Krzysztof
On 20/11/23 5:06 pm, Krzysztof Kozlowski wrote: > On 20/11/2023 05:47, Bongsu Jeon wrote: >> >> On 20/11/2023 01:47, Nguyen Dinh Phi wrote: >> >>> syzbot reported an memory leak that happens when an skb is add to >>> send_buff after virtual nci closed. >>> This patch adds a variable to track if the ndev is running before >>> handling new skb in send function. >>> >>> Reported-by: syzbot+6eb09d75211863f15e3e@syzkaller.appspotmail.com >>> Closes: https://lore.kernel.org/lkml/00000000000075472b06007df4fb@google.com >>> Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com> >>> --- >>> drivers/nfc/virtual_ncidev.c | 9 +++++++-- >>> 1 file changed, 7 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/nfc/virtual_ncidev.c b/drivers/nfc/virtual_ncidev.c >>> index b027be0b0b6f..ac8226db54e2 100644 >>> --- a/drivers/nfc/virtual_ncidev.c >>> +++ b/drivers/nfc/virtual_ncidev.c >>> @@ -20,26 +20,31 @@ >>> NFC_PROTO_ISO14443_MASK | \ >>> NFC_PROTO_ISO14443_B_MASK | \ >>> NFC_PROTO_ISO15693_MASK) >>> +#define NCIDEV_RUNNING 0 >> This define isn't used. >> >>> >>> struct virtual_nci_dev { >>> struct nci_dev *ndev; >>> struct mutex mtx; >>> struct sk_buff *send_buff; >>> struct wait_queue_head wq; >>> + bool running; >>> }; >>> >>> static int virtual_nci_open(struct nci_dev *ndev) >>> { >>> + struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>> + >>> + vdev->running = true; >>> return 0; >>> } >>> >>> static int virtual_nci_close(struct nci_dev *ndev) >>> { >>> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>> - >>> mutex_lock(&vdev->mtx); >>> kfree_skb(vdev->send_buff); >>> vdev->send_buff = NULL; >>> + vdev->running = false; >>> mutex_unlock(&vdev->mtx); >>> >>> return 0; >>> @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) >>> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>> >>> mutex_lock(&vdev->mtx); >>> - if (vdev->send_buff) { >>> + if (vdev->send_buff || !vdev->running) { >> >> Dear Krzysztof, >> >> I agree this defensive code. >> But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) >> Could you check this? > > This code looks not effective. At this point vdev->send_buff is always > false, so the additional check would not bring any value. > > I don't see this fixing anything. Syzbot also does not seem to agree. > > Nguyen, please test your patches against syzbot *before* sending them. > If you claim this fixes the report, please provide me the link to syzbot > test results confirming it is fixed. > > I looked at syzbot dashboard and do not see this issue fixed with this > patch. > > Best regards, > Krzysztof > Hi Krzysztof, I've submitted it to syzbot, it is the test request that created at [2023/11/20 09:39] in dashboard link https://syzkaller.appspot.com/bug?extid=6eb09d75211863f15e3e Best regards, Phi
On 20/11/2023 11:39, Nguyen Dinh Phi wrote: >>>> mutex_lock(&vdev->mtx); >>>> kfree_skb(vdev->send_buff); >>>> vdev->send_buff = NULL; >>>> + vdev->running = false; >>>> mutex_unlock(&vdev->mtx); >>>> >>>> return 0; >>>> @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) >>>> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>>> >>>> mutex_lock(&vdev->mtx); >>>> - if (vdev->send_buff) { >>>> + if (vdev->send_buff || !vdev->running) { >>> >>> Dear Krzysztof, >>> >>> I agree this defensive code. >>> But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) >>> Could you check this? >> >> This code looks not effective. At this point vdev->send_buff is always >> false, so the additional check would not bring any value. >> >> I don't see this fixing anything. Syzbot also does not seem to agree. >> >> Nguyen, please test your patches against syzbot *before* sending them. >> If you claim this fixes the report, please provide me the link to syzbot >> test results confirming it is fixed. >> >> I looked at syzbot dashboard and do not see this issue fixed with this >> patch. >> >> Best regards, >> Krzysztof >> > > Hi Krzysztof, > > I've submitted it to syzbot, it is the test request that created at > [2023/11/20 09:39] in dashboard link > https://syzkaller.appspot.com/bug?extid=6eb09d75211863f15e3e ...and I see there two errors. I don't know, maybe I miss something obvious (our brains like to do it sometimes), but please explain me how this could fix anything? Best regards, Krzysztof
On 11/20/2023 6:45 PM, Krzysztof Kozlowski wrote: > On 20/11/2023 11:39, Nguyen Dinh Phi wrote: >>>>> mutex_lock(&vdev->mtx); >>>>> kfree_skb(vdev->send_buff); >>>>> vdev->send_buff = NULL; >>>>> + vdev->running = false; >>>>> mutex_unlock(&vdev->mtx); >>>>> >>>>> return 0; >>>>> @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) >>>>> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>>>> >>>>> mutex_lock(&vdev->mtx); >>>>> - if (vdev->send_buff) { >>>>> + if (vdev->send_buff || !vdev->running) { >>>> >>>> Dear Krzysztof, >>>> >>>> I agree this defensive code. >>>> But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) >>>> Could you check this? >>> >>> This code looks not effective. At this point vdev->send_buff is always >>> false, so the additional check would not bring any value. >>> >>> I don't see this fixing anything. Syzbot also does not seem to agree. >>> >>> Nguyen, please test your patches against syzbot *before* sending them. >>> If you claim this fixes the report, please provide me the link to syzbot >>> test results confirming it is fixed. >>> >>> I looked at syzbot dashboard and do not see this issue fixed with this >>> patch. >>> >>> Best regards, >>> Krzysztof >>> >> >> Hi Krzysztof, >> >> I've submitted it to syzbot, it is the test request that created at >> [2023/11/20 09:39] in dashboard link >> https://syzkaller.appspot.com/bug?extid=6eb09d75211863f15e3e > > ...and I see there two errors. > These are because I sent email wrongly and syzbot truncates the patch and can not compile > I don't know, maybe I miss something obvious (our brains like to do it > sometimes), but please explain me how this could fix anything? > > Best regards, > Krzysztof > The issue arises when an skb is added to the send_buff after invoking ndev->ops->close() but before unregistering the device. In such cases, the virtual device will generate a copy of skb, but with no consumer thereafter. Consequently, this object persists indefinitely. This problem seems to stem from the existence of time gaps between ops->close() and the destruction of the workqueue. During this interval, incoming requests continue to trigger the send function. best regards, Phi
On 20/11/2023 19:23, Phi Nguyen wrote: > On 11/20/2023 6:45 PM, Krzysztof Kozlowski wrote: >> On 20/11/2023 11:39, Nguyen Dinh Phi wrote: >>>>>> mutex_lock(&vdev->mtx); >>>>>> kfree_skb(vdev->send_buff); >>>>>> vdev->send_buff = NULL; >>>>>> + vdev->running = false; >>>>>> mutex_unlock(&vdev->mtx); >>>>>> >>>>>> return 0; >>>>>> @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) >>>>>> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>>>>> >>>>>> mutex_lock(&vdev->mtx); >>>>>> - if (vdev->send_buff) { >>>>>> + if (vdev->send_buff || !vdev->running) { >>>>> >>>>> Dear Krzysztof, >>>>> >>>>> I agree this defensive code. >>>>> But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) >>>>> Could you check this? >>>> >>>> This code looks not effective. At this point vdev->send_buff is always >>>> false, so the additional check would not bring any value. >>>> >>>> I don't see this fixing anything. Syzbot also does not seem to agree. >>>> >>>> Nguyen, please test your patches against syzbot *before* sending them. >>>> If you claim this fixes the report, please provide me the link to syzbot >>>> test results confirming it is fixed. >>>> >>>> I looked at syzbot dashboard and do not see this issue fixed with this >>>> patch. >>>> >>>> Best regards, >>>> Krzysztof >>>> >>> >>> Hi Krzysztof, >>> >>> I've submitted it to syzbot, it is the test request that created at >>> [2023/11/20 09:39] in dashboard link >>> https://syzkaller.appspot.com/bug?extid=6eb09d75211863f15e3e >> >> ...and I see there two errors. >> > These are because I sent email wrongly and syzbot truncates the patch > and can not compile > >> I don't know, maybe I miss something obvious (our brains like to do it >> sometimes), but please explain me how this could fix anything? >> >> Best regards, >> Krzysztof >> > > The issue arises when an skb is added to the send_buff after invoking > ndev->ops->close() but before unregistering the device. In such cases, > the virtual device will generate a copy of skb, but with no consumer > thereafter. Consequently, this object persists indefinitely. > > This problem seems to stem from the existence of time gaps between > ops->close() and the destruction of the workqueue. During this interval, > incoming requests continue to trigger the send function. I asked how this could fix anything. Can you respond to my original comment? Look: >>>> This code looks not effective. At this point vdev->send_buff is always >>>> false, so the additional check would not bring any value. Best regards, Krzysztof
On 20/11/2023 19:29, Krzysztof Kozlowski wrote: > On 20/11/2023 19:23, Phi Nguyen wrote: >> On 11/20/2023 6:45 PM, Krzysztof Kozlowski wrote: >>> On 20/11/2023 11:39, Nguyen Dinh Phi wrote: >>>>>>> mutex_lock(&vdev->mtx); >>>>>>> kfree_skb(vdev->send_buff); >>>>>>> vdev->send_buff = NULL; >>>>>>> + vdev->running = false; >>>>>>> mutex_unlock(&vdev->mtx); >>>>>>> >>>>>>> return 0; >>>>>>> @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) >>>>>>> struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); >>>>>>> >>>>>>> mutex_lock(&vdev->mtx); >>>>>>> - if (vdev->send_buff) { >>>>>>> + if (vdev->send_buff || !vdev->running) { >>>>>> >>>>>> Dear Krzysztof, >>>>>> >>>>>> I agree this defensive code. >>>>>> But i think NFC submodule has to avoid this situation.(calling send function of closed nci_dev) >>>>>> Could you check this? >>>>> >>>>> This code looks not effective. At this point vdev->send_buff is always >>>>> false, so the additional check would not bring any value. >>>>> >>>>> I don't see this fixing anything. Syzbot also does not seem to agree. >>>>> >>>>> Nguyen, please test your patches against syzbot *before* sending them. >>>>> If you claim this fixes the report, please provide me the link to syzbot >>>>> test results confirming it is fixed. >>>>> >>>>> I looked at syzbot dashboard and do not see this issue fixed with this >>>>> patch. >>>>> >>>>> Best regards, >>>>> Krzysztof >>>>> >>>> >>>> Hi Krzysztof, >>>> >>>> I've submitted it to syzbot, it is the test request that created at >>>> [2023/11/20 09:39] in dashboard link >>>> https://syzkaller.appspot.com/bug?extid=6eb09d75211863f15e3e >>> >>> ...and I see there two errors. >>> >> These are because I sent email wrongly and syzbot truncates the patch >> and can not compile >> >>> I don't know, maybe I miss something obvious (our brains like to do it >>> sometimes), but please explain me how this could fix anything? >>> >>> Best regards, >>> Krzysztof >>> >> >> The issue arises when an skb is added to the send_buff after invoking >> ndev->ops->close() but before unregistering the device. In such cases, >> the virtual device will generate a copy of skb, but with no consumer >> thereafter. Consequently, this object persists indefinitely. >> >> This problem seems to stem from the existence of time gaps between >> ops->close() and the destruction of the workqueue. During this interval, >> incoming requests continue to trigger the send function. > > I asked how this could fix anything. Can you respond to my original comment? > > Look: > >>>>> This code looks not effective. At this point vdev->send_buff is always >>>>> false, so the additional check would not bring any value. Uh, now I see, I missed that's the if here is for send_buff != NULL, so quite different case than virtual_nci_close(). Best regards, Krzysztof
On 19/11/2023 17:47, Nguyen Dinh Phi wrote: > syzbot reported an memory leak that happens when an skb is add to > send_buff after virtual nci closed. > This patch adds a variable to track if the ndev is running before > handling new skb in send function. > > Reported-by: syzbot+6eb09d75211863f15e3e@syzkaller.appspotmail.com > Closes: https://lore.kernel.org/lkml/00000000000075472b06007df4fb@google.com > Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com> > --- > drivers/nfc/virtual_ncidev.c | 9 +++++++-- > 1 file changed, 7 insertions(+), 2 deletions(-) > > diff --git a/drivers/nfc/virtual_ncidev.c b/drivers/nfc/virtual_ncidev.c > index b027be0b0b6f..ac8226db54e2 100644 > --- a/drivers/nfc/virtual_ncidev.c > +++ b/drivers/nfc/virtual_ncidev.c > @@ -20,26 +20,31 @@ > NFC_PROTO_ISO14443_MASK | \ > NFC_PROTO_ISO14443_B_MASK | \ > NFC_PROTO_ISO15693_MASK) > +#define NCIDEV_RUNNING 0 As pointed out, drop. > > struct virtual_nci_dev { > struct nci_dev *ndev; > struct mutex mtx; > struct sk_buff *send_buff; > struct wait_queue_head wq; > + bool running; > }; > > static int virtual_nci_open(struct nci_dev *ndev) > { > + struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); > + > + vdev->running = true; > return 0; > } > > static int virtual_nci_close(struct nci_dev *ndev) > { > struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); > - Drop this change. > mutex_lock(&vdev->mtx); > kfree_skb(vdev->send_buff); > vdev->send_buff = NULL; > + vdev->running = false; > mutex_unlock(&vdev->mtx); > > return 0; > @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) > struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); > > mutex_lock(&vdev->mtx); > - if (vdev->send_buff) { > + if (vdev->send_buff || !vdev->running) { Rest looks good, thank you. I think the driver is still not safe for closing the file descriptor, but that's separate issue. Please send v2 fixing above points, with a changelog under ---. Best regards, Krzysztof
On 20/11/2023 19:23, Phi Nguyen wrote: > The issue arises when an skb is added to the send_buff after invoking > ndev->ops->close() but before unregistering the device. In such cases, > the virtual device will generate a copy of skb, but with no consumer > thereafter. Consequently, this object persists indefinitely. > > This problem seems to stem from the existence of time gaps between > ops->close() and the destruction of the workqueue. During this interval, > incoming requests continue to trigger the send function. Dear Krzysztof, Even though i agree on this patch, i think that NFC subsystem could handle this scenario not to trigger the send function after close. Do you think it would be better that each nci driver has the responsibility to handle this scenario? Best regards, Bongsu
On 21/11/2023 02:31, Bongsu Jeon wrote: > On 20/11/2023 19:23, Phi Nguyen wrote: > >> The issue arises when an skb is added to the send_buff after invoking >> ndev->ops->close() but before unregistering the device. In such cases, >> the virtual device will generate a copy of skb, but with no consumer >> thereafter. Consequently, this object persists indefinitely. >> >> This problem seems to stem from the existence of time gaps between >> ops->close() and the destruction of the workqueue. During this interval, >> incoming requests continue to trigger the send function. > > Dear Krzysztof, > > Even though i agree on this patch, i think that NFC subsystem could handle this scenario not to trigger the send function after close. > Do you think it would be better that each nci driver has the responsibility to handle this scenario? It's better if core does it, but so far each driver was taking care of it. Send a patch if you have some better solution. Best regards, Krzysztof
diff --git a/drivers/nfc/virtual_ncidev.c b/drivers/nfc/virtual_ncidev.c index b027be0b0b6f..ac8226db54e2 100644 --- a/drivers/nfc/virtual_ncidev.c +++ b/drivers/nfc/virtual_ncidev.c @@ -20,26 +20,31 @@ NFC_PROTO_ISO14443_MASK | \ NFC_PROTO_ISO14443_B_MASK | \ NFC_PROTO_ISO15693_MASK) +#define NCIDEV_RUNNING 0 struct virtual_nci_dev { struct nci_dev *ndev; struct mutex mtx; struct sk_buff *send_buff; struct wait_queue_head wq; + bool running; }; static int virtual_nci_open(struct nci_dev *ndev) { + struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); + + vdev->running = true; return 0; } static int virtual_nci_close(struct nci_dev *ndev) { struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); - mutex_lock(&vdev->mtx); kfree_skb(vdev->send_buff); vdev->send_buff = NULL; + vdev->running = false; mutex_unlock(&vdev->mtx); return 0; @@ -50,7 +55,7 @@ static int virtual_nci_send(struct nci_dev *ndev, struct sk_buff *skb) struct virtual_nci_dev *vdev = nci_get_drvdata(ndev); mutex_lock(&vdev->mtx); - if (vdev->send_buff) { + if (vdev->send_buff || !vdev->running) { mutex_unlock(&vdev->mtx); kfree_skb(skb); return -1;