From patchwork Wed Mar 1 16:39:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 62981 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3745132wrd; Wed, 1 Mar 2023 08:58:01 -0800 (PST) X-Google-Smtp-Source: AK7set9tfZ8ReeW5mp//hNazL5wDrN1tfoQBfkhBYkMagbx+BfHdIiJ7bsTxlF3cqf4FjKiJVW02 X-Received: by 2002:a05:6a20:8f06:b0:cd:929d:2818 with SMTP id b6-20020a056a208f0600b000cd929d2818mr9935571pzk.11.1677689881554; Wed, 01 Mar 2023 08:58:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677689881; cv=none; d=google.com; s=arc-20160816; b=xPzEwAOtI9/qrRmkmiawUeQtMxZNxshH1cOn2iBGvgeCy16IHRJr06U+k4kxDRY9ei 2/p2XwubWWsGe3Rhp4oFc7Jrt5DNBCaxnKpwCZhvf+/vx0U92gSJ9XOaqt5jZhk5ENV/ 5HxlvRQmmR3ScrB6XOEd48reEaXPmE4ndrDFMumv4VKV204s1B51mMhHTbTBejRBBcvw 1aOx+psC+hNWmolGMgVHkT/kSfL/CvY5/1HZK70SiX+A2iY2sBuqN/CfIMehgbgpqqsn F9P3qp40AweDPmqbSKRnzO6i9zsGY0s75pvns1q9XYUxr4WGE07pARo4kIb6dZ8fuFD3 74Bg== 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=NSJcgD+Y6L1I9dbPpK5kubARotLSu9co9Llr60nk3dc=; b=C3pylzAuk14BNRTnPoPi2ffaUpB+zEOiJUWlyWUWBOik/sE7waWKRaGvHL2AiDAE8s b/fT90faA8nqaHICgW9AGWBBinDEyPUf0VacROVZgCX1ZppQwwTrY01NhaGIhDuxZ9yj zxfTgFcsc5T9oly6nU6WOg364Pzhm+r04ytvzXM7RTPC99QLxkbEaXTszhBUMUdH9GVs nCSYEvfs4sfVV/UeQ89y8RvCd9QDiqI1WU5x7VoBU5pdwi3H/eyvWurbFLq5tlIOZINc 3dEGYdFp7FKMvVgKfSHL481ux/qTcA6p5he/Vug6U4s3DyCStxycXFF2bYu2Yu+ULCkZ BzFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="aUajRa/J"; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x21-20020a63fe55000000b004fbacd1a872si11884727pgj.853.2023.03.01.08.57.43; Wed, 01 Mar 2023 08:58:01 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b="aUajRa/J"; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229888AbjCAQku (ORCPT + 99 others); Wed, 1 Mar 2023 11:40:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229774AbjCAQks (ORCPT ); Wed, 1 Mar 2023 11:40:48 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34FA53D92E for ; Wed, 1 Mar 2023 08:39:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677688784; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=NSJcgD+Y6L1I9dbPpK5kubARotLSu9co9Llr60nk3dc=; b=aUajRa/J37lms02MRhaHSsHi5DUaZLguKTXRwQaKWOVBjd9xZ8PUSmnWiEjFTvlW526xHh n6cPqr8W92E1cCxjQBnxrAbfhOvcOYwX0LVDESCqNVw+jRbXGhzws47WZywp2T9kMybCq4 96/hCqnmpfgeRKrQolCFAfd3RfOlqYU= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-402-uSyL-b7EP2e0rYDi05VSbw-1; Wed, 01 Mar 2023 11:39:43 -0500 X-MC-Unique: uSyL-b7EP2e0rYDi05VSbw-1 Received: by mail-pj1-f72.google.com with SMTP id f1-20020a17090aa78100b00239fd9e3e17so1837183pjq.5 for ; Wed, 01 Mar 2023 08:39:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677688782; 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=NSJcgD+Y6L1I9dbPpK5kubARotLSu9co9Llr60nk3dc=; b=2O+fFIrxDQ6IJpJVNvFlIaSpmwiXco/VjVpYJR5OdSruPJAJ7F2vL3XZWYLj4M9EXB m5TEpCBnsNJ9Qyw6iM6+s8GJd46Rwa3DVYuiEFT/0hkVUFlNxWMmTj62tsINs28wRhu4 K1f6LR9jNjFSZAaZmgnJZMrkuG/AKYJnOmsWy+vaqQxhqe0w5XK2fuTQyPgwO23jkR26 EGf20WaE74MF8tvzTAI7A0ukQQDFHaRNUfNhDAvAIt0waGyGMOliTssG7d/xWXnb8xJ1 prND0v7i9PnKmAhTHmVGi+Y308wEpnrL4d1H1qprIEF+PU244TeLhUqlEE9RxzBH/Kx2 i/gA== X-Gm-Message-State: AO0yUKWDzkjNg2vK0wWvPHdtUe3iiCa04XFE265jx286vLg2ygjdhnBw PrEwB935IrbxKEKjG3Yc5tPRETBSi/LKDcED4ZR829PTwdwVHIik+gTXGg8DnuJkzzt0P9tOqxV mE/RfBmMUrmNiPzfaX/g569Kv X-Received: by 2002:a17:902:f54e:b0:19a:98c9:8cea with SMTP id h14-20020a170902f54e00b0019a98c98ceamr8243572plf.39.1677688782464; Wed, 01 Mar 2023 08:39:42 -0800 (PST) X-Received: by 2002:a17:902:f54e:b0:19a:98c9:8cea with SMTP id h14-20020a170902f54e00b0019a98c98ceamr8243541plf.39.1677688782148; Wed, 01 Mar 2023 08:39:42 -0800 (PST) Received: from localhost.localdomain ([240d:1a:c0d:9f00:ca6:1aff:fead:cef4]) by smtp.gmail.com with ESMTPSA id x25-20020a63b219000000b00502ecc282e2sm7603216pge.5.2023.03.01.08.39.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Mar 2023 08:39:41 -0800 (PST) From: Shigeru Yoshida To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Shigeru Yoshida , "sjur . brandeland @ stericsson . com" , syzbot+b563d33852b893653a9e@syzkaller.appspotmail.com Subject: [PATCH net v2] net: caif: Fix use-after-free in cfusbl_device_notify() Date: Thu, 2 Mar 2023 01:39:13 +0900 Message-Id: <20230301163913.391304-1-syoshida@redhat.com> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759185345120122929?= X-GMAIL-MSGID: =?utf-8?q?1759185345120122929?= syzbot reported use-after-free in cfusbl_device_notify() [1]. This causes a stack trace like below: BUG: KASAN: use-after-free in cfusbl_device_notify+0x7c9/0x870 net/caif/caif_usb.c:138 Read of size 8 at addr ffff88807ac4e6f0 by task kworker/u4:6/1214 CPU: 0 PID: 1214 Comm: kworker/u4:6 Not tainted 5.19.0-rc3-syzkaller-00146-g92f20ff72066 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: netns cleanup_net Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 print_address_description.constprop.0.cold+0xeb/0x467 mm/kasan/report.c:313 print_report mm/kasan/report.c:429 [inline] kasan_report.cold+0xf4/0x1c6 mm/kasan/report.c:491 cfusbl_device_notify+0x7c9/0x870 net/caif/caif_usb.c:138 notifier_call_chain+0xb5/0x200 kernel/notifier.c:87 call_netdevice_notifiers_info+0xb5/0x130 net/core/dev.c:1945 call_netdevice_notifiers_extack net/core/dev.c:1983 [inline] call_netdevice_notifiers net/core/dev.c:1997 [inline] netdev_wait_allrefs_any net/core/dev.c:10227 [inline] netdev_run_todo+0xbc0/0x10f0 net/core/dev.c:10341 default_device_exit_batch+0x44e/0x590 net/core/dev.c:11334 ops_exit_list+0x125/0x170 net/core/net_namespace.c:167 cleanup_net+0x4ea/0xb00 net/core/net_namespace.c:594 process_one_work+0x996/0x1610 kernel/workqueue.c:2289 worker_thread+0x665/0x1080 kernel/workqueue.c:2436 kthread+0x2e9/0x3a0 kernel/kthread.c:376 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:302 When unregistering a net device, unregister_netdevice_many_notify() sets the device's reg_state to NETREG_UNREGISTERING, calls notifiers with NETDEV_UNREGISTER, and adds the device to the todo list. Later on, devices in the todo list are processed by netdev_run_todo(). netdev_run_todo() waits devices' reference count become 1 while rebdoadcasting NETDEV_UNREGISTER notification. When cfusbl_device_notify() is called with NETDEV_UNREGISTER multiple times, the parent device might be freed. This could cause UAF. Processing NETDEV_UNREGISTER multiple times also causes inbalance of reference count for the module. This patch fixes the issue by accepting only first NETDEV_UNREGISTER notification. Fixes: 7ad65bf68d70 ("caif: Add support for CAIF over CDC NCM USB interface") CC: sjur.brandeland@stericsson.com Link: https://syzkaller.appspot.com/bug?id=c3bfd8e2450adab3bffe4d80821fbbced600407f [1] Reported-by: syzbot+b563d33852b893653a9e@syzkaller.appspotmail.com Signed-off-by: Shigeru Yoshida --- net/caif/caif_usb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/caif/caif_usb.c b/net/caif/caif_usb.c index ebc202ffdd8d..bf61ea4b8132 100644 --- a/net/caif/caif_usb.c +++ b/net/caif/caif_usb.c @@ -134,6 +134,9 @@ static int cfusbl_device_notify(struct notifier_block *me, unsigned long what, struct usb_device *usbdev; int res; + if (what == NETDEV_UNREGISTER && dev->reg_state >= NETREG_UNREGISTERED) + return 0; + /* Check whether we have a NCM device, and find its VID/PID. */ if (!(dev->dev.parent && dev->dev.parent->driver && strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))